Search code examples
cdynamicmatrixmpiallocation

MPI Seg. fault when using send/recv with Dynamic Allocation


I'm dealing with dynamic allocated matrices, and need to send/recv them from a process to another. I'm not sure if I'm doing it correctly, but sometimes, with small matrices, it works; but when I increase their dimensions, it gets this error:

[caio-A790GXM-AD3:03111] *** Process received signal ***
[caio-A790GXM-AD3:03111] Signal: Segmentation fault (11)
[caio-A790GXM-AD3:03111] Signal code: Address not mapped (1)
[caio-A790GXM-AD3:03111] Failing at address: (nil)

Here is the basic part where I think the error might be:

float **alocarMatriz(int linhas, int colunas) {
    int i;
    float *dado = (float *)calloc(linhas*colunas,sizeof(float));
    float **array = (float **)calloc(linhas,sizeof(float*));
    for(i = 0; i < linhas; i++) 
        array[i] = &(dado[colunas*i]);
    return array;
};

if(taskid == MASTER) {
    float **matriz;
    matriz = alocarMatriz(numLin,numCol);
    MPI_Send(&matriz[0][0], colunasAVT*colunasDAT, MPI_DOUBLE, dest, mtype, MPI_COMM_WORLD);
}
if(taskid > MASTER) {
    float **matriz;
    matriz = alocarMatriz(numLin,numCol);
    MPI_Recv(&matriz[0][0], colunasAVT*colunasDAT, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status);
}

Can anyone tell me what is wrong?


EDIT:

Fixed the index of Recv, also tried to run the same program, but with fixed size arrays instead of calloc'd. No success anyway, it still points segmentation fault.


Solution

  • You have the pointer to the array correct for Send, but not for Recv. Recv, too, should be MPI_Recv(&(matriz[0][0]), ... (or, equivalently, MPI_Recv(matriz[0], ...).