MPI培训教材课程
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
a.out
p1 p2 p3 SPMD
p1 p2 p3 MPMD: Mster/slave
p1 p2 p3 MPMD: Couple Analysis
a.out
A process • Read array a() from
the input file • Set is=1 and ie=6 • Process from
Distributed memory
NUMA(Non-Uniform MPP(Massively Parallel
Memory Access)
Processors)
Coherent Shared Memory CPU 1 CPU 2
Memory
Easy to Program Hard to Scale Hardware
the input file
the input file
the input file
• Get my rank
• Get my rank
• Get my rank
• If(rank.eq.0)is=1,ie= • If(rank.eq.0)is=1,ie= • If(rank.eq.0)is=1,ie=
the input file
the input file
• Get my rank
• Get my rank
• Get my rank
• is=2*rank+1,ie=2*ra • is=2*rank+1,ie=2*ra • is=2*rank+1,ie=2*ra
nk+2
nk+2
nk+2
• Process from a(is)to • Process from a(is)to • Process from a(is)to
标准MPI包括:
1. 环境管理函数 2. 点对点通信 3. 集合操作 4. 派生数据类型 5. 通信因子 6. 进程组 7. 进程拓扑
sgi
Type
Subroutines
Environment Management MPI_INIT,
MPI_FINALIZE, MPI_ABORT,...
Point-to-Point
Memory
I/O
对称多处理器结构
sgi
Processor
Interconnection Network
Processor
Processor
Processor
Cache
Cache
Cache
Cache
Bus
Bus
Bus
Bus
Memory I/O Memory I/O Memory I/O Memory I/O
Communicator Process Group
MPI_COMM_SIZE, MPI_COMM_RANK,...
MPI_GROUP_SIZE, MPI_GROUP_RANK,...
Topology
MPI_CART_CREATE, MPI_GRAPH_CREATE,...
sgi
Number 18 35 30 21 17 13
2
2
2
• If(rank.eq.1)is=3,ie= • If(rank.eq.1)is=3,ie= • If(rank.eq.1)is=3,ie=
4
4
4
• If(rank.eq.2)is=5,ie= • If(rank.eq.2)is=5,ie= • If(rank.eq.2)is=5,ie=
6
6 Process from
sgi
并行加速比
加速比(p个处理器)=
时间(1个处理器) 时间(p个处理器)
sgi
并行程序设计关键:
•负载平衡 •减少通讯 •通信和计算交叠
第二章 MPI简介 sgi
在消息传递库方法的并行编程中,一组进程 所执行的程序是用标准串行语言书写的代码加上 用于消息接收和发送的库函数调用。其中, MPI(Message Passing Interface )是1994年5月 发布的一种消息传递接口。它实际上是一个消息 传递函数库的标准说明,吸取了众多消息传递系 统的优点,是目前国际上最流行的并行编程环境 之一。
大规模并行处理器结构
sgi
MIPS
MIPS
MIPS
MIPS
MIPS
MIPS
MIPS
MIPS
XIO+ Bedrock
ASIC
NUMA 3
XIO+ Bedrock
ASIC
8 GB Physical Memory
8 GB Physical Memory
sgi
分布存储系统的主要特点
sgi
Ø系统通过互连网络将多个处理机连接起来,每个处理机 均有自己的局部存储器,所有的局部存储器就构成了整 个地址空间;
sgi
Distributed Memory
CPU 1
CPU 2
Memory
Memory
Hard to Program Easy to Scale Hardware
sgi
Processor Processor Processor Processor
Cache Cache
Cache
Cache
Bus or Crosssbar Switch
MPI_SEND,
MPI_RECV, MPI_WAIT,... Collective Communication MPI_BCAST, MPI_GATHER,
Derived Data Type
MPI_REDUCE,... MPI_TYPE_CONTIGUOUS,
MPI_TYPE_COMMIT,...
file
sgi
1.4 并行语言
HPF 、OpenMP
具体由编译器负责数据分发、计算和 通信
MPI
1.5并行程序设计关键
并行加速比
加速比(p个处理器)=
性能(1个时间成倒数关系的则:
加速比(p个处理器)=
时间(1个处理器) 时间(p个处理器)
息传递库程序来实现的。
1.3 并行编程模型
S1 P1 P2 P3 P4 S2
sgi
sgi
S1
S1
thread
P1
P2
P1
P2
P3
P4
P3
S2 P4
S2
process
共享地址空间
sgi
S1
S1
S1
S1
S1
P1
P1
P2
P3
P4
P2
S2
S2
S2
S2
P3
P4
process0
process1
process2
strcpy(msg,”Hello there”); MPI_Send(msg,strlen(msg) + 1,MPI_CHAR,1,99,MPI_COMM_WORLD); }else if(myrank ==1){ MPI_Recv(msg,20,MPI_CHAR,0,99,MPI_COMM_WORLD,&status); printf(“Receive message = %s\n”,msg); } MPI_Finalize();
Ø整个地址空间有局部和全局两种编址方式,全局编址方 式是指系统的所有局部存储器统一编址,用户程序空间 是一个统一的全局的地址空间,其中远程存储器的访问 与局部存储器的访问一样用指令来完成,其指令地址由 处理器号和局部存储器地址所组成;
Ø局部编址方式是系统中各局部存储器单独编址,用户程 序空间是多地址空间,远程存储器的访问要通过调用消
Signal processing: Networking and compute
Media streaming:
Data
Access storage and networking
Database/CRM/ERP: Storage
Web serving: Small, integrated system
a(ie)
a(ie)
a(ie)
• Gather the result to • Gather the result to • Gather the result to
process 0
process 0
process 0
• If rank=0 then write • If rank=0 then write • If rank=0 then write
sgi
采用并行计算:
1. 提高求解问题的速度 2. 在相同的时间内求解更多更复杂的问题 3. 物理极限约束处理器的处理速度
sgi
sgi
1.2 并行计算机体系结构
Centralized memory
Share Address Space
SMP(Symmetric Multiprocessor)
Individual Address Space
a(is)to a(ie) • Write array a() to
the output file
a.out
process 0
process 1
process 2
• Read array a() from • Read array a() from • Read array a() from
the input file
sgi
MPI培训教程
美国sgi 电子计算机公司
曹银锋 2003.7
第一章并行编程简介
1.1 为什么要采用并行计算?
sgi
CPU I/O
sgi
Traditional supercomputer: Compute, networking, storage
Genomics: Compute cycles
2.1 简单例子
sgi
#include “mpi.h” main(int argc, char **argv) {
int myrank,i,j,k; MPI_Status status; char msg[20];
MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&myrank); if(myrank == 0){
MPI优点:
1. 具有可移植性 2. 易用性; 3. 有完备的异步通信功能; 4. 有正式和详细的精确定义。
sgi
在基于MPI编程模型中,计算是由一个或多个彼 此通过调用库函数进行消息收、发通信的进程所组成 。在绝大部分MPI实现中,一组固定的进程在程序初 始化时生成,一般情况下,一个处理器只生成一个进 程。这些进程可以执行相同或不同的程序(相应地称 为单程序多数据(SPMD)或多程序多数据(MPMD)模 式)。进程间的通信可以是点到点的,也可以是集合 的。
16
sgi
u MPI_INIT: 启动MPI环境 u MPI_FIANLIZE: 结束MPI环境 u MPI_COMM_SIZE: 确定进程数 u MPI_COMM_RANK: 确定自己的进程标识符 u MPI_SEND: 发送一条消息 u MPI_RECV: 接收一条消息
process3
S2
消息传递
sgi 数据并行和消息传递
编程级别 执行效率 存储类型 通信实现 问题类型 目前状况
数据并行
高 依赖于编译器 共享内存 编译器负责 数据并行类问题 缺乏高效的编译器支持
消息传递
低 高 没有限制 程序员负责 数据并行、任务并行 使用广泛
SPMD 和 MPMD
a.out
a.out
6 Process from
• Process from a(is)to
a(is)to a(ie)
a(is)to a(ie)
a(ie)
• Gather the result to • Gather the result to
• Gather the result to
process 0
process 0
array a() to the ouput
array a() to the ouput array a() to the oupu
file
file
file
a.out
process 0
process 1
process 2
• Read array a() from • Read array a() from • Read array a() from
MPI只是为程序员提供一个并行环境库,程序员 通过调用MPI的库程序来达到程序员所要达到的并行 目的,MPI提供C语言和Fortran语言接口。
sgi
MPI是个复杂的系统,它包含了129个函数(根据 1994年发布的MPI标准)。事实上,1997年修订的标 准,称之为MPI-2,已超过200多个,目前最常用的也 有约30个。然而我们可以只使用其中的6个最基本的 函数就能编写一个完整的MPI程序去求解很多问题。 这6个基本函数,包括启动和结束MPI环境,识别进程 以及发送和接收消息:
process 0
• If rank=0 then write • If rank=0 then write
• If rank=0 then write
array a() to the ouput array a() to the oupu
array a() to the ouput
file
file