MPI发送和接收错误未运行

MPI发送和接收错误未运行,mpi,openmpi,message-passing,Mpi,Openmpi,Message Passing,我已经编写了以下代码作为测试 我从每个处理器接收一个数组,并将它们放置在ad 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;

我已经编写了以下代码作为测试 我从每个处理器接收一个数组,并将它们放置在ad 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;
}
我得到以下信息:

    [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).
--------------------------------------------------------------------------

有什么帮助吗?

正如赫里斯托·伊利耶夫指出的,数组sendBuf应该是MPI_Send的参数。对于表[i],它的工作方式相同

另一个事实:MPI_Send和MPI_Recv不分配内存。这些函数只是将消息从一个地方复制到另一个地方。sendBuff和表[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;
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
int*sendBuff;
int**表格;
整数大小、等级;
MPI_状态统计;
int pass=1;
MPI_Init(&argc,&argv);
MPI_通信大小(MPI_通信世界和大小);
MPI通信等级(MPI通信世界和等级);
sendBuff=新整数[10];
printf(“第一个任务%d已通过%d\n”,排名,通过);//1
pass++;
如果(秩==0)
{
表=新整数*[大小];
}
对于(int i=0;i<10;i++)
{
sendBuff[i]=等级;
}
printf(“第二个任务%d通过了%d\n”,排名,通过);//2
pass++;
如果(秩!=0)
{
MPI_发送(sendBuff,10,MPI_INT,0,等级,MPI_COMM_WORLD);
}
printf(“第三个任务%d通过了%d\n”,排名,通过);//3
pass++;
如果(秩==0)
{
表[0]=新整数[10];

对于(int i=0;i)当将指针变量(如
sendBuf
传递到
MPI\u Send
MPI\u Recv
)时,不需要额外的
&
#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;
}