1

EDIT: There is no problem with this code in particular. I created a reduced version of my code and this part works perfectly. I still don't understand why it is not working in my whole code, because I have everything commented but this, but that's maybe too particular. Sorry, wrong question.

(I edited and added at the bottom the error that I get).

I'm trying to parallelize a C program.

I'm encountering errors when I try to pass an array allocated with malloc from the master process to the rest of processes. Or better, when I try to receive it.

This is the piece of code I'm having trouble with:

if (rank == 0)
{
    int *data=(int *) malloc(size*sizeof(int));
    int error_code = MPI_Send(data, size, MPI_INT, 1, 1, MPI_COMM_WORLD);
    if (error_code != MPI_SUCCESS) {
        char error_string[BUFSIZ];
        int length_of_error_string;

        MPI_Error_string(error_code, error_string, &length_of_error_string);
        printf("%3d: %s\n", rank, error_string);
    }

    printf("Data sent.");
}
else if (rank == 1)
{
    int *data=(int *) malloc(size*sizeof(int));
    int error_code = MPI_Recv(data, size, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
    if (error_code != MPI_SUCCESS) {
        char error_string[BUFSIZ];
        int length_of_error_string;

        MPI_Error_string(error_code, error_string, &length_of_error_string);
        printf("%3d: %s\n", rank, error_string);
    }
    printf("Received.");
}

"Data sent." is printed, followed by a segmentation fault (with memory dump) caused by the second process and "Received" is never printed.

I think I'm not receiving well the data. But I tried several possibilities, I think I have to pass the address of the variable and not just the pointer to the first position, so I thought this was the correct way, but it is not working.

From the error codes nothing gets printed.

Does anyone know what's causing the error and what was my mistake?

Thanks!

EDIT:

This is the exact error:

*** Process received signal ***
*** End of error message ***
Signal: Segmentation fault (11)
Signal code: Address not mapped (1)

EDIT 2:

This code works:

int main(int argc, char* argv[])
{
    int size_x = 12;
    int size_y = 12;

    int rank, size, length;
    char nodename[BUFSIZ];
    MPI_Status status;

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

    MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);

    if (rank == 0)
    {
        int *data=malloc(size*sizeof(int));
        int error_code = MPI_Send(data, size, MPI_INT, 1, 1, MPI_COMM_WORLD);
        if (error_code != MPI_SUCCESS)
        {
            char error_string[BUFSIZ];
            int length_of_error_string;

            MPI_Error_string(error_code, error_string, &length_of_error_string);
            printf("%3d: %s\n", rank, error_string);
        }
        printf("Data sent.");
    }
    else if (rank > 0)
    {
        int *data=malloc(size*sizeof(int));
        int error_code = MPI_Recv(data, size, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
        if (error_code != MPI_SUCCESS)
        {
            char error_string[BUFSIZ];
            int length_of_error_string;

            MPI_Error_string(error_code, error_string, &length_of_error_string);
            printf("%3d: %s\n", rank, error_string);
        }
        printf("Received.");
    }

    MPI_Finalize();

    return 0;
}
DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71
questing
  • 162
  • 1
  • 10
  • 1
    I don't see a problem here; can you post a complete runnable piece of code which reproduces the problem? How is size set, and where is status declared? As another, more nitpicky point - it's generally considered best practice not to cast the return pointer from malloc. See, eg, http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc . – Jonathan Dursi Jun 20 '14 at 17:51
  • You were right, I created a stand-alone version of the code and it works. So the problem must be elsewhere. I will update my question. – questing Jun 20 '14 at 18:08

1 Answers1

0

I found the problem, it was not the MPI calls, but there was a problem with a previous function (forgot a variable in "printf") which I didn't notice. That made the whole code break. Tricky MPI...

questing
  • 162
  • 1
  • 10