fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <mpi.h>
  4.  
  5. #define MASTER 0
  6. #define TAG 0
  7. #define MESSAGE_SIZE 13 // Size of "Hello World!"
  8.  
  9. int main(int argc, char *argv[]) {
  10. int rank, size;
  11. char message[MESSAGE_SIZE];
  12.  
  13. MPI_Init(&argc, &argv);
  14. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  15. MPI_Comm_size(MPI_COMM_WORLD, &size);
  16.  
  17. if (rank == MASTER) {
  18. // Master process
  19. strcpy(message, "Hello World!");
  20.  
  21. // Send message to all workers
  22. for (int i = 1; i < size; i++) {
  23. MPI_Send(message, MESSAGE_SIZE, MPI_CHAR, i, TAG, MPI_COMM_WORLD);
  24. }
  25. } else {
  26. // Worker processes
  27. MPI_Recv(message, MESSAGE_SIZE, MPI_CHAR, MASTER, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  28. printf("Worker %d received message: %s\n", rank, message);
  29. }
  30.  
  31. MPI_Finalize();
  32. return 0;
  33. }
  34.  
Success #stdin #stdout #stderr 0.28s 40516KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted