gpt4 book ai didi

c - 关于 MPI_Reduce

转载 作者:行者123 更新时间:2023-12-01 13:47:32 25 4
gpt4 key购买 nike

只是一个问题。如果我使用函数 MPI_Reduce,只有根可以分配接收缓冲区,当这是一个动态数组时?。例如:

int r = 10;
int *yloc, *y;
...
yloc = calloc(r*sizeof(int)); // for all processes

if (I'm the master process) {
y = calloc(r*sizeof(int)) // inside the if-block ...
...
}
y = calloc(r*sizeof(int)) // ...or outside the if-block?
...
MPI_Reduce(yloc, y, r, MPI_FLOAT, MPI_SUM, ROOT, MPI_COMM_WORLD);

什么是正确的?在 if block 的内部还是外部?提前致谢。

最佳答案

两者都是正确的。但我想你想要的答案是 y 是否是一个有效的内存地址只对 MPI_Reduce() 调用的根进程有影响。因此,除了根进程之外,无需为任何其他进程分配内存。

为了完整起见,这里是 MPI_Reduce 手册页的摘录,我们可以从中了解到接收缓冲区仅对根进程有意义:

NAME
MPI_Reduce - Reduces values on all processes to a single value

SYNOPSIS
int MPI_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
MPI_Op op, int root, MPI_Comm comm)

INPUT PARAMETERS
sendbuf
- address of send buffer (choice)
count - number of elements in send buffer (integer)
datatype
- data type of elements of send buffer (handle)
op - reduce operation (handle)
root - rank of root process (integer)
comm - communicator (handle)

OUTPUT PARAMETERS
recvbuf
- address of receive buffer (choice, significant only at root )

关于c - 关于 MPI_Reduce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34796403/

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