c - 具有相同类型字段的结构上的 MPI_Allreduce 是否可移植?

标签 c mpi reduce

考虑这样的事情:

<小时/>
typedef struct TS { 
    double a,b,c; 
} S; 

... 
S x,y; 
... 
MPI_Allreduce(&x, &y, 3, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); 
<小时/>

上面的代码是否完全可移植(不使用 MPI_Type_struct 等;假定结构中的所有变量都具有相同类型)?还是在不同节点上使用不同硬件的情况下?

提前致谢, 江淮汽车

最佳答案

赫里斯托·伊利耶夫 (Hristo Iliev) 完全正确; C 标准允许字段之间任意填充。因此不能保证这与三个 double 组的内存布局相同,并且您的reduce可能会给您带来垃圾。

因此,您可以在这里采取两种不同的方法。一是忽略这个问题,因为大多数 C 编译器可能会将其视为三个连续 double 的数组。我通常根本不会提及这一点,甚至不会将其作为一个选项,除非在这种情况下很容易测试假设;在你的代码中你可以有

assert ( offsetof(S,b) == sizeof(double) );
assert ( offsetof(S,c) == 2*sizeof(double) );

如果你的代码继续执行断言,那么你就很好了。 (请注意,这仍然不能保证两个这些结构的数组相当于 6 个连续 double 的数组...)

第二种方法是自己创建结构并减少操作以确保安全。事实上,这并不是太困难,然后你就知道它会起作用,所以这确实是正确的方法;然后您可以安全地使用该类型进行任何其他操作:

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

typedef struct TS {
    double a,b,c;
} S;

/* our reduction operation */
void sum_struct_ts(void *in, void *inout, int *len, MPI_Datatype *type){
    /* ignore type, just trust that it's our struct type */

    S *invals    = in;
    S *inoutvals = inout;

    for (int i=0; i<*len; i++) {
        inoutvals[i].a  += invals[i].a;
        inoutvals[i].b  += invals[i].b;
        inoutvals[i].c  += invals[i].c;
    }

    return;
}

void defineStruct(MPI_Datatype *tstype) {
    const int count = 3;
    int          blocklens[count];
    MPI_Datatype types[count];
    MPI_Aint     disps[count];

    for (int i=0; i < count; i++) {
        types[i] = MPI_DOUBLE;
        blocklens[i] = 1;
    }

    disps[0] = offsetof(S,a);
    disps[1] = offsetof(S,b);
    disps[2] = offsetof(S,c);

    MPI_Type_create_struct(count, blocklens, disps, types, tstype);
    MPI_Type_commit(tstype);
}

int main (int argc, char **argv) {

    int rank, size;
    MPI_Datatype structtype;
    MPI_Op       sumstruct;
    S   local, global;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    defineStruct(&structtype);
    MPI_Op_create(sum_struct_ts, 1, &sumstruct);

    local.a = rank;
    local.b = 2*rank;
    local.c = 3*rank;

    MPI_Reduce(&local, &global, 1, structtype, sumstruct, 0, MPI_COMM_WORLD);

    if (rank == 0) {
        printf("global.a = %lf; expected %lf\n", global.a, 1.*size*(size-1)/2);
        printf("global.b = %lf; expected %lf\n", global.b, 2.*size*(size-1)/2);
        printf("global.c = %lf; expected %lf\n", global.c, 3.*size*(size-1)/2);
    }

    MPI_Finalize();
    return 0;
}

运行给予

$ mpicc -o foo foo.c -std=c99

$ mpirun -np 1 ./foo
global.a = 0.000000; expected 0.000000
global.b = 0.000000; expected 0.000000
global.c = 0.000000; expected 0.000000

$ mpirun -np 2 ./foo
global.a = 1.000000; expected 1.000000
global.b = 2.000000; expected 2.000000
global.c = 3.000000; expected 3.000000

$ mpirun -np 3 ./foo
global.a = 3.000000; expected 3.000000
global.b = 6.000000; expected 6.000000
global.c = 9.000000; expected 9.000000

$ mpirun -np 12 ./foo
global.a = 66.000000; expected 66.000000
global.b = 132.000000; expected 132.000000
global.c = 198.000000; expected 198.000000

关于c - 具有相同类型字段的结构上的 MPI_Allreduce 是否可移植?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14712837/

相关文章:

javascript - 如何使用reduce()连接数组并删除重复项

ios - 可以在 objective c 中使用 swift 中的 array reduce 概念吗?

c++ - fopen 在 "w"模式下比在 "a"模式下花费更多时间

r - 新安装 doMPI 抛出 MPI_ERR_SPAWN 错误

gcc - 减少直接使用 GCC 创建的 DLL 的大小

C++:奇怪的指针损坏错误

C-MPI : Parallel Processing of Column Arrays

c - C 中的系统命令强制在 OSX 上串行运行?

c - If 条件包含指针和整数的 AND

c - 使用先前声明的数组初始化结构