c - MPI - 段错误退出代码 : 139

标签 c parallel-processing segmentation-fault mpi

我有一个成功运行的简单 MPI 代码,但就在终止之前显示以下错误。

===
=   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.

下面是我的源代码。

/*
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;
}

最佳答案

我不太确定其余的代码(看起来不错,但我没有仔细看),但可以肯定的是你已经得到了 MPI_Recv()/MPI_Send() 配对错误。问题是您发送和接收 30 个整数的数组,而您只为每个数组分配一个内存。 尝试在 3 个 MPI_Send()MPI_Recv() 调用中用 1 替换您的 30 参数,然后您代码可能会正常工作。

关于c - MPI - 段错误退出代码 : 139,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39459173/

相关文章:

c - 使用 memcpy 将字符串复制到字符数组时出现段错误

c - 简单的 C 代码在 gcc 上给出段错误

c - 像 kbhit 这样的标准函数是什么?

c - 为什么这个函数的 while 循环不无限运行?

即使可执行文件存在,C `execlp`也会失败

r - 如何并行运行多个独立且不相关的函数而无需修改更大的代码?

python - 如何沟通两个独立的python进程?

python - Python 中的线程和多处理操作系统模块

c - 奇怪的输出?

c - 尝试 Cormen 的堆排序,但出现段错误