Advertisement
Guest User

lab 5.1

a guest
Dec 11th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <mpi.h>
  3.  
  4. using namespace std;
  5.  
  6. typedef struct three_args_struct {
  7.     int first;
  8.     int second;
  9.     double third;
  10. } triplet;
  11.  
  12. void buildDerivedType(MPI_Datatype* mpi_triplet_type) {
  13.     int nitems = 3;
  14.     int block_lengths[3] = { 1, 1, 1 };
  15.     MPI_Aint offsets[3];
  16.     MPI_Datatype typelist[3] = { MPI_INT, MPI_INT, MPI_DOUBLE };
  17.  
  18.     offsets[0] = offsetof(triplet, first);
  19.     offsets[1] = offsetof(triplet, second);
  20.     offsets[2] = offsetof(triplet, third);
  21.  
  22.     MPI_Type_create_struct(nitems, block_lengths, offsets, typelist, mpi_triplet_type);
  23.     MPI_Type_commit(mpi_triplet_type);
  24. }
  25.  
  26. void initializeArray(triplet* arr, int size) {
  27.     for (int i = 0; i < size; i++) {
  28.         arr[i].first = i;
  29.         arr[i].second = i * i;
  30.         arr[i].third = i / 10.0;
  31.     }
  32. }
  33.  
  34. int main(int argc, char* argv[]) {
  35.     MPI_Init(&argc, &argv);
  36.     cout << "prj5.1" << endl;
  37.     double startTime = MPI_Wtime();
  38.  
  39.     int rank;
  40.     int threads_count;
  41.     MPI_Status status;
  42.  
  43.     MPI_Comm_size(MPI_COMM_WORLD, &threads_count);
  44.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  45.     int side_threads_count = threads_count - 1;
  46.  
  47.     if (threads_count < 2) {
  48.         fprintf(stderr, "Requires at least two processes.\n");
  49.         exit(-1);
  50.     }
  51.  
  52.     triplet* buf = new triplet[threads_count];
  53.     if (rank == 0) {
  54.         initializeArray(buf, threads_count);
  55.         for (int i = 0; i < threads_count; i++) {
  56.             cout << i << ". first = " << buf[i].first << ", second = " << buf[i].second << ", third = " << buf[i].third << endl;
  57.         }
  58.     }
  59.  
  60.     MPI_Datatype* mpi_triplet_type = new MPI_Datatype;
  61.     buildDerivedType(mpi_triplet_type);
  62.     triplet* receivedInf = new triplet;
  63.  
  64.     MPI_Scatter(buf, 1, *mpi_triplet_type, receivedInf, 1, *mpi_triplet_type, 0, MPI_COMM_WORLD);
  65.     int flag = 0;
  66.     if (rank == 0) {
  67.         flag = -1;
  68.         MPI_Send(&flag, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
  69.     }
  70.     else {
  71.         MPI_Recv(&flag, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD, &status);
  72.         cout << rank << ". Printing structure: first = " << receivedInf->first << ", second = " << receivedInf->second << ", third = " << receivedInf->third << endl;
  73.         if (rank != threads_count - 1) {
  74.             MPI_Send(&flag, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD);
  75.         }
  76.     }
  77.  
  78.     double endTime = MPI_Wtime();
  79.     double elapsed = endTime - startTime;
  80.     cout << "Program runtime: " << elapsed << endl;
  81.     MPI_Finalize();
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement