I have written the following code as test I am receiving from each processor an array and I am placing them in ad 2D array each row is for an array from a different processor

#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;
}

but It is not runing I run using

mpirun -np 4 a.out

and I get the following:

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

Any help?

有帮助吗?

解决方案

As Hristo Iliev pointed out, the array sendBuf should be the argument of MPI_Send. It works the same way for table[i].

Another fact : MPI_Send and MPI_Recv do not allocate memory. These functions just copy a message from one place to another. Both sendBuff and table[i] should be allocated previously. And writting table[0]=sendBuff would therefore trigger a memory leak.

Here is a code that may help you :

#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;
}

A function that may help you : MPI_Gather(...). It seems to be what you are looking for ! Watch for memory allocation if you want to use it : all the values of table should be allocated as one contiguous chunk of memory.

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

Bye,

Francis

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top