Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ MPI_File_write_at:两次写入同一结构会导致二进制文件中的数据块略有不同_C++_C_Parallel Processing_Mpi_Mpi Io - Fatal编程技术网

C++ MPI_File_write_at:两次写入同一结构会导致二进制文件中的数据块略有不同

C++ MPI_File_write_at:两次写入同一结构会导致二进制文件中的数据块略有不同,c++,c,parallel-processing,mpi,mpi-io,C++,C,Parallel Processing,Mpi,Mpi Io,我有以下简单的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(MP

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

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

前20个字节:

  • 整数:0100 0000
  • 双倍:fe7f 0000
  • 双人:0000 e03f 0000 e03f
第二个20字节:

  • 整数:0100 0000
  • 双人:ff7f 0000
  • 双精度:0000 e03f 0000 0000
现在基本上,我不太清楚为什么这里的双打不同。

而不是:

MPI_Aint disp[3] = {0, 4, 12};
请执行以下操作:

disp[0] = offsetof(test, rank);
disp[1] = offsetof(test, begin);
disp[2] = offsetof(test, end);
不要忘记包括“
”和调整偏移量(即,
偏移量=大小(测试)*世界排名;

不要试图手动硬编码偏移量,最好使用
中的
偏移量来计算。无法保证结构内部使用的填充将与数组中硬编码的位移值相匹配(即,
disp

结构填充是C语言中的一个概念,它在内存地址之间添加一个或多个空字节,以对齐内存中的数据。(). 为了更好地理解填充,请查看以下内容:

而不是:

MPI_Aint disp[3] = {0, 4, 12};
请执行以下操作:

disp[0] = offsetof(test, rank);
disp[1] = offsetof(test, begin);
disp[2] = offsetof(test, end);
不要忘记包括“
”和调整偏移量(即,
偏移量=大小(测试)*世界排名;

不要试图手动硬编码偏移量,最好使用
中的
偏移量来计算。无法保证结构内部使用的填充将与数组中硬编码的位移值相匹配(即,
disp

结构填充是C语言中的一个概念,它在内存地址之间添加一个或多个空字节,以对齐内存中的数据。(). 为了更好地理解填充,请查看以下内容: