如何将 MPI_BCast 与自己的数据类型对象的动态数组一起使用
通过 MPI_BCast(...)
传递动态对象指针数组时出现问题。当我尝试发送数组时,我收到错误提前结束并且可能已经崩溃。退出代码 0xc0000005
。如果我将 MPI_Bcast(...)
与一个对象一起使用(例如 MPI_Bcast(myObjArray[0], dataTypeMyObject, 1, 0, MPI_COMM_WORLD);
),它可以正常工作。我需要在实现中更改什么才能发送整个数组? 这是我的类的代码
class Vector3 final
{
public:
double x;
double y;
double z;
//...(methods)
}
class MyObject final
{
public:
Vector3 Force;
Vector3 Speed;
//...(methods)
};
这是数据类型 init 的代码
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Datatype dataTypeVec3;
int lenVec3[3] = { 1, 1, 1 };
MPI_Aint posVec3[3] = { offsetof(class Vector3, x),offsetof(class Vector3, y), offsetof(class Vector3, z) };
MPI_Datatype typVec3[3] = { MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE };
MPI_Type_create_struct(3, lenVec3, posVec3, typVec3, &dataTypeVec3);
MPI_Type_commit(&dataTypeVec3);
MPI_Datatype dataTypeMyObject;
int lenMyObject[2] = { 1, 1 };
MPI_Aint posMyObject[2] = { offsetof(class MyObject, Force),offsetof(class MyObject, Speed)};
MPI_Datatype typMyObject[2] = { dataTypeVec3, dataTypeVec3};
MPI_Type_create_struct(2, lenMyObject, posMyObject, typMyObject, &dataTypeMyObject);
MPI_Type_commit(&dataTypeMyObject);
这是代码的一部分 MPI_BCast(...)
MyObject** myObjArray = new MyObject * [10];
for (int i = 0; i < 10; ++i)
{
myobjArray[i] = new MyObject();
}
if(rank == 0)
myObjArray[0]->Speed = {5, 0, 0};
MPI_Bcast(myObjArray, 10, dataTypeMyObject, 0, MPI_COMM_WORLD); // Problem is here
if(rank != 0)
std::cout << "rank = "<< rank << " and speed.x = " << myObjArray[0].Speed.x << std::endl;
There was a problem with passing dynamic array of object pointers via MPI_BCast(...)
. When I try to send array that I got error ended prematurely and may have crashed. exit code 0xc0000005
. If I use MPI_BCast(...)
with one object (like this MPI_Bcast(myObjArray[0], dataTypeMyObject, 1, 0, MPI_COMM_WORLD);
) it work correctly. What I need to change in my implementation to send the whole array?
Here is the code of my classes
class Vector3 final
{
public:
double x;
double y;
double z;
//...(methods)
}
class MyObject final
{
public:
Vector3 Force;
Vector3 Speed;
//...(methods)
};
Here is a code of datatype init
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Datatype dataTypeVec3;
int lenVec3[3] = { 1, 1, 1 };
MPI_Aint posVec3[3] = { offsetof(class Vector3, x),offsetof(class Vector3, y), offsetof(class Vector3, z) };
MPI_Datatype typVec3[3] = { MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE };
MPI_Type_create_struct(3, lenVec3, posVec3, typVec3, &dataTypeVec3);
MPI_Type_commit(&dataTypeVec3);
MPI_Datatype dataTypeMyObject;
int lenMyObject[2] = { 1, 1 };
MPI_Aint posMyObject[2] = { offsetof(class MyObject, Force),offsetof(class MyObject, Speed)};
MPI_Datatype typMyObject[2] = { dataTypeVec3, dataTypeVec3};
MPI_Type_create_struct(2, lenMyObject, posMyObject, typMyObject, &dataTypeMyObject);
MPI_Type_commit(&dataTypeMyObject);
Here is a part of the code with MPI_BCast(...)
MyObject** myObjArray = new MyObject * [10];
for (int i = 0; i < 10; ++i)
{
myobjArray[i] = new MyObject();
}
if(rank == 0)
myObjArray[0]->Speed = {5, 0, 0};
MPI_Bcast(myObjArray, 10, dataTypeMyObject, 0, MPI_COMM_WORLD); // Problem is here
if(rank != 0)
std::cout << "rank = "<< rank << " and speed.x = " << myObjArray[0].Speed.x << std::endl;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MPI_Bcast()
期望输入元素在内存中是连续的,即 10 个MyObject
实例在内存中位于“彼此后面”。但在您的情况下,您将指针数组传递给MPI_Bcast()
,但MPI_Bcast()
不会取消引用各个指针。相反,尝试
MPI_Bcast()
expects that the input elements are continuous in memory, i.e. that the 10MyObject
instances are located "behind each other" in memory. But in your case you pass in an array of pointers intoMPI_Bcast()
, butMPI_Bcast()
will not dereference the individual pointers.Instead, try