Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ MPI Ibcast的使用_C++_Mpi_Broadcast - Fatal编程技术网

C++ MPI Ibcast的使用

C++ MPI Ibcast的使用,c++,mpi,broadcast,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 requ

我试图了解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();
}
#包括
#包括
#包括
#包括
使用名称空间std;
甲级{
公众:
int a[3];
MPI_请求;
int标签;
A(){}
int foo(){
整数秩;
MPI通信等级(MPI通信世界和等级);

cout我不完全确定您想要做什么,但这里是您示例的固定版本。我纠正的主要问题是使用请求,因为您没有等待正确的请求

#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();
}

现在它可以工作了,希望它能达到您的预期。但是,您对进程组的描述让我相信您应该使用例如
MPI\u Comm\u split()创建几个子通信程序
为了允许在…

中使用集体调用,我不完全确定您想要做什么,但这里是您示例的固定版本。我纠正的主要问题是使用请求,因为您没有等待正确的请求

#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();
}
现在它可以工作了,希望它能达到您的预期。但是,您对进程组的描述使我相信您应该使用例如
MPI\u Comm\u split()
来创建几个子通信程序,以便在