gpt4 book ai didi

c - 使用 MPI_Reduce 时检测到堆栈粉碎

转载 作者:太空宇宙 更新时间:2023-11-03 23:21:06 25 4
gpt4 key购买 nike

我学会了使用一些 MPI 函数。当我尝试使用 MPI_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();
}

我的代码似乎没问题。它将打印 sum of all rank in recvBuf with process 0。在这种情况下,如果我运行我的代码,它将打印 Sum is 45 10 进程 mpirun -np 10 myexecutefile。但是我不知道为什么我的代码有错误:

Sum is 45
*** stack smashing detected ***: example6 terminated
[ubuntu:06538] *** Process received signal ***
[ubuntu:06538] Signal: Aborted (6)
[ubuntu:06538] Signal code: (-6)
[ubuntu:06538] *** Process received signal ***
[ubuntu:06538] Signal: Segmentation fault (11)
[ubuntu:06538] Signal code: (128)
[ubuntu:06538] Failing at address: (nil)
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 0 on node ubuntu exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------

问题是什么,我该如何解决?

最佳答案

MPI_Reduce(&sendBuf, &recvBuf, count, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

参数 count 必须是 number of elements in send buffer .由于 sendBuf 是单个整数,请尝试使用 count = 1; 而不是 count = size;

Sum is 45 被正确打印的原因很难解释。越界访问值是未定义的行为:问题可能一直未被注意到,或者在打印 Sum is 45 之前可能已经引发段错误。未定义行为的魔力...

关于c - 使用 MPI_Reduce 时检测到堆栈粉碎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39427637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com