VTK-m  2.2
RuntimeDeviceConfigurationCuda.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_internal_RuntimeDeviceConfigurationCuda_h
11 #define vtk_m_cont_cuda_internal_RuntimeDeviceConfigurationCuda_h
12 
16 
17 #include <vtkm/cont/Logging.h>
19 
21 #include <cuda.h>
23 
24 #include <vector>
25 
26 namespace vtkm
27 {
28 namespace cont
29 {
30 namespace internal
31 {
32 
33 template <>
34 class RuntimeDeviceConfiguration<vtkm::cont::DeviceAdapterTagCuda>
35  : public vtkm::cont::internal::RuntimeDeviceConfigurationBase
36 {
37 public:
38  RuntimeDeviceConfiguration<vtkm::cont::DeviceAdapterTagCuda>()
39  {
40  this->CudaDeviceCount = 0;
41  this->CudaProp.clear();
43  if (detector.Exists())
44  {
45  try
46  {
47  int tmp;
48  VTKM_CUDA_CALL(cudaGetDeviceCount(&tmp));
49  this->CudaDeviceCount = tmp;
50  this->CudaProp.resize(this->CudaDeviceCount);
51  for (int i = 0; i < this->CudaDeviceCount; ++i)
52  {
53  VTKM_CUDA_CALL(cudaGetDeviceProperties(&this->CudaProp[i], i));
54  }
55  }
56  catch (...)
57  {
59  "Error retrieving CUDA device information. Disabling.");
60  this->CudaDeviceCount = 0;
61  }
62  }
63  }
64 
65  VTKM_CONT vtkm::cont::DeviceAdapterId GetDevice() const override final
66  {
68  }
69 
70  VTKM_CONT virtual RuntimeDeviceConfigReturnCode SetDeviceInstance(
71  const vtkm::Id& value) override final
72  {
73  if (value >= this->CudaDeviceCount)
74  {
75  VTKM_LOG_S(
77  "Failed to set CudaDeviceInstance, supplied id exceeds the number of available devices: "
78  << value << " >= " << this->CudaDeviceCount);
79  return RuntimeDeviceConfigReturnCode::INVALID_VALUE;
80  }
81  VTKM_CUDA_CALL(cudaSetDevice(value));
82  return RuntimeDeviceConfigReturnCode::SUCCESS;
83  }
84 
85  VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetDeviceInstance(
86  vtkm::Id& value) const override final
87  {
88  int tmp;
89  VTKM_CUDA_CALL(cudaGetDevice(&tmp));
90  value = tmp;
91  return RuntimeDeviceConfigReturnCode::SUCCESS;
92  }
93 
94  VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetMaxDevices(
95  vtkm::Id& value) const override final
96  {
97  value = this->CudaDeviceCount;
98  return RuntimeDeviceConfigReturnCode::SUCCESS;
99  }
100 
103  VTKM_CONT RuntimeDeviceConfigReturnCode
104  GetCudaDeviceProp(std::vector<cudaDeviceProp>& value) const
105  {
106  value = CudaProp;
107  return RuntimeDeviceConfigReturnCode::SUCCESS;
108  }
109 
110 private:
111  std::vector<cudaDeviceProp> CudaProp;
112  vtkm::Id CudaDeviceCount;
113 };
114 } // namespace vtkm::cont::internal
115 } // namespace vtkm::cont
116 } // namespace vtkm
117 
118 #endif //vtk_m_cont_cuda_internal_RuntimeDeviceConfigurationCuda_h
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
DeviceAdapterRuntimeDetectorCuda.h
DeviceAdapterTagCuda.h
vtkm::cont::DeviceAdapterRuntimeDetector< vtkm::cont::DeviceAdapterTagCuda >::Exists
bool Exists() const
Returns true if the given device adapter is supported on the current machine.
VTKM_CUDA_CALL
#define VTKM_CUDA_CALL(command)
A macro that can be wrapped around a CUDA command and will throw an ErrorCuda exception if the CUDA c...
Definition: ErrorCuda.h:38
RuntimeDeviceConfiguration.h
VTKM_LOG_F
#define VTKM_LOG_F(level,...)
Writes a message using printf syntax to the indicated log level.
Definition: Logging.h:209
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::cont::LogLevel::Error
@ Error
Important but non-fatal errors, such as device fail-over.
VTKM_LOG_S
#define VTKM_LOG_S(level,...)
Writes a message using stream syntax to the indicated log level.
Definition: Logging.h:208
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
VTKM_THIRDPARTY_PRE_INCLUDE
#define VTKM_THIRDPARTY_PRE_INCLUDE
Definition: Configure.h:191
Logging.h
Logging utilities.
vtkm::cont::DeviceAdapterRuntimeDetector< vtkm::cont::DeviceAdapterTagCuda >
Class providing a CUDA runtime support detector.
Definition: DeviceAdapterRuntimeDetectorCuda.h:38
ErrorCuda.h