C: Signal code: Address not mapped (1) mpirecv
Asked Answered
N

1

9

I have the following code written in C with MPI:

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

int main(int argc, char *argv[])
{
    int size, rank;
    MPI_Status status;
    int buf[1000];

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

    if (rank == 0) {
        int i = 0;

        while (i != 1000) {
            buf[i] = i;
            i++;
        }

        MPI_Send(buf, 999, MPI_INT, 1, 1, MPI_COMM_WORLD);
        printf("msg has been sent\n");
    }

    if (rank == 1) {
        int sz = sizeof(buf);
        int lst = buf[sz-1];

        MPI_Recv(buf, 999, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
        printf("la taille du buf %d et dernier %d", sz, lst);
    }

    MPI_Finalize();
}

And after run it it gives this message:

msg has been sente
[blitzkrieg-TravelMate-P253:03395] *** Process received signal ***
[blitzkrieg-TravelMate-P253:03395] Signal: Segmentation fault (11)
[blitzkrieg-TravelMate-P253:03395] Signal code: Address not mapped (1)
[blitzkrieg-TravelMate-P253:03395] Failing at address: 0xbfee8574
[blitzkrieg-TravelMate-P253:03395] [0] [0xb772d40c]
[blitzkrieg-TravelMate-P253:03395] [1] mpii(main+0x12f) [0x8048883]
[blitzkrieg-TravelMate-P253:03395] [2] /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3) [0xb74c84d3]
[blitzkrieg-TravelMate-P253:03395] [3] mpii() [0x80486c1]
[blitzkrieg-TravelMate-P253:03395] *** End of error message ***

mpirun noticed that process rank 1 with PID 3395 on node blitzkrieg-

TravelMate-P253 exited on signal 11 (Segmentation fault).

Any suggestion will help thnx.

Ninefold answered 16/5, 2015 at 11:55 Comment(0)
H
12

The stack trace shows that the error is not in the MPI_Recv as the question title suggests. The error is actually here:

int sz = sizeof(buf);
int lst = buf[sz-1]; // <---- here

Since buf is an array of int and sizeof(buf) returns its size in bytes, sz is set to 4 times the number of elements in the array. Accessing buf[sz-1] goes way beyond the bounds of buf and into an unmapped memory region above the stack of the process.

You should divide the total size of the array by the size of one of its elements, e.g. the first one:

int sz = sizeof(buf) / sizeof(buf[0]);
int lst = buf[sz-1];
Headwork answered 16/5, 2015 at 16:47 Comment(2)
How did you know from the stack trace that the error was not in MPI_Recv?Unquestioned
@rasen58, because neither MPI_Recv nor any other function from the MPI library is present in the stack trace.Headwork

© 2022 - 2024 — McMap. All rights reserved.