如何使用 C 在 MPI 中使用 MPI_Datatype 发送嵌套结构

发布于 2024-11-06 23:08:55 字数 1254 浏览 1 评论 0原文

我尝试使用 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 技术交流群。

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

发布评论

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

评论(1

近箐 2024-11-13 23:08:55

我不确定您的结构是否会按照您的预期方式工作,但我可以分享我使用 MPI_Send 发送结构的经验。

无需创建显式 MPI 数据类型,只需发送结构本身即可,因为它的所有内容都位于连续的内存中。诀窍是为 MPI_Send 操作提供正确的大小和数据类型。

使用您的结构,这就是我过去所做的(假设变量 vertex_list list 已经定义):

MPI_Send(&list, sizeof(vertex_list), MPI_BYTE, <rankToSendTo>, <tagInteger>, <comm>);

因此要发送的数据缓冲区是指向列表结构的指针, buffer 是 vertex_list 的大小(以字节为单位),MPI 数据类型只是字节。

在接收端,您只需要提供一个 vertex_list 引用作为接收缓冲区:

vertex_list receiveList;
MPI_Recv(&receiveList, sizeof(vertex_list), MPI_BYTE, <rankOfSender>, <tagInteger>, <comm>, <status>);

希望有帮助!

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):

MPI_Send(&list, sizeof(vertex_list), MPI_BYTE, <rankToSendTo>, <tagInteger>, <comm>);

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:

vertex_list receiveList;
MPI_Recv(&receiveList, sizeof(vertex_list), MPI_BYTE, <rankOfSender>, <tagInteger>, <comm>, <status>);

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文