LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
CudaQueryDevice.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008 - 2012 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
37 #include "lsst/pex/exceptions.h"
40 
41 using namespace lsst::afw::gpu;
42 
43 #ifndef GPU_BUILD //if no GPU support, throw exceptions
44 
45 #include <stdio.h>
46 
47 namespace lsst {
48 namespace afw {
49 namespace gpu {
50 namespace detail {
51 
53  printf("Afw not compiled with GPU support\n");
54 }
55 
57  throw LSST_EXCEPT(GpuRuntimeError, "AFW not built with GPU support");
58 }
59 
61  throw LSST_EXCEPT(GpuRuntimeError, "AFW not built with GPU support");
62 }
63 
65  throw LSST_EXCEPT(GpuRuntimeError, "AFW not built with GPU support");
66 }
67 
69  throw LSST_EXCEPT(GpuRuntimeError, "AFW not built with GPU support");
70 }
71 
73  throw LSST_EXCEPT(GpuRuntimeError, "AFW not built with GPU support");
74 }
75 
77  throw LSST_EXCEPT(GpuRuntimeError, "AFW not built with GPU support");
78 }
79 
80 }
81 }
82 }
83 }
84 
85 #else
86 
87 #include <cuda.h>
88 #include <cuda_runtime.h>
89 #include <stdio.h>
90 
91 using namespace std;
92 
93 namespace lsst {
94 namespace afw {
95 namespace gpu {
96 namespace detail {
97 
98 void PrintDeviceProperties(int id, cudaDeviceProp deviceProp)
99 {
100  printf("Name : %s |", deviceProp.name );
101  printf(" CUDA Capable SM %d.%d hardware, %d multiproc.\n", deviceProp.major, deviceProp.minor,
102  deviceProp.multiProcessorCount);
103  printf(" Clock rate: %6.2f GHz \t", deviceProp.clockRate / (1000.0 * 1000));
104  printf(" Memory on device: %6zu MiB\n", deviceProp.totalGlobalMem / (1 << 20) );
105  printf(" Multiprocessors: %6d\n", deviceProp.multiProcessorCount);
106  printf(" Warp size: %6d \t", deviceProp.warpSize );
107  printf(" Shared memory:%6zu KiB\n", deviceProp.sharedMemPerBlock / (1 << 10) );
108  printf(" Registers: %6d \t", deviceProp.regsPerBlock );
109  printf(" Max threads: %6d \n", deviceProp.maxThreadsPerBlock );
110 
111  printf(" Compute mode (device sharing) : ");
112  if (deviceProp.computeMode == cudaComputeModeDefault) {
113  printf("Default - shared between threads\n" );
114  }
115  if (deviceProp.computeMode == cudaComputeModeExclusive) {
116  printf("Exclusive - only one thread at a time\n" );
117  }
118  if (deviceProp.computeMode == cudaComputeModeProhibited) {
119  printf("Prohibited - cannot use this device\n" );
120  }
121 
122  printf(" Timeout enabled: %3s ", deviceProp.kernelExecTimeoutEnabled == 1 ? "Yes" : "No" );
123  printf(" Overlapped copying: %3s ", deviceProp.deviceOverlap == 1 ? "Yes" : "No" );
124  printf(" Intergrated on MB: %3s\n", deviceProp.integrated == 1 ? "Yes" : "No" );
125  printf(" Memory pitch: %12zu \t", deviceProp.memPitch );
126  printf(" Constant memory: %6zu kiB \n", deviceProp.totalConstMem / (1 << 10) );
127 }
128 
129 void PrintCudaErrorInfo(cudaError_t cudaError, const char* errorStr)
130 {
131  printf("\nSupplied error string: %s\n", errorStr);
132  printf( "CUDA error : %d\n", cudaError);
133  printf( "CUDA error string : %s\n", cudaGetErrorString(cudaError));
134  exit(0);
135 }
136 
137 void PrintCudaDeviceInfo()
138 {
139  fflush(stdout);
140 
141  cudaError_t cudaError;
142 
143  int driverVersion;
144  cudaError = cudaDriverGetVersion(&driverVersion);
145  if (cudaError != cudaSuccess) PrintCudaErrorInfo(cudaError, "Could not get CUDA driver version");
146  printf("Driver ver.: %d.%d ", driverVersion / 1000, driverVersion % 1000);
147  fflush(stdout);
148 
149  int runtimeVersion;
150  cudaError = cudaRuntimeGetVersion(&runtimeVersion);
151  if (cudaError != cudaSuccess) PrintCudaErrorInfo(cudaError, "Could not get CUDA runtime version");
152  printf("Runtime ver.: %d.%d ", runtimeVersion / 1000, runtimeVersion % 1000);
153  fflush(stdout);
154 
155  //int preferredDeviceId = 0;
156 
157  int cudaDevicesN = 0;
158  cudaError = cudaGetDeviceCount(&cudaDevicesN);
159  if (cudaError != cudaSuccess) PrintCudaErrorInfo(cudaError, "Could not get CUDA device count");
160 
161  printf("Device count: %d ", cudaDevicesN);
162  fflush(stdout);
163  if(cudaDevicesN < 1) {
164  printf("Your system does not have a CUDA capable device\n");
165  exit(0);
166  }
167 
168  int curDevId;
169  cudaError = cudaGetDevice(&curDevId);
170  if (cudaError != cudaSuccess) PrintCudaErrorInfo(cudaError, "Could not get CUDA device id");
171  printf("Info for device %d\n", curDevId);
172  fflush(stdout);
173 
174  cudaDeviceProp deviceProp;
175  cudaError = cudaGetDeviceProperties(&deviceProp, curDevId);
176  if (cudaError != cudaSuccess) PrintCudaErrorInfo(cudaError, "Could not get CUDA device properties");
177  PrintDeviceProperties(curDevId, deviceProp);
178  fflush(stdout);
179 
180  for (int i = 0; i < 79; i++) {
181  printf("-");
182  }
183  printf("\n");
184  fflush(stdout);
185 }
186 
187 int GetCudaCurDeviceId()
188 {
189  int curDevId;
190  cudaError_t cudaError = cudaGetDevice(&curDevId);
191  if (cudaError != cudaSuccess) PrintCudaErrorInfo(cudaError, "GetCudaDeviceId> Could not get CUDA device id");
192  return curDevId;
193 }
194 
196 {
197  int curDevId = GetCudaCurDeviceId();
198  cudaDeviceProp deviceProp;
199  cudaError_t cudaError = cudaGetDeviceProperties(&deviceProp, curDevId);
200  if (cudaError != cudaSuccess) PrintCudaErrorInfo(cudaError, "GetCudaSMSharedMemorySize> Could not get CUDA device properties");
201 
202  return deviceProp.sharedMemPerBlock;
203 }
204 
206 {
207  int curDevId = GetCudaCurDeviceId();
208  cudaDeviceProp deviceProp;
209  cudaError_t cudaError = cudaGetDeviceProperties(&deviceProp, curDevId);
210  if (cudaError != cudaSuccess) {
211  PrintCudaErrorInfo(cudaError, "GetCudaCurGlobalMemorySize> Could not get CUDA device properties");
212  }
213  return deviceProp.totalGlobalMem;
214 }
215 
217 {
218  int curDevId = GetCudaCurDeviceId();
219  cudaDeviceProp deviceProp;
220  cudaError_t cudaError = cudaGetDeviceProperties(&deviceProp, curDevId);
221  if (cudaError != cudaSuccess) {
222  PrintCudaErrorInfo(cudaError, "GetCudaSMRegisterCount> Could not get CUDA device properties");
223  }
224  return deviceProp.regsPerBlock;
225 }
226 
227 int GetCudaCurSMCount()
228 {
229  int curDevId = GetCudaCurDeviceId();
230  cudaDeviceProp deviceProp;
231  cudaError_t cudaError = cudaGetDeviceProperties(&deviceProp, curDevId);
232  if (cudaError != cudaSuccess) {
233  PrintCudaErrorInfo(cudaError, "GetCudaSMCount> Could not get CUDA device properties");
234  }
235  return deviceProp.multiProcessorCount;
236 }
237 
239 {
240  int curDevId = GetCudaCurDeviceId();
241  cudaDeviceProp deviceProp;
242  cudaError_t cudaError = cudaGetDeviceProperties(&deviceProp, curDevId);
243  if (cudaError != cudaSuccess) {
244  PrintCudaErrorInfo(cudaError, "GetCudaIsDoublePrecisionSupported> Could not get CUDA device properties");
245  }
246  return deviceProp.major >= 2 || (deviceProp.major == 1 && deviceProp.minor >= 3);
247 }
248 
249 
250 }
251 }
252 }
253 }
254 
255 #endif
256 
int GetCudaCurSMRegisterCount()
returns the number of registers per block of currently selected cuda device
Include files required for standard LSST Exception handling.
additional GPU exceptions
int GetCudaCurGlobalMemorySize()
returns global memory size of currently selected cuda device
void PrintCudaDeviceInfo()
prints some cuda device information to stdout
int GetCudaCurSMSharedMemorySize()
returns shared memory size per block of currently selected cuda device
bool GetCudaCurIsDoublePrecisionSupported()
returns whether currently selected cuda device supports double precision
int GetCudaCurSMCount()
returns the number of streaming multiprocessors of currently selected cuda device ...
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
int GetCudaCurDeviceId()
returns device ID of currently selected cuda device
Functions to query the properties of currently selected GPU device.