【问题标题】:Stack Smashing Error MPI_Send() and MPI_Recv()堆栈粉碎错误 MPI_Send() 和 MPI_Recv()
【发布时间】:2019-06-08 05:57:12
【问题描述】:

我想解决代码中的堆栈粉碎错误

我已尝试使用 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;
}

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

* 堆栈粉碎错误 *

我不明白为什么会这样?

【问题讨论】:

  • 我不必进行错误检查,因为默认情况下 MPI 中的错误是致命的
  • status 应该是 MPI_Status 类型。可能是您的错误的原因,也可能不是。

标签: c mpi


【解决方案1】:

您需要使用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;
}

【讨论】:

  • 附注我不确定您使用什么来编译代码,但如果您使用 mpicc 表示 C(或 mpic++ 表示 C++),它应该警告您 “不兼容的指针类型”(即使没有任何额外的编译器标志,例如-Wall -Werr -pedantic -Wextra -Wshadow 等)。
猜你喜欢
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多