MPI的全称是Message Passing Interface即标准消息传递界面,可以用于并行计算。MPI的具体实现一般采用MPICH。下面介绍如何在Windows 7系统下VC6中搭建MPI环境来编写MPI程序。
1.安装MPI的SDK——MPICH2
mpich2-1.4.1p1-win-ia32安装程序的下载地址:http://www.mcs.anl.gov/research/projects/mpich2/downloads/tarballs/1.4.1p1/mpich2-1.4.1p1-win-ia32.msi
本文以设置安装在C:\Program Files\MPICH2目录下为例。
测试所安装的MPICH2
测试前首先需要注册一个用户,具体操作如下:“开始”按钮–>所有程序–>MPICH2–>wmpiregister.exe。输入用户名、密码。有一点需要说明:该用户名须为有效的操作系统管理员账户,密码对应为系统登录密码。如图所示:
接下来选择开始–>所有程序–>MPICH2–>wmpiexec.exe;
选择Application为 c:\program files\mpich2\examples\cpi.exe (就是自带的一个计算圆周率的例子程序)。在Number of processes的数量选择2表示用二个进程来协同完成。选中“run in separate windw”选项。再点击Excute就可以执行了。
然后在控制台窗口下提示输入number of intervals ,随便输入个大点的数字(4000,4000000)就可以看到求的的圆周率值。如下图:
运行结果如下:
2.在VC6.0中添加MPICH
在VC6.0中加入mpi的include和lib。VC6.0程序菜单中“工具” –> “选项”–>“目录”然后添加,如下图所示:
3.本以为到此就已经安装好了,然后就赶紧写了个Hello World的程序。
- #include <mpi.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <conio.h>
- #pragma comment (lib, “mpi.lib”)
- int main(int argc, char* argv[])
- {
- int myid,numprocs;
- int namelen;
- char processor_name[MPI_MAX_PROCESSOR_NAME];
- MPI_Init(&argc, &argv);
- //用MPI_Comm_rank 获得进程的rank,该rank值为0到p-1间的整数,相当于进程的ID
- MPI_Comm_rank(MPI_COMM_WORLD, &myid);
- //用MPI_Comm_size 获得进程个数 int MPI_Comm_size(MPI_Comm comm, int *size);
- MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
- MPI_Get_processor_name(processor_name, &namelen);
- printf(“Hello World! by MoreWindows\nProcess %d of %d on %s\n”, myid, numprocs, processor_name);
- MPI_Finalize();
- if (myid == 1)
- {
- printf(“\nPress a key and exit.\n”);
- getch();
- }
- return 0;
- }
编译运行之后各种如下错误:
c:\program files\mpich2\include\mpicxx.h(1536) : error C2555: ‘MPI::Intercomm::Clone’ : overriding virtual function differs from ‘MPI::Comm::Clone’ only by return type or calling convention
c:\program files\mpich2\include\mpicxx.h(1107) : see declaration of ‘Comm’
郁闷,这么个小东西都装不好,还谈什么写程序啊!
终于找到一个不错的解决办法!如下:
细看错误,都是一些函数重载错误,基本上是mpicxx.h文件导致的错误,于是想是否MPI_Init等函数与此文件有关。通过搜索包含文字,发现MPI_Init等函数只在mpi.h中定义,于是想办法不包含mpicxx.h文件以避开问题。在mpi.h中发现代码:
#if !defined(MPICH_SKIP_MPICXX)
#include “mpicxx.h”
#endif
在程序中定义宏MPICH_SKIP_MPICXX,然后重新编译程序(注意在包含mpi.h前定义)果然避开了mpicxx.h文件,OK!
运行结果如下:
终于尝到了一点甜头,然后就写一个计算PI的并行程序:
- #define MPICH_SKIP_MPICXX
- #include “mpi.h”
- #include <stdio.h>
- double f( double a ) { return (4.0 / (1.0 + a*a)); }
- int main( int argc, char *argv[])
- {
- int n, myid, numprocs, i, namelen;
- double PI25DT = 3.141592653589793238462643;
- double mypi, pi, h, sum, x;
- double startwtime, endwtime;
- char processor_name[MPI_MAX_PROCESSOR_NAME];
- MPI_Init(&argc,&argv);
- MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
- MPI_Comm_rank(MPI_COMM_WORLD,&myid);
- MPI_Get_processor_name(processor_name,&namelen);
- fprintf(stderr,“Process %d on %s\n”, myid, processor_name);
- if (myid == 0)
- {
- n=10000;
- startwtime = MPI_Wtime();
- }
- MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
- h = 1.0 / (double) n;
- sum = 0.0;
- for (i = myid; i < n; i += numprocs)
- {
- x = h * ((double)i + 0.5);
- sum += f(x);
- }
- mypi = h * sum;
- MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
- if (myid == 0)
- {
- endwtime = MPI_Wtime();
- printf(“pi is approximately %.16f, error is %.16f\n”, pi, pi – PI25DT);
- printf(“wall clock time = %f\n”, endwtime-startwtime);
- }
- MPI_Finalize();
- return 0;
- }
编译没有错误,但是链接时又出现了如下图所示错误:
进入各种抓狂状态,在网上找了半天,要将MPICH的include和lib加入VC6.0,是啊!我不是都已经加了吗?最后在网上找到了一个在Visual Studio 2005里面的配置MPICH的博客,相关内容如下:
新建一个空白“Windows控制台应用程序”项目,假设命名为FFT,在“项目”菜单中选择“FFT属性”,在弹出对话框中依次开左侧树型列表的“配置属性”、“链接器”、“输入”,在“附加依赖项”一栏输入:mpi.lib。选择“配置”下拉列表中的release选项,同样将mpi.lib添加上去。(也可以使用#pragma预处理指令:#pragmacomment(lib,”mpi.lib”) )
在VC6.0中找连接器找半天没有找到,所以就试了一下#pragma comment(lib,”mpi.lib”)
其实上面那个Hello World的程序中已经有这句话了,只是那个Hello World是我在网上粘贴的一段,就没有意识到那个问题!
- #define MPICH_SKIP_MPICXX
- #include “mpi.h”
- #include <stdio.h>
- #pragma comment (lib, “mpi.lib”)////////////////////这句话很重要
- double f( double a ) { return (4.0 / (1.0 + a*a)); }
- int main( int argc, char *argv[])
- {
- int n, myid, numprocs, i, namelen;
- double PI25DT = 3.141592653589793238462643;
- double mypi, pi, h, sum, x;
- double startwtime, endwtime;
- char processor_name[MPI_MAX_PROCESSOR_NAME];
- MPI_Init(&argc,&argv);
- MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
- MPI_Comm_rank(MPI_COMM_WORLD,&myid);
- MPI_Get_processor_name(processor_name,&namelen);
- fprintf(stderr,“Process %d on %s\n”, myid, processor_name);
- if (myid == 0)
- {
- n=10000;
- startwtime = MPI_Wtime();
- }
- MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
- h = 1.0 / (double) n;
- sum = 0.0;
- for (i = myid; i < n; i += numprocs)
- {
- x = h * ((double)i + 0.5);
- sum += f(x);
- }
- mypi = h * sum;
- MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
- if (myid == 0)
- {
- endwtime = MPI_Wtime();
- printf(“pi is approximately %.16f, error is %.16f\n”, pi, pi – PI25DT);
- printf(“wall clock time = %f\n”, endwtime-startwtime);
- }
- MPI_Finalize();
- return 0;
- }
编译运行通过,结果如下: