1. 使用shared memory
如果在kernel函数中,需要对global memory同一个数据多次访问,最好使用shared memory,先将数据从globalmemory拷贝到shared memory中,
然后使用shared memory中数据进行读取。
如果仅读取一次,则使用shared memory效果不大。
2. 使用限定符__restrict__提高性能
对于read-only的指针,可以使用const * __restrict__进行限定,提高性能,如下面的kernel函数。
使用__restrict__告诉nvcc此指针不会重叠,放心使用。并且此限定符仅对指针有效,注意不能是__restrict__ * ptr。
__global__ void myKernel( const short * __restrict__ src , short* __restrict__ dst ){
dst[ threadIdx.x ] = src[ threadIdx.x ] + 42;
}