Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 26th, 2012  |  syntax: None  |  size: 1.11 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. MPI communicator error
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <mpi.h>
  5.  
  6. #define RNumber     3
  7.  
  8. using namespace std;
  9.  
  10. int main() {
  11.     /*Initiliaze MPI*/
  12.     int     my_rank;    //My process rank
  13.     int         comm_sz;    //Number of processes
  14.     MPI_Comm    GathComm;   //Communicator for MPI_Gather
  15.     MPI_Init(NULL, NULL);
  16.     MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  17.     MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
  18.  
  19.     /*Initialize an array for results*/
  20.     long rawT[RNumber];
  21.     long * Times = NULL;        //Results from threads
  22.     if (my_rank == 0) Times = (long*) malloc(comm_sz*RNumber*sizeof(long));
  23.  
  24.     /*Fill rawT with results at threads*/
  25.     for (int i = 0; i < RNumber; i++) {
  26.         rawT[i] = i;
  27.     }
  28.  
  29.  
  30.     if (my_rank == 0) {
  31.         /*Main thread recieves data from other threads*/
  32.         MPI_Gather(rawT, RNumber, MPI_LONG, Times, RNumber, MPI_LONG, 0, GathComm);
  33.  
  34.     }
  35.     else {
  36.         /*Other threads send calculation results to main thread*/
  37.         MPI_Gather(rawT, RNumber, MPI_LONG, Times, RNumber, MPI_LONG, 0, GathComm);
  38.     }
  39.  
  40.     /*Finalize MPI*/
  41.     MPI_Finalize();
  42.     return 0;
  43. };