Compute the population mean and population variance of a 1D float array on the GPU. Write two scalar results to two separate 1-element device buffers.
μ=N1i=0∑N−1input[i] σ2=N1i=0∑N−1(input[i]−μ)2The divisor is N (population, biased variance), not N−1. The buffers mean_out and var_out are device pointers to single floats; solve writes the mean to mean_out[0] and the variance to var_out[0]. Both reductions run on the GPU; no host-side accumulation.
Input: input = [1, 2, 3, 4, 5], N = 5
Output: mean = 3.0, variance = 2.0
Mean is (1+2+3+4+5)/5=3. Squared deviations are 4,1,0,1,4; their mean is 10/5=2.
Input: input = [2, 2, 2, 2], N = 4
Output: mean = 2.0, variance = 0.0
A constant array has zero variance.
You can compute both reductions in one pass by accumulating ∑xi and ∑xi2 together, then using Var(X)=E[X2]−E[X]2.
Block-local shared-memory reductions followed by one atomicAdd per block to the global sum and sum-of-squares keep atomic traffic at O(N/256).
After the reduction kernel finishes, a tiny single-thread finalize kernel can divide by N to write mean_out and var_out.
Accepts: array
Submit a correct solution to see your kernel performance.
Compute the population mean and population variance of a 1D float array on the GPU. Write two scalar results to two separate 1-element device buffers.
μ=N1i=0∑N−1input[i] σ2=N1i=0∑N−1(input[i]−μ)2The divisor is N (population, biased variance), not N−1. The buffers mean_out and var_out are device pointers to single floats; solve writes the mean to mean_out[0] and the variance to var_out[0]. Both reductions run on the GPU; no host-side accumulation.
Input: input = [1, 2, 3, 4, 5], N = 5
Output: mean = 3.0, variance = 2.0
Mean is (1+2+3+4+5)/5=3. Squared deviations are 4,1,0,1,4; their mean is 10/5=2.
Input: input = [2, 2, 2, 2], N = 4
Output: mean = 2.0, variance = 0.0
A constant array has zero variance.
You can compute both reductions in one pass by accumulating ∑xi and ∑xi2 together, then using Var(X)=E[X2]−E[X]2.
Block-local shared-memory reductions followed by one atomicAdd per block to the global sum and sum-of-squares keep atomic traffic at O(N/256).
After the reduction kernel finishes, a tiny single-thread finalize kernel can divide by N to write mean_out and var_out.
Accepts: array
Submit a correct solution to see your kernel performance.