目录
第一步:下载安装mpi 官网:http://www.mpich.org/ windows版官网:https://msdn.microsoft.com/en-us/library/bb524831(v=vs.85).aspx
第二步:Visual Studio 2017配置环境
点击下载后选择不同操作系统的版本这里选择windows
二者都勾选下载,下载后分别点击安装msmpisdk.msi与MsMpiSetup.exe(根据提示安装即可)
1.新建项目->windows 桌面向导->选择控制台应用程序(勾选空项目)
2.源文件->添加新建项->C++文件
3.配置项目属性
包含目录:加入mpi SDK中的include文件夹
库目录:加入mpi SDK中的lib和lib/X64文件夹
链接器->输入->附加依赖项
编译完成后,进入程序生成的.exe文件所在的文件夹,命令行运行.exe程序
mpiexec -n 4 Project8.exe
源代码:
#include
#include
#include "mpi.h"
void Hello(void);
int main(int argc, char *argv[])
{
int me, option, namelen, size;
char process_name[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &me);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size < 2) {
fprintf(stderr, "system requires at least 2 processes");
MPI_Abort(MPI_COMM_WORLD, 1);
}
MPI_Get_processor_name(process_name, &namelen);
fprintf(stderr, "Process %d is alive on %s\n", me, process_name);
MPI_Barrier(MPI_COMM_WORLD);
Hello();
MPI_Finalize();
}
void Hello()
{
int nproc, me;
int type = 1;
int buffer[2], node;
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &me);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
if (me==0) {
printf("\nHello test from all to all\n");
fflush(stdout);
}
for(node = 0; node < nproc; node++)
{
if (node != me) {
buffer[0] = me;
buffer[1] = node;
MPI_Send(buffer, 2, MPI_INT, node, type, MPI_COMM_WORLD);
MPI_Recv(buffer, 2, MPI_INT, node, type, MPI_COMM_WORLD, &status);
if (buffer[0] != node || buffer[1] != me) {
fprintf(stderr, "Hello: %d != %d or %d != %d\n", buffer[0], node, buffer[1], me);
printf("Mismatch on hello process ids; node = %d\n",node);
}
printf("Hello from %d to %d\n",me,node);
fflush(stdout);
}
}
}