向 MPI 结构添加填充

Add padding to MPI structure

我有一个 C 结构数组,我想通过读取文件来填充它(并行地,使用 set_view 等等)

typedef struct
{
    char   type;
    double value;
    double velocity;
} Cell;

我的问题是某些文件(type1)将只有 typevalue(在这种情况下速度必须留给 O,而在其他一些文件(type2)中我有typevaluevelocity

因此,当读取文件中的 n 块时,我要么读取 n x 9 位(case1),要么读取 n x 17 位((case2),我必须将其放入具有良好对齐方式的缓冲区中.

我从 mpi_cell_aligned 类型开始

MPI_Datatype mpi_cell_aligned;
int          count[] = { 1,                    1,                     1                        };
MPI_Aint     displ[] = { offsetof(Cell, type), offsetof(Cell, value), offsetof(Cell, velocity) };
MPI_Datatype types[] = { MPI_CHAR,             MPI_DOUBLE,            MPI_DOUBLE               };
switch(type)
{
    case 1: MPI_Type_create_struct(2, count, displ, types, &mpi_cell_aligned); break;
    case 2: MPI_Type_create_struct(3, count, displ, types, &mpi_cell_aligned); break;
}
MPI_Type_commit(&mpi_cell_aligned);

并且使用 MPI_Type_contiguous 我还 built 一个 mpi_cell_packed 类型代表 9/17 连续位(ui 二进制文件中的格式)。

我的问题是写入我的缓冲区,我正在尝试 build 包含多个 mpi_cell_aligned 的向量类型。在情况 2 中很容易,因为每种类型都紧挨着另一个,但在情况 1 中,我必须考虑我的类型之间的填充,它对应于 1 double 的长度。

不幸的是,MPI_Type_Vector 的步幅必须以结构的数量来衡量,而不是以字节为单位。与此同时,我不能只使用 MPI_BYTE 来描述我的向量,因为我的单元格结构不完整(char 和第一个 double 之间的对齐填充)。

我如何才能创建uild 相应的 MPI 数据类型,以正确表示情况 1 中的 Cell 数组?

情况1中,您必须修改mpi类型的范围。

类型的范围是用于知道在 send/recv/write/read 操作中在哪里找到以下元素的大小。

主要功能是MPI_Type_create_resized。在您的情况下,案例 1 中 mpi 类型的范围必须与案例 2 中 mpi 类型的范围相同。

所以你必须这样做:

/* Temporary type */
MPI_Type_create_struct(2, count, displ, types, &mpi_cell_aligned_temp);
/* Compute new extent */
MPI_Type_size(mpi_cell_aligned_temp,&size_double);
extent = offsetof(Cell, velocity)+size_double;
/* Create new type with new extent */
MPI_Type_create_resized(mpi_cell_aligned_temp,0, extent,&mpi_cell_aligned_temp);