向 MPI 结构添加填充

Amx*_*mxx 3 io types mpi memory-alignment

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

typedef struct
{
    char   type;
    double value;
    double velocity;
} Cell;
Run Code Online (Sandbox Code Playgroud)

我的问题是,一些文件(TYPE1)将只具有typevalue(与速度的情况下,必须留给O,以及其他一些文件(2型)都在我typevaluevelocity

因此,在读取n文件中的块时,我要么读取 nx 9 位(case1)要么读取 nx 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);
Run Code Online (Sandbox Code Playgroud)

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

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

不幸的是,给定的步幅MPI_Type_Vector必须以结构数量来衡量,而不是以字节为单位。我同时我不能仅仅描述我的向量,MPI_BYTE因为我的单元格结构不完整(字符和第一个双精度之间的对齐填充)。

如何构建相应的 MPI 数据类型,以便在情况 1 中正确表示 Cell 数组?

小智 5

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

类型的范围是用于知道在发送/接收/写入/读取操作中在哪里可以找到以下元素的大小。

主要函数是 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);
Run Code Online (Sandbox Code Playgroud)