【问题标题】:MPI_Comm_spawn and MPI_ReduceMPI_Comm_spawn 和 MPI_Reduce
【发布时间】:2013-01-21 19:25:38
【问题描述】:

我有两个程序。产生执行一些计算的“工人”的“主人”,我希望主人从工人那里得到结果并存储总和。我正在尝试使用 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++ for osx):

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

这是运行产生工人的主人的正确方法吗?

在 master 中,我总是从 MPI_Reduce 得到 0。我可以使用来自 intercommunicators 的 MPI_reduce 还是应该使用来自 worker 的 MPI_Send 和来自 master 的 MPI_Recv?我真的不确定为什么它不起作用。

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

【问题讨论】:

    标签: c++ mpi


    【解决方案1】:

    MPI_Comm_get_parent 返回包含原始进程和所有衍生进程的父交互器。在这种情况下,调用MPI_Comm_rank(parent, &amp;parent_id) 不会返回父进程的排名,而是返回当前进程在交互者本地组中的排名:

    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)与交互器的操作有点不同。首先必须决定两个组中的哪一个将托管根。一旦根组被识别,根进程必须将MPI_ROOT 作为MPI_REDUCE 中的root 的值传递,并且根组中的所有其他进程必须传递MPI_PROC_NULL。即接收组中的进程根本不参与根集体操作。由于主代码被编写成主组中可能只有一个进程,那么将主代码中对MPI_Reduce的调用更改为:

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

    请注意,master 本身也不参与归约操作,例如sendbuf(在这种情况下为&amp;send)的值无关紧要,因为根不会发送要减少的数据 - 它只是收集对远程组中进程的值执行的减少的结果。

    【讨论】:

    • 感谢您的解释,帮了大忙!
    • @Hristo Iliev 在这里,您在工作代码中将根的等级硬编码为 0。我想知道是否有办法从工人内部找出根的等级。
    • @takwing,在这种特殊情况下,初始 MPI 作业由单个 MPI 进程组成,因此从工作进程的角度来看,父交互器的远程组在其中只有一个等级它始终为 0。
    • @Hristo,我想我问问题的方式不清楚。实际上,我的问题不仅限于这个特定的代码。我正在寻找一种方法来从另一个组中找到一个进程的等级?让我们来看看这个场景:如果根在运行时从一个进程到另一个进程会发生变化怎么办,这意味着工作人员现在必须弄清楚内部通信器另一侧的根相对于远程组的等级是多少.有没有办法从工人端提取这些信息?
    • @takwing,从远程广播其排名的实际根。我没有更好的主意。人们通常通过算法修复此类过程或使用一些预编码逻辑(可能通过通信)来确定它。我认为生成的作业没有理由不能使用相同的逻辑来找出根进程是什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 2011-05-14
    相关资源
    最近更新 更多