Advertisement
egisss633

C++MPI

Jan 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include "mpi.h"
  4. #include <cstdio>
  5. using namespace std;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.  
  10.     int ierr = MPI_Init(&argc, &argv);
  11.     int numprocs;
  12.     int myid;
  13.     int a;
  14.     int senderID =  0;
  15.     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
  16.     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  17.  
  18.     // process 1-5 receivess the send a, adds or substracts a, and sends it further. process 5 prints all the changes.
  19.     if (myid != 0) {
  20.         MPI_Recv(&a, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  21.         if (myid == 1 || myid == 2 || myid == 3)
  22.         {
  23.             a += 10;
  24.             senderID = myid;
  25.             MPI_Send(&a, 1, MPI_INT, MPI_ANY_TAG, 0, MPI_COMM_WORLD);
  26.             MPI_Send(&senderID, 1, MPI_INT, 5, 0, MPI_COMM_WORLD);
  27.         }
  28.         else if (myid == 4)
  29.         {
  30.             a -= 50;
  31.             senderID = myid;
  32.             MPI_Send(&a, 1, MPI_INT, MPI_ANY_TAG, 0, MPI_COMM_WORLD);
  33.             MPI_Send(&senderID, 1, MPI_INT, 5, 0, MPI_COMM_WORLD);
  34.         }
  35.         else if (myid == 5)
  36.         {
  37.             while (a >= 0)
  38.             {
  39.                 MPI_Recv(&senderID, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  40.                 if (senderID == 0)
  41.                 {
  42.                     cout << "Spausdino" << endl;
  43.                 }
  44.                 if (1 <= senderID <= 3)
  45.                 {
  46.                     cout << "Pridėjo" << endl;
  47.                 }
  48.                 else
  49.                 {
  50.                     cout << "Atėmė" << endl;
  51.                 }
  52.             }
  53.         }
  54.     }
  55.  
  56.     // process P0 initializes a and sends it to all other processes.
  57.     if (myid == 0) {
  58.         a = 100;
  59.         MPI_Send(&a, 1, MPI_INT, MPI_ANY_TAG, 0,MPI_COMM_WORLD);
  60.         MPI_Recv(&a, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  61.         if (a <= 0)
  62.         MPI_Send(&senderID, 1, MPI_INT, 5, 0, MPI_COMM_WORLD);
  63.  
  64.     }
  65.     while (a >= 0)
  66.     {
  67.         MPI_Send(&a, 1, MPI_INT, MPI_ANY_TAG, 0, MPI_COMM_WORLD);
  68.     }
  69.     MPI_Finalize();
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement