1
votes

I need to calculate N signals' mean values using reduction. The input is a 1D array of size MN, where M is the length of each signal.

Originally I had additional shared memory to first copy the data and do the reduction on each signal. However, the original data is corrupted.

My program tries to minimize the shared memory. So I was wondering how I can use registers to do a reduction sum on N signals. I have N threads, a shared memory (float) s_m[N*M], 0....M-1 is the first signal, etc.

Do I need N registers (or one) to store do mean value of N different signals? (I know how to do with sequential addition using multi-thread programming and 1 register). The next step I want to do is subtract every value in the input from its correspondent signal's mean.

2
Uh, I should explain this more clearly. I have N threads, a shared memory (float) s_m[N*M], 0....M-1 is the first signal, etc. Do I need N registers (or one) to store do mean value of N different signals? The next step I want to do is subtract every value in the input from its correspondent signal's mean. That's why I don't want additional shared memory. Hope that explains a little bit more. Thanks - andyzhangcr7
Might want to edit that into your question instead of as a comment. - Jesus Ramos
What are some example sizes of M and N? - harrism
I love: the original corrupt is corrupted - fabrizioM
I would expect N = 32 and M < 128 - andyzhangcr7

2 Answers

3
votes

Your problem is very small (N = 32 and M < 128). However, some guidelines:

Assuming you are reducing across N values for each of N threads.

  • If N is very large (> 10s of thousands) large, just do the reductions over M sequentially in each thread.
  • If N is < 10s of thousands, consider using one warp or one thread block to perform each of the N reductions.
  • If N is very small but M is very large, consider using multiple thread blocks per each of the N reductions.
  • If N is very small and M is very small (as your numbers are), only consider using the GPU for the reductions if the computations that generate and / or consume the input / output of the reductions are also running on the GPU.
-1
votes

Based on my understanding of the question, I say that you don't need N registers to store the mean value of N different signals.

If you already have N threads [Given that each thread do reduction on only one signal], then you don't need N registers to store the reduction of one signal. All you need is one register to store the mean value.

dim3 threads (N,1);
reduction<<<threads,1>>>(signals);  // signals is the [N*M] array 

__global__ reduction (int *signals)
{
   int id = threadIdx.x;
   float meanValue = 0.0;

   for(int i = 0; i < M; i++)
          meanValue = signals[id*M +i];

   meanValue =  meanValue/M;

   // Then do the subtraction
   for(int i = 0; i < M; i++)
          signals[id*M +i] -= meanValue;
}

If you need to do Kind of global reduction of all the meanValues of N different signals, then you need to use 2 registers [one to store the local mean and another to store the global mean] and the shared memory

dim3 threads (N,1);
reduction<<<threads,1>>>(signals);  // signals is the [N*M] array 

__global__ reduction (int *signals)
{
   __shared__ float means[N];      // shared value
   int id = threadIdx.x;
   float meanValue = 0.0;
   float globalMean = 0.0;

   for(int i = 0; i < M; i++)
          meanValue += signals[id*M +i];

   means[id] =  meanValue/M;

   __syncthreads();

   // do the global reduction
   for(int i = 0; i < N; i++)
          globalMean += means[i];

   globalMean = globalMean/N;

   // Then do the subtraction
   for(int i = 0; i < M; i++)
          signals[id*M +i] -= globalMean;
}

I hope this helps you. Any doubts, let me know.