c++并行编程_并行计算笔记(001)

并行计算笔记(001)-安装mpi,openmp 动力
需要用高性能计算求解微分方程,这也将是我这接下来三年的学习方向之一。目前以MPI为主,将来肯定会涉及到异构计算。希望写这些笔记记录我的修炼历程。目前的学习目标及就是掌握将偏微分方程组的差分形式稠密化。 对将来的看法
主要的方向是并行计算的程序开发。希望能够做数学性更强的研究。并行计算最合适的看来还是神经网络和计算机图形学。因为如果要将计算潜力发挥出来,需要时间复杂度和空间复杂度是一个量级的。偏微分方程组的差分求解的问题在于,将偏微分方程组差分化之后,得到的是稀疏矩阵,无法完全发挥计算潜力。
所以将来可能会涉及一些计算机图形学和神经网络,主要还是应用,计算机体系结构会涉及的比较少。 为并行机编程[1]
1988年McGraw和Axelrod定义了为并行机开发应用软件的四条不同的途径。
1. 扩展现有编译器以便将串行程序转化为并行程序。
2. 扩展现有编程语言,增加新操作以允许用户表达并行性。
3. 在现有串行语言上增加一个并行语言层。
4. 定义全新的并行语言和编译系统。

现状:最流行的是在现有的串行语言上,通过增加用函数调用或编译指导语句来表示低层语句,使用MPI,OpenMP就是这方面的例子。低层并行编程可以产生具有高性能和好的移植性的并行程序。与高层并行编程语言相比,其不足之处在于编程和调试比较难。 windows下利用Mingw和msmpi编译运行mpi并行程序[2][3] 1. 安装Mingw和msmpi 1.1 mingw-w64
参考这个 https://blog.csdn.net/ZHAOJUNWEI08/article/details/86602120
下载一套mingw不用安装,直接解压到某个位置,把mingw下的bin目录加入环境变量。
输入gcc -v
得到gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project),说明安装好了。 1.2 msmpi
[msmpi](https://docs.microsoft.com/en-us/message-passing-interface/microsoft-mpi)是微软提供的mpi实现。下载好两个文件msmpisdk.msi and MSMpiSetup.exe都装好。
在cmd中输入set MSMPI,得到
MSMPI_BENCHMARKS=C:Program FilesMicrosoft MPIBenchmarks
MSMPI_BIN=C:Program FilesMicrosoft MPIBin
MSMPI_INC=C:Program Files (x86)Microsoft SDKsMPIInclude
MSMPI_LIB32=C:Program Files (x86)Microsoft SDKsMPILibx86
MSMPI_LIB64=C:Program Files (x86)Microsoft SDKsMPILibx64

64位系统得到这样的结果表示安装好了msmpi。
按照[MinGW coding under Windows (C, C++, OpenMP, MPI)](https://www.math.ucla.edu/~wotaoyin/windows_coding.html)
处理一下msmpi文件,其中$MSMPI_LIB64这些路径由set MSMPI查看。

> mkdir ~/msmpi # create a temporary folder under your home directory
> cd ~/msmpi # enter the folder
> cp "$MSMPI_LIB64/msmpi.lib" . # copy msmpi.lib to ~/msmpi/; the import library, which is a placeholder for dll
> cp "$WINDIR/system32/msmpi.dll" . # copy msmpi.dll to ~/msmpi/; the runtime library
> gendef msmpi.dll # generate msmpi.def. For 32-bit, use: gendef -a msmpi.dll, which specifies the stdcall format
> dlltool -d msmpi.def -D msmpi.dll -l libmsmpi.a # generate the (static) library file libmsmpi.a
> cp libmsmpi.a /mingw64/lib # copy this library file to where g++ looks for them;
# try "g++ --print-search-dirs"
> cp "$MSMPI_INC/mpi.h" . # copy the header file mpi.h to ~/msmpi/

在 ~/msmpi 文件夹中,打开 mpi.h文件 , 找到“typedef __int64 MPI_Aint”,在这条语句上面一行加上 “#include <stdint.h>” (without the quotes), 来定义 __int64.
> cp mpi.h /mingw64/include # copy the header file to the default include folder
c程序的编译命令和运行命令为:

>gcc -o mpi_hello_world.exe mpi_hello_world.c -l msmpi -L "C:Program Files (x86)Microsoft SDKsMPILibx64" -I "C:Program Files (x86)Microsoft SDKsMPIInclude"

运行程序命令为mpiexec -n 3 mpi_hello_world.exe,这里3是进程数
得到

Hello world from processor LAPTOP-8JDFVNA8, rank 2 out of 3 processors
Hello world from processor LAPTOP-8JDFVNA8, rank 0 out of 3 processors
Hello world from processor LAPTOP-8JDFVNA8, rank 1 out of 3 processors

1.3 openmp
编译使用openmp需要加上 -fopenmp
gcc -fopenmp omp_hello.c -o omp_hello

总结

win10环境下使用msmpi其实有点麻烦,linux使用openmpi其实非常简单。关于msmpi,下面是今天安装测试用到的所有文件。

https://github.com/chenlin0/Learning/tree/master/MPI

其实visual studio配置msmpi看起来更好用,不过因为以后主要还是使用linux,所以还是使用gcc

参考文献

[1] Parallel Programming in C with MPI and OpenMP, Michael J. Quinn, McGraw-Hill, 2003.

[2] https://blog.csdn.net/xenonhu/article/details/78196443

[3] https://www.math.ucla.edu/~wotaoyin/windows_coding.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值