C语言 C:信号代码:地址未映射 (1) mpirecv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30275341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:56:51  来源:igfitidea点击:

C: Signal code: Address not mapped (1) mpirecv

cmpi

提问by Tato Tia

I have the following code written in C with MPI:

我用 MPI 用 C 编写了以下代码:

#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-

mpirun 注意到节点 blitzkrieg 上的进程等级为 1,PID 为 3395-

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

TravelMate-P253 在信号 11(分段错误)上退出。

Any suggestion will help thnx.

任何建议都会有所帮助。

回答by Hristo Iliev

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

堆栈跟踪显示错误不在MPI_Recv问题标题中。错误实际上在这里:

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

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

由于buf是一个数组intsizeof(buf)以字节sz为单位返回其大小,因此设置为数组中元素数的 4 倍。访问buf[sz-1]超出buf了进程堆栈上方的未映射内存区域的边界并进入其中。

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];