fork download
  1. #include <mpi.h>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. int main( int argc, char *argv[] )
  6.  
  7. {
  8.  
  9. // declare rank and the size of the process
  10. int rank, size;
  11.  
  12. MPI_Init( &argc, &argv );
  13.  
  14. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  15.  
  16. MPI_Comm_size(MPI_COMM_WORLD, &size);
  17.  
  18. int number = -1;
  19.  
  20. double startT = 0.0, endT = 0.0;
  21.  
  22. //MPI_Request ireq,ireq1;
  23.  
  24. //MPI_Status istatus,istatus2;
  25.  
  26. if (rank != 0) {
  27.  
  28. MPI_Recv(&number, 1, MPI_INT, rank-1, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); //
  29. std::cout<<"Process "<< rank <<"recieved number "<< number << "from process "<< rank-1 << std::endl;
  30.  
  31. MPI_Send(&number, 1, MPI_INT, (rank+1)%size, 0, MPI_COMM_WORLD); // 0 sending to 1
  32. std::cout<<"Process "<< rank <<"sent number "<< number << "to process "<< rank+1 << std::endl;
  33.  
  34.  
  35. } else
  36. {
  37. number = 23;
  38. startT = MPI_Wtime();
  39. MPI_Send(&number, 1, MPI_INT, (rank+1)%size, 0, MPI_COMM_WORLD); // 0 sending to 1
  40. //std::cout<<"Process "<< rank <<"sent number "<< number << "to process "<< rank+1 << std::endl;
  41.  
  42. }
  43.  
  44.  
  45. if (rank == 0) {
  46.  
  47.  
  48. MPI_Recv(&number, 1, MPI_INT, size-1, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); //
  49. std::cout<<"Process "<< rank <<"recieved number "<< number << "from process "<< size-1 << std::endl;
  50. endT= MPI_Wtime();
  51. std::cout << "Duration =" << endT-startT << std::endl;
  52.  
  53.  
  54. }
  55.  
  56. MPI_Finalize();
  57. return 0;
  58.  
  59. }
  60.  
Success #stdin #stdout #stderr 0.27s 38820KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted