MaCh3  2.5.0
Reference Guide
Functions
gpuUtils.cu File Reference
#include "Manager/gpuUtils.cuh"
Include dependency graph for gpuUtils.cu:

Go to the source code of this file.

Functions

void __cudaSafeCall (cudaError err, const char *file, const int line)
 Check for a safe call on GPU. More...
 
void __cudaCheckError (const char *file, const int line)
 Check if there's been an error. More...
 
void checkGpuMem ()
 KS: Get some fancy info about VRAM usage. More...
 
void PrintNdevices ()
 KS: Get some fancy info about GPU. More...
 
void ResetDevice ()
 KS: Completely clean GPU, this is time consuming and may lead to unexpected behaviour. More...
 
void SetDevice (const int deviceId)
 Only useful if using multiple GPU. More...
 
int GetNumGPUThreads (const int Device)
 KS: Get number of GPU threads for currently used GPU. More...
 
size_t GetL2CacheSize (const int device)
 KS: Get L2 cache size (in bytes) for the specified GPU device. More...
 
size_t GetMaxTexture1DSize (const int device)
 KS: Get the maximum size for 1D textures on the specified GPU device. More...
 
size_t GetSharedMemoryPerBlock (const int device)
 KS: Returns the maximum shared memory per block for a given GPU device. More...
 

Function Documentation

◆ __cudaCheckError()

void __cudaCheckError ( const char *  file,
const int  line 
)

Check if there's been an error.

This function checks if there has been any CUDA runtime API error and reports it.

Parameters
fileThe file name where the error occurred.
lineThe line number where the error occurred.

Definition at line 18 of file gpuUtils.cu.

18  {
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 }

◆ __cudaSafeCall()

void __cudaSafeCall ( cudaError  err,
const char *  file,
const int  line 
)

Check for a safe call on GPU.

Todo:

KS: There is plenty of useful stuff here https://github.com/NVIDIA/cuda-samples/blob/master/Samples/1_Utilities/deviceQuery/deviceQuery.cpp

KS: We might want to port some of these utilities, for example having bool if there is unified memory etc.

This function checks the error status returned by CUDA runtime API functions and reports any errors.

Parameters
errThe CUDA error code.
fileThe file name where the error occurred.
lineThe line number where the error occurred.

Definition at line 6 of file gpuUtils.cu.

6  {
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 }

◆ checkGpuMem()

void checkGpuMem ( )

KS: Get some fancy info about VRAM usage.

Definition at line 43 of file gpuUtils.cu.

43  {
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 }
#define CudaCheckError()
Definition: gpuUtils.cuh:21

◆ GetL2CacheSize()

size_t GetL2CacheSize ( const int  device = 0)

KS: Get L2 cache size (in bytes) for the specified GPU device.

Parameters
deviceThe ID of the device. Defaults to 0.
Returns
The size of the L2 cache in bytes

Definition at line 120 of file gpuUtils.cu.

120  {
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 }

◆ GetMaxTexture1DSize()

size_t GetMaxTexture1DSize ( const int  device = 0)

KS: Get the maximum size for 1D textures on the specified GPU device.

Parameters
deviceThe ID of the device. Defaults to 0.
Returns
The maximum 1D texture size supported by the device

Definition at line 132 of file gpuUtils.cu.

132  {
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 }

◆ GetNumGPUThreads()

int GetNumGPUThreads ( const int  Device = 0)

KS: Get number of GPU threads for currently used GPU.

Parameters
DeviceThe ID of the device. Defaults to 0.
Returns
The number of GPU threads.

Definition at line 100 of file gpuUtils.cu.

100  {
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 }

◆ GetSharedMemoryPerBlock()

size_t GetSharedMemoryPerBlock ( const int  device = 0)

KS: Returns the maximum shared memory per block for a given GPU device.

Parameters
deviceCUDA device ID (default = 0)
Returns
Maximum shared memory per block in bytes

Definition at line 144 of file gpuUtils.cu.

144  {
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 }

◆ PrintNdevices()

void PrintNdevices ( )

KS: Get some fancy info about GPU.

Definition at line 58 of file gpuUtils.cu.

58  {
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 }

◆ ResetDevice()

void ResetDevice ( )

KS: Completely clean GPU, this is time consuming and may lead to unexpected behaviour.

Definition at line 75 of file gpuUtils.cu.

75  {
76 // *******************************************
77  cudaDeviceReset();
79 }

◆ SetDevice()

void SetDevice ( const int  deviceId)

Only useful if using multiple GPU.

KS: Only useful if using multiple GPU.

Definition at line 83 of file gpuUtils.cu.

83  {
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 }