fork download
  1. #include <iostream>
  2. #include <mpi.h>
  3. #include <cstdlib>
  4.  
  5. int main(int argc, char **argv) {
  6. MPI_Init(&argc, &argv);
  7.  
  8. int rank;
  9. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  10.  
  11. // Read the local value of the process
  12. // local_value will hold a specific int for process 0, and another for process 1
  13. int local_value;
  14. local_value = atoi(argv[1]);
  15.  
  16. int other_value;
  17. if (rank == 0) {
  18. // Code for the first process:
  19. // 1- Send the value to process 1
  20. // 2- Receive the value from process 1 (in other_value)
  21. // 3- Print the sum of the two values on stdout
  22. MPI_Send(&local_value, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
  23. MPI_Recv(&other_value, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  24. std::cout << "SUM IS: " << (local_value + other_value) << std::endl;
  25. }
  26. else {
  27. // Code for the second process:
  28. // 1- Receive the value from process 0 (in other_value)
  29. // 2- Send the value to process 0
  30. // 3- Print the product of the two values on stdout
  31. MPI_Recv(&other_value, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  32. MPI_Send(&local_value, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
  33. std::cout << "PRODUCT IS: " << (local_value * other_value) << std::endl;
  34. }
  35.  
  36. MPI_Finalize();
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout #stderr 0.26s 40844KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted