Medium
A float4 kernel called on a sub-range starting at element offset
d_in and d_out come straight from cudaMalloc. To use 128-bit memory instructions, the kernel reinterprets its pointers as float4. It has been tested with offset values of 0 and 1024, with count always a multiple of 4. A new caller passes offset = 6. What happens?
kernel.cu
__global__ void scale4(const float* in, float* out, float s, int n4) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n4) {
float4 v = reinterpret_cast<const float4*>(in)[i];
v.x *= s; v.y *= s; v.z *= s; v.w *= s;
reinterpret_cast<float4*>(out)[i] = v;
}
}
// host
scale4<<<blocks, 256>>>(d_in + offset, d_out + offset, 2.0f, count / 4);41 of 46 in GPU / CUDA