简体   繁体   中英

MPI_Send and MPI_Recv Segmentation Fault

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?

看起来您需要将MPI_Send的第一个参数从&slice更改为slice[0] ,并对MPI_Recv进行相同操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM