在C ++中的MPI中并行执行可执行文件 [英] parallel run of executable within MPI in C++

查看:418
本文介绍了在C ++中的MPI中并行执行可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用MPI已有一段时间了,但是我没有经验.所以 我在这里问有关以下实现的一般结构的建议. 说,我的主C ++文件带有

i have been using MPI for a while, but i'm not experienced. So i'm here to ask an advice on general structure of the following implementation. Say, I have the main C++ file with

MPI_Init(&narg,&arg);
int me,nprocs;
MPI_Comm_rank(MPI_COMM_WORLD,&me);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);

int N = 10;

for (int i=0;i<N;i++) {
   //(1)do some stuff in parallel...
   //(2)gather results and write an input file for executable

   MPI_Barrier(MPI_COMM_WORLD);
   //(3)run executable in parallel.
   // which is usually run from command line as:
   // 
   // mpirun -np 6 external.exe < input.file
   //
   MPI_Barrier(MPI_COMM_WORLD);

   //(4)gather output from executable, distribute info among processors and keep running 
}
MPI_Finalize();

这是(3)处,我在理解如何执行并告诉它可以使用多少个处理器方面遇到问题.我的困惑还在于,应该从单个处理器/实例执行某种运行"命令.那么,如何使它工作并让并行可执行文件使用提供给主程序的所有处理器?如果可能的话.

it's the (3) where i have a problem understanding how to do it and tell how many processors can it use. My confusion is also that some kind of "run" command should probably be executed from a single processor/instance. So how do i make it work and let parallel executable use all processors which were provided to the main program? If it is possible.

p/s/我在stackoverflow中看到了类似的问题,但是没有确定的答案.

p/s/ i saw similar questions here in stackoverflow, but no definite answer on if it is possible or not.

推荐答案

您对exe有控制权,即您可以更改其代码吗?如果是这样,我建议重新开发它,以便该exe只是您所需行为的包装,然后您可以将实际操作链接到您的应用程序中.

Do you have control over the exe, i.e. can you change its code? If so, I'd suggest re-developing it so that the exe is simply a wrapper around the behavior you need, and then you can link in the actual action into your application.

如果这不是一个选择,我建议您仅从您的主进程(等级0)中调用可执行文件,然后让其他人等待.效率不高,但可以完成工作:

If that is not an option, I suggest just calling the executable from your master (rank 0) process, and let the others wait. Not super efficient, but it'll do the job:

if (me == 0) {
    system("mpirun -np 6 external.exe < input.file")
}

您必须找出一种等待命令完成的方法,但是根据系统 mpirun 就像检查是否system(...)的返回值是零,然后继续(在屏障之后,如您的示例所示).

You'll have to figure out a way to wait until the command is finished, but according to the docs of system and mpirun it should be as simple as checking if the return value from system(...) is zero, and then continue (after a barrier, as in your example).

这篇关于在C ++中的MPI中并行执行可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆