Image Utilities (IU)
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Groups Pages
iucutil.h
1 #pragma once
2 
3 #include <driver_types.h>
4 #include "iucore/coredefs.h"
5 #include <cufft.h>
6 
7 // includes for time measurements
8 #ifdef _WIN32
9  #include <time.h>
10  #include <windows.h>
11 #else
12  #include <sys/time.h>
13 #endif
14 
16 
17 #include <stdio.h>
25 {
26 public:
27  IuCudaException(const cudaError_t cudaErr,
28  const char* file=NULL, const char* function=NULL, int line=0) throw() :
29  IuException(std::string("CUDA Error: ") + cudaGetErrorString(cudaErr), file, function, line),
30  cudaErr_( cudaErr )
31  {
32  }
33 
34 protected:
35  cudaError_t cudaErr_;
36 };
37 
38 namespace iu {
47 static inline void checkCudaErrorState( const char* file, const char* function, const int line )
48 {
49  cudaDeviceSynchronize();
50  cudaError_t err = cudaGetLastError();
51  if( err != cudaSuccess )
52  throw IuCudaException( err, file, function, line );
53 }
54 
58 static inline void checkCudaErrorState(cudaError_t err, const char *file, const char* function,
59  const int line)
60 {
61  if (cudaSuccess != err)
62  {
63  throw IuCudaException(err, file, function, line);
64  }
65 }
66 
74 {
75 public:
76  IuCufftException(const cufftResult cudaErr, const char* file = NULL,
77  const char* function = NULL, int line = 0) throw () :
78  IuException(std::string("CUFFT Error: ") + cufftGetErrorString(cudaErr),
79  file, function, line),
80  cufftResult_(cudaErr)
81  {
82  }
83 
84 protected:
85  cufftResult cufftResult_;
86 
87 private:
88  static const char *cufftGetErrorString(cufftResult err)
89  {
90  switch (err)
91  {
92  case CUFFT_SUCCESS:
93  return "The cuFFT operation was successful.";
94  case CUFFT_INVALID_PLAN:
95  return "cuFFT was passed an invalid plan handle.";
96  case CUFFT_ALLOC_FAILED:
97  return "cuFFT failed to allocate GPU or CPU memory.";
98  case CUFFT_INVALID_VALUE:
99  return "User specified an invalid pointer or parameter.";
100  case CUFFT_INTERNAL_ERROR:
101  return "Driver or internal cuFFT library error";
102  case CUFFT_EXEC_FAILED:
103  return "Failed to execute an FFT on the GPU";
104  case CUFFT_SETUP_FAILED:
105  return "The cuFFT library failed to initialize";
106  case CUFFT_INVALID_SIZE:
107  return "User specified an invalid transform size";
108  case CUFFT_INCOMPLETE_PARAMETER_LIST:
109  return "Missing parameters in call";
110  case CUFFT_INVALID_DEVICE:
111  return "Execution of a plan was on different GPU than plan creation";
112  case CUFFT_PARSE_ERROR:
113  return "Internal plan database error";
114  case CUFFT_NO_WORKSPACE:
115  return "No workspace has been provided prior to plan execution";
116  default:
117  return "Unknown CUFFT error.";
118  }
119  }
120 };
121 
125 static inline void checkCufftErrorState(const cufftResult status,
126  const char* file, const char* function,
127  const int line)
128 {
129  if (status != CUFFT_SUCCESS)
130  throw IuCufftException(status, file, function, line);
131 }
132 
136 static inline float getTotalGPUMemory()
137 {
138  size_t total = 0;
139  size_t free = 0;
140  cudaMemGetInfo(&free, &total);
141  return total/(1024.0f*1024.0f); // return value in Megabytes
142 }
143 
147 static inline float getFreeGPUMemory()
148 {
149  size_t total = 0;
150  size_t free = 0;
151  cudaMemGetInfo(&free, &total);
152  return free/(1024.0f*1024.0f); // return value in Megabytes
153 }
154 
158 static inline void printGPUMemoryUsage()
159 {
160  float total = iu::getTotalGPUMemory();
161  float free = iu::getFreeGPUMemory();
162 
163  printf("GPU memory usage\n");
164  printf("----------------\n");
165  printf(" Total memory: %.2f MiB\n", total);
166  printf(" Used memory: %.2f MiB\n", total-free);
167  printf(" Free memory: %.2f MiB\n", free);
168 }
169 
170 
171 
175 static inline double getTime()
176 {
177  cudaDeviceSynchronize();
178 #ifdef WIN32
179  LARGE_INTEGER current_time,frequency;
180  QueryPerformanceCounter (&current_time);
181  QueryPerformanceFrequency(&frequency);
182  return current_time.QuadPart*1000.0/frequency.QuadPart;
183 #else
184  timeval time;
185  gettimeofday(&time, NULL);
186  return time.tv_sec * 1000.0 + time.tv_usec / 1000.0;
187 #endif
188 }
190 } //namespace iu
191 
192 // MACROS
193 
194 #define IU_CUDA_CHECK iu::checkCudaErrorState(__FILE__, __FUNCTION__, __LINE__)
195 #define IU_CUDA_SAFE_CALL(fun) iu::checkCudaErrorState(fun, __FILE__, __FUNCTION__, __LINE__)
196 #define IU_CUFFT_SAFE_CALL(state) iu::checkCufftErrorState(state, __FILE__, __FUNCTION__, __LINE__ )
Exceptions with additional error information.
Definition: coredefs.h:32
Exceptions related to cuda issues.
Definition: iucutil.h:24
Exceptions related to cufft issues.
Definition: iucutil.h:73