我正在尝试使用 C++ 语言使用 MPI 库来理解并行代码和顺序代码之间的区别

I am trying to understand the difference between parallel and sequential code using MPI library with C++ language

这是代码“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!

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

如果你阅读 doc 你会说

This will run X copies of in your current run-time environment

这正是发生的事情。