VTK  9.2.6
vtkSMPToolsAPI.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkSMPToolsAPI.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #ifndef vtkSMPToolsAPI_h
17 #define vtkSMPToolsAPI_h
18 
19 #include "vtkCommonCoreModule.h" // For export macro
20 #include "vtkNew.h"
21 #include "vtkObject.h"
22 #include "vtkSMP.h"
23 
24 #include <memory>
25 
27 #if VTK_SMP_ENABLE_SEQUENTIAL
28 #include "SMP/Sequential/vtkSMPToolsImpl.txx"
29 #endif
30 #if VTK_SMP_ENABLE_STDTHREAD
31 #include "SMP/STDThread/vtkSMPToolsImpl.txx"
32 #endif
33 #if VTK_SMP_ENABLE_TBB
34 #include "SMP/TBB/vtkSMPToolsImpl.txx"
35 #endif
36 #if VTK_SMP_ENABLE_OPENMP
37 #include "SMP/OpenMP/vtkSMPToolsImpl.txx"
38 #endif
39 
40 namespace vtk
41 {
42 namespace detail
43 {
44 namespace smp
45 {
46 
48 
49 class VTKCOMMONCORE_EXPORT vtkSMPToolsAPI
50 {
51 public:
52  //--------------------------------------------------------------------------------
53  static vtkSMPToolsAPI& GetInstance();
54 
55  //--------------------------------------------------------------------------------
56  BackendType GetBackendType();
57 
58  //--------------------------------------------------------------------------------
59  const char* GetBackend();
60 
61  //--------------------------------------------------------------------------------
62  bool SetBackend(const char* type);
63 
64  //--------------------------------------------------------------------------------
65  void Initialize(int numThreads = 0);
66 
67  //--------------------------------------------------------------------------------
68  int GetEstimatedNumberOfThreads();
69 
70  //------------------------------------------------------------------------------
71  void SetNestedParallelism(bool isNested);
72 
73  //--------------------------------------------------------------------------------
74  bool GetNestedParallelism();
75 
76  //--------------------------------------------------------------------------------
77  bool IsParallelScope();
78 
79  //--------------------------------------------------------------------------------
80  int GetInternalDesiredNumberOfThread() { return this->DesiredNumberOfThread; }
81 
82  //------------------------------------------------------------------------------
83  template <typename Config, typename T>
84  void LocalScope(Config const& config, T&& lambda)
85  {
86  const Config oldConfig(*this);
87  *this << config;
88  try
89  {
90  lambda();
91  }
92  catch (...)
93  {
94  *this << oldConfig;
95  throw;
96  }
97  *this << oldConfig;
98  }
99 
100  //--------------------------------------------------------------------------------
101  template <typename FunctorInternal>
102  void For(vtkIdType first, vtkIdType last, vtkIdType grain, FunctorInternal& fi)
103  {
104  switch (this->ActivatedBackend)
105  {
107  this->SequentialBackend->For(first, last, grain, fi);
108  break;
110  this->STDThreadBackend->For(first, last, grain, fi);
111  break;
112  case BackendType::TBB:
113  this->TBBBackend->For(first, last, grain, fi);
114  break;
115  case BackendType::OpenMP:
116  this->OpenMPBackend->For(first, last, grain, fi);
117  break;
118  }
119  }
120 
121  //--------------------------------------------------------------------------------
122  template <typename InputIt, typename OutputIt, typename Functor>
123  void Transform(InputIt inBegin, InputIt inEnd, OutputIt outBegin, Functor& transform)
124  {
125  switch (this->ActivatedBackend)
126  {
128  this->SequentialBackend->Transform(inBegin, inEnd, outBegin, transform);
129  break;
131  this->STDThreadBackend->Transform(inBegin, inEnd, outBegin, transform);
132  break;
133  case BackendType::TBB:
134  this->TBBBackend->Transform(inBegin, inEnd, outBegin, transform);
135  break;
136  case BackendType::OpenMP:
137  this->OpenMPBackend->Transform(inBegin, inEnd, outBegin, transform);
138  break;
139  }
140  }
141 
142  //--------------------------------------------------------------------------------
143  template <typename InputIt1, typename InputIt2, typename OutputIt, typename Functor>
144  void Transform(
145  InputIt1 inBegin1, InputIt1 inEnd, InputIt2 inBegin2, OutputIt outBegin, Functor& transform)
146  {
147  switch (this->ActivatedBackend)
148  {
150  this->SequentialBackend->Transform(inBegin1, inEnd, inBegin2, outBegin, transform);
151  break;
153  this->STDThreadBackend->Transform(inBegin1, inEnd, inBegin2, outBegin, transform);
154  break;
155  case BackendType::TBB:
156  this->TBBBackend->Transform(inBegin1, inEnd, inBegin2, outBegin, transform);
157  break;
158  case BackendType::OpenMP:
159  this->OpenMPBackend->Transform(inBegin1, inEnd, inBegin2, outBegin, transform);
160  break;
161  }
162  }
163 
164  //--------------------------------------------------------------------------------
165  template <typename Iterator, typename T>
166  void Fill(Iterator begin, Iterator end, const T& value)
167  {
168  switch (this->ActivatedBackend)
169  {
171  this->SequentialBackend->Fill(begin, end, value);
172  break;
174  this->STDThreadBackend->Fill(begin, end, value);
175  break;
176  case BackendType::TBB:
177  this->TBBBackend->Fill(begin, end, value);
178  break;
179  case BackendType::OpenMP:
180  this->OpenMPBackend->Fill(begin, end, value);
181  break;
182  }
183  }
184 
185  //--------------------------------------------------------------------------------
186  template <typename RandomAccessIterator>
187  void Sort(RandomAccessIterator begin, RandomAccessIterator end)
188  {
189  switch (this->ActivatedBackend)
190  {
192  this->SequentialBackend->Sort(begin, end);
193  break;
195  this->STDThreadBackend->Sort(begin, end);
196  break;
197  case BackendType::TBB:
198  this->TBBBackend->Sort(begin, end);
199  break;
200  case BackendType::OpenMP:
201  this->OpenMPBackend->Sort(begin, end);
202  break;
203  }
204  }
205 
206  //--------------------------------------------------------------------------------
207  template <typename RandomAccessIterator, typename Compare>
208  void Sort(RandomAccessIterator begin, RandomAccessIterator end, Compare comp)
209  {
210  switch (this->ActivatedBackend)
211  {
213  this->SequentialBackend->Sort(begin, end, comp);
214  break;
216  this->STDThreadBackend->Sort(begin, end, comp);
217  break;
218  case BackendType::TBB:
219  this->TBBBackend->Sort(begin, end, comp);
220  break;
221  case BackendType::OpenMP:
222  this->OpenMPBackend->Sort(begin, end, comp);
223  break;
224  }
225  }
226 
227  // disable copying
228  vtkSMPToolsAPI(vtkSMPToolsAPI const&) = delete;
229  void operator=(vtkSMPToolsAPI const&) = delete;
230 
231 private:
232  //--------------------------------------------------------------------------------
233  vtkSMPToolsAPI();
234 
235  //--------------------------------------------------------------------------------
236  void RefreshNumberOfThread();
237 
238  //--------------------------------------------------------------------------------
239  // This operator overload is used to unpack Config parameters and set them
240  // in vtkSMPToolsAPI (e.g `*this << config;`)
241  template <typename Config>
242  vtkSMPToolsAPI& operator<<(Config const& config)
243  {
244  this->Initialize(config.MaxNumberOfThreads);
245  this->SetBackend(config.Backend.c_str());
246  this->SetNestedParallelism(config.NestedParallelism);
247  return *this;
248  }
249 
253  BackendType ActivatedBackend = DefaultBackend;
254 
258  int DesiredNumberOfThread = 0;
259 
263 #if VTK_SMP_ENABLE_SEQUENTIAL
264  std::unique_ptr<vtkSMPToolsImpl<BackendType::Sequential>> SequentialBackend;
265 #else
266  std::unique_ptr<vtkSMPToolsDefaultImpl> SequentialBackend;
267 #endif
268 
272 #if VTK_SMP_ENABLE_STDTHREAD
273  std::unique_ptr<vtkSMPToolsImpl<BackendType::STDThread>> STDThreadBackend;
274 #else
275  std::unique_ptr<vtkSMPToolsDefaultImpl> STDThreadBackend;
276 #endif
277 
281 #if VTK_SMP_ENABLE_TBB
282  std::unique_ptr<vtkSMPToolsImpl<BackendType::TBB>> TBBBackend;
283 #else
284  std::unique_ptr<vtkSMPToolsDefaultImpl> TBBBackend;
285 #endif
286 
290 #if VTK_SMP_ENABLE_OPENMP
291  std::unique_ptr<vtkSMPToolsImpl<BackendType::OpenMP>> OpenMPBackend;
292 #else
293  std::unique_ptr<vtkSMPToolsDefaultImpl> OpenMPBackend;
294 #endif
295 };
296 
297 } // namespace smp
298 } // namespace detail
299 } // namespace vtk
300 
301 #endif
void Transform(InputIt inBegin, InputIt inEnd, OutputIt outBegin, Functor &transform)
void Sort(RandomAccessIterator begin, RandomAccessIterator end, Compare comp)
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
int vtkIdType
Definition: vtkType.h:332
void For(vtkIdType first, vtkIdType last, vtkIdType grain, FunctorInternal &fi)
void Transform(InputIt1 inBegin1, InputIt1 inEnd, InputIt2 inBegin2, OutputIt outBegin, Functor &transform)
void LocalScope(Config const &config, T &&lambda)
void Sort(RandomAccessIterator begin, RandomAccessIterator end)
VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkIndent &o)
void Fill(Iterator begin, Iterator end, const T &value)