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 =========================================================================*/
31 #ifndef vtkSMPTools_h__
32 #define vtkSMPTools_h__
33 
34 #include "vtkCommonCoreModule.h" // For export macro
35 #include "vtkObject.h"
36 
37 #include "vtkSMPThreadLocal.h" // For Initialized
38 #include "vtkSMPToolsInternal.h"
39 
40 
41 #ifndef DOXYGEN_SHOULD_SKIP_THIS
42 #ifndef __WRAP__
43 namespace vtk
44 {
45 namespace detail
46 {
47 namespace smp
48 {
49 template <typename T>
51 {
52  typedef char (&no_type)[1];
53  typedef char (&yes_type)[2];
54  template <typename U, void (U::*)()> struct V {};
55  template <typename U> static yes_type check(V<U, &U::Initialize>*);
56  template <typename U> static no_type check(...);
57 public:
58  static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
59 };
60 
61 template <typename T>
63 {
64  typedef char (&no_type)[1];
65  typedef char (&yes_type)[2];
66  template <typename U, void (U::*)() const> struct V {};
67  template <typename U> static yes_type check(V<U, &U::Initialize>*);
68  template <typename U> static no_type check(...);
69 public:
70  static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
71 };
72 
73 template <typename Functor, bool Init>
75 
76 template <typename Functor>
77 struct vtkSMPTools_FunctorInternal<Functor, false>
78 {
79  Functor& F;
80  vtkSMPTools_FunctorInternal(Functor& f): F(f) {}
81  void Execute(vtkIdType first, vtkIdType last)
82  {
83  this->F(first, last);
84  }
85  void For(vtkIdType first, vtkIdType last, vtkIdType grain)
86  {
87  vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
88  }
93 };
94 
95 template <typename Functor>
96 struct vtkSMPTools_FunctorInternal<Functor, true>
97 {
98  Functor& F;
100  vtkSMPTools_FunctorInternal(Functor& f): F(f), Initialized(0) {}
101  void Execute(vtkIdType first, vtkIdType last)
102  {
103  unsigned char& inited = this->Initialized.Local();
104  if (!inited)
105  {
106  this->F.Initialize();
107  inited = 1;
108  }
109  this->F(first, last);
110  }
111  void For(vtkIdType first, vtkIdType last, vtkIdType grain)
112  {
113  vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
114  this->F.Reduce();
115  }
120 };
121 
122 template <typename Functor>
124 {
125  static bool const init = vtkSMPTools_Has_Initialize<Functor>::value;
126 public:
128 };
129 
130 template <typename Functor>
131 class vtkSMPTools_Lookup_For<Functor const>
132 {
133  static bool const init = vtkSMPTools_Has_Initialize_const<Functor>::value;
134 public:
136 };
137 } // namespace smp
138 } // namespace detail
139 } // namespace vtk
140 #endif // __WRAP__
141 #endif // DOXYGEN_SHOULD_SKIP_THIS
142 
144 {
145 public:
146 
148 
154  template <typename Functor>
155  static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor& f)
156  {
158  fi.For(first, last, grain);
159  }
161 
163 
169  template <typename Functor>
170  static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const& f)
171  {
173  fi.For(first, last, grain);
174  }
176 
178 
184  template <typename Functor>
185  static void For(vtkIdType first, vtkIdType last, Functor& f)
186  {
187  vtkSMPTools::For(first, last, 0, f);
188  }
190 
192 
198  template <typename Functor>
199  static void For(vtkIdType first, vtkIdType last, Functor const& f)
200  {
201  vtkSMPTools::For(first, last, 0, f);
202  }
204 
212  static void Initialize(int numThreads=0);
213 
215 
219  static int GetEstimatedNumberOfThreads();
220 };
222 
223 #endif
224 // VTK-HeaderTest-Exclude: vtkSMPTools.h
vtkSMPTools_FunctorInternal< Functor const, init > type
Definition: vtkSMPTools.h:135
#define VTKCOMMONCORE_EXPORT
void For(vtkIdType first, vtkIdType last, vtkIdType grain)
Definition: vtkSMPTools.h:111
static void For(vtkIdType first, vtkIdType last, Functor const &f)
Definition: vtkSMPTools.h:199
int vtkIdType
Definition: vtkType.h:275
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:185
static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor &f)
Definition: vtkSMPTools.h:155
A set of parallel (multi-threaded) utility functions.
Definition: vtkSMPTools.h:143
static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const &f)
Definition: vtkSMPTools.h:170
void For(vtkIdType first, vtkIdType last, vtkIdType grain)
Definition: vtkSMPTools.h:85
vtkSMPTools_FunctorInternal< Functor, init > type
Definition: vtkSMPTools.h:127