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 =========================================================================*/
28 #ifndef vtkSMPTools_h
29 #define vtkSMPTools_h
30 
31 #include "vtkCommonCoreModule.h" // For export macro
32 #include "vtkObject.h"
33 
34 #include "vtkSMPThreadLocal.h" // For Initialized
35 #include "vtkSMPToolsInternal.h"
36 
37 
38 #ifndef DOXYGEN_SHOULD_SKIP_THIS
39 #ifndef __WRAP__
40 namespace vtk
41 {
42 namespace detail
43 {
44 namespace smp
45 {
46 template <typename T>
48 {
49  typedef char (&no_type)[1];
50  typedef char (&yes_type)[2];
51  template <typename U, void (U::*)()> struct V {};
52  template <typename U> static yes_type check(V<U, &U::Initialize>*);
53  template <typename U> static no_type check(...);
54 public:
55  static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
56 };
57 
58 template <typename T>
60 {
61  typedef char (&no_type)[1];
62  typedef char (&yes_type)[2];
63  template <typename U, void (U::*)() const> struct V {};
64  template <typename U> static yes_type check(V<U, &U::Initialize>*);
65  template <typename U> static no_type check(...);
66 public:
67  static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
68 };
69 
70 template <typename Functor, bool Init>
72 
73 template <typename Functor>
74 struct vtkSMPTools_FunctorInternal<Functor, false>
75 {
76  Functor& F;
77  vtkSMPTools_FunctorInternal(Functor& f): F(f) {}
78  void Execute(vtkIdType first, vtkIdType last)
79  {
80  this->F(first, last);
81  }
82  void For(vtkIdType first, vtkIdType last, vtkIdType grain)
83  {
84  vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
85  }
90 };
91 
92 template <typename Functor>
93 struct vtkSMPTools_FunctorInternal<Functor, true>
94 {
95  Functor& F;
97  vtkSMPTools_FunctorInternal(Functor& f): F(f), Initialized(0) {}
98  void Execute(vtkIdType first, vtkIdType last)
99  {
100  unsigned char& inited = this->Initialized.Local();
101  if (!inited)
102  {
103  this->F.Initialize();
104  inited = 1;
105  }
106  this->F(first, last);
107  }
108  void For(vtkIdType first, vtkIdType last, vtkIdType grain)
109  {
110  vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
111  this->F.Reduce();
112  }
117 };
118 
119 template <typename Functor>
121 {
122  static bool const init = vtkSMPTools_Has_Initialize<Functor>::value;
123 public:
125 };
126 
127 template <typename Functor>
128 class vtkSMPTools_Lookup_For<Functor const>
129 {
130  static bool const init = vtkSMPTools_Has_Initialize_const<Functor>::value;
131 public:
133 };
134 } // namespace smp
135 } // namespace detail
136 } // namespace vtk
137 #endif // __WRAP__
138 #endif // DOXYGEN_SHOULD_SKIP_THIS
139 
141 {
142 public:
143 
145 
151  template <typename Functor>
152  static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor& f)
153  {
155  fi.For(first, last, grain);
156  }
158 
160 
166  template <typename Functor>
167  static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const& f)
168  {
170  fi.For(first, last, grain);
171  }
173 
175 
181  template <typename Functor>
182  static void For(vtkIdType first, vtkIdType last, Functor& f)
183  {
184  vtkSMPTools::For(first, last, 0, f);
185  }
187 
189 
195  template <typename Functor>
196  static void For(vtkIdType first, vtkIdType last, Functor const& f)
197  {
198  vtkSMPTools::For(first, last, 0, f);
199  }
201 
209  static void Initialize(int numThreads=0);
210 
215  static int GetEstimatedNumberOfThreads();
216 
218 
221  template<typename RandomAccessIterator>
222  static void Sort(RandomAccessIterator begin, RandomAccessIterator end)
223  {
225  }
227 
229 
233  template<typename RandomAccessIterator, typename Compare>
234  static void Sort(RandomAccessIterator begin, RandomAccessIterator end,
235  Compare comp)
236  {
238  }
240 
241 };
242 
243 #endif
244 // VTK-HeaderTest-Exclude: vtkSMPTools.h
vtkSMPTools_FunctorInternal< Functor const, init > type
Definition: vtkSMPTools.h:132
#define VTKCOMMONCORE_EXPORT
void For(vtkIdType first, vtkIdType last, vtkIdType grain)
Definition: vtkSMPTools.h:108
static void For(vtkIdType first, vtkIdType last, Functor const &f)
Definition: vtkSMPTools.h:196
int vtkIdType
Definition: vtkType.h:247
static void vtkSMPTools_Impl_Sort(RandomAccessIterator begin, RandomAccessIterator end)
static void Sort(RandomAccessIterator begin, RandomAccessIterator end)
Definition: vtkSMPTools.h:222
static void vtkSMPTools_Impl_For(vtkIdType first, vtkIdType last, vtkIdType grain, FunctorInternal &fi)
static void For(vtkIdType first, vtkIdType last, Functor &f)
Definition: vtkSMPTools.h:182
static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor &f)
Definition: vtkSMPTools.h:152
A set of parallel (multi-threaded) utility functions.
Definition: vtkSMPTools.h:140
static void Sort(RandomAccessIterator begin, RandomAccessIterator end, Compare comp)
Definition: vtkSMPTools.h:234
static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const &f)
Definition: vtkSMPTools.h:167
void For(vtkIdType first, vtkIdType last, vtkIdType grain)
Definition: vtkSMPTools.h:82
vtkSMPTools_FunctorInternal< Functor, init > type
Definition: vtkSMPTools.h:124