Run Coder运行mpi但是结果不正确的问题

博主在CSDN上分享了配置MPI环境并使用RunCoder运行C++ MPI程序的步骤,但遇到程序只执行了非MPI部分的问题。解决方法包括检查编译器版本和替换旧版MinGW64。更新编译器后,问题得到解决。
摘要由CSDN通过智能技术生成

我根据CSDN现有博客中的方式配置了运行mpi需要的环境。

1. 步骤

步骤一:下载mpi包

步骤二:在 Run Coder的settings中更换C/C++的命令(如下)

"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt -fopenmp -l msmpi -L \"C:\\Program Files (x86)\\Microsoft SDKs\\MPI\\Lib\\x64\" -I \"C:\\Program Files (x86)\\Microsoft SDKs\\MPI\\Include\" && mpiexec -n 4 $fileNameWithoutExt",
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -fopenmp -l msmpi -L \"C:\\Program Files (x86)\\Microsoft SDKs\\MPI\\Lib\\x64\" -I \"C:\\Program Files (x86)\\Microsoft SDKs\\MPI\\Include\" && mpiexec -n 4 $fileNameWithoutExt",

步骤三:用Run Coder运行代码即可。

这部分CSDN有线程的博客,不再赘述。

2. 出现的问题

我按照这个流程运行的时候发现,程序能够运行但是结果却不正确。例如下面这一段代码:

#include <iostream>
#include <mpi.h>
#include <stdint.h>
using namespace std;

int main(int argc,char* argv[]){
    cout<<"hello"<<endl;
    int myid, numprocs;
    int  namelen;
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Get_processor_name(processor_name,&namelen); 
    cout<<"Hello World! Process "<<myid<<" of "<<numprocs<<" on "<<processor_name<<endl;
    MPI_Finalize();
    return 0;
}

/*
正确的输出是:(猫头鹰是我的电脑名字)
hello
hello
hello
hello
hello
Hello World! Process 3 of 5 on 猫头鹰
Hello World! Process 2 of 5 on 猫头鹰
Hello World! Process 0 of 5 on 猫头鹰
Hello World! Process 4 of 5 on 猫头鹰
Hello World! Process 1 of 5 on 猫头鹰
*/
/*
我的错误输出:
hello
hello
hello
hello
hello
*/

编译命令是:

mpiexec -n 5 gpu_t.exe  //源文件:gpu_t.cpp

我们发现,程序调用了5个线程去执行这个代码,但是没有执行和mpi有关的部分,仅仅执行了

cout<<"hello"<<endl;

3. 解决办法

3.1 编译器的版本太低

电脑的编译器版本不够,不能够正确地编译mpi部分的代码,虽然没有报错,但是不会有正确的结果。

3.2 vscode默认编译器的更改

更换vscode C/C++编译器的时候发现,仅仅将新的编译器添加到环境变量中,并没有解决我的问题。所以,我把新的编译器 MinGW64-8.01文件 替换了我原来的MinGW64 4.9文件,问题得到了解决。采取了比较暴力的方法,如果能通过指定vscode的编译器路径更好,但是我没有成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值