C MPI创建并发送具有字段char [16]和整数的结构数组



我知道该网站上有一个问题,但这是在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库。

至少有两个问题

  • oldtypes[1]不是初始化的
  • offsets[1]看起来很奇怪,您可以使用offsetsof()宏代替

如果您的编译器在word严格的末尾添加一些填充物,则可能还必须进行MPI_Type_create_resized()

最新更新