高性能计算.pptx_第1页
高性能计算.pptx_第2页
高性能计算.pptx_第3页
高性能计算.pptx_第4页
高性能计算.pptx_第5页
已阅读5页,还剩56页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

MPI并行程序设计 解决方案中心何沧平 MPI简介基本功能6个基本函数实例 Helloworld 集合通信数据广播 收集 散发 转置 归约综合实例 并行计算 值MPI软件包的安装 使用OpenMPI Mpich2 mvapich2 MPI简介 多线程库标准Win32API POSIXthreads 编译制导标准OpenMP 可移植共享存储并行编程标准 消息传递库标准MPIPVM 并行虚拟机 并行编程标准 消息传递并行程序设计指用户必须通过显式地发送和接收消息来实现处理机间的数据交换 在这种并行编程中 每个并行进程均有自己独立的地址空间 相互之间访问不能直接进行 必须通过显式的消息传递来实现 这种编程方式是大规模并行处理机 MPP 和机群 Cluster 采用的主要编程方式 并行计算粒度大 特别适合于大规模可扩展并行算法由于消息传递程序设计要求用户很好地分解问题 组织不同进程间的数据交换 并行计算粒度大 特别适合于大规模可扩展并行算法 消息传递是当前并行计算领域的一个非常重要的并行程序设计方式 消息传递程序特点 MessagePassingInterface 是消息传递函数库的标准规范 由MPI论坛开发 支持Fortran和C一种新的库描述 不是一种语言 共有上百个函数调用接口 在Fortran和C语言中可以直接对这些函数进行调用MPI是一种标准或规范的代表 而不是特指某一个对它的具体实现MPI是一种消息传递编程模型 并成为这种编程模型的代表和事实上的标准 什么是MPI MPI1 1 1995MPICH 是MPI最流行的非专利实现 由Argonne国家实验室和密西西比州立大学联合开发 具有更好的可移植性 MPI1 2 2 0 动态进程 并行I O 远程存储访问 支持F90和C 1997 MPI2 2 2009年 当前最新版本MPI3 02012年8月2号刚刚讨论过标准草案 MPI的发展历程 常用的MPI实现 MPI基本功能 C必须包含mpi h MPI函数返回出错代码或MPI SUCCESS成功标志 MPI 前缀 且只有MPI以及MPI 标志后的第一个字母大写 其余小写 Fortran必须包含mpif h fortran77 usempi fortran95 通过子函数形式调用MPI 函数最后一个参数为返回值MPI 前缀 且函数名全部为大写 C和Fortran中MPI函数约定 MPI程序中 一个独立参与通信的个体称为一个进程 process 一个MPI进程通常对应于一个普通的进程或线程在共享内存 消息传递混合模式程序中 一个MPI进程可能代表一组UNIX线程部分或全部进程构成的一个有序集合称为一个进程组 进程组中的每个进程都被赋于一个唯一的序号 rank 称为进程号 从0开始编号一个进程组及其相关属性 进程拓扑连接关系等 称为一个通信器 communicator 两个内置通信器 MPI COMM WORLD包含所有进程 MPI COMM SELF只包含进程自身 进程 通信器 MPI调用接口的总数虽然庞大 但根据实际编写MPI的经验 使用其中的6个最基本的函数就能编写一个完整的MPI程序去求解很多问题 这6个基本函数 包括启动和结束MPI环境 识别进程以及发送和接收消息 MPI Init MPI Comm size MPI Comm rank MPI Send MPI Recv MPI Finalize 最基本的MPI函数 MPI Init 并行代码 MPI Fainalize 只能有串行代码 最先被调用的MPI函数 完成MPI程序的所有初始化工作 启动MPI环境 标志并行代码的开始 参数argc和argv遇main函数传来 从而main必须带参数运行参数用来初始化并行环境 进程数 计算结点名 通信协议等 启动 结束MPI环境 最后调后的MPI函数 清除MPI并行环境 转为串行此后 MPI语句的运行结果是不可预知的之后串行代码仍可在主进程 rank 0 上运行 如果必须 实例 HelloWorld HelloWorld c include include mpi h intmain intargc char argv intnumprocs 进程数 该变量为各处理器中的同名变量 intmyid 本进程ID 存储也是分布的 MPI Statusstatus 消息接收状态变量 存储也是分布的 charmessage 100 消息buffer 存储也是分布的 intsourceMPI Init if myid 0 建立消息 sprintf message HelloWorld fromprocess d d myid numprocs 发送长度取strlen message 1 使 0也一同发送出去 MPI Send message 100 MPI CHAR 0 99 MPI COMM WORLD else myid 0 for source 1 source numprocs source MPI Recv message 100 MPI CHAR source 99 MPI COMM WORLD Endmain MPI编译工具安装 OpenMPI 最新稳定版1 6 http www open mpi org software ompi v1 6 tarzxvfopenmpi 1 6 tar gz cdopenmpi 1 6 configure prefix public software mpi ompi 1 6 gnuCC gccCXX g FC gfortranF77 gfortran make j8 makeinstallintel编译器版 configureCC iccCXX iccF77 ifortFC ifort 创建环境变量文件 public software profile d ompi 1 6 gnu sh bin bashMPI HOME public software mpi openmpi 1 6 gnuexportPATH MPI HOME bin PATHexportLD LIBRARY PATH MPI HOME lib LD LIBRARY PATHexportMANPATH MPI HOME share man MANPATHunsetMPI HOME source public software profile d ompi 1 6 gnu sh mpicc ohellohello c hello 0 Abortingprogram Couldnotcreatep4procgroup Possiblemissingfileorprogramstartedwithoutmpirun mpirun np4hello HelloWorld fromprocess1 4 HelloWorld fromprocess2 4 HelloWorld fromprocess3 4 编译 运行 计算机打印字符 我们输入的命令 SPMD SingleProgramMultipleData单程序多数据 Hello是如何被执行的 include mpi h includemain intargc char argv MPI Init include mpi h includemain intargc char argv MPI Init include mpi h includemain intargc char argv MPI Init include mpi h includemain intargc char argv MPI Init hellohellohellohello include mpi h includemain intargc char argv MPI Init rsh ssh MPI原生数据类型 MPI集合通信 Broadcast 数据广播 数据 MPI Bcast 数据 MPI Bcast 进程0myrank 0 进程1myrank 1 进程p 1myrank p 1 intMPI Bcast void buffer 发送 接收buf intcount 元素个数 MPI Datatypedatatype introot 指定根进程 MPI Commcomm 根进程既是发送缓冲区也是接收缓冲区 数据 MPI Bcast 进程2myrank 2 include include mpi h usingnamespacestd intmain intargc char argv intarray 2 intmyrank MPI Init bcast示例 mpirun np4 bcast exebrank0 00arank0 00brank3 33brank1 11arank1 00brank2 22arank2 00arank3 00 数据收集 gather intMPI Gather void sendbuf intsendcount MPI Datatypesendtype void recvbuf intrecvcount MPI Datatyperecvtype introot MPI Commcomm include include mpi h usingnamespacestd intmain intargc char argv MPI Init Gather示例 introot 0 int rbuf if myrank root rbuf newint gsize 2 MPI Gather sendarray 2 MPI INT rbuf 2 MPI INT root MPI COMM WORLD if myrank root cout sendarray endl for inti 0 i gsize i cout rbuf i 2 rbuf i 2 1 cout endl delete rbuf MPI Finalize return0 mpirun np4 gather exerank0 00rank2 22rank1 11rank3 33sendarray 00112233 Gather算例输出 intMPI Gatherv void sendbuf intsendcount MPI Datatypesendtype void recvbuf int recvcounts int displs MPI Datatyperecvtype introot MPI Commcomm 更强数据收集 gatherv 更强数据收集 gatherv MPI SCATTER sendbuf sendcount sendtype recvbuf recvcount recvtype root comm sendbuf待发送数据首地址sendcounts待发送数据数量sendtype待发送数据的类型recvbuf接收到数据的存放位置recvcount接收的数据数量recvtype接收数据的类型root根进程号comm通信器名字 数据散发 scatter intMPI Scatter void sendbuf intsendcount MPI Datatypesendtype void recvbuf intrecvcount MPI Datatyperecvtype introot MPI Commcomm scatterC接口 include include mpi h usingnamespacestd intmain intargc char argv intmyrank gsize sendbuf MPI Init MPI Scatter sendbuf 2 MPI INT rbuf 2 MPI INT root MPI COMM WORLD cout Arank myrank rbuf 0 rbuf 1 endl if myrank root delete sendbuf MPI Finalize return0 mpirun np4 scatter exerank0 00Arank0 100100rank1 11Arank1 100100rank2 22Arank2 100100rank3 33Arank3 100100 MPI SCATTERV sendbuf sendcounts displs sendtype recvbuf recvcount recvtype root comm 更强散发 scatterv 通信域中所有进程从其他进程收集数据 同时将自己的数据散发给其他进程 intMPI Alltoall void sendbuf intsendcount MPI Datatypesendtype void recvbuf intrecvcnt MPI Datatyperecvtype MPI Commcomm Alltoall 数据转置 对组中所有进程的发送缓冲区中的数据用OP参数指定的操作进行运算 并将结果送回到根进程的接收缓冲区中 intMPI Reduce void sendbuf void recvbuf intcount MPI Datatypedatatype MPI Opop introot MPI Commcomm 全局归约reduce data MPI Scatter data MPI Scatter data MPI Scatter Process0myrank 0 Process1myrank 1 Processp 1myrank p 1 buf 支持的归约操作运算 两个长度为N的向量a i b i 作内积 S a 0 b 0 a 1 b 1 a N 1 b N 1 向量a 和b 分布在多个进程上 示例 向量内积 include include include include mpi h usingnamespacestd intmain intargc char argv intmyrank gsize sendbuf MPI Init for inti 0 i 10 i a i random 10 b i random 4 sum part a i b i cout sum part sum part endl introot 0 MPI Reduce mpirun np4 reduce exesum part 50sum part 49sum part 76sum part 93sum 268 实例 求 includeusingnamespacestd doublef doublea return 4 0 1 0 a a intmain void intn 100 doubleh x pi sum 0 0 h 1 0 double n for inti 1 i n i x h double i 0 5 sum f x pi h sum cout pi pi endl return0 串行代码 include include mpi h usingnamespacestd doublef doublea return 4 0 1 0 a a intmain intargc char argv intn myrank nprocs doublePI25DT 3 141592653589793238 doublemypi pi h sum x doublestartwtime 0 0 endwtime MPI Init 并行代码 MPI Bcast MPI软件包的安装 使用 MPI编译工具安装 OpenMPI 最新稳定版1 6 http www open mpi org software ompi v1 6 tarzxvfopenmpi 1 6 tar gz cdopenmpi 1 6 configure prefix public software mpi ompi 1 6 gnuCC gccCXX g FC gfortranF77 gfortran make j8 makeinstallintel编译器版 configureCC iccCXX iccF77 ifortFC ifort 创建环境变量文件 public software profile d ompi 1 6 gnu sh bin bashMPI HOME public software mpi openmpi 1 6 gnuexportPATH MPI HOME bin PATHexportLD LIBRARY PATH MPI HOME lib LD LIBRARY PATHexportMANPATH MPI HOME share man MANPATHunsetMPI HOME OpenMPI编译命令 mpirun用法 mpirun np4 hostfilehostfileprogram npX 运行X个进程 hostfile 指定计算节点 hostfile形式一 每行一个结点node1node1node2node2 hostfile形式二 指定重复次数node1slots 2node2slots 2 选择通信网络 节点内使用共享内存 节点间使用InfiniBand通信 mpirun np12 hostfilehosts mcabtlself sm openib program mvapich2简介 包装mpich2 兼容性好添加支持InfiniBand最新版本1 8 支持MPI2 2标准支持GPU通信 结点内 跨结点包含多个模块 Mvapich2安装 同时编译IB版和TCP IP版 tarzxvfmvapich2 1 7rc1 tgz cdmvapich2 1 7rc1 configure prefix public software mpi mvapich2 17rc1 gnuCC gccCXX g FC gfortranF77 gfortran with device ch3 nemesis ib tcp make makeinstall 编译IB版 显式选择 configure with device ch3 mrail with rdma gen2编译IB版 默认选择 configure 编译IB GPU版 configure enable cuda 编译TCP IP版 configure with device ch3 sock Mvapich2安装 2 编译IB GPU PGI编译器 configureCC pgccCXX pgCCFC pgf90F77 pgf77 enable cudaCPPFLAGS D x86 64 D align n attribute aligned n D location a annotate a DCUDARTAPI Mvapich2编译命令 mpirun rsh命令 在n0 n1两个结点上运行4个进程 默认SSH通信 mpirun rsh np4n0n0n1n1 cpi 在n0 n1两个结点上运行4个进程 改用RSH通信 mpirun rsh rsh np4n0n0n1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论