我的内核使用float
大小为 8 x 8 的数组,下面带有随机访问模式。
// inds - big array of indices in range 0,...,7
// flts - 8 by 8 array of floats
// kernel essentially processes large 2D array by looping through slow coordinate
// and having block/thread parallelization of fast coordinate.
__global__ void kernel (int const* inds, float const* flt, ...)
{
int idx = threadIdx.x + blockDim.x * blockIdx.x; // Global fast coordinate
int idy; // Global slow coordinate
int sx = gridDim.x * blockDim.x; // Stride of fast coordinate
for ( idy = 0; idy < 10000; idy++ ) // Slow coordinate loop
{
int id = idx + idy * sx; // Global coordinate in 2D array
int ind = inds[id]; // Index of random access to small array
float f0 = flt[ind * 8 + 0];
...
float f7 = flt[ind * 8 + 7];
NEXT I HAVE SOME ALGEBRAIC FORMULAS THAT USE f0, ..., f7
}
}
flt
访问数组的最佳方式是什么?
- 不要通过
flt
,使用__const__
内存。我不确定当不同线程访问不同数据时 const 内存有多快。 - 使用如上。不会使用负载统一,因为线程访问不同的数据。由于缓存,它仍然会很快吗?
- 复制到共享内存并使用共享内存数组。
- 使用纹理。从未使用过纹理...这种方法可以快速吗?
对于共享内存,转置flt
数组可能更好,即以这种方式访问它以避免银行冲突:
float fj = flt_shared[j * 8 + ind]; // where j = 0, ..., 7
PS:目标架构是费米和开普勒。