OpenMPI MPI_Gather段错误或断言错误

时间:2013-08-07 19:50:10

标签: c++ segmentation-fault mpi openmpi assertion

我正在尝试使用OpenMPI为我的本科高级项目构建一个多进程光线跟踪器,以便我可以在我学校的超级计算机上运行它。

我得到的代码编译得很好并运行正常,直到我到达

MPI_Gather(subscn,w*rows,MPI_BYTE,&scn,w*rows,MPI_BYTE,0,MPI_COMM_WORLD);

我的问题是这行的问题是什么,因为它导致我的程序段错误或导致(到目前为止)最多5个不同的断言错误,具体取决于我运行的进程数或我现在的场景试图渲染。

有问题的函数,删除了不必要的printf()语句。

int main(int argc, char **argv) {
init_MPI(argc, argv);

if (argc == 2) {
    string arg1 = argv[1];
    if (arg1 == "--help" || arg1 == "-?") print_help_message();
}

glutInit(&argc,argv);

bool OK = get_params(argc, argv);

Ray cam;
if (buildScene(scene, cam) == -1) {
    MPI_Abort(MPI_COMM_WORLD,rc);
    exit(1);
}
if (tid == MASTER) scn.initNewRGBApixmap(w,h);  /* RGBA pixel map to display */

int rows = h / numprocs;
RGBA *subscn = new RGBA[w*rows];
samples = samples > 0 ? whitted ? 1 : samples : 1;

MPI_Barrier(MPI_COMM_WORLD);            /* Synchronize all processes */
rtrace_time = 0.0 - MPI_Wtime();        /* Begin timer */

MPI_Scatter(&scn,w*rows,MPI_BYTE,subscn,w*rows,MPI_BYTE,0,MPI_COMM_WORLD);

raytrace(cam, rows, subscn);

MPI_Gather(subscn,w*rows,MPI_BYTE,&scn,w*rows,MPI_BYTE,0,MPI_COMM_WORLD);

MPI_Barrier(MPI_COMM_WORLD);            /* Synchronize all processes */
rtrace_time += MPI_Wtime();             /* End timer */

if (tid == MASTER) {
    initGlut(argc, argv);
    glutMainLoop();
}

MPI_Finalize();
return 0;
}

函数init_MPI(argc,argv)如下:

void init_MPI(int argc, char **argv) {
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&tid);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
if (numprocs < 2) {
    printf("\nError: At least two MPI tasks must be created. Exiting.\n");
    MPI_Abort(MPI_COMM_WORLD,rc);
    exit(1);
} else if (numprocs > MAX_PROCS) {
    printf("\nError: More than %d MPI tasks started. Exiting.\n",MAX_PROCS);
    MPI_Abort(MPI_COMM_WORLD,rc);
    exit(1);
}

}

1 个答案:

答案 0 :(得分:0)

感谢Hristo Iliev的评论。事实证明,我在调用MPI_Gather()时使用了两种不同的数据类型。

修改后的代码如下:

int main(int argc, char **argv) {
init_MPI(argc, argv);

if (argc == 2) {
    string arg1 = argv[1];
    if (arg1 == "--help" || arg1 == "-?") print_help_message();
}

glutInit(&argc,argv);

bool OK = get_params(argc, argv);

Ray cam;
if (buildScene(scene, cam) == -1) {
    MPI_Abort(MPI_COMM_WORLD,rc);
    exit(1);
}
if (pid == MASTER) scn = new RGBApixmap[w*h];

int rows = h / numprocs;
RGBApixmap *subscn = new RGBApixmap[w*rows];
samples = samples > 0 ? whitted ? 1 : samples : 1;

MPI_Barrier(MPI_COMM_WORLD);            /* Synchronize all processes */
rtrace_time = 0.0 - MPI_Wtime();        /* Begin timer */

raytrace(cam, rows, subscn);

MPI_Gather(subscn,w*rows,MPI_BYTE,scn,w*rows,MPI_BYTE,0,MPI_COMM_WORLD);

MPI_Barrier(MPI_COMM_WORLD);            /* Synchronize all processes */
rtrace_time += MPI_Wtime();             /* End timer */

if (pid == MASTER) {
    initGlut(argc, argv);
    glutMainLoop();
}

MPI_Finalize();
return 0;
}

所以这里的教训是始终检查您的数据类型,并确保您没有在MPI Scatter和Gather中使用不同的类。