CUDA超大规模并行程序设计.ppt
《CUDA超大规模并行程序设计.ppt》由会员分享,可在线阅读,更多相关《CUDA超大规模并行程序设计.ppt(93页珍藏版)》请在三一办公上搜索。
1、CUDA 超大规模并行程序设计,邓仰东清华大学微电子学研究所,提纲,从GPGPU到CUDACUDA并行程序组织并行执行模型CUDA基础CUDA存储器CUDA程序设计工具程序优化,Graphic Processing Unit(GPU),用于个人计算机、工作站和游戏机的专用图像显示设备显示卡nVidia和ATI(now AMD)是主要制造商Intel准备通过Larrabee进入这一市场主板集成Intel,3维图像流水线,Framebuffer,Texture,CPU,GPU,实时3维高速图形处理,一帧典型图像1M triangles3M vertices25M fragments30 frame
2、s/s30M triangles/s90M vertices/s750M fragments/s,传统GPU架构,Graphics program,Vertex processors,Fragment processors,Pixel operations,Output image,GPU的强大运算能力,数据级并行:计算一致性,专用存储器通道有效隐藏存储器延时,General Purpose Computing on GPU(GPGPU),GPGPU,核心思想用图形语言描述通用计算问题把数据映射到vertex或者fragment处理器但是硬件资源使用不充分存储器访问方式严重受限难以调试和查错高
3、度图形处理和编程技巧,NVidia G200 Architecture,CUDA:Compute Unified Device Architecture,通用并行计算模型单指令、多数据执行模式(SIMD)所有线程执行同一段代码(1000s threads on the fly)大量并行计算资源处理不同数据隐藏存储器延时提升计算通信比例合并相邻地址的内存访问 快速线程切换1 cycleGPU vs.1000 cyclesCPU,混合计算模型,CUDA:集成CPU+GPU C应用程序CPU:顺序执行代码GPU=超大规模数据并行协处理器“批发”式执行大量细粒度线程,kernel 0,CPU Seri
4、al Code,CPU Serial Code,GPU Parallel Code,GPU Parallel Code,Concurrent execution!,kernel 1,CUDA成功案例,CUDA性能,BLAS3:127 GFLOPS/基本线性代数:matrix-matrixFFT:52 benchFFT*GFLOPSFDTD:1.2 Gcells/sec/计算电动力学SSEARCH:5.2 Gcells/sec/Smith-Waterman基因序列比较Black Scholes:4.7GOptions/sec/期权定价模型VMD:290 GFLOPS/分子动力学图形显示,Prob
5、lem Instances for Sparse Matrix Vector Product(SMVP),SPMV Throughput on GTX280,SMVP Application:Static Timing Analysis,Adapted from Ramalingam,A.et.al.An Accurate Sparse Matrix Based Framework for Statistical Static Timing Analysis.ICCAD.2006.,Static Timing Analysis Results on GTX280,提纲,从GPGPU到CUDAC
6、UDA并行程序组织并行执行模型CUDA基础CUDA存储器CUDA程序设计工具程序优化,并行性的维度,1维y=a+b/y,a,b vectors2维P=M N/P,M,N matrices3维CT or MRI imaging,=,并行线程组织结构,Thread:并行的基本单位Thread block:互相合作的线程组Cooperative Thread Array(CTA)允许彼此同步通过快速共享内存交换数据以1维、2维或3维组织最多包含512个线程Grid:一组thread block以1维或2维组织共享全局内存Kernel:在GPU上执行的核心程序One kernel one grid,P
7、arallel Program Organization in CUDA,Thread,Thread block,Grid,SP,Software,Hardware,SM,GPU,并行线程执行,调用kernel function 需要指定执行配置Threads和blocks具有IDsthreadIdx:1D,2D,or 3DblockIdx:1D,or 2D由此决定相应处理数据,_global_ void kernel(.);dim3 DimGrid(3,2);/6 thread blocks dim3 DimBlock(16,16);/256 threads per block kernel
8、(.);,实例1:Element-Wise Addition,/CPU program/sum of two vectors a and bvoid add_cpu(float*a,float*b,int N)for(int idx=0;idxN;idx+)aidx+=bidx;void main().fun_add(a,b,N);,/CUDA program/sum of two vectors a and b _global_ void add_gpu(float*a,float*b,int N)Int idx=blockIdx.x*blockDim.x+threadIdx.x;if(id
9、x(a,b,N);,提纲,从GPGPU到CUDACUDA并行程序组织并行执行模型CUDA基础CUDA存储器CUDA程序设计工具程序优化,CUDA Processing Flow,并行线程执行,SM内以(warp即32 threads)为单位并行执行Warp内线程执行同一条指令Half-warp是存储操作的基本单位,Warp,GPU负载分配,Global block scheduler管理thread block级并行从CPU获得线程组织信息根据硬件结构分配thread block到SM,Streaming Multiprocessor(SM),Streaming Multiprocessor执
10、行Thread Blocks,线程以block为单位分配到SM视资源需求,一个SM分配至多8个blockSM in G80可以接受768个线程256(threads/block)*3 blocks 或128(threads/block)*6 blocks,etc.线程并发(concurrently)运行SM分配并维护线程IDSM管理并调度线程,Thread Life Cycle,Grid在GPU上启动Thread blocks顺序分配到SMs一般SM应有1 thread blockSM把线程组织为warpsSM调度并执行就绪的warpWarps和thread blocks 执行结束后释放资源G
11、PU继续分发thread blocks,Example of Hiding Memory Latency,G80:执行warp全部线程的一条指令需要8个时钟cycle假定1 global memory access/8 instructionsA 400-cycle global memory latencyHow many warps are needed to tolerate the latency?,400 cycles*1 MEM/8 cycles=50 cycles per instruction on average50 cycles/4 cycles per warp=12.5
12、 13 warps to keep an SM busy,Arithmetic Instruction Throughput,4 clock cyclesSingle-precision floating-point add,multiply,and multiply-add,Integer add,24-bit integer multiplicationBitwise operations,compare,min,max,type conversion instruction;,Note.1.A warp is issued in 4 cycles2.Arithmetic ops are
13、pipelined.3.Still possible to have 8 ops ready in each cycle.,Arithmetic Instruction Throughput,16 clock cyclesReciprocal,reciprocal square root,32-bit Integer multiplicationOther functions are combinations of the abovey/x=rcp(x)*y/20 cycles per warpsqrt(x)=rcp(rsqrt(x)/32 cycles per warpInteger div
14、ision and modulo operation are costly!,浮点数精度,GT200之前的GPUIEEE-754 Floating Point Standard单精度浮点数GT200增加双精度浮点数支持SP仍然只支持单精度浮点数每个SM配置一个双精度浮点单元双精度比单精度运算慢8-12倍,控制流(Control Flow),同一warp内的分支语句可能执行不同的指令路径不同指令路径的线程只能顺序执行每次执行warp中一条可能的路径N条指令路径1/N throughput只需要考虑同一warp即可,不同warp的不同的指令路径不具相关性 G80上使用指令预测技术加速指令执行,控制
15、流(Control Flow),常见情况:分支条件是thread ID的函数时,容易导致divergenceExample with divergence:If(threadIdx.x 2)在thread block产生两条不同指令路径Branch granularity 2)也在thread block产生两条不同指令路径Branch granularity is a whole multiple of warp size同一warp的所有线程具备相同指令路径,线程同步,void _syncthreads();Barrier synchronization同步thread block之内的所
16、有线程避免访问共享内存时发生RAW/WAR/WAW 冒险(hazard),_shared_ float scratch256;scratchthreadID=beginthreadID;_syncthreads();int left=scratchthreadID-1;,在此等待,直至所有线程到达才开始执行下面的代码,Dead-Lock with _syncthreads,Dead-lock ifSome threads have val larger than thresholdAnd others not,_global_ void compute(.)/do some computati
17、on for valif(val threshold)return;_syncthreads();/work with val,提纲,从GPGPU到CUDACUDA并行程序组织并行执行模型CUDA基础CUDA存储器CUDA程序设计工具程序优化,CUDA扩展语言结构,Declspecsglobal,device,shared,local,constantKeywordsthreadIdx,blockIdxthreadDim,blockDimIntrinsics_syncthreadsRuntime APIMemory,symbol,execution managementFunction lau
18、nch,_device_ float filterN;_global_ void convolve(float*image)_shared_ float regionM;.regionthreadIdx=imagei;_syncthreads().imagej=result;/Allocate GPU memoryvoid*myimage=cudaMalloc(bytes)/100 blocks,10 threads per blockfoo(parameters);,存储器空间,R/W per-thread registers1-cycle latencyR/W per-thread loc
19、al memorySlow register spilling to global memoryR/W per-block shared memory1-cycle latencyBut bank conflicts may drag downR/W per-grid global memory500-cycle latencyBut coalescing accessing could hide latencyRead only per-grid constant and texture memories500-cycle latencyBut cached,GPU Global Memor
20、y分配,cudaMalloc()分配显存中的global memory两个参数对象数组指针和数组尺寸cudaFree()释放显存中的global memory对象数组指针,int blk_sz=64;float*Md;int size=blk_sz*blk_sz*sizeof(float);cudaMalloc(void*),Host Device数据交换,cudaMemcpy()Memory data transferRequires four parametersPointer to destinationPointer to sourceNumber of bytes copiedTyp
21、e of transfer Host to Host,Host to Device,Device to Host,Device to Device,cudaMemcpy(Md,M.elements,size,cudaMemcpyHostToDevice);cudaMemcpy(M.elements,Md,size,cudaMemcpyDeviceToHost);,CUDA引入的新变量类型,_device_ 储存于GPU上的global memory空间 和应用程序具有相同的生命期(lifetime)可被grid中所有线程存取,CPU代码通过runtime函数存取 _constant_储存于GP
22、U上的constant memory空间 和应用程序具有相同的生命期(lifetime)可被grid中所有线程存取,CPU代码通过runtime函数存取_shared_储存于GPU上thread block内的共享存储器和thread block具有相同的生命期(lifetime)只能被thread block内的线程存取Local变量储存于SM内的寄存器和local memory和thread具有相同的生命期(lifetime)Thread私有,CUDA函数定义,_global_ 定义kernel函数必须返回void_device_ 函数不能用&运算符取地址,不支持递归调用,不支持静态变量(
23、static variable),不支持可变长度参数函数调用,CUDA数学函数,pow,sqrt,cbrt,hypot,exp,exp2,expm1,log,log2,log10,log1p,sin,cos,tan,asin,acos,atan,atan2,sinh,cosh,tanh,asinh,acosh,atanh,ceil,floor,trunc,round,etc.只支持标量运算许多函数有一个快速、较不精确的对应版本以”_”为前缀,如_sin()编译开关-use_fast_math强制生成该版本的目标码每个多处理器包含两个超越函数计算单元,实例2:矩阵相乘,矩阵数据类型 不属于CUD
24、A!单精度浮点数width height个元素矩阵元素在elements中1-D数组存放矩阵数据Row-major storagetypedef struct int width;int height;float*elements;Matrix;,A,B,C,WM.width=N.heightI,M.height,M.width,N.width,实例2:矩阵相乘,C=A B of size WIDTH x WIDTH一个线程处理一个矩阵元素简化:假定 WIDTH x WIDTH 512只需要一个thread block线程载入A的一行和B的一列A和B的一对相应元素作一次乘法和一次加法,A,B,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CUDA 超大规模 并行 程序设计

链接地址:https://www.31ppt.com/p-5426052.html