fork download
  1. #include <stdio.h>
  2. #include <mpi.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. const int tag = 42; /* Message tag */
  7. int id, ntasks, source_id, dest_id, err, i;
  8. MPI_Status status;
  9. int msg[2]; /* Message array */
  10.  
  11. err = MPI_Init(&argc, &argv); /* Initialize MPI */
  12. if (err != MPI_SUCCESS) {
  13. printf("MPI initialization failed!\n");
  14. exit(1);
  15. }
  16. err = MPI_Comm_size(MPI_COMM_WORLD, &ntasks); /* Get nr of tasks */
  17. err = MPI_Comm_rank(MPI_COMM_WORLD, &id); /* Get id of this process */
  18. if (ntasks < 2) {
  19. printf("You have to use at least 2 processors to run this program\n");
  20. MPI_Finalize(); /* Quit if there is only one processor */
  21. exit(0);
  22. }
  23. if (id == 0) { /* Process 0 (the receiver) does this */
  24. for (i=1; i<ntasks; i++) {
  25. err = MPI_Recv(msg, 2, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, \
  26. &status); /* Receive a message */
  27. source_id = status.MPI_SOURCE; /* Get id of sender */
  28. printf("Received message %d %d from process %d\n", msg[0], msg[1], \
  29. source_id);
  30. }
  31. }
  32. else { /* Processes 1 to N-1 (the senders) do this */
  33. msg[0] = id; /* Put own identifier in the message */
  34. msg[1] = ntasks; /* and total number of processes */
  35. dest_id = 0; /* Destination address */
  36. err = MPI_Send(msg, 2, MPI_INT, dest_id, tag, MPI_COMM_WORLD);
  37. }
  38.  
  39. err = MPI_Finalize(); /* Terminate MPI */
  40. if (id==0) printf("Ready\n");
  41. exit(0);
  42. return 0;
  43. }
  44.  
Success #stdin #stdout #stderr 0.3s 40480KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted