Muhammad Ali Lab 4.docx

  • Uploaded by: Muhammad Ali
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Muhammad Ali Lab 4.docx as PDF for free.

More details

  • Words: 158
  • Pages: 1
Muhammad Ali

02-235162-021

parallel programming

Exercises Exercise 1 (write a code for MPI ping pong program by using MPI_Send and MPI_Recv processes,use MPI_Send and MPI_Recv to continually bounce messages off of each other until they decide to stop at 10 steps.). Code #include "stdafx.h" #include "mpi.h" #include "iostream" using namespace std; int main(int argc, char** argv) { const int PING_PONG_LIMIT = 10; MPI_Init(&argc, &argv); int world_rank; MPI_Comm_rank(MPI_COMM_WORLD,&world_rank); int world_size; MPI_Comm_size(MPI_COMM_WORLD,&world_size); if (world_size != 2) { cout << "World size must be two"; MPI_Abort(MPI_COMM_WORLD, 1);} int ping_pong_count = 0; int partner_rank = (world_rank + 1) % 2;

Output:

while (ping_pong_count < PING_PONG_LIMIT){ if (world_rank == ping_pong_count % 2) { ping_pong_count++; MPI_Send(&ping_pong_count, 1, MPI_INT, partner_rank, 0, MPI_COMM_WORLD); cout << world_rank << " sent and incremented ping_pong_count" << ping_pong_count << " to " << partner_rank<< endl;} else { MPI_Recv(&ping_pong_count, 1, MPI_INT, partner_rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);cout << world_rank << "received ping_pong_count" << ping_pong_count << " to " << partner_rank << endl;}}MPI_Finalize(); }

Related Documents

Muhammad Ali
November 2019 32
Muhammad Ali
November 2019 27
Muhammad Ali
July 2020 19

More Documents from "Muhammad Ali"