简体   繁体   English

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

[英]Stack Smashing Error MPI_Send() and MPI_Recv()

I want to resolve stack smashing error in the code 我想解决代码中的堆栈粉碎错误

I have tried running the code with mpicc and mpiexec 我尝试使用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;
}

I expected the code to print the receiving and sending statements but the actual output prints the receiving and sending statements and gives 我希望代码能打印接收和发送语句,但实际输出会打印接收和发送语句并给出

* stack smashing error * *堆栈粉碎错误*

I can't understand why does it happen? 我不明白为什么会这样?

You need to use MPI_Status instead of int for declaring the status variable, as shown in the code below. 您需要使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM