如何使用C++在MPI中使用MPI_DataType发送嵌套结构 [英] How to send nested structure using MPI_Datatype in MPI using C

查看:0
本文介绍了如何使用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=en

推荐答案

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

不需要创建显式的MPI数据类型,也可以简单地发送结构本身,因为它的所有内容都在一个连续的内存块中。诀窍在于为MPI_SEND操作提供正确的大小和数据类型。

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

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

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

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

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

希望能有所帮助!

这篇关于如何使用C++在MPI中使用MPI_DataType发送嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆