VTK  9.0.1
vtkSMPTools.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkSMPTools.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 =========================================================================*/
26 #ifndef vtkSMPTools_h
27 #define vtkSMPTools_h
28 
29 #include "vtkCommonCoreModule.h" // For export macro
30 #include "vtkObject.h"
31 
32 #include "vtkSMPThreadLocal.h" // For Initialized
33 #include "vtkSMPToolsInternal.h"
34 
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 #ifndef __VTK_WRAP__
37 namespace vtk
38 {
39 namespace detail
40 {
41 namespace smp
42 {
43 template <typename T>
44 class vtkSMPTools_Has_Initialize
45 {
46  typedef char (&no_type)[1];
47  typedef char (&yes_type)[2];
48  template <typename U, void (U::*)()>
49  struct V
50  {
51  };
52  template <typename U>
53  static yes_type check(V<U, &U::Initialize>*);
54  template <typename U>
55  static no_type check(...);
56 
57 public:
58  static bool const value = sizeof(check<T>(nullptr)) == sizeof(yes_type);
59 };
60 
61 template <typename T>
62 class vtkSMPTools_Has_Initialize_const
63 {
64  typedef char (&no_type)[1];
65  typedef char (&yes_type)[2];
66  template <typename U, void (U::*)() const>
67  struct V
68  {
69  };
70  template <typename U>
71  static yes_type check(V<U, &U::Initialize>*);
72  template <typename U>
73  static no_type check(...);
74 
75 public:
76  static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
77 };
78 
79 template <typename Functor, bool Init>
80 struct vtkSMPTools_FunctorInternal;
81 
82 template <typename Functor>
83 struct vtkSMPTools_FunctorInternal<Functor, false>
84 {
85  Functor& F;
86  vtkSMPTools_FunctorInternal(Functor& f)
87  : F(f)
88  {
89  }
90  void Execute(vtkIdType first, vtkIdType last) { this->F(first, last); }
91  void For(vtkIdType first, vtkIdType last, vtkIdType grain)
92  {
93  vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
94  }
95  vtkSMPTools_FunctorInternal<Functor, false>& operator=(
96  const vtkSMPTools_FunctorInternal<Functor, false>&);
97  vtkSMPTools_FunctorInternal<Functor, false>(const vtkSMPTools_FunctorInternal<Functor, false>&);
98 };
99 
100 template <typename Functor>
101 struct vtkSMPTools_FunctorInternal<Functor, true>
102 {
103  Functor& F;
104  vtkSMPThreadLocal<unsigned char> Initialized;
105  vtkSMPTools_FunctorInternal(Functor& f)
106  : F(f)
107  , Initialized(0)
108  {
109  }
110  void Execute(vtkIdType first, vtkIdType last)
111  {
112  unsigned char& inited = this->Initialized.Local();
113  if (!inited)
114  {
115  this->F.Initialize();
116  inited = 1;
117  }
118  this->F(first, last);
119  }
120  void For(vtkIdType first, vtkIdType last, vtkIdType grain)
121  {
122  vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
123  this->F.Reduce();
124  }
125  vtkSMPTools_FunctorInternal<Functor, true>& operator=(
126  const vtkSMPTools_FunctorInternal<Functor, true>&);
127  vtkSMPTools_FunctorInternal<Functor, true>(const vtkSMPTools_FunctorInternal<Functor, true>&);
128 };
129 
130 template <typename Functor>
131 class vtkSMPTools_Lookup_For
132 {
133  static bool const init = vtkSMPTools_Has_Initialize<Functor>::value;
134 
135 public:
136  typedef vtkSMPTools_FunctorInternal<Functor, init> type;
137 };
138 
139 template <typename Functor>
140 class vtkSMPTools_Lookup_For<Functor const>
141 {
142  static bool const init = vtkSMPTools_Has_Initialize_const<Functor>::value;
143 
144 public:
145  typedef vtkSMPTools_FunctorInternal<Functor const, init> type;
146 };
147 } // namespace smp
148 } // namespace detail
149 } // namespace vtk
150 #endif // __VTK_WRAP__
151 #endif // DOXYGEN_SHOULD_SKIP_THIS
152 
153 class VTKCOMMONCORE_EXPORT vtkSMPTools
154 {
155 public:
157 
166  template <typename Functor>
167  static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor& f)
168  {
170  fi.For(first, last, grain);
171  }
173 
175 
184  template <typename Functor>
185  static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const& f)
186  {
188  fi.For(first, last, grain);
189  }
191 
201  template <typename Functor>
202  static void For(vtkIdType first, vtkIdType last, Functor& f)
203  {
204  vtkSMPTools::For(first, last, 0, f);
205  }
206 
216  template <typename Functor>
217  static void For(vtkIdType first, vtkIdType last, Functor const& f)
218  {
219  vtkSMPTools::For(first, last, 0, f);
220  }
221 
232  static void Initialize(int numThreads = 0);
233 
240  static int GetEstimatedNumberOfThreads();
241 
247  template <typename RandomAccessIterator>
248  static void Sort(RandomAccessIterator begin, RandomAccessIterator end)
249  {
250  vtk::detail::smp::vtkSMPTools_Impl_Sort(begin, end);
251  }
252 
259  template <typename RandomAccessIterator, typename Compare>
260  static void Sort(RandomAccessIterator begin, RandomAccessIterator end, Compare comp)
261  {
262  vtk::detail::smp::vtkSMPTools_Impl_Sort(begin, end, comp);
263  }
264 };
265 
266 #endif
267 // VTK-HeaderTest-Exclude: vtkSMPTools.h
vtkX3D::value
@ value
Definition: vtkX3D.h:226
vtkX3D::type
@ type
Definition: vtkX3D.h:522
vtkIdType
int vtkIdType
Definition: vtkType.h:338
vtkSMPTools
A set of parallel (multi-threaded) utility functions.
Definition: vtkSMPTools.h:153
detail
Definition: vtkGenericDataArrayLookupHelper.h:31
vtkSMPTools::For
static void For(vtkIdType first, vtkIdType last, Functor const &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:217
vtkSMPTools::For
static void For(vtkIdType first, vtkIdType last, Functor &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:202
vtkSMPTools::Sort
static void Sort(RandomAccessIterator begin, RandomAccessIterator end)
A convenience method for sorting data.
Definition: vtkSMPTools.h:248
vtkSMPTools::For
static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:185
vtkSMPTools::For
static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:167
vtkObject.h
vtkSMPTools::Sort
static void Sort(RandomAccessIterator begin, RandomAccessIterator end, Compare comp)
A convenience method for sorting data.
Definition: vtkSMPTools.h:260
vtk
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
Definition: vtkAtomicTypeConcepts.h:21