MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
gpuUtils.cu
Go to the documentation of this file.
1// MaCh3 includes
3
4// **************************************************
5// Check for a safe call on GPU
6void __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
18void __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
44// *******************************************
45 float free_m, total_m,used_m;
46 size_t free_t, total_t;
47
48 cudaMemGetInfo(&free_t, &total_t);
50
51 free_m = static_cast<uint>(free_t)/1048576.0;
52 total_m = static_cast<uint>(total_t)/1048576.0;
53 used_m = total_m - free_m;
54
55 printf(" Memory free %f MB, total memory %f MB, memory used %f MB\n", free_m, total_m, used_m);
56}
57
58// *******************************************
59// KS: Get some fancy info about GPU
61// *******************************************
62 int nDevices;
63 cudaGetDeviceCount(&nDevices);
65
66 if (nDevices == 0) {
67 printf("No CUDA devices found");
68 throw;
69 }
70
71 printf(" Found %i GPUs, currently I only support one GPU\n", nDevices);
72}
73
74
75// *******************************************
76// KS: Completely clean GPU, this is time consuming and may lead to unexpected behaviour.
78// *******************************************
79 cudaDeviceReset();
81}
82
83// *******************************************
85void SetDevice(const int deviceId) {
86// *******************************************
87 // Check if the device ID is valid
88 int deviceCount;
89 cudaGetDeviceCount(&deviceCount);
90 if (deviceId < 0 || deviceId >= deviceCount) {
91 printf("Invalid device ID: %i \n", deviceId);
92 throw;
93 }
94
95 cudaSetDevice(deviceId);
97 printf("GPU device set to ID: %i \n", deviceId);
98}
99
100// *******************************************
101// Get number of GPU threads for currently used GPU
102int GetNumGPUThreads(const int Device) {
103// *******************************************
104 int deviceCount;
105 cudaGetDeviceCount(&deviceCount);
106
107 if (deviceCount == 0) {
108 printf("No CUDA devices found");
109 throw;
110 }
111
112 cudaDeviceProp deviceProp;
113 cudaGetDeviceProperties(&deviceProp, Device);
114
115 // Define the number of threads per block
116 int nThreadsBlocks = (deviceProp.multiProcessorCount * deviceProp.maxThreadsPerMultiProcessor);
117
118 return nThreadsBlocks;
119}
120
121// *******************************************
122size_t GetL2CacheSize(const int device) {
123// *******************************************
124 cudaDeviceProp prop;
125 cudaError_t err = cudaGetDeviceProperties(&prop, device);
126 if (err != cudaSuccess) {
127 printf("No CUDA devices found");
128 throw;
129 }
130 return prop.l2CacheSize; // size in bytes
131}
132
133// *******************************************
134size_t GetMaxTexture1DSize(const int device) {
135// *******************************************
136 cudaDeviceProp prop;
137 cudaError_t err = cudaGetDeviceProperties(&prop, device);
138 if (err != cudaSuccess) {
139 printf("No CUDA devices found");
140 throw;
141 }
142 return prop.maxTexture1D;
143}
144
145// *******************************************
146size_t GetSharedMemoryPerBlock(const int device) {
147// *******************************************
148 cudaDeviceProp prop;
149 cudaError_t err = cudaGetDeviceProperties(&prop, device);
150
151 if (err != cudaSuccess) {
152 printf("No CUDA devices found");
153 throw;
154 }
155 return static_cast<size_t>(prop.sharedMemPerBlock);
156}
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:134
void SetDevice(const int deviceId)
Only useful if using multiple GPU.
Definition: gpuUtils.cu:85
void ResetDevice()
KS: Completely clean GPU, this is time consuming and may lead to unexpected behaviour.
Definition: gpuUtils.cu:77
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:102
size_t GetL2CacheSize(const int device)
KS: Get L2 cache size (in bytes) for the specified GPU device.
Definition: gpuUtils.cu:122
void PrintNdevices()
KS: Get some fancy info about GPU.
Definition: gpuUtils.cu:60
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:146
Common CUDA utilities and definitions for shared GPU functionality.
#define CudaCheckError()
Definition: gpuUtils.cuh:22