【发布时间】:2017-05-08 01:15:42
【问题描述】:
我想用这个最少的代码将一个 struct 类型的向量中的多个元素分散到多个进程中
struct node
{
scale2 P;
scale2 V;
float M;
//
node(float M, float Px, float Py) // constructor
:P(Px,Py)
, V( 0.f, 0.f )
, M(m)
{}
};
主要功能
int main(int argc, char **argv){
std::vector<node> bodies;
std::vector<node> b;
int rank, size, ROOT =0;
long int pC;
MPI_Datatype MPI_NODE;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
//creating data type that represents node
//setup description of scale2 and float
// define and commit structure type
if(rank == ROOT){
// initialise bodies
pC = bodies.size();
cout <<pC << " nodes" << endl;
}
MPI_Bcast(&pC, 1, MPI_LONG, ROOT, MPI_COMM_WORLD);
MPI_Scatter(&bodies, pC/size, MPI_NODE, &b, pC/size, MPI_NODE, ROOT, MPI_COMM_WORLD);
cout << "rank "<<rank<<" has " <<b.size()<<" values of bodies"<<endl;
//other stuff
MPI_Finalize();
return 0;
}
我正在寻找的输出(有 4 个进程和主体大小 = 64)是
rank 0 has 16 values of bodies
rank 1 has 16 values of bodies
rank 2 has 16 values of bodies
rank 3 has 16 values of bodies
我得到的输出
rank 0 has 64 values of bodies
rank 2 has 2818 values of bodies
rank 1 has 0 values of bodies
rank 3 has 11311978710794943764 values of bodies
我确定问题出在MPI_Scatter 行或它下面的行,即b.size() 以错误的方式输出。我的问题也可能是vector<node> b 在MPI_scatter 中传递的方式 其余代码工作正常,因为我在没有这两行的情况下进行了测试。如果这部分代码不是问题(我对此表示怀疑),那么我定义数据类型MPI_NODE 的方式是错误的,在这种情况下,我将编辑我的代码以显示该部分。
【问题讨论】: