Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "mpi.h" // message passing interface
  5. using namespace std;
  6.  
  7. // Do this ONCE when you start up thomas
  8. // module load openmpi-x86_64
  9.  
  10. // New compile and run commands for MPI!
  11. // mpicxx -o blah file.cpp
  12. // mpirun -np 32 blah
  13.  
  14. int main (int argc, char * argv[]) {
  15.  
  16. int my_rank; // my CPU number for this process
  17. int p; // number of CPUs that we have
  18. int source; // rank of the sender
  19. int dest; // rank of destination
  20. int tag = 0; // message number
  21. char message[100]; // message itself
  22. MPI_Status status; // return status for receive
  23.  
  24. // Start MPI
  25. MPI_Init(&argc, &argv);
  26.  
  27. // Find out my rank!
  28. MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  29.  
  30. // Find out the number of processes!
  31. MPI_Comm_size(MPI_COMM_WORLD, &p);
  32.  
  33. // THE REAL PROGRAM IS HERE
  34. /*
  35. int a[5] = {7, 4, 3, 6, -2};
  36. int psum[5]={0};
  37.  
  38. psum [0]= a[0];
  39. for (int x=1; x<5; x++)
  40. psum[x] = psum [x-1] + a[x];
  41. */
  42.  
  43. int srand (time (0));
  44. int n = 300000;
  45. int a [300000], psum [300000];
  46.  
  47. if (my_rank==0)
  48. for (int x =0; x<300000; x++)
  49. a[x]= rand () %8;
  50.  
  51. int locala[10000];
  52. double localpsum[10000];
  53. int local_n= n/p;
  54. MPI_Scatter(&a[0], local_n, MPI_INT, locala, local_n, MPI_INT, 0, MPI_COMM_WORLD) ;
  55.  
  56. localpsum[0] =locala [0] ;
  57. for (int x =0; x < 300000; x++)
  58. localpsum[x] = localpsum[x-1] + locala[x];
  59.  
  60. //send my last partial prefix sum to all later processors
  61.  
  62. for (x= my_rank +1; x < p; x++)
  63. MPI_Send(&localpsum[local_n-1], 1, MPI_DOUBLE, x, tag, MPI_COMM_WORLD);
  64.  
  65. // get all of the partial prefix sum answers from all earlier processors
  66.  
  67. double temp=0;
  68. int num_messRec=0; // keeps track of how many messages i processed so far
  69. double localadd=0; // final number we need to add to our prefix sums
  70.  
  71. while (num_messRec < my_rank){
  72. MPI_Recv(&temp, 1, MPI_DOUBLE, MPI_ANY_SOURCE, tag, MPI_ COMM_WORLD, &status);
  73. localadd+= temp;
  74. num_mesg_recvd++;
  75. }
  76.  
  77. //time to add the localadditive to each entry of my local sum arrray
  78.  
  79. for (int x= 0; x <localn; x++)
  80. localpsum[x]+= localadd;
  81.  
  82. // finally collect all the answers by reversing our scatter
  83.  
  84. MPI_Gather(&localpsum[0], local_n, MPI_DOUBLE, psum, local_n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  85.  
  86. if (my_rank ==0)
  87. for (int x=0; x<n; x++){
  88. cout << psum[x] << " ";
  89. if (x %10= 0)
  90. cout << endl;
  91.  
  92. }
  93.  
  94. // Shut down MPI
  95. MPI_Finalize();
  96.  
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement