MPI_Comm_spawn和MPI_Reduce(MPI_Comm_spawn and MPI_R

2019-07-19 09:52发布

我有两个方案。 “大师”,它会生成“工人”,它执行一些计算,我想师傅得到工人的结果和存储的总和。 我试图用MPI_Reduce从工人收集结果,工人用MPI_Reduce发送到主人MPI_Comm。 我不知道如果这是正确的。 下面是我的程序:

主:

#include <mpi.h>
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) { 
    int world_size, universe_size, *universe_sizep, flag; 

    int rc, send, recv;

    // intercommunicator
    MPI_Comm everyone;

    MPI_Init(&argc, &argv); 
    MPI_Comm_size(MPI_COMM_WORLD, &world_size); 

    if (world_size != 1) {
        cout << "Top heavy with management" << endl;
    } 

    MPI_Attr_get(MPI_COMM_WORLD, MPI_UNIVERSE_SIZE, &universe_sizep, &flag);  
    if (!flag) { 
        cout << "This MPI does not support UNIVERSE_SIZE. How many processes total?";
        cout << "Enter the universe size: ";
        cin >> universe_size; 
    } else {
        universe_size = *universe_sizep;
    }
    if (universe_size == 1) {
        cout << "No room to start workers" << endl;
    }

    MPI_Comm_spawn("so_worker", MPI_ARGV_NULL, universe_size-1,  
             MPI_INFO_NULL, 0, MPI_COMM_SELF, &everyone,  
             MPI_ERRCODES_IGNORE);

    send = 0;

    rc = MPI_Reduce(&send, &recv, 1, MPI_INT, MPI_SUM, 0, everyone);

    // store result of recv ...
    // other calculations here
    cout << "From spawned workers recv: " << recv << endl;

    MPI_Finalize(); 
    return 0; 
}

工人:

#include <mpi.h>
#include <iostream>
using namespace std;

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

    int rc, send,recv;


    int parent_size, parent_id, my_id, numprocs; 
    // parent intercomm
    MPI_Comm parent; 
    MPI_Init(&argc, &argv); 

    MPI_Comm_get_parent(&parent); 
    if (parent == MPI_COMM_NULL) {
        cout << "No parent!" << endl;
    }
    MPI_Comm_remote_size(parent, &parent_size); 
    MPI_Comm_rank(parent, &parent_id) ; 
    //cout << "Parent is of size: " << size << endl;
    if (parent_size != 1) {
        cout << "Something's wrong with the parent" << endl;
    }

    MPI_Comm_rank(MPI_COMM_WORLD, &my_id) ;     
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs) ;  

    cout << "I'm child process rank "<< my_id << " and we are " << numprocs << endl;
    cout << "The parent process rank "<< parent_id << " and we are " << parent_size << endl;

    // get value of send
    send = 7; // just an example
    recv = 0;

    rc = MPI_Reduce(&send, &recv, 1, MPI_INT, MPI_SUM, parent_id, parent);
    if (rc != MPI_SUCCESS)
        cout << my_id << " failure on mpi_reduce in WORKER" << endl;

    MPI_Finalize(); 
    return 0; 
} 

我既编译并执行这样的(MPIC ++的OSX):

mpic++ so_worker.cpp -o so_worker
mpic++ so_master.cpp -o so_master
mpirun -n 1 so_master

这是运行产卵的工人掌握正确的方法是什么?

在主我总是得到0从MPI_Reduce回来。 我可以使用MPI_reduce从intercommunicators或者我应该使用MPI_SEND工人和MPI_RECV从主? 我真的不知道为什么它不工作。

任何帮助,将不胜感激。 谢谢!

Answer 1:

MPI_Comm_get_parent返回包含原工艺和所有的衍生者父intercommunicator。 在这种情况下调用MPI_Comm_rank(parent, &parent_id)不返回父的排名,而是在本地组intercommunicator当前进程的排名:

I'm child process rank 0 and we are 3
The parent process **rank 0** and we are 1
I'm child process rank 1 and we are 3
The parent process **rank 1** and we are 1
I'm child process rank 2 and we are 3
The parent process **rank 2** and we are 1

(观察突出显示的值有什么不同 - 人们会认为父进程的排名应该是一样的,不应该吗?)

这就是为什么MPI_Reduce()调用将不会成功,因为所有的工作进程根级别指定不同的值。 由于最初有一个主进程,在它的远端组秩parent将是0和因此所有工人应该指定0作为根到MPI_Reduce

//
// Worker code
//
rc = MPI_Reduce(&send, &recv, 1, MPI_INT, MPI_SUM, 0, parent);

这只是问题的一半。 另一半是根深蒂固的集体操作(如MPI_REDUCE )操作与intercommunicators有点不同。 一个第一必须决定哪两组将举办根。 一旦根组被识别,根进程必须通过MPI_ROOT为的值rootMPI_REDUCE和根组中的所有其它工艺必须通过MPI_PROC_NULL 。 这是接收组的过程不会在扎根集体经营参与的。 由于主代码是这么写的,有可能是主人的组中只有一个进程,那么这将足以改变呼叫MPI_Reduce在主代码:

//
// Master code
//
rc = MPI_Reduce(&send, &recv, 1, MPI_INT, MPI_SUM, MPI_ROOT, everyone);

请注意,主也不参与还原操作本身,例如该值sendbuf&send在这种情况下)是不相关的作为根不会将数据发送到被降低-它仅仅收集在进行还原的结果从远程组中的工艺值。



文章来源: MPI_Comm_spawn and MPI_Reduce
标签: c++ mpi