VTK-m  2.0
RuntimeDeviceConfigurationOpenMP.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_openmp_internal_RuntimeDeviceConfigurationOpenMP_h
11 #define vtk_m_cont_openmp_internal_RuntimeDeviceConfigurationOpenMP_h
12 
15 
16 #include <vtkm/cont/Logging.h>
17 
18 VTKM_THIRDPARTY_PRE_INCLUDE
19 #include <omp.h>
20 VTKM_THIRDPARTY_POST_INCLUDE
21 
22 namespace vtkm
23 {
24 namespace cont
25 {
26 namespace internal
27 {
28 
29 template <>
30 class RuntimeDeviceConfiguration<vtkm::cont::DeviceAdapterTagOpenMP>
31  : public vtkm::cont::internal::RuntimeDeviceConfigurationBase
32 {
33 public:
34  RuntimeDeviceConfiguration<vtkm::cont::DeviceAdapterTagOpenMP>()
35  : HardwareMaxThreads(InitializeHardwareMaxThreads())
36  , CurrentNumThreads(this->HardwareMaxThreads)
37  {
38  }
39 
40  VTKM_CONT vtkm::cont::DeviceAdapterId GetDevice() const override final
41  {
42  return vtkm::cont::DeviceAdapterTagOpenMP{};
43  }
44 
45  VTKM_CONT virtual RuntimeDeviceConfigReturnCode SetThreads(const vtkm::Id& value) override final
46  {
47  if (omp_in_parallel())
48  {
49  VTKM_LOG_S(vtkm::cont::LogLevel::Error, "OpenMP SetThreads: Error, currently in parallel");
50  return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
51  }
52  if (value > 0)
53  {
54  if (value > this->HardwareMaxThreads)
55  {
57  "OpenMP: You may be oversubscribing your CPU cores: "
58  << "process threads available: " << this->HardwareMaxThreads
59  << ", requested threads: " << value);
60  }
61  this->CurrentNumThreads = value;
62  omp_set_num_threads(this->CurrentNumThreads);
63  }
64  else
65  {
66  this->CurrentNumThreads = this->HardwareMaxThreads;
67  omp_set_num_threads(this->CurrentNumThreads);
68  }
69  return RuntimeDeviceConfigReturnCode::SUCCESS;
70  }
71 
72  VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetThreads(vtkm::Id& value) const override final
73  {
74  value = this->CurrentNumThreads;
75  return RuntimeDeviceConfigReturnCode::SUCCESS;
76  }
77 
78  VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetMaxThreads(
79  vtkm::Id& value) const override final
80  {
81  value = this->HardwareMaxThreads;
82  return RuntimeDeviceConfigReturnCode::SUCCESS;
83  }
84 
85 private:
86  VTKM_CONT vtkm::Id InitializeHardwareMaxThreads() const
87  {
88  vtkm::Id count = 0;
89 
90  if (omp_in_parallel())
91  {
92  count = omp_get_num_threads();
93  }
94  else
95  {
96  VTKM_OPENMP_DIRECTIVE(parallel)
97  {
98  VTKM_OPENMP_DIRECTIVE(atomic)
99  ++count;
100  }
101  }
102  return count;
103  }
104 
105  vtkm::Id HardwareMaxThreads;
106  vtkm::Id CurrentNumThreads;
107 };
108 } // namespace vtkm::cont::internal
109 } // namespace vtkm::cont
110 } // namespace vtkm
111 
112 #endif //vtk_m_cont_openmp_internal_RuntimeDeviceConfigurationOpenMP_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::LogLevel::Warn
@ Warn
Less important user errors, such as out-of-bounds parameters.
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
DeviceAdapterTagOpenMP.h
RuntimeDeviceConfiguration.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
VTKM_OPENMP_DIRECTIVE
#define VTKM_OPENMP_DIRECTIVE(directive)
Definition: FunctorsOpenMP.h:37
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:261
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
Logging.h
Logging utilities.