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__
44namespace vtk
45{
46namespace detail
47{
48namespace smp
49{
50template <typename T>
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(...);
58public:
59 static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
60};
61
62template <typename T>
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(...);
70public:
71 static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
72};
73
74template <typename Functor, bool Init>
76
77template <typename Functor>
78struct 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 }
94};
95
96template <typename Functor>
97struct 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 }
121};
122
123template <typename Functor>
125{
126 static bool const init = vtkSMPTools_Has_Initialize<Functor>::value;
127public:
129};
130
131template <typename Functor>
132class vtkSMPTools_Lookup_For<Functor const>
133{
135public:
137};
138} // namespace smp
139} // namespace detail
140} // namespace vtk
141#endif // __VTK_WRAP__
142#endif // DOXYGEN_SHOULD_SKIP_THIS
143
144class VTKCOMMONCORE_EXPORT vtkSMPTools
145{
146public:
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
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
A set of parallel (multi-threaded) utility functions.
Definition: vtkSMPTools.h:145
static void Initialize(int numThreads=0)
Initialize the underlying libraries for execution.
static int GetEstimatedNumberOfThreads()
Get the estimated number of threads being used by the backend.
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 const &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:209
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 const &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:177
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 &f)
Execute a for operation in parallel.
Definition: vtkSMPTools.h:159
vtkSMPTools_FunctorInternal< Functor const, init > type
Definition: vtkSMPTools.h:136
vtkSMPTools_FunctorInternal< Functor, init > type
Definition: vtkSMPTools.h:128
vtkSMPTools_FunctorInternal< Functor, false > & operator=(const vtkSMPTools_FunctorInternal< Functor, false > &)
void For(vtkIdType first, vtkIdType last, vtkIdType grain)
Definition: vtkSMPTools.h:86
vtkSMPTools_FunctorInternal< Functor, true > & operator=(const vtkSMPTools_FunctorInternal< Functor, true > &)
void For(vtkIdType first, vtkIdType last, vtkIdType grain)
Definition: vtkSMPTools.h:112
int vtkIdType
Definition: vtkType.h:287