【问题标题】:I am trying to understand the difference between parallel and sequential code using MPI library with C++ language我试图了解使用 MPI 库和 C++ 语言的并行代码和顺序代码之间的区别
【发布时间】:2020-11-14 17:36:24
【问题描述】:

这是代码“expl.cpp”:

#include <iostream>
#include "mpi.h"
using namespace std;
int main (int argc,char **argv)
{   
cout << "this line of code is executed in sequential mode " << endl ;
MPI::Init();
cout << "hello MPI world!" << endl ;
MPI::Finalize();
return 0;
}

用上面的代码编译后

mpicc -o expl expl.cpp 

并使用此命令执行二进制文件

mpirun -np 4 ./expl

我的预期输出是:

this line of code is executed in sequential mode
hello MPI world!
hello MPI world!
hello MPI world!
hello MPI world!

但我得到了这个输出:

this line of code is executed in sequential mode
this line of code is executed in sequential mode
this line of code is executed in sequential mode
this line of code is executed in sequential mode
hello MPI world!
hello MPI world!
hello MPI world!
hello MPI world!

我正在寻找一个简单的说明。

【问题讨论】:

  • MPI 标准未指定在 MPI_Init()MPI_Finalize() 之前发生的情况。大多数 MPI 实现选择生成所有任务(这就是你最终拥有多个“顺序模式”行的原因),但启动单个 MPI 任务是完全有效的,然后 fork() MPI_Init() 所以你会有只有一个“顺序模式”行。如果您想编写可移植代码,在MPI_Init()MPI_Finalize() 之前不要做任何事情会更安全/更好

标签: c++ parallel-processing mpi embedded-linux


【解决方案1】:

如果您阅读了doc,它会说

这将在您当前的运行时环境中运行 X 个副本

这正是发生的事情。

【讨论】:

  • 如果所有的代码都会被mpirun命令并行执行,那么这个例子中MPI::Init();MPI::Finalize();有什么用呢?
  • 阅读这些文档,说他们初始化并破坏了 MPI 环境。你真正要找的一定是对线程的一些使用,而不是多次调用同一个程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2010-10-25
  • 1970-01-01
  • 2013-12-07
  • 1970-01-01
相关资源
最近更新 更多