日期:2014-05-17  浏览次数:20851 次

Windows 7系统下搭建MPI(并行计算)环境

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!
<