【发布时间】: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