c - 使用具有自定义操作功能的 MPI reduceAll

标签 c mpi

我正在尝试了解 reduceAll MPI 函数的用法。

在我的主项目中,我有一个数字数组,每个进程处理数组的一部分,然后让每个进程处理它们的结果,形成新的数组供程序重复。

所以对于这个基本示例,我只是有一个大小为 2503 的数组,并让每个 MPI 进程在数组的不同位置存储一些硬编码数字。然后在 reduceAll 之后有一个大小为 2503 的数组,但最后一个位置 [2502] 中的值将与最初放入的值发生变化。

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

void stitchResultArrays(double *in, double *inout, int *len){
    printf("0= %f\n", in[0]);
    printf("out1= %f\n", inout[0]);
    printf("out1= %f\n", inout[1]);
    printf("1= %f\n", in[1]);
    printf("len = %d\n", *len);

    inout[2502] += in[2502];
}

int main(int argc, char** argv) {
    MPI_Init(&argc, &argv);

    // Get the number of processes
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
    int arraySize = 50;

    double sendMpiMessage[2503];
    double test[2503];

    // Get the rank of the process
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    printf("size = %d\n", (arraySize * arraySize) + 3);
    sendMpiMessage[2502]= 4.1;
    sendMpiMessage[2501] = 4.4;
    sendMpiMessage[2500] = 4.2;

    sendMpiMessage[0] = 4.5;
    sendMpiMessage[1] = (double) (world_rank + 5);

    MPI_Op stitchResultArraysOp;
    MPI_Op_create((MPI_User_function *)stitchResultArrays, 1, &stitchResultArraysOp);

    printf("------");
    MPI_Allreduce(sendMpiMessage, test, 2503, MPI_DOUBLE, stitchResultArraysOp, MPI_COMM_WORLD);

    printf("result = %f\n", test[2502]);

    MPI_Finalize();

    return 0;
}

在这个例子中,我假设 reduceAll 函数中的长度为 2503(与数组大小相同),但是看起来 *len 在一个进程中为 1251,在另一个进程中为 1252,并且我不确定为什么会这样?

因此,我的行为似乎是:

inout[2502] += in[2502];

导致段错误,大概是因为试图访问大于数组大小的内容。

最佳答案

MPI_Allreduce 被定义为对数组的各个元素进行并行操作(概念上,不是并发意义上的)。用户定义的操作有一个 len 参数是为了提高效率(通过函数指针和可能的向量化调用更少),正如您所看到的,实现可以将数组 segmentation 为任何方便大小的 block 。 (在这种情况下,它可能受到用于通信的缓冲区大小的限制。)

作为Gilles said ,您必须创建自己的数据类型以强制将数组视为一个单元。

关于c - 使用具有自定义操作功能的 MPI reduceAll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48123153/

相关文章:

mpi - 为什么 MPI_Init 接受指向 argc 和 argv 的指针?

c++ - MySQL C++ 连接器是否比 MySQL C API 慢?

c - 骰子游戏坚持原始分数但成功循环

c - MPI_Waitany 不等待某些进程

c - MPI 数据类型对齐

parallel-processing - MPI 虚拟图拓扑广播

c - 从子进程检索 PID 和退出状态

c - 在C中用什么技术获得NTFS访问权限?

C - 来自先前分配的 void* 的 sizeof 值?

io - 从文本文件读取MPI