VTK-m  2.0
RuntimeDeviceConfigurationTBB.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_tbb_internal_RuntimeDeviceConfigurationTBB_h
11 #define vtk_m_cont_tbb_internal_RuntimeDeviceConfigurationTBB_h
12 
15 
16 VTKM_THIRDPARTY_PRE_INCLUDE
17 #if TBB_VERSION_MAJOR >= 2020
18 #define TBB_PREVIEW_GLOBAL_CONTROL
19 #include <tbb/global_control.h>
20 #include <tbb/task_arena.h>
21 #else
22 #include <tbb/tbb.h>
23 #endif
24 VTKM_THIRDPARTY_POST_INCLUDE
25 
26 #include <memory>
27 
28 namespace vtkm
29 {
30 namespace cont
31 {
32 namespace internal
33 {
34 
35 template <>
36 class RuntimeDeviceConfiguration<vtkm::cont::DeviceAdapterTagTBB>
37  : public vtkm::cont::internal::RuntimeDeviceConfigurationBase
38 {
39 public:
40  VTKM_CONT
41  RuntimeDeviceConfiguration<vtkm::cont::DeviceAdapterTagTBB>()
42  :
43 #if TBB_VERSION_MAJOR >= 2020
44  HardwareMaxThreads(::tbb::task_arena{}.max_concurrency())
45  ,
46 #else
47  HardwareMaxThreads(::tbb::task_scheduler_init::default_num_threads())
48  ,
49 #endif
50  CurrentNumThreads(this->HardwareMaxThreads)
51  {
52  }
53 
54  VTKM_CONT vtkm::cont::DeviceAdapterId GetDevice() const override final
55  {
56  return vtkm::cont::DeviceAdapterTagTBB{};
57  }
58 
59  VTKM_CONT virtual RuntimeDeviceConfigReturnCode SetThreads(const vtkm::Id& value) override final
60  {
61  this->CurrentNumThreads = value > 0 ? value : this->HardwareMaxThreads;
62 #if TBB_VERSION_MAJOR >= 2020
63  GlobalControl.reset(new ::tbb::global_control(::tbb::global_control::max_allowed_parallelism,
64  this->CurrentNumThreads));
65 #else
66  TaskSchedulerInit.reset(
67  new ::tbb::task_scheduler_init(static_cast<int>(this->CurrentNumThreads)));
68 #endif
69  return RuntimeDeviceConfigReturnCode::SUCCESS;
70  }
71 
72  VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetThreads(vtkm::Id& value) const override final
73  {
74 #if TBB_VERSION_MAJOR >= 2020
75  value = ::tbb::global_control::active_value(::tbb::global_control::max_allowed_parallelism);
76 #else
77  value = this->CurrentNumThreads;
78 #endif
79  return RuntimeDeviceConfigReturnCode::SUCCESS;
80  }
81 
82  VTKM_CONT virtual RuntimeDeviceConfigReturnCode GetMaxThreads(
83  vtkm::Id& value) const override final
84  {
85  value = this->HardwareMaxThreads;
86  return RuntimeDeviceConfigReturnCode::SUCCESS;
87  }
88 
89 private:
90 #if TBB_VERSION_MAJOR >= 2020
91  std::unique_ptr<::tbb::global_control> GlobalControl;
92 #else
93  std::unique_ptr<::tbb::task_scheduler_init> TaskSchedulerInit;
94 #endif
95  vtkm::Id HardwareMaxThreads;
96  vtkm::Id CurrentNumThreads;
97 };
98 } // namespace vktm::cont::internal
99 } // namespace vtkm::cont
100 } // namespace vtkm
101 
102 #endif //vtk_m_cont_tbb_internal_RuntimeDeviceConfigurationTBB_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
DeviceAdapterTagTBB.h
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
RuntimeDeviceConfiguration.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52