如何将 MPI_BCast 与自己的数据类型对象的动态数组一起使用

发布于 2025-01-15 10:32:17 字数 1850 浏览 3 评论 0原文

通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花想c 2025-01-22 10:32:17

MPI_Bcast() 期望输入元素在内存中是连续的,即 10 个 MyObject 实例在内存中位于“彼此后面”。但在您的情况下,您将指针数组传递给 MPI_Bcast(),但 MPI_Bcast() 不会取消引用各个指针。
相反,尝试

MyObject * myObjArray = new MyObject[10];
// ... initialize objects on rank 0
MPI_Bcast(myObjArray, 10, dataTypeMyObject, 0, MPI_COMM_WORLD);

MPI_Bcast() expects that the input elements are continuous in memory, i.e. that the 10 MyObject instances are located "behind each other" in memory. But in your case you pass in an array of pointers into MPI_Bcast(), but MPI_Bcast() will not dereference the individual pointers.
Instead, try

MyObject * myObjArray = new MyObject[10];
// ... initialize objects on rank 0
MPI_Bcast(myObjArray, 10, dataTypeMyObject, 0, MPI_COMM_WORLD);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文