user2799508
user2799508

Reputation: 848

Template usage in CUDA code

I am trying to understand how template is being used here:

template <uint32_t N_ATOMIC=32>
struct ParallelCounter {
public:
  uint32_t count[N_ATOMIC];

   // spread the counts across the counter
  __device__ __host__ void set(uint32_t x) {
    for(int i=0; i < N_ATOMIC; i++) count[i]=x/N_ATOMIC;

  } 

};



#ifndef SPREAD
#define SPREAD 32
#endif
__device__ ParallelCounter<SPREAD> myCounter;
__global__ void initCounter() {
  int tid = threadIdx.x + blockIdx.x * blockDim.x;

  if(tid == 0)
    myCounter.set(0);
}

All the examples on templates that I have seen so far have some variable in < > in first line above. But why we are having a constant uint32_t N_ATOMIC=32 here. if it is a constant, how would the template be useful? It is fixed for a particular type. Am I right? Thanks

Upvotes: 1

Views: 176

Answers (2)

kangshiyin
kangshiyin

Reputation: 9781

It's not true that only typename can be used as a template parameter. A int const can also be a template parameter.

It's useful because this parameter can be determined in compile-time, rather than run-time. Earlier determination is helpful in generating more optimized run-time code.

In your particular case, the size of the struct ParallelCounter have to be determined during the compile-time. C++ won't accept a type whose size can not be determined until run-time.

If template is not used in your code, you may have to write a series of struct ParallelCounter and void initCounter() for different values of SPREAD

Upvotes: 1

Robert Crovella
Robert Crovella

Reputation: 151849

This question has nothing to do with CUDA.

If you define a template this way, it means that if you instantiate the template but don't provide that parameter specifically, it will default to the value indicated. You can still instantiate the template with other values, to get different behavior.

Upvotes: 1

Related Questions