Medium
Contiguous versus strided global memory access across a warp
Both kernels below are launched with the same total thread count and move the same number of useful floats; Kernel B's arrays are 32 times larger so its strided indices stay in bounds. Which is the more accurate performance prediction?
kernel.cu
// Kernel A
__global__ void copyContiguous(float* out, const float* in) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
out[i] = in[i];
}
// Kernel B (stride = 32)
__global__ void copyStrided(float* out, const float* in, int stride) {
int i = (blockIdx.x * blockDim.x + threadIdx.x) * stride;
out[i] = in[i];
}5 of 46 in GPU / CUDA