1

Trying to pass an array pointer between MPI Processes and receiving it to dynamically allocated memory. it keeps giving a segmentation fault, which we believe is due to the way we are sending it between processes. Our code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "mpi.h"
#include <math.h>

int main(int argc, char* argv[])
{
    int my_rank, i, j, p;
    MPI_Request     request, request2;
    MPI_Status      mpi_status;

MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Barrier(MPI_COMM_WORLD);

    if (my_rank == 0) 
    {
            int size = 5;
            int **slice;
             slice = (int **)malloc(sizeof(int*)* size);
             slice[0] = (int *)malloc(sizeof(int)* size*size);

             for(i = 0; i< size;i++)
             {
                    slice[i] = (*slice + size*i);
            }

            for (i = 0; i < size; i++)
            {
                    for (j = 0; j < size; j++)
                    {
                            slice[i][j] = i*j;
                    }
            }
           for (i = 0; i < size; i++)
            {
                    for (j = 0; j < size; j++) 
                    {
                            printf("slice[%d][%d]: %d\n", i, j, slice[i][j]);
                    }
            }

            MPI_Send(&slice, size * size, MPI_INT, 1, 1, MPI_COMM_WORLD);

    } else {
            int local_size=5;
            int **local_slice;
            local_slice = (int **)malloc(sizeof(int*)* local_size);
            local_slice[0] = (int *)malloc(sizeof(int)* local_size*local_size);

             for(i = 0; i< local_size;i++)
             {
                    local_slice[i] = (*local_slice + local_size*i);
            }

            MPI_Recv(&local_slice, local_size * local_size, MPI_INT, 0, 1, MPI_COMM_WORLD, &mpi_status);

           for (i = 0; i < local_size; i++)
            {
                    for (j = 0; j < local_size; j++) 
                    {
                            printf("local_slice[%d][%d]: %d\n", i, j, local_slice[i][j]); 
                    }
            }

    }

    MPI_Finalize();
    return 0;
}

Can someone explain how to properly pass this type of array between MPI Processes please?

1 Answer 1

2

It looks like you need to change the first argument to MPI_Send from &slice to slice[0] and do the same for MPI_Recv.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.