Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab2MPI.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <mpi.h>
- #include <stddef.h>
- using namespace std;
- const int N=10; //size of vector
- const int n = 4;
- #define MSMPI_NO_DEPRECATE_20
- struct sub_vec {
- vector < double > a;
- double left,right;
- int rank;
- };
- MPI_Datatype createUserType()
- {
- // Set-up the arguments for the type constructor
- MPI_Datatype new_type;
- int count = 4;
- int blocklens[] = { N/n,1,1,1};
- MPI_Aint indices[4];
- indices[0] = (MPI_Aint)offsetof(struct sub_vec, a);
- indices[1] = (MPI_Aint)offsetof(struct sub_vec, left);
- indices[2] = (MPI_Aint)offsetof(struct sub_vec, right);
- indices[3] = (MPI_Aint)offsetof(struct sub_vec, rank);
- MPI_Datatype old_types[] = { MPI_DOUBLE ,MPI_DOUBLE ,MPI_DOUBLE ,MPI_INT };
- MPI_Type_struct(count, blocklens, indices, old_types, &new_type);
- MPI_Type_commit(&new_type);
- return new_type;
- }
- int main(int argc, char *argv[])
- {
- int num_proc=4;
- int my_rank;
- vector <double> main_vec(N); //вектор, в ктором хранится наш начальный массив
- main_vec[0] = main_vec[N / 2] = main_vec[N - 1] = 1;
- sub_vec main_struct; // наша структура
- if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
- cout<< "Error :(" << endl;
- }
- MPI_Request request;
- MPI_Status status;
- MPI_Comm_size(MPI_COMM_WORLD, &num_proc);
- MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
- if (my_rank==0)
- {
- for (int i = 1; i <= num_proc; i++)
- {
- cout << "rank: " << my_rank << endl;
- main_struct.a.assign( main_vec.begin() + (i - 1)*N / num_proc, main_vec.begin() + (i*N / num_proc));
- main_struct.rank = i;
- if (i == 1) main_struct.left = NULL;
- else main_struct.left = main_vec[(i - 1)*N / num_proc-1];
- if (i == num_proc) main_struct.right = NULL;
- else main_struct.right = main_vec[i *N / num_proc];
- // cout << "rank: " << my_rank << " 1left: " << main_struct.left << endl;
- // cout << "rank: " << my_rank << " 1right: " << main_struct.right << endl;
- // cout << "rank: " << my_rank << " 1rank: " << my_rank << endl;
- MPI_Isend(&main_struct, 1, createUserType(), i,0, MPI_COMM_WORLD, &request);
- MPI_Wait(&request, &status);
- }
- }
- else
- {
- cout << "rank: " << my_rank << endl;
- for (int i = 1; i <= num_proc; i++)
- {
- MPI_Irecv(&main_struct, 1, createUserType(), 0, 0, MPI_COMM_WORLD, &request);
- // cout <<"rank: "<<main_struct.rank<< " left: "<< main_struct.left<<endl;
- // cout << "rank: " << main_struct.rank << " right: " << main_struct.right<<endl;
- // cout << "rank: " << main_struct.rank << " elements: " << main_struct.a[0] <<" "<< main_struct.a[1] << endl;
- for (int j = 0; j <3; j++)
- {
- cout<< "rank: " << main_struct.rank << " elements: " << main_struct.a[j] << endl;
- }
- MPI_Wait(&request, &status);
- }
- }
- MPI_Finalize();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement