MPI_使用C

时间:2019-04-28 04:44:53

标签: c mpi

我正在尝试使用MPI_Gather收集结构体数组。我通过使用MPI_Type_contiguous(每个元素都是双精度)创建了一个结构“ Final”的派生数据类型“ mystruct”。然后,我在简单的MPI_Send和MPI_Receive中使用了此派生数据类型,以检查它是否正确并且可以正常工作。现在,我想使用MPI_Gather来收集整个结构数组,每个结构都具有派生的数据类型“ mystruct”。

使用发送和接收,我知道MPI_Type_contiguous部分是正确的。在MPI_Gather之后,MASTER只是在应该从子进程中收集信息的位置上为元素打印零。

typedef struct{
        double Angle;
        double E_ODD;
        double OD_KE;
        double OD_L;
        double D_E;
}Final;

int main(int argc, char** argv)
{
ierr = MPI_Init(&argc,&argv);
ierr = MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
ierr = MPI_Comm_rank(MPI_COMM_WORLD,&taskid);

int totalnum_trajectories = file_size/9 //File that has xyz cartesian
                                        //coordinates for three atoms

int localnum_trajectories = totalnum_trajectories/numprocs;


Final worker_results[localnum_trajectory];
//Each processor does some analysis for its local number of trajectories
//and results go into worker_results buffer for each trajectory. 

//Create a datatype for the nth worker_results[n] struct
MPI_Datatype mystruct;
MPI_Type_contiguous(5,MPI_DOUBLE,&mystruct);
MPI_Type_commit(&mystruct);

//MASTER buffer: should get all local struct arrays from 
//children processors to one larger array of structs, equaling 
//the total number of trajectories
Final master_results[totalnum_trajectories];

ierr = MPI_Gather(worker_results, localnum_trajectory, mystruct, \
                  master_results, totalnum_trajectories, \
                  mystruct, MASTER, MPI_COMM_World);

//Do some I/O with MASTER; everything should be in master_results buffer

MPI_Finalize();
return(0);
}

正确的MPI_Gather之后,MASTER应该获得所有子级结果以进行正确的I / O。

1 个答案:

答案 0 :(得分:0)

我知道了。正如评论所暗示的那样,MASTER的接收计数应该是每个处理器的项目数(即localnum_trajectoires),而不是所有处理器的计数总数。因此,除了MPI_Gather MASTER计数之外,其他一切都是正确的,

ierr = MPI_Gather(worker_results, localnum_trajectories, mystruct,\
                  master_results, localnum_trajectories, mystruct,\
                  MASTER, MPI_COMM_WORLD);

干杯!