【问题标题】:C++ MPI create and send array of structs which has fields char[16] and integerC++ MPI 创建并发送具有字段 char[16] 和 integer 的结构数组
【发布时间】:2018-05-30 07:37:45
【问题描述】:

我知道here 网站上有一个关于此的问题,但这是在 c 中实现的。我需要它的 c++ 版本。

问题如下: 我创建了一个具有以下形式的结构

struct word
{
   char value[WORD_MAX_LENGTH];
   int freq;
};

我需要发送这些结构的数组。如何序列化这个结构并使用 MPI_Send 将其中的 500 个发送到另一个进程。

这是我到目前为止的地方:

    MPI_Datatype word_type, oldtypes[2];
    int blockcounts[2];
    MPI_Aint offsets[2], extent;

    //Determine the offset and block length of the first element of the struct which is a char array.
    offsets[0] = 0;
    oldtypes[0] = MPI_CHAR;
    blockcounts[0] = WORD_MAX_LENGTH;

    //Determine the offset of int ferq of the struct.
    MPI_Type_extent(MPI_CHAR, &extent);
    offsets[1] = 16 * extent;
    blockcounts[1] = 1;

    // Finally create the type.
    MPI_Type_create_struct(2, blockcounts, offsets, oldtypes, &word_type);
    MPI_Type_commit(&word_type);

当我这样做时,它编译时没有错误,但是当我尝试运行它时。 它抱怨

Fatal error in PMPI_Type_create_struct: Invalid datatype, error stack:

PMPI_Type_create_struct(173): MPI_Type_create_struct(count=2, 
array_of_blocklengths=0x7fffbd059f70,   
array_of_displacements=0x7fffbd059f80, array_of_types=0x7fffbd059f60, 
newtype=0x7fffbd059f14) failed

PMPI_Type_create_struct(142): Invalid datatype

任何帮助将不胜感激。 注意:我不能使用 boost 库。

【问题讨论】:

    标签: c++ struct mpi mpich


    【解决方案1】:

    至少有两个问题

    • oldtypes[1] 未初始化
    • offsets[1] 看起来很奇怪,您可以改用 offsetsof()

    如果您的编译器在 word 严格的末尾添加一些填充,您可能还需要 MPI_Type_create_resized()

    【讨论】:

    • 非常感谢!问完问题后我忘记了编辑偏移量。无论如何,我已经更改了它,感谢您的提醒。它们看起来像这样 offsets[1] = (size_t)&(word.freq) - (size_t)&word;现在。我还将 MPI_INT 添加到 oldtypes。现在看来可以了,谢谢:D
    • 这是有效的。我怎样才能将这些结构中的多个发送到从站?
    • MPI_Send(words, n, word_type, ...)
    猜你喜欢
    • 2016-05-08
    • 2015-08-20
    • 2013-12-12
    • 2014-12-20
    • 2021-07-23
    • 2021-06-11
    • 2011-08-23
    • 2016-07-01
    • 2015-01-09
    相关资源
    最近更新 更多