3
votes

I have a simple MPI code which runs successfully but just before terminating it shows following error.

===
=   BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
=   EXIT CODE: 139
=   CLEANING UP REMAINING PROCESSES
=   YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
===================================================================================
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)
This typically refers to a problem with your application.

Below is my source code.

/*
AUTHOR ::: KHAYAM ANJAM
*/

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

int main (int argc, char *argv[])
{
    int rank, size, ball_value, ball_present;

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

    srandom(rank);
        int delta = rand() % 13;
    int random = rand() % 5;
        if (random == 0) delta = -1*delta;
    if (rank == 0) {
        ball_present = 1;
        ball_value = 0;
    }
    else ball_present = 0;
    while (1) {
        if(ball_present == 0) 
            MPI_Recv(&ball_value, 30, MPI_INT, MPI_ANY_SOURCE, 10, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        ball_present = 1;
        printf("Task %d has Ball with value %d\n", rank, ball_value);
        if (ball_value == 1000) break;
        if (abs(ball_value) > 100) {
            int send_1000 = 1000;
            int i;
            for (i = 0; i < size; i++)
                if (i != rank) MPI_Send(&send_1000, 30, MPI_INT, i, 10, MPI_COMM_WORLD); //Broadcast to all others
            break;  
        }
        ball_value += delta;
        int next_to_send = rand() % size;
        if (next_to_send != rank) {
            printf("Sending ball to %d\n", next_to_send);
            MPI_Send(&ball_value, 30, MPI_INT, next_to_send, 10, MPI_COMM_WORLD);
            ball_present = 0;
            }
    }
    MPI_Finalize();
    return 0;
}
1
I am no MPI user, but I notice you do not check the return value from any of the MPI_xxx() functions, which is provided for your benefit. Strangely, perhaps, of several web pages I googled, only the MS page specifically mentions the return value. The others all seem to plagiarise what others have written. - Weather Vane

1 Answers

6
votes

I'm not too sure for the rest of the code (seems OK but I didn't look too closely), but what is sure is that you've got he MPI_Recv() / MPI_Send() pairs wrong. The problem is that you send and receive arrays of 30 integers, while you only allocated memory for one of each. Try replacing your 30 parameter by a 1 in the 3 MPI_Send() or MPI_Recv() calls, and you code might just work.