2

I have implemented a 2d array Mpi scatter which works well. I mean that the master processor can scatter 2d parts of the initial big array. The problem is when I use as input the 2d image file dynamically allocated it doesn't work. I suppose that there must be something wrong with the memory. Is there any way of obtaining 2d parts of a big 2d array dynamically.

Mike B
  • 1,522
  • 1
  • 14
  • 24
  • 3
    The answer is almost certainly the same answer as [here](http://stackoverflow.com/a/5107489/463827) - the usual C way of allocating a multidimensional array doesn't allocate a contiguous block of memory, and sending the data with MPI (or most other communications libraries, or writing it to disk) requires a chunk of contiguous data. The solution is almost certainly to change how you allocate the array, as in the answer above. – Jonathan Dursi Nov 15 '12 at 02:23

1 Answers1

0

I had a similar problem, but it was one-dimensional vector with dynamically allocated. Solved my problem as follows:

#include <stdio.h>
#include "mpi.h"

main(int argc, char** argv) {

    /* .......Variables Initialisation ......*/
    int Numprocs, MyRank, Root = 0;
    int index;
    int *InputBuffer, *RecvBuffer;
    int Scatter_DataSize;
    int DataSize;
    MPI_Status status;

    /* ........MPI Initialisation .......*/
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &MyRank);
    MPI_Comm_size(MPI_COMM_WORLD, &Numprocs);

    if (MyRank == Root) {
        DataSize = 80000;

        /* ...Allocate memory.....*/

        InputBuffer = (int*) malloc(DataSize * sizeof(int));

        for (index = 0; index < DataSize; index++)
            InputBuffer[index] = index;
    }

    MPI_Bcast(&DataSize, 1, MPI_INT, Root, MPI_COMM_WORLD);
    if (DataSize % Numprocs != 0) {
        if (MyRank == Root)
            printf("Input is not evenly divisible by Number of Processes\n");
        MPI_Finalize();
        exit(-1);
    }

    Scatter_DataSize = DataSize / Numprocs;
    RecvBuffer = (int *) malloc(Scatter_DataSize * sizeof(int));

    MPI_Scatter(InputBuffer, Scatter_DataSize, MPI_INT, RecvBuffer,
            Scatter_DataSize, MPI_INT, Root, MPI_COMM_WORLD);

    for (index = 0; index < Scatter_DataSize; ++index)
        printf("MyRank = %d, RecvBuffer[%d] = %d \n", MyRank, index,
                RecvBuffer[index]);

    MPI_Finalize();

}

This link has examples that have helped me: http://www.cse.iitd.ernet.in/~dheerajb/MPI/Document/hos_cont.html

Hope this helps.

csk
  • 188
  • 1
  • 10