1、 下载安装MPICH,安装过程中最好把密码设置为自己的开机密码
关于安装完成后设置
对MPICH2的wmpiregister.exe 设置用户名密码,是计算机名和开机密码
可以来个简单的测试,打开wmpirexec.exe在application中添加Program Files (x86)\MPICH2\examples中例子,选中“run in an separate window” 然后点击execute…(如果有问题,有可能是wmpiregister.exe用户名设置的问题)
现在设置vc++6.0------------------------------------------------------------------
2、打开vc++6.0 在(工具-选项-目录)把mpich相对应的include和lib添加到include files/library files
3、新建一个工程,可以写mpitest.cpp,为了避免宏定义冲突,在#include”mpi.h”之前要加入#define MPICH_SKIP_MPICXX(这样就可以通过编译)
4、编译通过了,但是链接仍然会出错 继续设置
(在工程—>设置->链接中的对象/库模板块后加入 mpi.lib)上图
这样就可以通过链接了(每次建工程都要加入mpi.lib的操作)
再贴一个简单的例子吧
#define MPICH_SKIP_MPICXX
#include "mpi.h"
#include <stdio.h>
#include <math.h>
double f(double);
double f(double a)
{
return (4.0 / (1.0 + a*a));
}
int main(int argc,char *argv[])
{
int done = 0, n, myid, numprocs, i;
double PI25DT = 3.141592653589793238462643;
double mypi, pi, h, sum, x;
double startwtime = 0.0, endwtime;
int namelen;
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(stdout,"Process %d of %d is on %s\n",
myid, numprocs, processor_name);
fflush(stdout);
*/
while (!done) {
if (myid == 0) {
fprintf(stdout, "Enter the number of intervals: (0 quits) ");
fflush(stdout);
if (scanf("%d",&n) != 1) {
fprintf(stdout, "No number entered; quitting\n" );
n= 0;
}
startwtime = MPI_Wtime();
}
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (n == 0)
done = 1;
else {
h = 1.0 / (double) n;
sum = 0.0;
for (i = myid + 1; 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) {
printf("pi isapproximately %.16f, Error is %.16f\n",
pi, fabs(pi - PI25DT));
endwtime= MPI_Wtime();
printf("wallclock time = %f\n", endwtime-startwtime);
fflush(stdout );
}
}
}
MPI_Finalize();
return 0;
}