Jovfer

mpiReduce

May 8th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <iostream>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. void myFunc(long long int *inp, long long int *inout, int *n, MPI_Datatype *type) {
  7.     for (int i=0; i<*n; i++) {
  8.         long long int mod = 10;
  9.         while (mod <= inout[i]) mod*=10;
  10.         inout[i] += inp[i]*mod;
  11.         stringstream buf;
  12.         buf << inout[i] << endl;
  13.         cout << buf.str();
  14.     }
  15. }
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.     int myrank, tag = 0;
  20.     MPI_Status status;
  21.     MPI_Init(&argc, &argv);
  22.     MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
  23.  
  24.     long long int myData[] = {myrank+1};
  25.     long long int result[1] = {-1};
  26.     MPI_Op myOp;
  27.     MPI_Op_create((MPI_User_function*)myFunc, true, &myOp);
  28.     MPI_Reduce(myData, result, 1, MPI_LONG, myOp, 0, MPI_COMM_WORLD);
  29.    
  30.     if (myrank == 0) cout << result[0] << endl;
  31.  
  32.     MPI_Op_free(&myOp);
  33.     MPI_Finalize();
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment