MPI发送和接收错误未运行

时间:2013-12-12 20:10:52

标签: mpi openmpi message-passing

我已将以下代码编写为test 我从每个处理器接收一个数组,我将它们放在广告2D数组中,每一行都是来自不同处理器的数组

#include <iostream>
#include <mpi.h>

using namespace std;

int main(int argc, char* argv[])
{

    int *sendBuff;
    int **table;
    int size, rank;
    MPI_Status stat;
    int pass = 1;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    sendBuff = new int[10];
    printf("task %d passed %d\n", rank, pass); //1
    pass++;
    if (rank == 0)
    {
        table = new int*[size];
    }
    for (int i = 0; i < 10; i++)
    {
        sendBuff[i] = rank;
    }

    printf("task %d passed %d\n", rank, pass); //2
    pass++;
    if (rank != 0)
    {
        MPI_Send(&sendBuff, 10, MPI_INT, 0, rank, MPI_COMM_WORLD);
    }

    printf("task %d passed %d\n", rank, pass); //3
    pass++;
    if (rank == 0)
    {
        table[0] = sendBuff;
        for (int i = 1; i < size; i++)
        {
            MPI_Recv(&table[i], 10, MPI_INT, i, i, MPI_COMM_WORLD, &stat);
        }
    }
    printf("task %d passed %d\n", rank, pass); //4
    pass++;
    delete[] sendBuff;
    if (rank == 0)
    {
        for (int i = 0; i < size; i++)
        {
            delete[] table[i];
        }
        delete[] table;
    }

    MPI_Finalize();
    return 0;
}

但它没有运行 我用

运行
mpirun -np 4 a.out

我得到以下内容:

    [arch:03429] *** Process received signal ***
[arch:03429] Signal: Aborted (6)
[arch:03429] Signal code:  (-6)
[arch:03429] [ 0] /usr/lib/libpthread.so.0(+0xf870) [0x7fd2675bd870]
[arch:03429] [ 1] /usr/lib/libc.so.6(gsignal+0x39) [0x7fd2672383d9]
[arch:03429] [ 2] /usr/lib/libc.so.6(abort+0x148) [0x7fd2672397d8]
[arch:03429] [ 3] /usr/lib/libc.so.6(+0x72e64) [0x7fd267275e64]
[arch:03429] [ 4] /usr/lib/libc.so.6(+0x7862e) [0x7fd26727b62e]
[arch:03429] [ 5] /usr/lib/libc.so.6(+0x79307) [0x7fd26727c307]
[arch:03429] [ 6] a.out() [0x408704]
[arch:03429] [ 7] /usr/lib/libc.so.6(__libc_start_main+0xf5) [0x7fd267224bc5]
[arch:03429] [ 8] a.out() [0x408429]
[arch:03429] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 3429 on node arch exited on signal 6 (Aborted).
--------------------------------------------------------------------------

任何帮助?

1 个答案:

答案 0 :(得分:3)

正如Hristo Iliev所指出的,数组sendBuf应该是MPI_Send的参数。它对table [i]的工作方式相同。

另一个事实:MPI_Send和MPI_Recv不分配内存。这些功能只是将消息从一个地方复制到另一个地方。 sendBuff和table [i]都应该先前分配。因此,写表[0] = sendBuff会触发内存泄漏。

这是一个可以帮助您的代码:

#include <iostream>
#include <mpi.h>

using namespace std;

int main(int argc, char* argv[])
{

    int *sendBuff;
    int **table;
    int size, rank;
    MPI_Status stat;
    int pass = 1;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    sendBuff = new int[10];
    printf("firts task %d passed %d\n", rank, pass); //1
    pass++;
    if (rank == 0)
    {
        table = new int*[size];
    }
    for (int i = 0; i < 10; i++)
    {
        sendBuff[i] = rank;
    }

    printf("second task %d passed %d\n", rank, pass); //2
    pass++;
    if (rank != 0)
    {
        MPI_Send(sendBuff, 10, MPI_INT, 0, rank, MPI_COMM_WORLD);
    }

    printf("thrid task %d passed %d\n", rank, pass); //3
    pass++;
    if (rank == 0)
    {
    table[0]=new int[10];
    for(int i=0;i<10;i++){
        table[0][i]=sendBuff[i];
}
       // table[0] = sendBuff;
        for (int i = 1; i < size; i++)
        {
    table[i]=new int[10];
            MPI_Recv(table[i], 10, MPI_INT, i, i, MPI_COMM_WORLD, &stat);
        }
    }
    printf("fourth task %d passed %d\n", rank, pass); //4
    pass++;


    if (rank == 0)
    {
        for (int i = 0; i < size; i++)
        {
            delete [] table[i];
        table[i]=NULL;
        }
        delete [] table;
    }

delete [] sendBuff;

    MPI_Finalize();
    return 0;
}

可以帮助您的功能:MPI_Gather(...)。这似乎是你在找什么!如果你想使用它,请注意内存分配:表的所有值都应该被分配为一个连续的内存块。

http://www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Gather.html

再见,

弗朗西斯