Types 为具有动态数组MPI_Type_get_extent和MPI_Type_create_Struct的结构创建数据类型的最佳方法

Types 为具有动态数组MPI_Type_get_extent和MPI_Type_create_Struct的结构创建数据类型的最佳方法,types,struct,parallel-processing,mpi,Types,Struct,Parallel Processing,Mpi,使用MPI函数传播消息时需要使用结构,例如MPI_Send、MPI_Recv struct results { char filename[300];//text for file name the results to be stored in double *t;//time buffer double *x;//X coordinate buffer double *y;//Y coordinate

使用MPI函数传播消息时需要使用结构,例如MPI_Send、MPI_Recv

     struct results
        {
        char filename[300];//text for file name the results to be stored in
        double *t;//time buffer 
        double *x;//X coordinate buffer
        double *y;//Y coordinate buffer
        double *z;//Z coordinate buffer
        } resultbuffer;
字段的大小根据长度动态变化

     resultbuffer.t = (double*)calloc(length,sizeof(double));
     resultabuffer.x = (double*)calloc(length,sizeof(double));
     resultabuffer.y = (double*)calloc(length,sizeof(double));
     resultabuffer.z = (double*)calloc(length,sizeof(double));
如果我使用

     MPI_Datatype struct_type,oldtypes[2]={MPI_CHAR,MPI_DOUBLE};
并将长度乘以4,因为在任何情况下,所有字段都是同一类型的double

     int blockcounts[2]={300,4*length};
     MPI_Aint offsets[2],extent;
     //setup description for the fields in the message structure 
     int ofst=0;
     for(i=0;i<2;i++)
        {
            offsets[i]=ofst;
            MPI_Type_extent(oldtypes[i], &extent);
            ofst = ofst + blockcounts[i]*extent;
        } 

     // Now define structured type and commit it.
     MPI_Type_create_struct(2, blockcounts, offsets, oldtypes, &struct_type);
     MPI_Type_commit(&struct_type);
或者需要分别定义每个字段的字段和偏移量,即使类型相同

  MPI_Datatype struct_type,oldtypes[5]={MPI_CHAR,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE};
  int blockcounts[5]={300,length,length,length,length};
  MPI_Aint offsets[5],extent;
  //setup description for the fields in the message structure 
  int ofst=0;
    for(int i=0;i<5;i++)
    {
        offsets[i]=ofst;
        MPI_Type_extent(oldtypes[i], &extent);
        ofst = ofst + blockcounts[i]*extent;
    } 

    // Now define structured type and commit it.
    MPI_Type_create_struct(5, blockcounts, offsets, oldtypes, &struct_type);
    MPI_Type_commit(&struct_type);

可能重复的问题请参见相关侧栏中的其他一些问题。MPI数据类型描述了数据的数量和布局,而calloced数组不是从文件名的零偏移量开始的,而是一个接一个地放置的,每个数组可以位于内存中的任何位置。您必须找到每个分配的缓冲区的偏移量,并将其放入type_结构定义中。下面列出了几个Q&A,它们描述了如何做到这一点。MPI\u类型\u索引如何,会有帮助吗?