VR-Forces 4.7 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtHelperCuda.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2018 VT-MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
11 #pragma once
12 
13 #include <helper_cuda.h>
14 
15 namespace makVrv
16 {
18  template <typename T>
19  void DtCheck(T result, char const *const func, const char *const file, int const line, DtDe* theDe)
20  {
21  if (result)
22  {
23  if (theDe != NULL)
24  {
25  theDe->DT_LOG_WARN << "CUDA error at " << file << ":" << line <<
26  "code=" << static_cast<unsigned int>(result) << "(" << _cudaGetErrorEnum(result) <<
27  ") \"" << func << "\"" << std::endl;
28  }
29  DEVICE_RESET
30  // Make sure we call CUDA Device Reset before exiting
31  }
32  }
33 
37 #define DtCheckCudaErrors(val, theDe) DtCheck((val), #val, __FILE__, __LINE__, theDe)
38 
40  inline int DtGpuGetMaxGflopsDeviceId(DtDe* theDe)
41  {
42  int current_device = 0, sm_per_multiproc = 0;
43  int max_perf_device = 0;
44  int device_count = 0, best_SM_arch = 0;
45  int devices_prohibited = 0;
46 
47  uint64_t max_compute_perf = 0;
48  cudaDeviceProp deviceProp;
49  DtCheckCudaErrors(cudaGetDeviceCount(&device_count), theDe);
50 
51  if (device_count == 0)
52  {
53  if (theDe != NULL)
54  {
55  theDe->DT_LOG_WARN << "gpuGetMaxGflopsDeviceId() CUDA error:" <<
56  " no devices supporting CUDA. Try updating your NVidia driver to a more recent one that support the CUDA version of this application." << std::endl;
57  }
58  return -1;
59  }
60 
61  // Find the best major SM Architecture GPU device
62  while (current_device < device_count)
63  {
64  cudaGetDeviceProperties(&deviceProp, current_device);
65 
66  // If this GPU is not running on Compute Mode prohibited,
67  // then we can add it to the list
68  if (deviceProp.computeMode != cudaComputeModeProhibited)
69  {
70  if (deviceProp.major > 0 && deviceProp.major < 9999)
71  {
72  best_SM_arch = MAX(best_SM_arch, deviceProp.major);
73  }
74  }
75  else
76  {
77  devices_prohibited++;
78  }
79 
80  current_device++;
81  }
82 
83  if (devices_prohibited == device_count)
84  {
85  if (theDe != NULL)
86  {
87  theDe->DT_LOG_WARN << "gpuGetMaxGflopsDeviceId() CUDA error:" <<
88  " all devices have compute mode prohibited." << std::endl;
89  }
90  return -1;
91  }
92 
93  // Find the best CUDA capable GPU device
94  current_device = 0;
95 
96  while (current_device < device_count)
97  {
98  cudaGetDeviceProperties(&deviceProp, current_device);
99 
100  // If this GPU is not running on Compute Mode prohibited,
101  // then we can add it to the list
102  if (deviceProp.computeMode != cudaComputeModeProhibited)
103  {
104  if (deviceProp.major == 9999 && deviceProp.minor == 9999)
105  {
106  sm_per_multiproc = 1;
107  }
108  else
109  {
110  sm_per_multiproc =
111  _ConvertSMVer2Cores(deviceProp.major, deviceProp.minor);
112  }
113 
114  uint64_t compute_perf = (uint64_t)deviceProp.multiProcessorCount *
115  sm_per_multiproc * deviceProp.clockRate;
116 
117  if (compute_perf > max_compute_perf)
118  {
119  // If we find GPU with SM major > 2, search only these
120  if (best_SM_arch > 2)
121  {
122  // If our device==dest_SM_arch, choose this, or else pass
123  if (deviceProp.major == best_SM_arch)
124  {
125  max_compute_perf = compute_perf;
126  max_perf_device = current_device;
127  }
128  }
129  else
130  {
131  max_compute_perf = compute_perf;
132  max_perf_device = current_device;
133  }
134  }
135  }
136 
137  ++current_device;
138  }
139 
140  return max_perf_device;
141  }
142 }

Document ID: Generated on Fri Apr 26 21:53:14 EDT 2019 from SVN revision 197883
Copyright © 2005-2019 VT MAK. All Rights Reserved (www.mak.com)