VTK
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 
36 #ifndef DOXYGEN_SHOULD_SKIP_THIS
37 #ifndef __VTK_WRAP__
38 namespace vtk
39 {
40 namespace detail
41 {
42 namespace smp
43 {
44 template <typename T>
46 {
47  typedef char (&no_type)[1];
48  typedef char (&yes_type)[2];
49  template <typename U, void (U::*)()> struct V {};
50  template <typename U> static yes_type check(V<U, &U::Initialize>*);
51  template <typename U> static no_type check(...);
52 public:
53  static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
54 };
55 
56 template <typename T>
58 {
59  typedef char (&no_type)[1];
60  typedef char (&yes_type)[2];
61  template <typename U, void (U::*)() const> struct V {};
62  template <typename U> static yes_type check(V<U, &U::Initialize>*);
63  template <typename U> static no_type check(...);
64 public:
65  static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
66 };
67 
68 template <typename Functor, bool Init>
70 
71 template <typename Functor>
72 struct vtkSMPTools_FunctorInternal<Functor, false>
73 {
74  Functor& F;
75  vtkSMPTools_FunctorInternal(Functor& f): F(f) {}
76  void Execute(vtkIdType first, vtkIdType last)
77  {
78  this->F(first, last);
79  }
80  void For(vtkIdType first, vtkIdType last, vtkIdType grain)
81  {
82  vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
83  }
88 };
89 
90 template <typename Functor>
91 struct vtkSMPTools_FunctorInternal<Functor, true>
92 {
93  Functor& F;
94  vtkSMPThreadLocal<unsigned char> Initialized;
95  vtkSMPTools_FunctorInternal(Functor& f): F(f), Initialized(0) {}
96  void Execute(vtkIdType first, vtkIdType last)
97  {
98  unsigned char& inited = this->Initialized.Local();
99  if (!inited)
100  {
101  this->F.Initialize();
102  inited = 1;
103  }
104  this->F(first, last);
105  }
106  void For(vtkIdType first, vtkIdType last, vtkIdType grain)
107  {
108  vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
109  this->F.Reduce();
110  }
115 };
116 
117 template <typename Functor>
119 {
120  static bool const init = vtkSMPTools_Has_Initialize<Functor>::value;
121 public:
123 };
124 
125 template <typename Functor>
126 class vtkSMPTools_Lookup_For<Functor const>
127 {
128  static bool const init = vtkSMPTools_Has_Initialize_const<Functor>::value;
129 public:
131 };
132 } // namespace smp
133 } // namespace detail
134 } // namespace vtk
135 #endif // __VTK_WRAP__
136 #endif // DOXYGEN_SHOULD_SKIP_THIS
137 
138 class VTKCOMMONCORE_EXPORT vtkSMPTools
139 {
140 public:
141 
143 
152  template <typename Functor>
153  static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor& f)
154  {
156  fi.For(first, last, grain);
157  }
159 
161 
170  template <typename Functor>
171  static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const& f)
172  {
174  fi.For(first, last, grain);
175  }
177 
187  template <typename Functor>
188  static void For(vtkIdType first, vtkIdType last, Functor& f)
189  {
190  vtkSMPTools::For(first, last, 0, f);
191  }
192 
202  template <typename Functor>
203  static void For(vtkIdType first, vtkIdType last, Functor const& f)
204  {
205  vtkSMPTools::For(first, last, 0, f);
206  }
207 
218  static void Initialize(int numThreads=0);
219 
226  static int GetEstimatedNumberOfThreads();
227 
233  template<typename RandomAccessIterator>
234  static void Sort(RandomAccessIterator begin, RandomAccessIterator end)
235  {
236  vtk::detail::smp::vtkSMPTools_Impl_Sort(begin,end);
237  }
238 
245  template<typename RandomAccessIterator, typename Compare>
246  static void Sort(RandomAccessIterator begin, RandomAccessIterator end,
247  Compare comp)
248  {
249  vtk::detail::smp::vtkSMPTools_Impl_Sort(begin,end,comp);
250  }
251 
252 };
253 
254 #endif
255 // VTK-HeaderTest-Exclude: vtkSMPTools.h
vtkSMPTools_FunctorInternal< Functor const, init > type
Definition: vtkSMPTools.h:130
void For(vtkIdType first, vtkIdType last, vtkIdType grain)
Definition: vtkSMPTools.h:106
static void For(vtkIdType first, vtkIdType last, Functor const &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:203
int vtkIdType
Definition: vtkType.h:345
static void Sort(RandomAccessIterator begin, RandomAccessIterator end)
A convenience method for sorting data.
Definition: vtkSMPTools.h:234
static void For(vtkIdType first, vtkIdType last, Functor &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:188
static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:153
A set of parallel (multi-threaded) utility functions.
Definition: vtkSMPTools.h:138
static void Sort(RandomAccessIterator begin, RandomAccessIterator end, Compare comp)
A convenience method for sorting data.
Definition: vtkSMPTools.h:246
static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:171
void For(vtkIdType first, vtkIdType last, vtkIdType grain)
Definition: vtkSMPTools.h:80
vtkSMPTools_FunctorInternal< Functor, init > type
Definition: vtkSMPTools.h:122