windows下MPI使用
安装
MS MPI下载
https://www.microsoft.com/en-us/download/details.aspx?id=57467
-
打开vs,创建一个 Win32 控制台应用程序
-
在解决方案上右键属性
-
-
设置附加包含目录为
$(MSMPI_INC);$(MSMPI_INC)\x64
如果要生成32位程序需要设置为$(MSMPI_INC);$(MSMPI_INC)\x86
-
-
设置 链接器 的属性,设置 附加库目录 为
$(MSMPI_LIB64)
如果要生成32位程序 设置为$(MSMPI_LIB32)
-
设置 附加依赖项 为
msmpi.lib
-
-
编写一个helloworld程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main(int argc ,char * argv[])
{
MPI_Init(&argc, &argv);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
char helloStr[] = "Hello World";
MPI_Send(helloStr, _countof(helloStr), MPI_CHAR, 1, 0, MPI_COMM_WORLD);
}
else if (rank == 1) {
char helloStr[12];
MPI_Recv(helloStr, _countof(helloStr), MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Rank 1 received string %s from Rank 0\n", helloStr);
}
MPI_Finalize();
return 0;
} -
运行:
-
拷贝程序 MPITest.exe 到每台电脑下的 c:\1 文件夹下
1
mpiexec -hosts 2 10.12.220.168 10.12.220.206 c:\1\MPITest.exe
-
圆周率计算程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61// 圆周率计算程序
//《基于MPI的大数据高性能计算导论》
// Frank Nielsen 张伟哲译 2018年7月第一版第一次印刷
// https://www.lix.polytechnique.fr/~nielsen/HPC4DS/
// filename: MPIMonteCarloPi.cpp
// mpiexec MPIMonteCarloPi.exe
int main(int argc, char** argv) {
int comm_sz;
int my_rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
long long int count_time; // 总的计算次数
long long int Cal[2] = { 0 };
if (my_rank == 0) {
count_time = 10000000;
long long int tempCal[2] = { 0 };
assert(comm_sz > 1);
long long int width = count_time / (comm_sz - 1);
long long int reminder = count_time % (comm_sz - 1);
long long int temp_count_time;
for (int i = 1; i < comm_sz; ++i)
{
temp_count_time = width;
if (i <= reminder) temp_count_time++;
MPI_Send(&temp_count_time, 1, MPI_LONG_LONG_INT, i, 0, MPI_COMM_WORLD);
}
for (int i = 1; i < comm_sz; ++i)
{
MPI_Recv(tempCal, 2, MPI_LONG_LONG_INT, i, 0, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
for (int j = 0; j < 2; ++j) { Cal[j] += tempCal[j]; }
}
std::cout << 4.0 * Cal[0] / count_time << std::endl;
} // 运算的子节点
else {
srand((unsigned)time(NULL));
MPI_Recv(&count_time, 1, MPI_LONG_LONG_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUSES_IGNORE);
Cal[1] = count_time;
Cal[0] = 0;
double x, y, dst;
for (int i = 0; i < count_time; ++i) {
x = random();
y = random();
dst = x * x + y * y;
if (dst <= 1) Cal[0] += 1;
}
MPI_Send(Cal, 2, MPI_LONG_LONG_INT, 0, 0, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
在多台电脑上运行
-
关闭防火墙
-
-
-
-
多台电脑上都要安装MS MPI
-
多台电脑上都要添加相同名称相同密码且为管理员权限的账号
-
-
-
-
-
-
填写用户信息
-
-
-
-
每台电脑上首先用管理员权限运行 smpd
-
同时按 键盘上 windows图标键以及x键
-
-
-
输入
smpd -p 8677
启动smpd服务 -
每台电脑上将程序拷贝到相同的目录上(也可以不拷贝,将磁盘设置成共享也可)
-
-
将程序拷贝到所有主机的 c盘 的文件夹下
1
mpiexec -hosts 2 10.12.220.168 10.12.220.206 c:\1\MPITest.exe
-
-
如果运行圆周率计算程序 会输出计算结果,其中 -hosts 后跟主机的个数 之后就是主机的名字或者IP地址 默认使用每个主机上的一个核,如果要用多核可以在ip后写上要用的cpu核数目,更多参数解析可以 输入
mpiexec -help2
查看帮助 -
不拷贝程序到所有主机,只在一个主机上放程序,如:设置10.12.220.168主机上的c盘为共享磁盘
1
mpiexec -hosts 2 10.12.220.168 10.12.220.206 \\10.12.220.168\c\MPITest.exe