Advertisement
Tvor0zhok

ParProg8.7

May 15th, 2023
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <iomanip>
  4. #include "mpi.h"
  5. using namespace std;
  6.  
  7. struct TParticle
  8. {
  9.     double Coords[3];
  10.     int Charge;
  11.     double Mass;
  12.     char Chr[8];
  13. };
  14.  
  15. int main(int argc, char** argv)
  16. {
  17.     TParticle Particle[3];
  18.  
  19.     MPI_Init(NULL, NULL);
  20.  
  21.     int Rank;
  22.     MPI_Comm_rank(MPI_COMM_WORLD, &Rank);
  23.  
  24.     MPI_Datatype TType[4] = { MPI_DOUBLE, MPI_INT, MPI_DOUBLE, MPI_CHAR };
  25.     int BlockLen[4] = { 3,1,1,8 };
  26.  
  27.     MPI_Aint Disp[4];
  28.     MPI_Aint Base;
  29.  
  30.     MPI_Get_address(&Particle[0], &Base);
  31.     MPI_Get_address(&(Particle[0].Coords), &Disp[0]);
  32.     MPI_Get_address(&(Particle[0].Charge), &Disp[1]);
  33.     MPI_Get_address(&(Particle[0].Mass), &Disp[2]);
  34.     MPI_Get_address(&(Particle[0].Chr), &Disp[3]);
  35.  
  36.     for (int k = 0; k < 4; k++)
  37.         Disp[k] -= Base;
  38.  
  39.     MPI_Datatype My_Particle;
  40.     MPI_Type_create_struct(4, BlockLen, Disp, TType, &My_Particle);
  41.     MPI_Type_commit(&My_Particle);
  42.  
  43.     if (Rank == 0)
  44.     {
  45.         for (int k = 0; k < 3; k++)
  46.             for (int j = 0; j < 8; j++)
  47.                 Particle[k].Chr[j] = 0;
  48.  
  49.         Particle[0].Coords[0] = 1.1;
  50.         Particle[0].Coords[1] = 7.3;
  51.         Particle[0].Coords[2] = 3.7;
  52.         Particle[0].Charge = 1;
  53.         Particle[0].Mass = 7.83;
  54.         Particle[0].Chr[0] = 'P';
  55.         Particle[0].Chr[1] = '1';
  56.         Particle[1].Coords[0] = 1.1;
  57.         Particle[1].Coords[1] = -7.3;
  58.         Particle[1].Coords[2] = 3.7;
  59.         Particle[1].Charge = -1;
  60.         Particle[1].Mass = 1.83;
  61.         Particle[1].Chr[0] = 'P';
  62.         Particle[1].Chr[1] = '2';
  63.         Particle[2].Coords[0] = -1.1;
  64.         Particle[2].Coords[1] = 7.3;
  65.         Particle[2].Coords[2] = -3.7;
  66.         Particle[2].Charge = 0;
  67.         Particle[2].Mass = 7.87;
  68.         Particle[2].Chr[0] = 'P';
  69.         Particle[2].Chr[1] = '3';
  70.  
  71.         cout << "Process 0 sends the data" << endl;
  72.  
  73.         for (int k = 0; k < 3; k++)
  74.             cout << "Particle " << Particle[k].Chr << endl
  75.             << "Coordinates " << Particle[k].Coords[0] << " "
  76.             << Particle[k].Coords[1] << " " << Particle[k].Coords[2] << endl
  77.             << "Mass " << Particle[k].Mass
  78.             << " Charge " << Particle[k].Charge << endl;
  79.  
  80.         MPI_Send(&Particle[0], 3, My_Particle, 1, 5, MPI_COMM_WORLD);
  81.     }
  82.  
  83.     if (Rank == 1)
  84.     {
  85.         MPI_Status St;
  86.         MPI_Recv(&Particle[0], 3, My_Particle, 0, 5, MPI_COMM_WORLD, &St);
  87.         cout << "Process 1 has accepted the data" << endl;
  88.  
  89.         for (int k = 0; k < 3; k++)
  90.             cout << "Particle " << Particle[k].Chr << endl
  91.             << "Coordinates " << Particle[k].Coords[0] << " "
  92.             << Particle[k].Coords[1]
  93.             << " " << Particle[k].Coords[2] << endl
  94.             << "Mass " << Particle[k].Mass
  95.             << " Charge " << Particle[k].Charge << endl;
  96.     }
  97.  
  98.     MPI_Type_free(&My_Particle);
  99.     MPI_Finalize();
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement