MaCh3  2.5.0
Reference Guide
gpuUtils.cu
Go to the documentation of this file.
1 // MaCh3 includes
2 #include "Manager/gpuUtils.cuh"
3 
4 // **************************************************
5 // Check for a safe call on GPU
6 void __cudaSafeCall( cudaError err, const char *file, const int line ) {
7 // **************************************************
8 #ifdef CUDA_ERROR_CHECK
9  if (cudaSuccess != err) {
10  fprintf(stderr, "%s failed at %s:%i : %s\n", __func__, file, line, cudaGetErrorString(err));
11  throw;
12  }
13 #endif
14 }
15 
16 // **************************************************
17 // Check if there's been an error
18 void __cudaCheckError( const char *file, const int line ) {
19 // **************************************************
20 #ifdef CUDA_ERROR_CHECK
21  cudaError err = cudaGetLastError();
22  if (cudaSuccess != err) {
23  fprintf(stderr, "%s failed at %s:%i : %s\n", __func__, file, line, cudaGetErrorString(err));
24  throw;
25  }
26 
27  // More careful checking. However, this will affect performance.
28  // Comment away if needed.
29  err = cudaDeviceSynchronize();
30  if (cudaSuccess != err) {
31  fprintf(stderr, "%s with sync failed at %s:%i : %s\n", __func__, file, line, cudaGetErrorString(err));
32  throw;
33  }
34 #endif
35 }
36 
37 // *******************************************
38 // Utils
39 // *******************************************
40 
41 // *******************************************
42 // KS: Get some fancy info about VRAM usage
43 void checkGpuMem() {
44 // *******************************************
45  size_t free_t, total_t;
46  cudaMemGetInfo(&free_t, &total_t);
48 
49  double free_m = static_cast<double>(free_t) / 1024.0 / 1024.0;
50  double total_m = static_cast<double>(total_t) / 1024.0 / 1024.0;
51  double used_m = total_m - free_m;
52 
53  printf(" Memory free %f MB, total memory %f MB, memory used %f MB\n", free_m, total_m, used_m);
54 }
55 
56 // *******************************************
57 // KS: Get some fancy info about GPU
58 void PrintNdevices() {
59 // *******************************************
60  int nDevices;
61  cudaGetDeviceCount(&nDevices);
63 
64  if (nDevices == 0) {
65  printf("No CUDA devices found");
66  throw;
67  }
68 
69  printf(" Found %i GPUs, currently I only support one GPU\n", nDevices);
70 }
71 
72 
73 // *******************************************
74 // KS: Completely clean GPU, this is time consuming and may lead to unexpected behaviour.
75 void ResetDevice() {
76 // *******************************************
77  cudaDeviceReset();
79 }
80 
81 // *******************************************
83 void SetDevice(const int deviceId) {
84 // *******************************************
85  // Check if the device ID is valid
86  int deviceCount;
87  cudaGetDeviceCount(&deviceCount);
88  if (deviceId < 0 || deviceId >= deviceCount) {
89  printf("Invalid device ID: %i \n", deviceId);
90  throw;
91  }
92 
93  cudaSetDevice(deviceId);
95  printf("GPU device set to ID: %i \n", deviceId);
96 }
97 
98 // *******************************************
99 // Get number of GPU threads for currently used GPU
100 int GetNumGPUThreads(const int Device) {
101 // *******************************************
102  int deviceCount;
103  cudaGetDeviceCount(&deviceCount);
104 
105  if (deviceCount == 0) {
106  printf("No CUDA devices found");
107  throw;
108  }
109 
110  cudaDeviceProp deviceProp;
111  cudaGetDeviceProperties(&deviceProp, Device);
112 
113  // Define the number of threads per block
114  int nThreadsBlocks = (deviceProp.multiProcessorCount * deviceProp.maxThreadsPerMultiProcessor);
115 
116  return nThreadsBlocks;
117 }
118 
119 // *******************************************
120 size_t GetL2CacheSize(const int device) {
121 // *******************************************
122  cudaDeviceProp prop;
123  cudaError_t err = cudaGetDeviceProperties(&prop, device);
124  if (err != cudaSuccess) {
125  printf("No CUDA devices found");
126  throw;
127  }
128  return prop.l2CacheSize; // size in bytes
129 }
130 
131 // *******************************************
132 size_t GetMaxTexture1DSize(const int device) {
133 // *******************************************
134  cudaDeviceProp prop;
135  cudaError_t err = cudaGetDeviceProperties(&prop, device);
136  if (err != cudaSuccess) {
137  printf("No CUDA devices found");
138  throw;
139  }
140  return prop.maxTexture1D;
141 }
142 
143 // *******************************************
144 size_t GetSharedMemoryPerBlock(const int device) {
145 // *******************************************
146  cudaDeviceProp prop;
147  cudaError_t err = cudaGetDeviceProperties(&prop, device);
148 
149  if (err != cudaSuccess) {
150  printf("No CUDA devices found");
151  throw;
152  }
153  return static_cast<size_t>(prop.sharedMemPerBlock);
154 }
void __cudaCheckError(const char *file, const int line)
Check if there's been an error.
Definition: gpuUtils.cu:18
size_t GetMaxTexture1DSize(const int device)
KS: Get the maximum size for 1D textures on the specified GPU device.
Definition: gpuUtils.cu:132
void SetDevice(const int deviceId)
Only useful if using multiple GPU.
Definition: gpuUtils.cu:83
void ResetDevice()
KS: Completely clean GPU, this is time consuming and may lead to unexpected behaviour.
Definition: gpuUtils.cu:75
void checkGpuMem()
KS: Get some fancy info about VRAM usage.
Definition: gpuUtils.cu:43
int GetNumGPUThreads(const int Device)
KS: Get number of GPU threads for currently used GPU.
Definition: gpuUtils.cu:100
size_t GetL2CacheSize(const int device)
KS: Get L2 cache size (in bytes) for the specified GPU device.
Definition: gpuUtils.cu:120
void PrintNdevices()
KS: Get some fancy info about GPU.
Definition: gpuUtils.cu:58
void __cudaSafeCall(cudaError err, const char *file, const int line)
Check for a safe call on GPU.
Definition: gpuUtils.cu:6
size_t GetSharedMemoryPerBlock(const int device)
KS: Returns the maximum shared memory per block for a given GPU device.
Definition: gpuUtils.cu:144
Common CUDA utilities and definitions for shared GPU functionality.
#define CudaCheckError()
Definition: gpuUtils.cuh:21