C++ 使用自定义MPI_数据类型作为文件类型的并行文件输出的MPI_文件\u集\u视图

C++ 使用自定义MPI_数据类型作为文件类型的并行文件输出的MPI_文件\u集\u视图,c++,mpi,C++,Mpi,我很难理解MPI_File_set_view是如何工作的,尤其是第四个参数让我感到困惑 int MPI_文件集视图(MPI_文件fh、MPI_偏移量显示、MPI_数据类型etype、MPI_数据类型文件类型、常量字符*数据代表、MPI_信息) 假设我有n个线程,每个线程都有一个长度为10的字符数组,称为“v”,这个数组的所有条目都等于线程ID thread0:v={0,0,0,0,0,0,0,0} thread1:v={1,1,1,1,1,1,1,1} thread2:v={2,2,2,2,2,

我很难理解MPI_File_set_view是如何工作的,尤其是第四个参数让我感到困惑

int MPI_文件集视图(MPI_文件fh、MPI_偏移量显示、MPI_数据类型etype、MPI_数据类型文件类型、常量字符*数据代表、MPI_信息)

假设我有n个线程,每个线程都有一个长度为10的字符数组,称为“v”,这个数组的所有条目都等于线程ID

thread0:v={0,0,0,0,0,0,0,0}

thread1:v={1,1,1,1,1,1,1,1}

thread2:v={2,2,2,2,2,2,2,2}

现在,我想按以下顺序编写一个包含这些数组值的二进制文件:

文件=thread0.v[0],thread1.v[0],thread2.v[0],…,thread0.v[1],thread1.v[1],thread2.v[1],…,thread0.v[2],thread1.v[2],thread2.v[2]

据我所知,我必须为filetype参数构造一个大小为[sizeof(char)*n]的MPI_数据类型,其中在MPI_char数据之前有一个大小为[sizeof(char)*threadID]的“洞”,在数据之后有另一个大小为[sizeof(char)*(n-threadID-1)]的“洞”。我想这就是我犯错误的地方

我的代码如下所示:

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

using namespace std;


int main(int argc, char* argv[]){
    int myId;
    int numProcs;
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numProcs);
    MPI_Comm_rank(MPI_COMM_WORLD,&myId);
    
    int size=100;
    char v[size];
    for(int i=0;i<size;i++){
        v[i]=myId;
    }
    
    int blocklen[numProcs];
    MPI_Aint disp[numProcs];
    MPI_Datatype type[numProcs];
    for(int i=0;i<numProcs;i++){
        blocklen[i]=1;
        disp[i]=sizeof(char)*myId;
        type[i]=MPI_CHAR;
    }
    
    MPI_Datatype mytype;
    MPI_Type_create_struct(numProcs, blocklen, disp, type, &mytype);
    MPI_Type_commit(&mytype);
    
    MPI_File fh;
    MPI_File_open(MPI_COMM_WORLD, "binfile", MPI_MODE_WRONLY | MPI_MODE_CREATE, MPI_INFO_NULL, &fh);
    MPI_File_set_view(fh, 0, MPI_BYTE, mytype, "native", MPI_INFO_NULL);
    MPI_File_write(fh, v, size, MPI_BYTE, MPI_STATUS_IGNORE);
    
    MPI_File_close(&fh);
    MPI_Finalize();
    return 0;
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
int-myId;
国际货币基金组织;
MPI_Init(&argc,&argv);
MPI通信大小(MPI通信世界和numProcs);
MPI通信等级(MPI通信世界和myId);
int size=100;
字符v[大小];
对于(int i=0;i多亏了s的评论,我才能够解决我的问题。
功能

int MPI_类型_创建_子阵列(int ndims,const int array,大小为[], 常量int数组_的子数组[],常量int数组_的子数组开始[],整数顺序, MPI_数据类型oldtype,MPI_数据类型*newtype)

这就是诀窍。数组的大小定义了新数据类型的大小。数组的大小定义了零件的大小,而零件不是“孔”的一部分,数组的开始定义了“非孔”零件的开始位置

对于任何感兴趣的人,这是工作代码:

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

using namespace std;


int main(int argc, char* argv[]){
    int myId;
    int numProcs;
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numProcs);
    MPI_Comm_rank(MPI_COMM_WORLD,&myId);

    int size=100;
    char v[size];
    for(int i=0;i<size;i++){
        v[i]=myId;
    }

    int array_of_sizes[1];
    array_of_sizes[0]=numProcs;
    int array_of_subsizes[1];
    array_of_subsizes[0]=1;
    int array_of_starts[1];
    array_of_starts[0]=myId;


    MPI_Datatype mytype;
    MPI_Type_create_subarray(1,array_of_sizes, array_of_subsizes, array_of_starts, MPI_ORDER_C, MPI_BYTE, &mytype);
    MPI_Type_commit(&mytype);

    MPI_File fh;
    MPI_File_open(MPI_COMM_WORLD, "binfile", MPI_MODE_WRONLY | MPI_MODE_CREATE, MPI_INFO_NULL, &fh);
    MPI_File_set_view(fh, 0, MPI_BYTE, mytype, "native", MPI_INFO_NULL);
    MPI_File_write(fh, v, size, MPI_BYTE, MPI_STATUS_IGNORE);

    MPI_File_close(&fh);
    MPI_Finalize();
    return 0;
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
int-myId;
国际货币基金组织;
MPI_Init(&argc,&argv);
MPI通信大小(MPI通信世界和numProcs);
MPI通信等级(MPI通信世界和myId);
int size=100;
字符v[大小];

对于(int i=0;iYou以错误的方式构造数据类型。请改用。非常感谢,通过您的回答,我能够解决我的问题