简体   繁体   English

MPI,通过其中一个过程产生一个孩子

[英]MPI, spawning a child by one of the processes

I am taking my first steps in the world of parallel programming with Open MPI. 我正在使用Open MPI在并行编程领域迈出第一步。 What I'm trying to achieve is to start an application with two processes (this can be achieved with mpirun -n 2 ...) and make the first process from these both spawn another instance of itself. 我想要实现的是使用两个进程启动一个应用程序(这可以通过mpirun -n 2实现...)并使这些进程中的第一个进程生成另一个自身实例。 Here is the sketch in c++: 这是c ++中的草图:

// mpi_spawn_cpp2.cc
#include "mpi.h"
#include <iostream>

using namespace std;

int main( int argc, char *argv[] )
{
    int rank, size;
    int errcode;
    MPI::Intercomm intercomm;

    MPI::Init( argc, argv );

    rank = MPI::COMM_WORLD.Get_rank();
    size = MPI::COMM_WORLD.Get_size();
    cout << "world size: " << size << endl;

    if (rank == 0) {
        intercomm = MPI::COMM_WORLD.Spawn("./mpi_spawn_cpp2", MPI::ARGV_NULL, 1, MPI::INFO_NULL, 0, &errcode);
        cout << "intercomm size: " << intercomm.Get_size() << endl;
    }

    MPI::Finalize();
    return 0;
}

Although it compiles flawlessly the problem with the above code is that it doesn't seem to return from the Spawn function, which means the process is in fact not spawned. 尽管它完美地编译了上述代码的问题,但它似乎并没有从Spawn函数返回,这意味着该进程实际上并未生成。 What is wrong with the above code and what should be corrected in order for it to work? 上面的代码有什么问题,为了使它工作,应该纠正什么?

Your help is greatly appreciated. 非常感谢您的帮助。

MPI_Comm_spawn is collective over the intracommunicator but you are calling it only from rank 0 and that's why it hangs. MPI_Comm_spawn对于内部MPI_Comm_spawn是集体的,但是你只是从0级调用它,这就是它挂起的原因。 Try this instead: 试试这个:

intercomm = MPI::COMM_WORLD.Spawn("./mpi_spawn_cpp2", MPI::ARGV_NULL, 1, MPI::INFO_NULL, 0, &errcode);
if (rank == 0) {
    cout << "intercomm size: " << intercomm.Get_size() << endl;
}

A word of advice. 一句忠告。 Since it is your first steps in MPI, I would recommend that you learn the C bindings instead. 由于这是您在MPI中的第一步,我建议您学习C绑定。 The C++ bindings are deprecated in the current MPI standard version 2.2 and will be completely dropped from the upcoming version 3.0. C ++绑定在当前的MPI标准版本2.2中已弃用,并将从即将发布的3.0版本中完全删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM