1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
#ifndef HPC_CUDA_PROPERTIES_H
#define HPC_CUDA_PROPERTIES_H 1

#ifndef __CUDACC__
#       error This source must be compiled using nvcc
#endif

#include <cstdio>
#include <cstdlib>
#include <hpc/cuda/check.h>

namespace hpc { namespace cuda {

inline const cudaDeviceProp& get_properties(int device = -1) {
   if (device < 0) {
      CHECK_CUDA(cudaGetDevice, &device);
   }
   static int current_device = -1;
   static cudaDeviceProp properties;
   if (device != current_device) {
      CHECK_CUDA(cudaGetDeviceProperties, &properties, device);
      current_device = device;
   }
   return properties;
}

int get_max_threads_per_block(int device = -1) {
   const cudaDeviceProp& properties(get_properties(device));
   return properties.maxThreadsPerBlock;
}

int get_number_of_multiprocessors(int device = -1) {
   const cudaDeviceProp& properties(get_properties(device));
   return properties.multiProcessorCount;
}

int get_warp_size(int device = -1) {
   const cudaDeviceProp& properties(get_properties(device));
   return properties.warpSize;
}

} } // namespaces cuda, hpc

#endif