fork download
  1. #include <mpi.h> // must have a system with an MPI library
  2. #include <stdio.h> //printf
  3. #include <stdlib.h> //malloc
  4. #define MASTER 0 //One process will take care of initialization
  5. #define ARRAY_SIZE 8 //Size of arrays that will be added together.
  6. /*
  7.  * In MPI programs, the main function for the program is run on every
  8.  * process that gets initialized when you start up this code using mpirun.
  9.  */
  10. int main (int argc, char *argv[])
  11. {
  12. // elements of arrays a and b will be added
  13. // and placed in array c
  14. int * a;
  15. int * b;
  16. int * c;
  17. int total_proc; // total nuber of processes
  18. int rank; // rank of each process
  19. int n_per_proc; // elements per process
  20. int n = ARRAY_SIZE; // number of array elements
  21. int i; // loop index
  22. MPI_Status status; // not used in this arguably poor example
  23. // that is devoid of error checking.
  24. // 1. Initialization of MPI environment
  25. MPI_Init (&argc, &argv);
  26. MPI_Comm_size (MPI_COMM_WORLD, &total_proc);
  27. // 2. Now you know the total number of processes running in parallel
  28. MPI_Comm_rank (MPI_COMM_WORLD,&rank);
  29. // 3. Now you know the rank of the current process
  30. // Smaller arrays that will be held on each separate process
  31. int * ap;
  32. int * bp;
  33. int * cp;
  34. // 4. We choose process rank 0 to be the root, or master,
  35. // which will be used to initialize the full arrays.
  36. if (rank == MASTER) {
  37. a = (int *) malloc(sizeof(int)*n);
  38. b = (int *) malloc(sizeof(int)*n);
  39. c = (int *) malloc(sizeof(int)*n);
  40.  
  41. // initialize arrays a and b with consecutive integer values
  42. // as a simple example
  43. for(i=0;i<n;i++)
  44. { a[i] = i;
  45. printf ("a[%d]=%d\t ",i, a[i]);}
  46. printf("\n");
  47. for(i=0;i<n;i++)
  48. { b[i] = i;
  49. printf ("b[%d]=%d\t ",i, b[i]);}
  50. printf("\n");
  51. }
  52.  
  53. n_per_proc = n/total_proc;
  54.  
  55. ap = (int *) malloc(sizeof(int)*n_per_proc);
  56. bp = (int *) malloc(sizeof(int)*n_per_proc);
  57. cp = (int *) malloc(sizeof(int)*n_per_proc);
  58. // 6.
  59. //scattering array a from MASTER node out to the other nodes
  60. MPI_Scatter(a, n_per_proc, MPI_INT, ap, n_per_proc, MPI_INT, MASTER, MPI_COMM_WORLD);
  61. //scattering array b from MASTER node out to the other node
  62. MPI_Scatter(b, n_per_proc, MPI_INT, bp, n_per_proc, MPI_INT, MASTER, MPI_COMM_WORLD);
  63. // 7. Compute the addition of elements in my subsection of the array
  64. for(i=0;i<n_per_proc;i++)
  65. cp[i] = ap[i]+bp[i];
  66. // 8. MASTER node gathering array c from the workers
  67. MPI_Gather(cp, n_per_proc, MPI_INT, c, n_per_proc, MPI_INT, MASTER, MPI_COMM_WORLD);
  68. /////////////////////// all concurrent processes are finished once they all communicate
  69. /////////////////////// data back to the master via the gather function.
  70. // Master process gets to here only when it has been able to gather from all processes
  71. if (rank == MASTER) {
  72. // sanity check the result (a test we would eventually leave out)
  73. int good = 1;
  74. for(i=0;i<n;i++) {
  75. printf ("c[%d]=%d\t ",i, c[i]);
  76. if (c[i] != a[i] + b[i]) {
  77. printf("problem at index %lld\n", i);
  78. good = 0;
  79. break;
  80. }
  81. }
  82. if (good) {
  83. printf ("Values correct!\n");
  84. }
  85. }
  86. // clean up memory
  87. if (rank == MASTER) {
  88. free(a); free(b); free(c);
  89. }
  90. free(ap); free(bp); free(cp);
  91.  
  92. // 9. Terminate MPI Environment and Processes
  93. MPI_Finalize();
  94.  
  95. return 0;
  96. }# your code goes here
Success #stdin #stdout #stderr 0.27s 40904KB
stdin
a[0]=0   a[1]=1  a[2]=2  a[3]=3  a[4]=4  a[5]=5  a[6]=6  a[7]=7
b[0]=0   b[1]=1  b[2]=2  b[3]=3  b[4]=4  b[5]=5  b[6]=6  b[7]=7
stdout
Standard output is empty
stderr
Error: unexpected '/' in "/"
Execution halted