fork download
  1. #include <iostream>
  2. #include <mpi.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. int nitems = 10;
  7. int mynode, totalnodes;
  8. MPI_Status status;
  9. double *array;
  10.  
  11. MPI_Init(&argc, &argv);
  12. MPI_Comm_size(MPI_COMM_WORLD, &totalnodes);
  13. MPI_Comm_rank(MPI_COMM_WORLD, &mynode);
  14.  
  15. array = new double[nitems];
  16.  
  17. if (mynode == 0)
  18. {
  19. // Initialize array only on the root node
  20. for (int i = 0; i < nitems; i++)
  21. array[i] = (double)i;
  22. }
  23.  
  24. if (mynode == 0)
  25. {
  26. // Send array to each other process once
  27. for (int i = 1; i < totalnodes; i++)
  28. {
  29. MPI_Send(array, nitems, MPI_DOUBLE, i, 1, MPI_COMM_WORLD);
  30. }
  31. }
  32. else
  33. {
  34. // Each non-root process receives the array only once
  35. MPI_Recv(array, nitems, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, &status);
  36. }
  37.  
  38. // Print array on each processor
  39. for (int i = 0; i < nitems; i++)
  40. {
  41. printf("Processor %d: array[%d] = %f\n", mynode, i, array[i]);
  42. }
  43.  
  44. delete[] array;
  45. MPI_Finalize();
  46. return 0;
  47. }
Success #stdin #stdout #stderr 0.28s 40532KB
stdin
2 hyey , hgha
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted