#include <mpi.h>#include<vector>std::vector<std::vector<int>> inds;int inds_size;std::vector<int> inds_i_size;if (myid == 0) {//在进程0里获取indsfor (int i1 = 0; i1 < 2; ++i1) {for (int i3 = 0; i3 < 4; ++i3) {std::vector<int> ind{ i1, i3 };inds.emplace_back(ind);}}inds_size = inds.size();inds_i_size.resize(inds.size());for (int i = 0; i < inds.size(); ++i) {inds_i_size[i] = inds[i].size();for (int j = 0; j < inds[i].size(); ++j) {std::cout << "inds[" << i << "][" << j << "]=" << inds[i][j] << ", ";}std::cout << std::endl;}}MPI_Bcast(&inds_size, 1 * sizeof(int), MPI_BYTE, 0, MPI_COMM_WORLD);//先广播inds的外层的大小if (myid > 0)inds_i_size.resize(inds_size);//再广播inds的内层的向量的大小MPI_Bcast(inds_i_size.data(), inds_i_size.size() * sizeof(int), MPI_BYTE, 0, MPI_COMM_WORLD);//然后在其他进程里开辟外层向量的空间大小,然后再开辟内层的向量的空间if (myid > 0) {inds.resize(inds_size);for (int i = 0; i < inds.size(); ++i)inds[i].resize(inds_i_size[i]);}//然后在其他进程里广播内层向量for (int i = 0; i < inds.size(); ++i) {MPI_Bcast(inds[i].data(), inds[i].size() * sizeof(int), MPI_BYTE, 0, MPI_COMM_WORLD);}std::vector<std::vector<std::vector<int>>> inds_id(numprocs - 1); // except process 0//从进程0发送inds给其他进程for (int i = 0; i < inds.size(); ++i) {if (myid == 0) {int ret = MPI_Send(inds[i].data(), inds[i].size() * sizeof(int), MPI_BYTE, i % (numprocs-1) + 1, i, MPI_COMM_WORLD);std::cout << "MPI_Send ret=" << ret << ", process 0 send to process " << (i % (numprocs - 1)) + 1 << " send: ";for (int j = 0; j < inds[i].size(); ++j) {std::cout << inds[i][j] << " ";}std::cout << std::endl;}if (myid == i % (numprocs - 1) + 1) {std::vector<int> ind_recv(inds[i].size());int ret = MPI_Recv(ind_recv.data(), ind_recv.size() * sizeof(int), MPI_BYTE, 0, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);std::cout << "MPI_Recv ret=" << ret <<", process " << myid << " received from process 0 : ";for (int j = 0; j < ind_recv.size(); ++j) {std::cout << ind_recv[j] << " ";}std::cout << std::endl;inds_id[myid-1].emplace_back(ind_recv);}}
C++ MPI_Send, MPI_Recv 双重向量
2025/1/31 15:39:30
来源:https://blog.csdn.net/reyyy/article/details/141301194
浏览:
次
关键词:C++ MPI_Send, MPI_Recv 双重向量
版权声明:
本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。
我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com