顺序MPI_BCast仅分配第二个调用的值

时间:2019-09-14 10:35:11

标签: c mpi openmpi message-passing

我正在尝试在某些节点上广播两个C数组。但是,尽管使用了MPI_Barrier(),两个数组都被分配了第二个MPI_Bcast调用广播的值。

我尝试动态分配vw,重新排列广播的顺序,并删除MPI_Barrier通话,但是问题仍然存在。

int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(&C, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(w, n, MPI_LONG, 0, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(v, n, MPI_LONG, 0, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);

if (rank == 1) {
    for (int i = 0; i < n; i++) {
        printf("%d, %d, %d, and %d\n", n, C, w[i], v[i]);
    }
}

我希望输出为:

4, 10, 1, and 4
4, 10, 2, and 3
4, 10, 5, and 5
4, 10, 4, and 3

但是我得到了:

4, 10, 4, and 4
4, 10, 3, and 3
4, 10, 5, and 5
4, 10, 3, and 3

使用输入C = 10n = 4w = {1, 2, 5, 4}v={4, 3, 5, 3}

这是一个最小的可重现示例,它捕获了我实际代码的要点:

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

void func(int C, int n, int w[], int v[]) {
    int rank, size;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Bcast(&C, 1, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Bcast(w, n, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Bcast(v, n, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Barrier(MPI_COMM_WORLD);

    if (rank == 1) {
        for (int i = 0; i < n; i++) {
            printf("%d, %d, %d, and %d\n", n, C, w[i], v[i]);
        }
    }
}

int main(int argc, char ** argv) {
    int C;
    int n;

    MPI_Init(&argc, &argv);
    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    if (rank == 0) {
        C = 10;
        n = 4;
    }

    int v[n], w[n]; /* value, weight */

    if (rank == 0) {
        v[0] = 4; v[1] = 3; v[2] = 5; v[3] = 3;
        w[0] = 1; w[1] = 2; w[2] = 5; w[3] = 4;
    }

    func(C, n, w, v);

    MPI_Finalize();

    return 0;
}

使用mpicc -o sample sample.c编译并使用mpirun -n 4 ./sample执行。

mpicc --version的输出:

Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

mpirun --version的输出:

mpirun (Open MPI) 4.0.1

Report bugs to http://www.open-mpi.org/community/help/

0 个答案:

没有答案