如何解决使用 MPI_Gatherv 处理结构向量时如何设置位移?
对于下面的代码(这不是整个程序;它只是一个代码片段),我不知道我应该如何设置位移数组来使用MPI_Gatherv。因为 getBrdy 的结果对于每个等级都是可变的;我必须使用 MPI_Gatherv
。在我搜索时,当我将消息作为结构向量发送时,我不确定人们对 int
或 string
等类型使用的相同方式是否仍然适用。
MPI_Init(NULL,NULL);
int rank,size;
int totalLengthBuffer,localSize;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_rank(MPI_COMM_WORLD,&size);
int *receiveCount = new int[size];
int *disps = new int[size];
std::vector<struct Bdry> temp;
temp=this->getBdry(rank);
localSize=temp.size();
MPI_Allreduce(&localSize,&totalLengthBuffer,1,MPI_INT,MPI_SUM,MPI_COMM_WORLD);
std::vector<struct Bdry> buffer(totalLengthBuffer);
constexpr std::size_t num_members=3;
int lengths[num_members]={1,3};
MPI_Aint offsets[num_members]={
offsetof(struct Bdry,ID),offsetof(struct Bdry,Part),coord)
};
MPI_Datatype types[num_members]={MPI_INT,MPI_DOUBLE};
MPI_Datatype type;
MPI_Type_create_struct(num_members,lengths,offsets,types,&type);
MPI_Type_commit(&type);
for(emInt i=0; i<nPart;i++){
receiveCount[i]= this->getBdry(i).size();
}
MPI_Gatherv(temp.data(),temp.size(),type,&buffer,receiveCount,const int *displs,MPI_COMM_WORLD);
MPI_Type_free(&type);
MPI_Finalize();
我的结构是这样的:
struct Bdry{
emInt ID;
emInt Part;
double coord[3];
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。