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 =========================================================================*/
32 #ifndef vtkSMPTools_h
33 #define vtkSMPTools_h
34 
35 #include "vtkCommonCoreModule.h" // For export macro
36 #include "vtkObject.h"
37 
38 #include "vtkSMPThreadLocal.h" // For Initialized
39 #include "vtkSMPToolsInternal.h"
40 
41 
42 #ifndef DOXYGEN_SHOULD_SKIP_THIS
43 #ifndef __VTK_WRAP__
44 namespace vtk
45 {
46 namespace detail
47 {
48 namespace smp
49 {
50 template <typename T>
51 class vtkSMPTools_Has_Initialize
52 {
53  typedef char (&no_type)[1];
54  typedef char (&yes_type)[2];
55  template <typename U, void (U::*)()> struct V {};
56  template <typename U> static yes_type check(V<U, &U::Initialize>*);
57  template <typename U> static no_type check(...);
58 public:
59  static bool const value = sizeof(check<T>(nullptr)) == sizeof(yes_type);
60 };
61 
62 template <typename T>
63 class vtkSMPTools_Has_Initialize_const
64 {
65  typedef char (&no_type)[1];
66  typedef char (&yes_type)[2];
67  template <typename U, void (U::*)() const> struct V {};
68  template <typename U> static yes_type check(V<U, &U::Initialize>*);
69  template <typename U> static no_type check(...);
70 public:
71  static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
72 };
73 
74 template <typename Functor, bool Init>
75 struct vtkSMPTools_FunctorInternal;
76 
77 template <typename Functor>
78 struct vtkSMPTools_FunctorInternal<Functor, false>
79 {
80  Functor& F;
81  vtkSMPTools_FunctorInternal(Functor& f): F(f) {}
82  void Execute(vtkIdType first, vtkIdType last)
83  {
84  this->F(first, last);
85  }
86  void For(vtkIdType first, vtkIdType last, vtkIdType grain)
87  {
88  vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
89  }
90  vtkSMPTools_FunctorInternal<Functor, false>& operator=(
91  const vtkSMPTools_FunctorInternal<Functor, false>&);
92  vtkSMPTools_FunctorInternal<Functor, false>(
93  const vtkSMPTools_FunctorInternal<Functor, false>&);
94 };
95 
96 template <typename Functor>
97 struct vtkSMPTools_FunctorInternal<Functor, true>
98 {
99  Functor& F;
100  vtkSMPThreadLocal<unsigned char> Initialized;
101  vtkSMPTools_FunctorInternal(Functor& f): F(f), Initialized(0) {}
102  void Execute(vtkIdType first, vtkIdType last)
103  {
104  unsigned char& inited = this->Initialized.Local();
105  if (!inited)
106  {
107  this->F.Initialize();
108  inited = 1;
109  }
110  this->F(first, last);
111  }
112  void For(vtkIdType first, vtkIdType last, vtkIdType grain)
113  {
114  vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
115  this->F.Reduce();
116  }
117  vtkSMPTools_FunctorInternal<Functor, true>& operator=(
118  const vtkSMPTools_FunctorInternal<Functor, true>&);
119  vtkSMPTools_FunctorInternal<Functor, true>(
120  const vtkSMPTools_FunctorInternal<Functor, true>&);
121 };
122 
123 template <typename Functor>
124 class vtkSMPTools_Lookup_For
125 {
126  static bool const init = vtkSMPTools_Has_Initialize<Functor>::value;
127 public:
128  typedef vtkSMPTools_FunctorInternal<Functor, init> type;
129 };
130 
131 template <typename Functor>
132 class vtkSMPTools_Lookup_For<Functor const>
133 {
134  static bool const init = vtkSMPTools_Has_Initialize_const<Functor>::value;
135 public:
136  typedef vtkSMPTools_FunctorInternal<Functor const, init> type;
137 };
138 } // namespace smp
139 } // namespace detail
140 } // namespace vtk
141 #endif // __VTK_WRAP__
142 #endif // DOXYGEN_SHOULD_SKIP_THIS
143 
144 class VTKCOMMONCORE_EXPORT vtkSMPTools
145 {
146 public:
147 
149 
158  template <typename Functor>
159  static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor& f)
160  {
162  fi.For(first, last, grain);
163  }
165 
167 
176  template <typename Functor>
177  static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const& f)
178  {
180  fi.For(first, last, grain);
181  }
183 
193  template <typename Functor>
194  static void For(vtkIdType first, vtkIdType last, Functor& f)
195  {
196  vtkSMPTools::For(first, last, 0, f);
197  }
198 
208  template <typename Functor>
209  static void For(vtkIdType first, vtkIdType last, Functor const& f)
210  {
211  vtkSMPTools::For(first, last, 0, f);
212  }
213 
224  static void Initialize(int numThreads=0);
225 
232  static int GetEstimatedNumberOfThreads();
233 
239  template<typename RandomAccessIterator>
240  static void Sort(RandomAccessIterator begin, RandomAccessIterator end)
241  {
242  vtk::detail::smp::vtkSMPTools_Impl_Sort(begin,end);
243  }
244 
251  template<typename RandomAccessIterator, typename Compare>
252  static void Sort(RandomAccessIterator begin, RandomAccessIterator end,
253  Compare comp)
254  {
255  vtk::detail::smp::vtkSMPTools_Impl_Sort(begin,end,comp);
256  }
257 
258 };
259 
260 #endif
261 // VTK-HeaderTest-Exclude: vtkSMPTools.h
static void For(vtkIdType first, vtkIdType last, Functor const &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:209
int vtkIdType
Definition: vtkType.h:347
static void Sort(RandomAccessIterator begin, RandomAccessIterator end)
A convenience method for sorting data.
Definition: vtkSMPTools.h:240
static void For(vtkIdType first, vtkIdType last, Functor &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:194
static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:159
A set of parallel (multi-threaded) utility functions.
Definition: vtkSMPTools.h:144
static void Sort(RandomAccessIterator begin, RandomAccessIterator end, Compare comp)
A convenience method for sorting data.
Definition: vtkSMPTools.h:252
static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:177