[system-track][computing][cuda]cuda : performance issue

本文探讨了CUDA中提高性能的关键因素,包括全局内存的内存对齐以优化带宽利用,使用共享内存减少延迟并解决银行冲突,以及如何通过避免分支分歧来优化线程执行。通过理解和应用这些概念,可以显著提升CUDA内核的效率。
摘要由CSDN通过智能技术生成

memory coalescing. about global memory

https://devblogs.nvidia.com/how-access-global-memory-efficiently-cuda-c-kernels/

nvidia-gpu memory hierarchy:

coalesce global memory load/store of threads in a warp into transactions as small as possible, better utilize memory bandwidth

32-byte, 64-byte, 128-byte / transaction

  • dis-aligned memory access e.g. a[thread.x+32] -> coalesce access into L1 cache 128-byte cache lines-> good bandwidth utilization
  • stride memory access e.g. a[thread.x*32] -> bad locality, hard to coalesce -> low bandwidth utilization

shared memory. about on-chip memory

https://devblogs.nvidia.com/using-shared-memory-cuda-cc/
stride memory access, bad memory bandwidth utilization, e.g. access multi-dim array -> tile, shared mem

shared memory, much lower latency than global memory. but bank conflicts happen
shared memory allocated per thread block, shared within thread block
e.g.
__shared__ int s[64]; //static, , use shared memory explicitly
extern __shared__ int s[]; //dynamically

usage(combined with thread synchronization):

  • user-managed data caches
  • high-performance cooperative parallel algorithms, e.g. parallel reduction
  • facilitate global memory coalescing in case not be possible without shared memory

threads need synchronization for shared memory access

  • __syncthreads(), a barrier across a thread block

performance issue: bank conflict
how memory addresses map to memory banks:

  • successive 32-bit words are assigned to successive banks
  • and the bandwidth is 32 bits per bank per clock cycle.
  • For devices of compute capability 2.0, the warp size is 32 threads and the number of banks is also 32. A shared memory request for a warp is not split as with devices of compute capability 1.x, meaning that bank conflicts can occur between threads in the first half of a warp and threads in the second half of the same warp.
  • Devices of compute capability 3.x have configurable bank size, which can be set using cudaDeviceSetSharedMemConfig() to either four bytes (cudaSharedMemBankSizeFourByte, the default) or eight bytes (cudaSharedMemBankSizeEightByte). Setting the bank size to eight bytes can help avoid shared memory bank conflicts when accessing double precision data.

e.g. for usage 3, matrix transpose optimizing
https://devblogs.nvidia.com/efficient-matrix-transpose-cuda-cc/
/*
This mapping is up to the programmer; the important thing to remember is that to ensure memory coalescing we want to map the quickest varying component to contiguous elements in memory. In Fortran contiguous addresses correspond to the first index of a multidimensional array, and threadIdx.x and blockIdx.x vary quickest within blocks and grids, respectively.
*/

branch divergence / threads divergence

https://stackoverflow.com/questions/17223640/is-branch-divergence-really-so-bad
e.g. reduction

  • naive code
__global__ void reduce(int *input, unsigned int N, int *total){
	unsigned int tid = threadIdx.x;
	unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
	__shared__ int x[BSIZE];
	x[tid] = (i<N) ? input[i] : 0;
	__syncthreads();

	for (unsigned int stride = 1; stride < blockDim.x; stride *= 2) {
		__syncthreads();
		if (tid % (2*stride) == 0){
			x[tid] += x[tid + stride];
		}
	}
	if (tid == 0) atomicAdd(total,x[tid]);
}
  • improved code : reduce divergence and bank conflict
    no divergence until stride < 32 ; all warps active when stride >=32
for (stride = blockDim.x/2; stride > 1; stride /= 2) { 
	 __syncthreads();
	if (tid < stride){
		x[tid] += x[tid + stride];
	}
}

tutorial from

https://devblogs.nvidia.com/even-easier-introduction-cuda/

https://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwj_ja-uy_zgAhWZOnAKHYfnAdYQFjAAegQIAxAC&url=http%3A%2F%2Fcseweb.ucsd.edu%2Fclasses%2Ffa12%2Fcse260-b%2FLectures%2FLec09.pdf&usg=AOvVaw2QqIAMqGhA6sfoxz_DN4Du

https://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=2ahUKEwjy_7i6y_zgAhUDF4gKHXuADqQQFjABegQIBBAC&url=http%3A%2F%2Fhomepages.math.uic.edu%2F~jan%2Fmcs572%2Fmemory_coalescing.pdf&usg=AOvVaw2GXJmYi5FaJuOc9om5vC4w

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值