【问题标题】:How can I create a new derived datatype from a struct type in MPI?如何从 MPI 中的结构类型创建新的派生数据类型?
【发布时间】:2013-12-02 13:33:51
【问题描述】:

我正在为 MPI 创建新的派生数据类型,以从 Counter 结构发送数据,您知道在 MPI 中创建新类型是痛苦和棘手的,因为如果我走在正确的轨道上,我需要一些帮助,谢谢?

typedef struct Counter{
int range1,range2,range3,range4;
double preset1 ,preset2 ,preset3 ,preset4;
}  countType;

MPI_Datatype createRecType()
{
    // Set-up the arguments for the type constructor
    MPI_Datatype new_type;
    int count = 2;

    int blocklens[] = { 4, 4 };
    MPI_Aint indices[4];
    indices[0] = 0;
     MPI_Type_extent( MPI_DOUBLE, &indices[1] );
     indices[1] *= 4;    // There are 2 doubles
    MPI_Datatype old_types[] = { MPI_INT ,MPI_DOUBLE};
        // Call the data type constructor
    MPI_Type_struct(count, blocklens, indices, old_types, &new_type);
    MPI_Type_commit(&new_type);

    return new_type;
}

【问题讨论】:

  • 什么是drive datatype?我对 MPI 知之甚少,但我无法将其与任何东西相匹配.. 你的意思是 derived datatype,即具有多个字段的结构将充当缓冲区的“项目”?
  • 如果您的意思是“衍生”,请更正您的问题和主题。 “驱动器”具有误导性。我没有更正,因为我不确定你的意思。

标签: c++ c struct mpi cluster-computing


【解决方案1】:

如果我猜对了,请参阅https://computing.llnl.gov/tutorials/mpi/#Derived_Data_Types 和“示例:结构派生数据类型”部分。它介绍了如何描述定义为的“粒子”:

typedef struct {
   float x, y, z;
   float velocity;
   int  n, type;
} Particle;

以便 MPI 可以处理它们的数组。整个相关的 sn-p 是:

/* Setup description of the 4 MPI_FLOAT fields x, y, z, velocity */
offsets[0] = 0;
oldtypes[0] = MPI_FLOAT;
blockcounts[0] = 4;

/* Setup description of the 2 MPI_INT fields n, type */
/* Need to first figure offset by getting size of MPI_FLOAT */
MPI_Type_extent(MPI_FLOAT, &extent);
offsets[1] = 4 * extent;
oldtypes[1] = MPI_INT;
blockcounts[1] = 2;

/* Now define structured type and commit it */
MPI_Type_struct(2, blockcounts, offsets, oldtypes, &particletype);
MPI_Type_commit(&particletype);

您的代码似乎遵循所有这些。除非您有一些拼写错误,否则您的代码似乎还可以。但除非我尝试编译和运行它,否则我不能保证;)

可以肯定的是,您的indices 太大了。 [2] 就足够了。但让它更大不会做任何坏事。 count 表示 2,因此 MPI 不会从该数组中读取那些额外的元素。

【讨论】:

  • 请记住,您必须确保所有节点都以相同的方式定义相同的结构。如果您对所有这些都运行一个相同的程序,那么不用担心,它们会的。但是,如果您在等级上做一些分支并拥有即。 #0-master, #1..#n-slaves,然后确保 slaves 和 master 都定义它。在主节点很容易忘记它。
  • @melnajjar:我已经检查过了,但您似乎已经删除了它:)
猜你喜欢
  • 2014-01-20
  • 2012-10-23
  • 2016-10-18
  • 2012-02-11
  • 2014-12-03
  • 2014-08-08
  • 2017-05-27
  • 2019-10-19
相关资源
最近更新 更多