alae
alae

Reputation: 130

Computing the mean of a 2D array CUDA

I need to compute the mean of a 2D array using CUDA, but I don't know how to proceed. I started by doing column reduction after that I will make the sum of the resulting array, and in the last step I will compute the mean.

To do this I need to do the whole work on the device at once? or I just do step by step and each step need a back and forth to and from the CPU and The GPU.

Upvotes: 2

Views: 3858

Answers (1)

pSoLT
pSoLT

Reputation: 1052

If it's simple arithmetic mean of all the elements in 2D array you can use thrust:

int* data;
int num;
get_data_from_library( &data, &num );

thrust::device_vector< int > iVec(data, data+num);

// transfer to device and compute sum
int sum = thrust::reduce(iVec.begin(), iVec.end(), 0, thrust::plus<int>());
double mean = sum/(double)num;

If you want to write your own kernel - keep in mind that 2D array is essentially an 1D array divided into row-size chunks and go through SDK 'parallel reduction' example : Whitepaper

Upvotes: 5

Related Questions