Advertisement
tatty_ko

Lab_2_MPI

Nov 1st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Lab2MPI.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <mpi.h>
  8. #include <stddef.h>  
  9. using namespace std;
  10.  
  11. const int N=10; //size of vector
  12. const int n = 4;
  13.  
  14. #define MSMPI_NO_DEPRECATE_20
  15.  
  16. struct sub_vec {
  17.     vector < double > a;
  18.     double left,right;
  19.     int rank;
  20. };
  21.  
  22.  
  23. MPI_Datatype createUserType()
  24. {
  25.     // Set-up the arguments for the type constructor
  26.     MPI_Datatype new_type;
  27.  
  28.     int count = 4;
  29.     int blocklens[] = { N/n,1,1,1};
  30.  
  31.     MPI_Aint indices[4];
  32.     indices[0] = (MPI_Aint)offsetof(struct sub_vec, a);
  33.     indices[1] = (MPI_Aint)offsetof(struct sub_vec, left);
  34.     indices[2] = (MPI_Aint)offsetof(struct sub_vec, right);
  35.     indices[3] = (MPI_Aint)offsetof(struct sub_vec, rank);
  36.  
  37.     MPI_Datatype old_types[] = { MPI_DOUBLE ,MPI_DOUBLE ,MPI_DOUBLE ,MPI_INT };
  38.  
  39.     MPI_Type_struct(count, blocklens, indices, old_types, &new_type);
  40.     MPI_Type_commit(&new_type);
  41.  
  42.     return new_type;
  43. }
  44.  
  45. int main(int argc, char *argv[])
  46. {
  47.     int num_proc=4;
  48.     int my_rank;
  49.     vector <double> main_vec(N); //вектор, в ктором хранится наш начальный массив
  50.     main_vec[0] = main_vec[N / 2] = main_vec[N - 1] = 1;
  51.     sub_vec main_struct;    // наша структура
  52.  
  53.  
  54.     if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
  55.         cout<< "Error :(" << endl;
  56.     }
  57.     MPI_Request request;
  58.     MPI_Status status;
  59.     MPI_Comm_size(MPI_COMM_WORLD, &num_proc);
  60.     MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  61.  
  62.  
  63.     if (my_rank==0)
  64.     {
  65.         for (int i = 1; i <= num_proc; i++)
  66.         {
  67.             cout << "rank: " << my_rank << endl;
  68.             main_struct.a.assign( main_vec.begin() + (i - 1)*N / num_proc, main_vec.begin() + (i*N / num_proc));
  69.             main_struct.rank = i;
  70.             if (i == 1) main_struct.left = NULL;
  71.             else main_struct.left = main_vec[(i - 1)*N / num_proc-1];
  72.             if (i == num_proc) main_struct.right = NULL;
  73.             else main_struct.right = main_vec[i *N / num_proc];
  74.  
  75.         //  cout << "rank: " << my_rank << "  1left: " << main_struct.left << endl;
  76.         //  cout << "rank: " << my_rank << "  1right: " << main_struct.right << endl;
  77.         //  cout << "rank: " << my_rank << "  1rank: " << my_rank << endl;
  78.             MPI_Isend(&main_struct, 1, createUserType(), i,0, MPI_COMM_WORLD, &request);
  79.             MPI_Wait(&request, &status);
  80.         }
  81.     }
  82.  
  83.     else
  84.     {
  85.         cout << "rank: " << my_rank << endl;
  86.         for (int i = 1; i <= num_proc; i++)
  87.         {
  88.             MPI_Irecv(&main_struct, 1, createUserType(), 0, 0, MPI_COMM_WORLD, &request);
  89.         //  cout <<"rank: "<<main_struct.rank<< "  left: "<< main_struct.left<<endl;
  90.         //  cout << "rank: " << main_struct.rank << "  right: " << main_struct.right<<endl;
  91.         //  cout << "rank: " << main_struct.rank << "  elements: " << main_struct.a[0] <<" "<< main_struct.a[1] << endl;
  92.             for (int j = 0; j <3; j++)
  93.             {  
  94.                 cout<< "rank: " << main_struct.rank << "  elements: " << main_struct.a[j] << endl;
  95.             }
  96.             MPI_Wait(&request, &status);
  97.         }
  98.     }
  99.  
  100.  
  101.    
  102.     MPI_Finalize();
  103.     system("pause");
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement