Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. /*Copyright 2011 Dmitry Khodakov
  2.   Just sum up natural numbers with MPI
  3.   coded: 1.03.11
  4. */
  5. // TODO(dimert): test on multicore machine!
  6. #include <mpi.h>
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9.  
  10. // mark of every message
  11. #define TAG 0
  12.  
  13. void master(int last_number);
  14. void node();
  15.  
  16. int main(int argc, char *argv[]) {
  17.   int myid = 0;
  18.  
  19.   MPI_Init(&argc, &argv);
  20.   MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  21.  
  22.   if (myid == 0)
  23.     master(atoi(argv[1]));
  24.   else
  25.     node();
  26.  
  27.   MPI_Finalize();
  28.   return 0;
  29. }
  30.  
  31. // master (id = 0) process, it split task and get first part of it
  32. // args: last_number - bottom of natural sequence.
  33. void master(int last_number) {
  34.   MPI_Status stat;  // status of receiving. I didn't use it here. Don't delete.
  35.   uint32_t sended_res;  // result of partial computation by node
  36.   uint32_t res = 0;     // overall result
  37.   int numproc = 1;  // number of nodes in network
  38.   // pair of numbert for node to sum, task[0]-first, task[1]-last
  39.   int task[2];
  40.  
  41. // getting number of machines
  42.   MPI_Comm_size(MPI_COMM_WORLD, &numproc);
  43.   printf("size of world = %d\n", numproc);
  44. // splitting to tasks
  45. // TODO(dimert): dynamically change task size depending on sum size
  46.   int task_size = last_number / numproc;
  47.   int modulo = last_number % numproc;  // counter for modulo.
  48.   int cur_size = 0;
  49.   printf("divided into parts of %d, size to proc, with modulo %d\n", task_size,
  50.       modulo);
  51. // sending tasks
  52.   int first_piece = (modulo == 0)? task_size : task_size + 1;
  53.   cur_size = first_piece;
  54.  
  55.   for (int i = 1, add_count = 0; i < numproc; ++i, ++add_count) {
  56.     task[0] = cur_size + 1;
  57.     cur_size = task_size;
  58.     if (add_count < modulo)
  59.       ++cur_size;
  60.     task[1] = cur_size;
  61.     printf("sending from %d to %d\n", task[0], task[1]);
  62.  
  63.     MPI_Send(task, 2, MPI_INT, i, TAG, MPI_COMM_WORLD);
  64.   }
  65. // first piece of computations
  66.   for (int n = 1; n <= first_piece; ++n)
  67.     res += n;
  68.  
  69. // gathering info
  70.   for (int i = 1; i < numproc; ++i) {
  71.     sended_res = 0;
  72.  
  73.     MPI_Recv(&sended_res, 1, MPI_UNSIGNED_LONG, i, TAG, MPI_COMM_WORLD, &stat);
  74.  
  75.     printf("processor %d, res = %d\n", i, sended_res);
  76.     res += sended_res;
  77.   }
  78.   printf("end res = %d\n", res);
  79. }
  80.  
  81. // calculating process, takes tasks from master
  82. void node() {
  83.   MPI_Status stat;
  84.   int task[2];
  85.   uint32_t sended_res;
  86.   MPI_Recv(task, 2, MPI_INT, 0, TAG, MPI_COMM_WORLD, &stat);
  87.   for (int n = task[0], sended_res = 0; n <= task[1]; ++n)
  88.     sended_res += n;
  89.   MPI_Send(&sended_res, 1, MPI_UNSIGNED_LONG, 0, TAG, MPI_COMM_WORLD);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement