Hard
Array-of-structs versus struct-of-arrays for a single-field update
Both kernels compute the same thing, doubling every particle's x-coordinate, over the same number of particles, launched with one thread per particle. Which layout achieves meaningfully higher effective memory bandwidth, and why?
kernel.cu
// Layout 1: array of structs
struct Particle { float x, y, z; };
__global__ void scaleAoS(Particle* p, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) p[i].x *= 2.0f;
}
// Layout 2: struct of arrays
__global__ void scaleSoA(float* px, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) px[i] *= 2.0f;
}6 of 46 in GPU / CUDA