Pipelined Computations 流水线计算
description
Transcript of Pipelined Computations 流水线计算
Pipelined Computations流水线计算
Lecture 6, Chapter 5
Pipelined Computations
问题被分成一系列必须是一个接一个完成的任务
( 顺序程序设计的基础 ). 每个任务由独立的进程或处理器所执行 .
例一:求和将数组 a 元素累加求和 :
for (i = 0; i < n; i++)sum = sum + a[i];
此 for 循环语句可拆成下面的语句
sum = sum + a[0];sum = sum + a[1];sum = sum + a[2];sum = sum + a[3];sum = sum + a[4];
.
.
.
用流水线法
例二频率过滤器 - 目标是从数字化信号 f(t) 中去掉特定的频率 (f0, f1, f2,f3, etc.) 信号从流水线的左方进入,每一级负责去掉一个频率 :
流水线计算适用范围
假设问题可分解成一系列的顺序任务,下面三种计算类型可以用流水线方法来加速 :
类型 1. 如果需要对该问题运行多个实例的。
类型 2. 如果必须处理一系列数据的,而每个数据需要多次操作的。
类型 3. 如果进程在完成自己的所有内部操作之前能够把下一个进程启动所需要的信息向前传送的。
“ 类型 1” 流水线 时空 图表
每个实例 (instance) 需要依次经历 6 个顺序进程
类型 1 的另一种时空图标
个人感觉:此问题还不如对每个实例按照工作池 (work pool) 的动态方法好
“ 类型 2” 流水线时空图表
个人感觉还是工作池的方法要好
“ 类型 3” 流水线 时空图表
在前一个进程结束之前就将信息从当前进程传到下一级。
If the number of stages is larger than the number of processors in any pipeline, a group of stages can be assigned to each processor:如果阶级数大于进程数,就将级分组成批给每个进程
Computing Platform for Pipelined Applications
Multiprocessor system with a line configuration
线性结构的多处理器系统:方便,廉价,相邻的处理器支持同时消息传递。
以太网
流水线程序举例(Examples of each type of computation)
Pipeline Program Examples
多组数 进行组内数的相加
类型 1 的流水线计算
ai aiai ai ai
中间的每个进程 Pi :
recv(&accumulation, Pi-1);accumulation = accumulation + number;send(&accumulation, Pi+1);
第一个进程 , P0,
send(&number, P1);
最后一个进程 , Pn-1,
recv(&number, Pn-2);accumulation = accumulation + number;
SPMD program
if (process > 0) {recv(&accumulation, process-1);accumulation = accumulation + number;
}if (process < n-1)
send(&accumulation, process+1);
在最后进程中得到结果。
除了加法运算,其他算术运算都可如此编程。
各进程的程序可以合写成:
数多了,应该采用数据划分来减少通信开销。
Pipelined addition numbers
Master process and ring configuration
Sorting NumbersA parallel version of insertion sort. ( 插入排序 )
Pipeline for sorting using insertion sort
类型 2 的流水线计算
进程 Pi 的基本算法
recv(&number, Pi-1);if (number > x) {
send(&x, Pi+1);x = number;
} else send(&number, Pi+1);
对于 n 个数 , 第 i 个进程要接收 n - i 个数。对于进程 i ,除了保留一个数外,其他 n - i – 1 个数要继续传下去。可以使用一个简单的循环语句:
right_procNum=n-i-1;recv(&number, Pi-1);for(j=0;j<right_procNum;j++){ if (number > x) { send(&x, Pi+1);
x = number; } else send(&number, Pi+1);}
Insertion sort with results returned to master process using bidirectional line configuration
Send(&x, Pi-1); /* 把进程 i 的最终数往前一进程传 */
For(j=0;j<right_procNum;j++){ /* 把进程 i 以后各进程排好的数往前传 */
recv(&number,Pi+1);
send(&number,Pi-1);
}
Insertion sort with results returned
Prime Number Generation 质数生成Eratosthenes 筛选
•所有的一系列整数从 数 2 开始产生 .• 第一个数 , 2, 是质数保留 .• 所有当前数( 2 )的倍数都被删除 .• 对下一个留下的数( 3 )重复上面的步骤 .• 算法移去所有的非质数,保留质数。
类型 2 流水线计算
注意:为了找到 n 为止的质数,只需考察从头到 的数。n
The code for a process, Pi, could be based upon
recv(&x, Pi-1);/* repeat following for each number */recv(&number, Pi-1);if ((number % x) != 0) send(&number, P i+1);
每个进程将接收不同数目的数且事先也不知道数的多少,肯定 不 会 超 过 n 个 , 用 for 语 句 加 上 在 数 的 序 列 末 尾加“ terminator” 信息就可令其终止。 每个进程的程序:
recv(&x, Pi-1);for (j = 0; j < n; j++) {
recv(&number, Pi-1);If (number == terminator) break; (number % x) != 0) send(&number, P i+1);
}
Solving a System of Linear Equations上三角形方程组求解
where a’s and b’s are constants and x’s are unknowns to be found.
Back SubstitutionFirst, unknown x0 is found from last equation; i.e.,
Value obtained for x0 substituted into next equation to obtain x1; i.e.,
Values obtained for x1 and x0 substituted into next equation to obtain x2:
and so on until all the unknowns are found.
Pipeline Solution
First pipeline stage computes x0 and passes x0 onto the second stage, which computes x1 from x0 and passes both x0 and x1 onto the next stage, which computes x2 from x0 and x1, and so on.
Type 3 pipeline computation
The ith process (0 < i < n) receives the values x0, x1, x2, …, xi-1 and computes xi from the equation:
Sequential CodeGiven constants ai,j and bk stored in arrays a[ ][ ] and b[ ], respectively, and values for unknowns to be stored in array, x[ ], sequential code could be
x[0] = b[0]/a[0][0]; /* computed separately */for (i = 1; i < n; i++) { /*for remaining unknowns*/
sum = 0;For (j = 0; j < i; j++)
sum = sum + a[i][j]*x[j];x[i] = (b[i] - sum)/a[i][i];
}
Parallel Code
Pseudocode of process Pi (1 < i < n) of could be
for (j = 0; j < i; j++) {recv(&x[j], Pi-1);send(&x[j], Pi+1);
}sum = 0;for (j = 0; j < i; j++)
sum = sum + a[i][j]*x[j];x[i] = (b[i] - sum)/a[i][i];send(&x[i], Pi+1);
Now have additional computations to do after receiving and resending values.
Pipeline processing using back substitution