当前位置:文档之家› MPI编程环境配置与示例

MPI编程环境配置与示例

MPI编程环境配置与示例
MPI编程环境配置与示例

目录

一、系统安装 (1)

1、下载地址 (1)

2、安装步骤 (1)

3、Visual Stdio设置 (1)

二、实验程序 (2)

1、简单得MPI编程示例 (2)

2、消息传递MPI编程示例1 (3)

3、消息传递MPI编程示例2 (4)

4、Monte Carlo方法计算圆周率 (6)

5、计算积分 (8)

三、心得体会 (9)

一.系统安装

1.下载地址

FTP匿名登陆,在pub/mpi/nt文件夹中

2.安装步骤

1)在安装有MPI得计算机上要建立一个有管理员权限得账户,不可以没有密

码;

2)双击exe文件,按默认设置安装MPI;

3)注册MPI账户,调用MPIRegister、exe,用户名与密码即为第一步中得账户。

3.Visual Stdio设置

为避免每新建一个项目都要设置一次,可以对它进行通用设置。

打开视图-其她窗口-属性管理器,点击Debug|Win32目录下得Microsoft、Cpp、Win32、user,在VC++目录下得包含目录中添加MPICH得Include路径,库目录中添加MPICH得Lib路径;在链接器-输入目录下得附加依赖项中添加mpich、lib、mpe、lib、mped、lib、mpichd、lib。

二.实验程序

1.简单得MPI编程示例

1)源代码

#include

#include

int main(int argc, char* argv[])

{

int num, rk;

MPI_Init(&argc, &argv);

MPI_Comm_size(MPI_COMM_WORLD, &num);

MPI_Comm_rank(MPI_COMM_WORLD, &rk);

printf("Hello world from Process %d of %d\n", rk, num);

MPI_Finalize();

return 0;

}

2)运行截图

2.消息传递MPI编程示例1

1)源代码

#include

#include

int main(int argc, char** argv)

{

int myid, numprocs,source;

MPI_Status status;

char messages[100];

MPI_Init(&argc, &argv);

MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

MPI_Comm_rank(MPI_COMM_WORLD, &myid);

if (myid != 0)

{

strcpy(messages, "Hello World!");

MPI_Send(messages, strlen(messages) + 1, MPI_CHAR, 0, 99, MPI_COMM_WORLD);

}

else

{

for (source = 1; source < numprocs; source++)

{

MPI_Recv(messages, 100, MPI_CHAR, source, 99, MPI_COMM_WORLD,&status);

printf("I am process %d I recv string '%s' from process %d\n", myid, messages, source);

}

}

MPI_Finalize();

return 0;

}

2)运行截图

3.消息传递MPI编程示例2

1)源代码

}

MPI_Send(&SqrtSum, 1, MPI_DOUBLE, 0, 99, MPI_COMM_WORLD);

}

printf("I am process %d、I recv total %d from process 0, and SqrtSum=%f、\n", myid, C, SqrtSum);_

MPI_Finalize();_

return 0;

}

2)运行截图

4.Monte Carlo方法计算圆周率

1)源代码

#include "mpi、h"

#include

#include

int main(int argc, char **argv)

{

int myid, numprocs;

int source;

2)运行截图

5.计算积分

1)源代码

#define N 100000000

#define a 0

#define b 10

#include

#include

#include

#include "mpi、h"

int main(int argc, char** argv)

{

int myid, numprocs;

int i; _

double local = 0、0, dx = ((double)b - (double)a) / (double)N;

double inte, x;

MPI_Init(&argc, &argv);_

MPI_Comm_rank(MPI_COMM_WORLD, &myid);_

MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

for (i = myid; i

{

x = a + i*dx + dx / 2;

local += x*x*dx;

}

MPI_Reduce(&local, &inte, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);_

if (myid == 0)

{

printf("The integal of x*x in region [%d,%d] =%16、15f\n", a, b,

inte);

}

MPI_Finalize();

return 0;

}

2)运行截图

三.心得体会

1.学习了MPICH得安装、注册。用户名与密码为装有MPI、得计算机得账号名

与密码(注册时密码不能为空),

2.学会执行MPI程序——基于图形界面得guiMPIRun

3.通过PPT上得程序,真正了解了什么就是并行实验程序,并自己实现与观察

了并行程序执行得过程,对并行程序设计有了进一步得了解。

相关主题
文本预览
相关文档 最新文档