MPI_Reduce 和 MPI_MIN 如何工作? [英] How does MPI_Reduce with MPI_MIN work?

查看:50
本文介绍了MPI_Reduce 和 MPI_MIN 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个代码:

int main(void) {
    int result=0;
    int num[6] = {1, 2, 4, 3, 7, 1};
    if (my_rank != 0) {
        MPI_Reduce(num, &result, 6, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
    } else {
        MPI_Reduce(num, &result, 6, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD)
        printf("result = %d\n", result);
    }
}

结果打印为 1 ;

但是如果 num[0]=9;那么结果是9

But if the num[0]=9; then the result is 9

为了解决这个问题,我必须将变量 num 定义为数组.我无法理解函数 MPI_Reduce 如何与 MPI_MIN 一起工作.为什么,如果num[0] 不等于最小数,那么我必须将变量num 定义为数组?

I read to solve this problem I must to define the variable num as array. I can't understand how the function MPI_Reduce works with MPI_MIN. Why, if the num[0] is not equal to the smallest number, then I must to define the variable num as array?

推荐答案

MPI_Reduce 对通信器的成员执行归约 - 而不是本地数组的成员.sendbufrecvbuf 必须具有相同的 size.

MPI_Reduce performs a reduction over the members of the communicator - not the members of the local array. sendbuf and recvbuf must both be of the same size.

我认为标准说得最好:

因此,所有进程都提供相同长度的输入缓冲区和输出缓冲区,具有相同类型的元素.每个进程可以提供一个元素或元素序列,在这种情况下,组合操作在序列的每个条目上按元素执行.

Thus, all processes provide input buffers and output buffers of the same length, with elements of the same type. Each process can provide one element, or a sequence of elements, in which case the combine operation is executed element-wise on each entry of the sequence.

MPI 不会获取数组中所有元素的最小值,您必须手动获取.

MPI does not get the minimum of all elements in the array, you have to do that manually.

这篇关于MPI_Reduce 和 MPI_MIN 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆