fork download
  1. #include <stdio.h>
  2. #include <mpi.h>
  3.  
  4. int main(int argc, char** argv) {
  5. int rank, size;
  6. MPI_Init(&argc, &argv);
  7. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  8. MPI_Comm_size(MPI_COMM_WORLD, &size);
  9.  
  10. if (size < 2) {
  11. fprintf(stderr, "This program requires at least 2 processes\n");
  12. MPI_Finalize();
  13. return 1;
  14. }
  15.  
  16. if (rank == 0) {
  17. // Process 0 sends a message to process 1
  18. int message = 123;
  19. MPI_Send(&message, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
  20. printf("Process 0 sent message: %d\n", message);
  21. } else if (rank == 1) {
  22. // Process 1 receives the message from process 0
  23. int received_message;
  24. MPI_Recv(&received_message, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  25. printf("Process 1 received message: %d\n", received_message);
  26. }
  27.  
  28. MPI_Finalize();
  29. return 0;
  30. }
  31.  
Success #stdin #stdout #stderr 0.27s 40756KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted