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. // All processes take part in the calculations concurrently
  53. // determine how many elements each process will work on
  54. n_per_proc = n/total_proc;
  55. /////// NOTE:
  56. // In this simple version, the number of processes needs to
  57. // divide evenly into the number of elements in the array
  58. ///////////
  59. // 5. Initialize my smaller subsections of the larger array
  60. ap = (int *) malloc(sizeof(int)*n_per_proc);
  61. bp = (int *) malloc(sizeof(int)*n_per_proc);
  62. cp = (int *) malloc(sizeof(int)*n_per_proc);
  63. // 6.
  64. //scattering array a from MASTER node out to the other nodes
  65. MPI_Scatter(a, n_per_proc, MPI_INT, ap, n_per_proc, MPI_INT, MASTER, MPI_COMM_WORLD);
  66. //scattering array b from MASTER node out to the other node
  67. MPI_Scatter(b, n_per_proc, MPI_INT, bp, n_per_proc, MPI_INT, MASTER, MPI_COMM_WORLD);
  68. // 7. Compute the addition of elements in my subsection of the array
  69. for(i=0;i<n_per_proc;i++)
  70. cp[i] = ap[i]+bp[i];
  71. // 8. MASTER node gathering array c from the workers
  72. MPI_Gather(cp, n_per_proc, MPI_INT, c, n_per_proc, MPI_INT, MASTER, MPI_COMM_WORLD);
  73. /////////////////////// all concurrent processes are finished once they all communicate
  74. /////////////////////// data back to the master via the gather function.
  75. // Master process gets to here only when it has been able to gather from all processes
  76. if (rank == MASTER) {
  77. // sanity check the result (a test we would eventually leave out)
  78. int good = 1;
  79. for(i=0;i<n;i++) {
  80. printf ("c[%d]=%d\t ",i, c[i]);
  81. if (c[i] != a[i] + b[i]) {
  82. printf("problem at index %lld\n", i);
  83. good = 0;
  84. break;
  85. }
  86. }
  87. if (good) {
  88. printf ("Values correct!\n");
  89. }
  90. }
  91. // clean up memory
  92. if (rank == MASTER) {
  93. free(a); free(b); free(c);
  94. }
  95. free(ap); free(bp); free(cp);
  96.  
  97. // 9. Terminate MPI Environment and Processes
  98. MPI_Finalize();
  99.  
  100. return 0;
  101. }
Success #stdin #stdout #stderr 0.25s 41036KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '/' in "/"
Execution halted