如何使用 C 在 MPI 中使用 MPI_Datatype 发送嵌套结构
我尝试使用 MPI_Datatype 发送以下结构,但 MPI_Send 在发送结构时崩溃。我想知道如何处理这种情况。以下是我编写的用于定义新 MPI 数据类型的代码:
typedef struct
{
double x;
double y;
} vertex;
typedef struct
{
int num_vertices;
vertex vertex[2];
} vertex_list;
MPI_Datatype vertexType;
MPI_Type_contiguous(2,MPI_DOUBLE,&vertexType);
MPI_Type_commit(&vertexType);
MPI_Datatype vertexListType;
MPI_Datatype typev[3] = {MPI_INT, vertexType, MPI_UB};
int blocklenv[3] = {1, 2, 1};
MPI_Aint dispv[3];
/* compute displacements of structure components */
MPI_Address( vertexl, dispv);
MPI_Address( vertexl[0].vertex, dispv+1);
MPI_Address( vertexl+1, dispv+2);
base = dispv[0];
for (i=0; i <3; i++)
dispv[i] -= base;
/* build datatype describing structure */
MPI_Type_struct( 3, blocklenv, dispv, typev, &vertexListType);
MPI_Type_commit(&vertexListType);
https: //docs.google.com/document/d/1OQFtx0ClkKQx7X91BlVgiizs5D9jShhtgsKafrgC7hk/edit?hl=zh-CN
I am trying to use MPI_Datatype to send the below structure but the MPI_Send crashes while sending the structure. I am wondering how to handle this situation. Here is the code I have written to define new MPI data-type:
typedef struct
{
double x;
double y;
} vertex;
typedef struct
{
int num_vertices;
vertex vertex[2];
} vertex_list;
MPI_Datatype vertexType;
MPI_Type_contiguous(2,MPI_DOUBLE,&vertexType);
MPI_Type_commit(&vertexType);
MPI_Datatype vertexListType;
MPI_Datatype typev[3] = {MPI_INT, vertexType, MPI_UB};
int blocklenv[3] = {1, 2, 1};
MPI_Aint dispv[3];
/* compute displacements of structure components */
MPI_Address( vertexl, dispv);
MPI_Address( vertexl[0].vertex, dispv+1);
MPI_Address( vertexl+1, dispv+2);
base = dispv[0];
for (i=0; i <3; i++)
dispv[i] -= base;
/* build datatype describing structure */
MPI_Type_struct( 3, blocklenv, dispv, typev, &vertexListType);
MPI_Type_commit(&vertexListType);
https://docs.google.com/document/d/1OQFtx0ClkKQx7X91BlVgiizs5D9jShhtgsKafrgC7hk/edit?hl=en
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您的结构是否会按照您的预期方式工作,但我可以分享我使用 MPI_Send 发送结构的经验。
无需创建显式 MPI 数据类型,只需发送结构本身即可,因为它的所有内容都位于连续的内存中。诀窍是为 MPI_Send 操作提供正确的大小和数据类型。
使用您的结构,这就是我过去所做的(假设变量
vertex_list list
已经定义):因此要发送的数据缓冲区是指向列表结构的指针, buffer 是 vertex_list 的大小(以字节为单位),MPI 数据类型只是字节。
在接收端,您只需要提供一个 vertex_list 引用作为接收缓冲区:
希望有帮助!
I'm not sure your structs are going to work the way you're intending here, but I can share my experience with sending structs with MPI_Send.
Rather than creating an explicit MPI datatype it's possible to simply send the struct itself since all of its contents are in a contiguous piece of memory. The trick is providing the correct size and datatype for the MPI_Send operation.
Using your structs, here's what I've done in the past (assuming variable
vertex_list list
has already been defined):So the data buffer to be sent is a pointer to the list struct, the size of the buffer is the size of a vertex_list in bytes and the MPI datatype is simply bytes.
On the receiving end, you just need to supply a vertex_list reference as the receiving buffer:
Hope that helps!