fork download
  1. #include <iostream>
  2. #include <mpi.h>
  3.  
  4. int main() {
  5. MPI_Init(NULL, NULL);
  6.  
  7. int rank, size;
  8. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  9. MPI_Comm_size(MPI_COMM_WORLD, &size);
  10.  
  11. if (size != 2) {
  12. std::cout << "This program requires exactly 2 processes.\n";
  13. MPI_Finalize();
  14. return 1;
  15. }
  16.  
  17. int x = 12345; // Sample data to be sent
  18.  
  19. if (rank == 0) {
  20. double T1 = MPI_Wtime();
  21. MPI_Send(&x, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); // Sending data to process 1
  22. double T2 = MPI_Wtime();
  23. double TP0 = T2 - T1;
  24. double TP1;
  25. MPI_Recv(&TP1, 1, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // Receiving data from process 1
  26. std::cout << "TP0: " << TP0 << std::endl;
  27. std::cout << "TP1: " << TP1 << std::endl;
  28. }
  29. else {
  30. double T1 = MPI_Wtime();
  31. MPI_Recv(&x, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // Receiving data from process 0
  32. double T2 = MPI_Wtime();
  33. double TP1 = T2 - T1;
  34. MPI_Send(&TP1, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); // Sending data to process 0
  35. }
  36.  
  37. MPI_Finalize();
  38. return 0;
  39. }
Success #stdin #stdout #stderr 0.27s 40844KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted