Viskores  1.0
RuntimeDeviceConfigurationCuda.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_cont_cuda_internal_RuntimeDeviceConfigurationCuda_h
19 #define viskores_cont_cuda_internal_RuntimeDeviceConfigurationCuda_h
20 
24 
25 #include <viskores/cont/Logging.h>
27 
29 #include <cuda.h>
31 
32 #include <vector>
33 
34 namespace viskores
35 {
36 namespace cont
37 {
38 namespace internal
39 {
40 
41 template <>
42 class RuntimeDeviceConfiguration<viskores::cont::DeviceAdapterTagCuda>
43  : public viskores::cont::internal::RuntimeDeviceConfigurationBase
44 {
45 public:
46  RuntimeDeviceConfiguration<viskores::cont::DeviceAdapterTagCuda>()
47  {
48  this->CudaDeviceCount = 0;
49  this->CudaProp.clear();
51  if (detector.Exists())
52  {
53  try
54  {
55  int tmp;
56  VISKORES_CUDA_CALL(cudaGetDeviceCount(&tmp));
57  this->CudaDeviceCount = tmp;
58  this->CudaProp.resize(this->CudaDeviceCount);
59  for (int i = 0; i < this->CudaDeviceCount; ++i)
60  {
61  VISKORES_CUDA_CALL(cudaGetDeviceProperties(&this->CudaProp[i], i));
62  }
63  }
64  catch (...)
65  {
67  "Error retrieving CUDA device information. Disabling.");
68  this->CudaDeviceCount = 0;
69  }
70  }
71  }
72 
73  VISKORES_CONT viskores::cont::DeviceAdapterId GetDevice() const override final
74  {
76  }
77 
78  VISKORES_CONT virtual RuntimeDeviceConfigReturnCode SetDeviceInstance(
79  const viskores::Id& value) override final
80  {
81  if (value >= this->CudaDeviceCount)
82  {
85  "Failed to set CudaDeviceInstance, supplied id exceeds the number of available devices: "
86  << value << " >= " << this->CudaDeviceCount);
87  return RuntimeDeviceConfigReturnCode::INVALID_VALUE;
88  }
89  VISKORES_CUDA_CALL(cudaSetDevice(value));
90  return RuntimeDeviceConfigReturnCode::SUCCESS;
91  }
92 
93  VISKORES_CONT virtual RuntimeDeviceConfigReturnCode GetDeviceInstance(
94  viskores::Id& value) const override final
95  {
96  int tmp;
97  VISKORES_CUDA_CALL(cudaGetDevice(&tmp));
98  value = tmp;
99  return RuntimeDeviceConfigReturnCode::SUCCESS;
100  }
101 
102  VISKORES_CONT virtual RuntimeDeviceConfigReturnCode GetMaxDevices(
103  viskores::Id& value) const override final
104  {
105  value = this->CudaDeviceCount;
106  return RuntimeDeviceConfigReturnCode::SUCCESS;
107  }
108 
111  VISKORES_CONT RuntimeDeviceConfigReturnCode
112  GetCudaDeviceProp(std::vector<cudaDeviceProp>& value) const
113  {
114  value = CudaProp;
115  return RuntimeDeviceConfigReturnCode::SUCCESS;
116  }
117 
118 private:
119  std::vector<cudaDeviceProp> CudaProp;
120  viskores::Id CudaDeviceCount;
121 };
122 } // namespace viskores::cont::internal
123 } // namespace viskores::cont
124 } // namespace viskores
125 
126 #endif //viskores_cont_cuda_internal_RuntimeDeviceConfigurationCuda_h
viskores::cont::LogLevel::Error
@ Error
Important but non-fatal errors, such as device fail-over.
VISKORES_CUDA_CALL
#define VISKORES_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:46
VISKORES_THIRDPARTY_POST_INCLUDE
#define VISKORES_THIRDPARTY_POST_INCLUDE
Definition: Configure.h:200
viskores::cont::DeviceAdapterTagCuda
Tag for a device adapter that uses a CUDA capable GPU device.
Definition: DeviceAdapterTagCuda.h:41
viskores::cont::DeviceAdapterRuntimeDetector< viskores::cont::DeviceAdapterTagCuda >
Class providing a CUDA runtime support detector.
Definition: DeviceAdapterRuntimeDetectorCuda.h:46
VISKORES_LOG_F
#define VISKORES_LOG_F(level,...)
Writes a message using printf syntax to the indicated log level.
Definition: Logging.h:217
DeviceAdapterRuntimeDetectorCuda.h
DeviceAdapterTagCuda.h
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
RuntimeDeviceConfiguration.h
viskores::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
VISKORES_LOG_S
#define VISKORES_LOG_S(level,...)
Writes a message using stream syntax to the indicated log level.
Definition: Logging.h:216
Logging.h
Logging utilities.
ErrorCuda.h
VISKORES_THIRDPARTY_PRE_INCLUDE
#define VISKORES_THIRDPARTY_PRE_INCLUDE
Definition: Configure.h:199
viskores::cont::DeviceAdapterRuntimeDetector< viskores::cont::DeviceAdapterTagCuda >::Exists
bool Exists() const
Returns true if the given device adapter is supported on the current machine.