1

This is a simple MPI code sample to send, receive and print the procids of all non-root processes. Running the following code with the number of process > 3 results in Segfault with Exit Code 11.

int main(int argc, char *argv[])
{
    int ierr, procid, numprocs;
    int root = 0;
    ierr = MPI_Init(&argc, &argv);
    ierr = MPI_Comm_rank(MPI_COMM_WORLD, &procid);
    ierr = MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

    int i;

    if (procid == root) {
        int num;
        for (i=1; i<numprocs; i++) {
            printf("Waiting for response from %d\n", i);
            MPI_Recv(&num, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            printf("Received: %d\n", num);
        }

    }

    else {
        printf("I am proc %d of %d\n", procid, numprocs);
        MPI_Send(&procid, 1, MPI_INT, root, 0, MPI_COMM_WORLD);
    }
    ierr = MPI_Finalize();
}
hal9000
  • 33
  • 5
  • Your code looks fine to me. Have you tried [debugging](http://stackoverflow.com/q/329259/620382)? Also consider actually checking the error codes by MPI. – Zulan Mar 20 '17 at 14:05
  • @Zulan, the error codes from MPI will either be `MPI_SUCCESS` or the program will abort - this is the default behaviour (`MPI_ERRORS_ARE_FATAL`) unless one sets a different MPI error handler. – Hristo Iliev Mar 20 '17 at 15:12
  • @HristoIliev good point, nevertheless setting `ierr` and not checking it is rather odd. – Zulan Mar 20 '17 at 16:30

0 Answers0