Search code examples
cudapycuda

How do I calculate variance of gpu_array?


I am trying to compute the variance of a 2D gpu_array. A reduction kernel sounds like a good idea:

http://documen.tician.de/pycuda/array.html

However, this documentation implies that reduction kernels just reduce 2 arrays into 1 array. How do I reduce a single 2D array into a single value?


Solution

  • I guess the first step is to define variance for this case. In matlab, the variance function on a 2D array returns a vector (1D-array) of values. But it sounds like you want a single-valued variance, so as others have already suggested, probably the first thing to do is to treat the 2D-array as 1D. In C we won't require any special steps to accomplish this. If you have a pointer to the array you can index into it as if it were a 1D array. I'm assuming you don't need help on how to handle a 2D array with a 1D index.

    Now if it's the 1D variance you're after, I'm assuming a function like variance(x)=sum((x[i]-mean(x))^2) where the sum is over all i, is what you're after (based on my read of the wikipedia article ). We can break this down into 3 steps:

    1. compute the mean (this is a classical reduction - one value is produced for the data set - sum all elements then divide by the number of elements)
    2. compute the value (x[i]-mean)^2 for all i - this is an element by element operation producing an output data set equal in size (number of elements) to the input data set
    3. compute the sum of the elements produced in step 2 - this is another classical reduction, as one value is produced for the entire data set.

    Both steps 1 and 3 are classical reductions which are summing all elements of an array. Rather than cover that ground here, I'll point you to Mark Harris' excellent treatment of the topic as well as some CUDA sample code. For step 2, I'll bet you could figure out the kernel code on your own, but it would look something like this:

    #include <math.h>
        __global__ void var(float *input, float *output, unsigned N, float mean){
    
          unsigned idx=threadIdx.x+(blockDim.x*blockIdx.x);
          if (idx < N) output[idx] = __powf(input[idx]-mean, 2);
        }
    

    Note that you will probably want to combine the reductions and the above code into a single kernel.