Jarrod Cabalzar
Jarrod Cabalzar

Reputation: 408

Sending 2D Int Array with MPI_Send and Recv

I am trying to send a 2D integer array of arbitrary length from slave processes to the master but I keep getting a segmentation fault. As MPI is quite difficult to debug, I'm not certain that the issue has to do with the send/recv but if it's not that then it will have to be with the way I am allocating the arrays themselves.

I followed a previous question on here in regards to ensuring that the memory allocated to the array is contiguous but that still didn't fix the segmentation fault.

Below are some sections of my code:

Create array:

int** create2DArray(int sizeX, int sizeY)
{
    int* data = (int *) malloc(sizeX * sizeY * sizeof(int));
    int** array= (int **) malloc(sizeX * sizeof(int*));

    int i;

    for (i=0; i<sizeX; i++)
    {
        array[i] = &(data[sizeY * i]);
    }

    return array;
}

Initialise arrays:

if(rank==0)
{
    display = x11setup(&win, &gc, width, height);
    pixels = create2DArray(X_RESN, Y_RESN);
}
else
{
    xStart = xPixels * (rank - 1);
    xFinish = xStart + xPixels;
    pixels = create2DArray(xPixels, Y_RESN);
} 

Send:

MPI_Send(&pixels[0][0], xPixels * Y_RESN, MPI_INT, 0, type, MPI_COMM_WORLD);

Recv:

for(i = 1; i < processes; i++)
{
    int** pixelChunk = create2DArray(xPixels, Y_RESN);
    MPI_Recv(&pixelChunk[0][0], xPixels * Y_RESN, MPI_INT, i, type, MPI_COMM_WORLD, &status);

    int xStart = xPixels * (i - 1);
    int xFinish = xStart + xPixels;

    int k;

    for(j = xStart; j < xFinish; j++)
    {
        for(k = 0; k < Y_RESN; k++)
        {
            pixels[j][k] = pixelChunk[j - (xPixels * i - 1)][k];
        }               
    }
}

Upvotes: 1

Views: 6778

Answers (1)

unkulunkulu
unkulunkulu

Reputation: 11922

This line looks suspicious:

pixels[j][k] = pixelChunk[j - (xPixels * i - 1)][k];

For example, say we have np = 2, so we're left with a single chunk, then

i = 1;
xStart = 0;
j = 0;
xPixels = 600;
pixelChunk[0 - (600 * 1 - 1)[k] == pixelChunk[-599][k]

Doesn't look right, does it?

This?

pixels[j][k] = pixelChunk[j - xPixels * (i - 1)][k];

The send/recv code is allright probably.

Upvotes: 1

Related Questions