环境:WIN10 VISUAL STUDIO2015
-
下载安装MPICH
点击下载
两个工具都要下载
-
安装
点击.exe文件,安装到D盘,注意路径里面不要有空格或者中文字符
点击.msi文件,安装到D盘,注意路径里面不要有空格或者中文字符
打开cmd命令,输入set MSMPI
- 配置visual studio
创建新项目Hello_MPI
,注意路径里面不要有空格或者中文字符
新建一个源文件.cpp
粘贴下面代码:
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
// Initialize the MPI environment
MPI_Init(NULL, NULL);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
// Print off a hello world message
printf("Hello world from processor %s, rank %d out of %d processors\n",
processor_name, world_rank, world_size);
// Finalize the MPI environment.
MPI_Finalize();
}
打开属性页面
先选择X64
第一步
VC++目录→ 包含目录 追加 你的路径\Microsoft SDKs\MPI\Include;
VC++目录→ 库目录 追加 你的路径\Microsoft SDKs\MPI\Lib\x64;
第二步:设置附加包含目录 $(MSMPI_INC);$(MSMPI_INC)\x64
C/C++ → 预处理器 → 预处理定义 末尾追加 ;MPICH_SKIP_MPICXX
C/C++ → 代码生成 → 运行库 选择 多线程调试/MTd
第三步:
链接器→输入→附加依赖项 追加 ;msmpi.lib
链接器→所有选项→附加库目录 $(MSMPI_LIB64)
链接器→所有选项→附加依赖项 末尾添加 ;msmpi.lib
最后点击确定就OK了!
CTRO+F5
这个是单线程
设置多线程:
配置属性页面→调试
命令 D:\Linux\Microsoft_MPI\Bin\mpiexec.exe
命令参数 -n 8 $(TargetPath)
n=8 表示8个线程
$(TargetPath)是编译器生成的test1.exe文件:
D:\Program Files (x86)\VisualStdio_Project\Hello_MPI\test1\x64\Debug
我路径有空格,所以运行失败,最后把exe文件拷贝到D盘目录下了
参考文章:
Windows10+VisualStudio2019配置MPI 附在VS中直接运行MPI程序的方法
MPI Hello World