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

时间:2014-07-31 07:05:05

标签: 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 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);

0 个答案:

没有答案