1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <mpi.h>
#include <vector>
int main(int argc, char *argv[])
{
int num_procs, myrank;
MPI_Init(&argc, &argv);
MPI_Status status;
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
std::vector<int> vec_local;
if (myrank == 0) {
std::vector<std::vector<int>> vec = { {1,2},{2,1,3},{2,3,7,4},{1,7,4,6,4,2,4} };
for (int i_proc = 0; i_proc < num_procs; i_proc++) {
if (i_proc == 0) {
vec_local = vec[i_proc];
}
else {
int size = vec[i_proc].size();
MPI_Send(&size, 1, MPI_INT, i_proc, 0, MPI_COMM_WORLD);
MPI_Send(vec[i_proc].data(), vec[i_proc].size(), MPI_INT, i_proc, 0, MPI_COMM_WORLD);
}
}
}
else {
int size;
MPI_Recv(&size, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
vec_local.resize(size);
MPI_Recv(vec_local.data(), size, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
}
printf("process %d prainting local_vector:\n", myrank);
for (int i = 0; i < vec_local.size(); i++)
{
printf("%d \t", vec_local[i], myrank);
printf("\n");
}
MPI_Finalize();
return 0;
}
| |