1

我有以下简单的 MPI 代码:

#include <iostream>
#include <mpi.h>

int main() {
    struct test {
        int rank = 1;
        double begin = 0.5;
        double end = 0.5;
    };

    MPI_Init(NULL, NULL);
    
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    MPI_File fh;
    MPI_Offset offset;
    MPI_Status status;
    MPI_Datatype TIMEDATA_DATATYPE;

    // Create datatype
    int blocklen[3] = {1,1,1};
    MPI_Datatype type[3] = { MPI_INT, MPI_DOUBLE, MPI_DOUBLE };
    MPI_Aint disp[3] = {0, 4, 12};
    MPI_Type_create_struct(3, blocklen, disp, type, &TIMEDATA_DATATYPE);
    MPI_Type_commit(&TIMEDATA_DATATYPE);

    // Open file
    offset = 20*world_rank;

    MPI_File_open(MPI_COMM_WORLD, "test.bin", MPI_MODE_CREATE | MPI_MODE_WRONLY,
            MPI_INFO_NULL, &fh);

    // Write to file
    test t1, t2;
    MPI_File_write_at(fh, offset, &t1, 1, TIMEDATA_DATATYPE, &status);
    MPI_File_write_at(fh, offset, &t2, 1, TIMEDATA_DATATYPE, &status);

    // Close file
    MPI_File_close(&fh);

    MPI_Finalize();
    
    return 0;
}

运行和编译

mpic++ Test.cpp
mpirun -n 2 a.out 

所以在上面的代码中,我基本上想两次写入同一个文件。每次来自不同的过程。数据是格式为 int、double、double 的结构体,因此 4+8+8=20 字节的数据。我们有该结构的两个对象,但都使用相同的值初始化。因此,我们将两个 20 字节的数据块写入文件 test.bin,我希望在二进制表示中看到这种“对称性”,但我得到:(我提供了两个不同 cli 工具的两个不同输出。)

$ xxd test.bin 
00000000: 0100 0000 fe7f 0000 0000 0000 0000 e03f  ...............?
00000010: 0000 e03f 0100 0000 ff7f 0000 0000 0000  ................
00000020: 0000 e03f 0000 0000                      ...?....

$ hexdump test.bin 
0000000 0001 0000 7ffe 0000 0000 0000 0000 3fe0
0000010 0000 0000 0001 0000 7fff 0000 0000 0000
0000020 0000 3fe0 0000 0000                    
0000028

现在,如果我们查看 xxd 的输出,我们会看到:

前 20 个字节:

  • 整数:0100 0000
  • 双倍:fe7f 0000 0000 0000
  • 双倍:0000 e03f 0000 e03f

第二个 20 字节:

  • 整数:0100 0000
  • 双倍:ff7f 0000 0000 0000
  • 双倍:0000 e03f 0000 0000

现在基本上,我不完全确定为什么这里的双打不同。

4

1 回答 1

3

代替:

MPI_Aint disp[3] = {0, 4, 12};

请执行下列操作:

disp[0] = offsetof(test, rank);
disp[1] = offsetof(test, begin);
disp[2] = offsetof(test, end);

不要忘记包括“ #include <stddef.h>”,并调整偏移量(即, offset = sizeof(test) * world_rank;

不要尝试手动硬编码偏移量,最好使用offsetoffrom<stddef.h>为您解决。无法保证结构内部使用的填充将与您在数组中硬编码的位移值相匹配(即, disp

结构填充是 C 中的一个概念,它在内存地址之间添加一个或多个空字节以对齐内存中的数据。(来源)。为了更好地理解填充查看这个 SO线程

于 2020-12-13T21:26:18.683 回答