MPI-Ibcast用法

时间:2017-02-26 06:50:45

标签: c++ mpi broadcast

我尝试了解MPI_Ibcast的工作原理。 我想出了一个能够说明我需要的测试。 这可能不正确,因为我可能不理解Ibcast的正确用法:

#include <iostream>
#include <mpi.h>
#include <string>
#include <vector>


using namespace std;

class A {
public:
        int a[3];
        MPI_Request request;
        int tag;
        A() {}
        int foo() {
                int rank;
                MPI_Comm_rank(MPI_COMM_WORLD, &rank);
                cout<<rank<<endl;
                if (rank == 0)
                        tag = 5;
                MPI_Ibcast(&tag, 1, MPI_INT, 0, MPI_COMM_WORLD, &request);
        }
};

int main(int argc, char* argv[]) {
        MPI_Init(&argc, &argv);
        int rank;
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        MPI_Status* status = new MPI_Status[2];
        MPI_Request* request = new MPI_Request[2];
        A* a = new A[2];
        for (int i = 0; i < 2; i++) {
                a[i].request = request[i];
        }

        for (int i = 0; i < 2; i++) {
                if (i == 0 && rank == 0) {
                        a[0].foo();
                }else if ( i == 1 && rank == 1) {
                        a[1].foo();
                }
                int a;
                for (int i = 0; i < 1000; i++)
                        a+=i;
        }
        MPI_Waitall(2, request, status);
        cout <<a[1].tag<<" "<<a[1].tag<<" "<<a[1].tag<<endl;
        MPI_Finalize();
}

我想看看,如果在第一次迭代中可以在函数中调用MPI_Ibcast,则此函数返回,在另一次迭代中,另一个进程也将调用MPI_Ibcast,然后全部完成(标记将被广播并设置为5 in所有两个过程)。我在这里使用类的原因是我将它们放在原始程序中,在这里我尝试模拟一个小问题。

所以任务是:在不同的周期中,在不同进程上的单独实例调用一个函数。其中一些是按组进行组织的,在这个组内,Ibcast广播标签。在这里,我有一个有两个班级的小组。也许我应该以某种方式纠正这个例子?

1 个答案:

答案 0 :(得分:0)

我不完全确定你想做什么,但这是你的例子的固定版本。我纠正的主要是使用请求,因为你没有等待合适的请求......

var ary_db_table = ["aTable", "bTable", "cTable"]

for(var i = 0; i<=1; i++){
     global.db.ary_db_table[i].build().instanceMethod(successcb, data, errcb)
}

以下是我在我的机器上使用两个进程编译并运行它时所提供的内容:

#include <iostream>
#include <mpi.h>

using namespace std;

class A {
    public:
        MPI_Request request;
        int tag;
        A() : request(MPI_REQUEST_NULL), tag(-1){}
        int foo() {
            int rank;
            MPI_Comm_rank(MPI_COMM_WORLD, &rank);
            cout<<rank<<endl;
            if (rank == 0)
                tag = 5;
            MPI_Ibcast(&tag, 1, MPI_INT, 0, MPI_COMM_WORLD, &request);
        }
};

int main(int argc, char* argv[]) {
    MPI_Init(&argc, &argv);
    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    A a[2];

    for (int i = 0; i < 2; i++) {
        if (i == 0 && rank == 0) {
            a[0].foo();
        }else if ( i == 1 && rank == 1) {
            a[1].foo();
        }
        int b = 0;
        for (int i = 0; i < 1000; i++)
            b+=i;
    }
    MPI_Wait(&a[rank].request, MPI_STATUS_IGNORE);
    cout <<a[1].tag<<" "<<a[1].tag<<" "<<a[1].tag<<endl;
    MPI_Finalize();
}

现在这样可行并希望能达到你的预期。但是,您对进程组的描述使我相信您应该使用例如~/tmp$ mpiicpc -O0 ibcst.cc ~/tmp$ mpirun -n 2 ./a.out 1 0 5 5 5 -1 -1 -1 创建多个子通信器,以允许在...内使用集体呼叫。