MPI调试,分段错误?

时间:2012-05-02 04:32:54

标签: c debugging mpi

编辑:我的问题类似于C, Open MPI: segmentation fault from call to MPI_Finalize(). Segfault does not always happen, especially with low numbers of processes,所以你回答那个问题,无论哪种方式都很好。 。

我希望能够帮助调试以下代码:

int main(){
        long* my_local;
        long n, s, f;
        MPI_Init(NULL, NULL);
        MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
        MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    if(my_rank == 0){
        /*  Get size n from user                            */
        printf("Total processes: %d\n", comm_sz);
        printf("Number of keys to be sorted?  ");
        fflush(stdout);
        scanf("%ld", &n);

        /*  Broadcast size n to other processes             */
        MPI_Bcast(&n, 1, MPI_LONG, 0, MPI_COMM_WORLD);

        /*  Create n/comm_sz keys                           
             NOTE! some processes will have 1 extra key if
              n%comm_sz != 0                                */
        create_Keys(&my_local, my_rank, comm_sz, n, &s, &f);
    }

    if(my_rank != 0){
        /* Receive n from process 0                         */
        MPI_Bcast(&n, 1, MPI_LONG, 0, MPI_COMM_WORLD);

        /*  Create n/comm_sz keys                           */
        create_Keys(&my_local, my_rank, comm_sz, n, &s, &f);
    }

        /* The offending function, f is a long set to num elements of my_local*/
        Odd_Even_Tsort(&my_local, my_rank, f, comm_sz);

        printf("Process %d completed the function", my_rank);
        MPI_Finalize();
        return 0;
}

void Odd_Even_Tsort(long** my_local, int my_rank, long my_size, int comm_sz)
{
    long nochange = 1;
    long phase = 0;
    long complete = 1;
    MPI_Status Stat;
    long your_size = 1;

    long* recv_buf = malloc(sizeof(long)*(my_size+1));
    printf("rank %d has size %ld\n", my_rank, my_size);

    while (complete!=0){
        if((phase%2)==0){
            if( ((my_rank%2)==0) && my_rank < comm_sz-1){
            /*  Send right                          */
                MPI_Send(&my_size, 1, MPI_LONG, my_rank+1, 0, MPI_COMM_WORLD);
                MPI_Send(*my_local, my_size, MPI_LONG, my_rank+1, 0, MPI_COMM_WORLD);
                MPI_Recv(&your_size, 1, MPI_LONG, my_rank+1, 0,  MPI_COMM_WORLD, &Stat);
                MPI_Recv(&recv_buf, your_size, MPI_LONG, my_rank+1, 0,  MPI_COMM_WORLD, &Stat);
            }
            if( ((my_rank%2)==1) && my_rank < comm_sz){
            /*  Send left                          */
                MPI_Recv(&your_size, 1, MPI_LONG, my_rank-1, 0,  MPI_COMM_WORLD, &Stat);
                MPI_Recv(&recv_buf, your_size, MPI_LONG, my_rank-1, 0,  MPI_COMM_WORLD, &Stat);
                MPI_Send(&my_size, 1, MPI_LONG, my_rank-1, 0, MPI_COMM_WORLD);
                MPI_Send(*my_local, my_size, MPI_LONG, my_rank-1, 0, MPI_COMM_WORLD);
            }
        }
        phase ++;
        complete = 0;
    }

    printf("Done!\n");
    fflush(stdout);
}

我得到的错误是:

[ubuntu:04968] *** Process received signal ***
[ubuntu:04968] Signal: Segmentation fault (11)
[ubuntu:04968] Signal code: Address not mapped (1)
[ubuntu:04968] Failing at address: 0xb
--------------------------------------------------------------------------
mpiexec noticed that process rank 1 with PID 4968 on node ubuntu exited on signal 11 (Segmentation fault).

我感到困惑的原因是函数后面的print语句仍然显示,但如果我注释掉函数,则没有错误。所以,堆在哪里我得到分段错误?我收到错误的mpiexec -n 2 ./a.out和'n'大小超过9。

如果你真的想要整个可运行的代码,请告诉我。真的,我希望不是那么多的精确答案,而是更多如何使用gdb / valgrind工具来调试这个问题以及其他类似的问题(以及如何阅读它们的输出)。

(是的,我意识到'排序'功能尚未排序)。

1 个答案:

答案 0 :(得分:11)

这里的问题很简单,但很难看到,除非你使用调试器或打印出详尽的调试信息:

查看调用MPI_Recv的代码。 recv_buf变量应作为参数而不是&recv_buf提供。

  MPI_Recv(   recv_buf  , your_size, MPI_LONG, my_rank-1, 0,  MPI_COMM_WORLD, &Stat);

其余的似乎没问题。