使用MPI_Reduce时检测到堆栈崩溃

使用MPI_Reduce时检测到堆栈崩溃,c,mpi,C,Mpi,我已经学会了使用一些MPI函数。当我尝试使用MPI\u Reduce时,我在运行代码时检测到堆栈崩溃: #include <stdio.h> #include <stdlib.h> #include <mpi.h> void main(int argc, char **argv) { int i, rank, size; int sendBuf, recvBuf, count; MPI_Init(&argc, &arg

我已经学会了使用一些MPI函数。当我尝试使用
MPI\u Reduce
时,我在运行代码时检测到堆栈崩溃:

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

void main(int argc, char **argv) {
    int i, rank, size;
    int sendBuf, recvBuf, count;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    sendBuf = rank;
    count = size;
    MPI_Reduce(&sendBuf, &recvBuf, count, MPI_INT, 
        MPI_SUM, 0, MPI_COMM_WORLD);

    if (rank == 0) {
        printf("Sum is %d\n", recvBuf);
    }

    MPI_Finalize();
}
问题是什么?我如何解决它?

MPI_Reduce(&sendBuf, &recvBuf, count, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
参数
count
必须是。由于
sendBuf
是单个整数,请尝试
count=1而不是<代码>计数=大小


很难解释为什么正确打印的
总和为45
。访问超出界限的值是未定义的行为:问题可能一直未被注意到,或者分段错误可能是在打印
Sum is 45
之前提出的。未定义行为的魔力…

main应该返回一个int。我已经尝试返回一个int,但仍然没有更正。
MPI_Reduce(&sendBuf, &recvBuf, count, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);