堆栈粉碎错误 MPI_Send() 和 MPI_Recv()

Stack Smashing Error MPI_Send() and MPI_Recv()

我想解决代码中的 stack smashing 错误

我已经尝试 运行 使用 mpicc 和 mpiexec 的代码

#include "mpi.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
    int rank,size,x,status;
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    if(rank==0)
    {
        scanf("%d",&x);
        MPI_Send(&x,1,MPI_INT,1,1,MPI_COMM_WORLD);
        printf("I have send %d from process 0\n",x);
        //fflush(stdout);
    }
    else
    {
        MPI_Recv(&x,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
        printf("I have received %d in process 1\n",x);
        //fflush(stdout);
    }
    MPI_Finalize();
    return 0;
}

我希望代码打印接收和发送语句 但实际输出打印接收和发送语句 并给出

* stack smashing error *

我不明白为什么会这样?

您需要使用 MPI_Status 而不是 int 来声明 status 变量,如下面的代码所示。

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

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

    MPI_Status status;

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    if(rank==0)
    {
        scanf("%d",&x);
        MPI_Send(&x,1,MPI_INT,1,1,MPI_COMM_WORLD);
        printf("I have send %d from process 0\n",x);
        //fflush(stdout);
    }
    else
    {
        MPI_Recv(&x,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
        printf("I have received %d in process 1\n",x);
        //fflush(stdout);
    }
    MPI_Finalize();
    return 0;
}