VTK-m  2.2
ChooseCudaDevice.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_cont_cuda_ChooseCudaDevice_h
11 #define vtk_m_cont_cuda_ChooseCudaDevice_h
12 
14 
19 
20 #include <algorithm>
21 #include <set>
22 #include <vector>
23 
25 #include <cuda.h>
27 
28 namespace vtkm
29 {
30 namespace cont
31 {
32 namespace cuda
33 {
34 
35 namespace
36 {
37 struct compute_info
38 {
39  compute_info(cudaDeviceProp prop, int index)
40  {
41  this->Index = index;
42  this->Major = prop.major;
43 
44  this->MemorySize = prop.totalGlobalMem;
45  this->Performance =
46  prop.multiProcessorCount * prop.maxThreadsPerMultiProcessor * (prop.clockRate / 100000.0);
47 
48  //9999 is equal to emulation make sure it is a super bad device
49  if (this->Major >= 9999)
50  {
51  this->Major = -1;
52  this->Performance = -1;
53  }
54  }
55 
56  //sort from fastest to slowest
57  bool operator<(const compute_info other) const
58  {
59  //if we are both SM3 or greater check performance
60  //if we both the same SM level check performance
61  if ((this->Major >= 3 && other.Major >= 3) || (this->Major == other.Major))
62  {
63  return betterPerformance(other);
64  }
65  //prefer the greater SM otherwise
66  return this->Major > other.Major;
67  }
68 
69  bool betterPerformance(const compute_info other) const
70  {
71  if (this->Performance == other.Performance)
72  {
73  if (this->MemorySize == other.MemorySize)
74  {
75  //prefer first device over second device
76  //this will be subjective I bet
77  return this->Index < other.Index;
78  }
79  return this->MemorySize > other.MemorySize;
80  }
81  return this->Performance > other.Performance;
82  }
83 
84  int GetIndex() const { return Index; }
85 
86 private:
87  int Index;
88  int Major;
89  size_t MemorySize;
90  double Performance;
91 };
92 }
93 
96 static int FindFastestDeviceId()
97 {
98  auto cudaDeviceConfig = dynamic_cast<
99  vtkm::cont::internal::RuntimeDeviceConfiguration<vtkm::cont::DeviceAdapterTagCuda>&>(
102  vtkm::Id numDevices;
103  cudaDeviceConfig.GetMaxDevices(numDevices);
104 
105  // multiset stores elements in sorted order (allows duplicate values)
106  std::multiset<compute_info> devices;
107  std::vector<cudaDeviceProp> cudaProp;
108  cudaDeviceConfig.GetCudaDeviceProp(cudaProp);
109  for (int i = 0; i < numDevices; ++i)
110  {
111  if (cudaProp[i].computeMode != cudaComputeModeProhibited)
112  {
113  devices.emplace(cudaProp[i], i);
114  }
115  }
116 
117  return devices.size() > 0 ? devices.begin()->GetIndex() : 0;
118 }
119 
121 static void SetFastestDeviceId()
122 {
123  auto deviceId = FindFastestDeviceId();
126  .SetDeviceInstance(deviceId);
127 }
128 
129 }
130 }
131 } //namespace
132 
133 #endif
VTKM_THIRDPARTY_POST_INCLUDE
#define VTKM_THIRDPARTY_POST_INCLUDE
Definition: Configure.h:192
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::DeviceAdapterTagCuda
Tag for a device adapter that uses a CUDA capable GPU device.
Definition: DeviceAdapterTagCuda.h:33
RuntimeDeviceConfigurationCuda.h
vtkm::cont::RuntimeDeviceInformation::GetRuntimeConfiguration
vtkm::cont::internal::RuntimeDeviceConfigurationBase & GetRuntimeConfiguration(DeviceAdapterId id, const vtkm::cont::internal::RuntimeDeviceConfigurationOptions &configOptions, int &argc, char *argv[]=nullptr) const
Returns a reference to a RuntimeDeviceConfiguration that will work with the given device.
DeviceAdapterTagCuda.h
Index
int Index
Definition: ChooseCudaDevice.h:87
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
ErrorExecution.h
Major
int Major
Definition: ChooseCudaDevice.h:88
VTKM_THIRDPARTY_PRE_INCLUDE
#define VTKM_THIRDPARTY_PRE_INCLUDE
Definition: Configure.h:191
MemorySize
size_t MemorySize
Definition: ChooseCudaDevice.h:89
RuntimeDeviceInformation.h
vtkm::cont::RuntimeDeviceInformation
A class that can be used to determine if a given device adapter is supported on the current machine a...
Definition: RuntimeDeviceInformation.h:29
ErrorCuda.h
Performance
double Performance
Definition: ChooseCudaDevice.h:90