Search code examples
cparallel-processingmallocmpidynamic-memory-allocation

mpi_gather, 2d dynamic array in c, exited on signal 6 (aborted)


After searching and searching finally I have function which allocate memory for nD array like vector or linear.
Function is:

int malloc2dint(int ***array, int n, int m) 
{
    /* allocate the n*m contiguous items */
    int *p = (int *)malloc(n*m*sizeof(int));
    if (!p) return -1;

    /* allocate the row pointers into the memory */
    (*array) = (int **)malloc(n*sizeof(int*));
    if (!(*array)) 
    {
        free(p);
        return -1;
    }

    /* set up the pointers into the contiguous memory */
    int i;
    for (i=0; i<n; i++) 
        (*array)[i] = &(p[i*m]);

    return 0;
}  

By using this method I can broadcast and also scatter 2d dynamic allocated array correctly but problem in MPI_Gather still exist.
main function is:

int length = atoi(argv[1]);
int rank, size, from, to, i, j, k, **first_array, **second_array, **result_array;

MPI_Init (&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

//2D dynamic memory allocation
malloc2dint(&first_array, length, length);
malloc2dint(&second_array, length, length);
malloc2dint(&result_array, length, length);

//Related boundary to each task
from = rank * length/size;
to = (rank+1) * length/size;

//Intializing first and second array
if (rank==0) 
{
    for(i=0; i<length; i++)
        for(j=0; j<length; j++)
        {
            first_array[i][j] = 1;
            second_array[i][j] = 1;
        }
}

//Broadcast second array so all tasks will have it
MPI_Bcast (&(second_array[0][0]), length*length, MPI_INT, 0, MPI_COMM_WORLD);

//Scatter first array so each task has matrix values between its boundary
MPI_Scatter (&(first_array[0][0]), length*(length/size), MPI_INT, first_array[from], length*(length/size), MPI_INT, 0, MPI_COMM_WORLD);


//Now each task will calculate matrix multiplication for its part
for (i=from; i<to; i++) 
    for (j=0; j<length; j++) 
    {
        result_array[i][j]=0;
        for (k=0; k<length; k++)
            result_array[i][j] += first_array[i][k]*second_array[k][j];

        //printf("\nrank(%d)->result_array[%d][%d] = %d\n", rank, i, j, result_array[i][j]);
        //this line print the correct value
    }

//Gathering info from all task and put each partition to resulat_array
MPI_Gather (&(result_array[from]), length*(length/size), MPI_INT, result_array, length*(length/size), MPI_INT, 0, MPI_COMM_WORLD);

if (rank==0) 
{
    for (i=0; i<length; i++) 
    {
        printf("\n\t| ");
        for (j=0; j<length; j++)
            printf("%2d ", result_array[i][j]);
        printf("|\n");
    }
}

MPI_Finalize();
return 0;  

Now when I run mpirun -np 2 xxx.out 4 the output is:

|  4  4  4  4 | ---> Good Job!

|  4  4  4  4 | ---> Good Job!

| 1919252078 1852795251 1868524912 778400882 | ---> Where are you baby?!!!

| 540700531 1701080693 1701734758 2037588068 | ---> Where are you baby?!!!

Finally mpirun notice that the process rank 0 exited on signal 6 (aborted).
Strange point for me is where MPI_Bcast and MPI_Scatter work fine but MPI_Gather not.
Any help will highly appreciated


Solution

  • The problem is with how you are passing the buffers. You are doing it correctly in MPI_Scatter, but then do it incorrectly for MPI_Gather.

    Passing the result_array as via &result_array[from] will read the memory where the pointer list is saved rather than the actual data of the matrix. Use &result_array[from][0] instead.

    Similarly for the receive buffer. Pass &result_array[0][0] instead of result_array to pass a pointer to the position where the data lies in memory.

    Hence, instead of:

    //Gathering info from all task and put each partition to resulat_array
    MPI_Gather (&(result_array[from]), length*(length/size), MPI_INT, result_array, length*(length/size), MPI_INT, 0, MPI_COMM_WORLD);
    

    Do:

    //Gathering info from all task and put each partition to resulat_array
    MPI_Gather (&(result_array[from][0]), length*(length/size), MPI_INT, &(result_array[0][0]), length*(length/size), MPI_INT, 0, MPI_COMM_WORLD);