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

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

本文关键字:代码 区别 并行 顺序 之间 C++ 语言 MPI      更新时间:2024-09-28

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

我想要一个简单的澄清。

如果你阅读文档,它会显示

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

这正是发生的事情。