File: vtkSMPTools2.h

package info (click to toggle)
vtk6 6.3.0%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 118,532 kB
  • ctags: 138,251
  • sloc: cpp: 1,443,749; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,127; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 471; makefile: 95; objc: 17
file content (245 lines) | stat: -rw-r--r-- 6,917 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkSMPTools.h

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#ifndef vtkSMPTools2_h__
#define vtkSMPTools2_h__

#include "vtkCommonCoreModule.h" // For export macro
#include "vtkObject.h"

#include "vtkSMPThreadLocal.h" // For Initialized

#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
#include <tbb/partitioner.h>

class vtkSMPTools;
class vtkParallelTree;

#ifndef DOXYGEN_SHOULD_SKIP_THIS
#ifndef __WRAP__
namespace vtk
{
namespace detail
{
namespace smp
{
template <typename Tree, typename Functor>
class TaskTraverse : public tbb::task {
    const Tree* tree;
    Functor& functor;
    const int level;
    const vtkIdType index, BranchingFactor;

  public:
    TaskTraverse(
        const Tree* t, Functor& f,
        int l, vtkIdType i, vtkIdType b)
          : tree(t), functor(f),
            level(l), index(i),
            BranchingFactor(b)
    {
    }

    ~TaskTraverse()
    {
    }

    tbb::task* execute()
    {
    functor.TestInit();
    if ( tree->TraverseNode(index, level, functor.F) )
      {
      int l = level + 1;
      this->set_ref_count(BranchingFactor + 1);
      TaskTraverse* t;
      for ( vtkIdType i = index * BranchingFactor + 1, j = 0;
            j < BranchingFactor; ++i, ++j )
        {
        t = new(this->allocate_child())
          TaskTraverse(tree, functor, l, i, BranchingFactor);
        tbb::task::spawn(*t);
        }
      this->wait_for_all();
      }
    return 0;
    }

};

template <typename T>
class vtkSMPTools2_Has_Initialize
{
  typedef char (&no_type)[1];
  typedef char (&yes_type)[2];
  template <typename U, void (U::*)()> struct V {};
  template <typename U> static yes_type check(V<U, &U::Initialize>*);
  template <typename U> static no_type check(...);
public:
  static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
};

template <typename T>
class vtkSMPTools2_Has_Initialize_const
{
  typedef char (&no_type)[1];
  typedef char (&yes_type)[2];
  template <typename U, void (U::*)() const> struct V {};
  template <typename U> static yes_type check(V<U, &U::Initialize>*);
  template <typename U> static no_type check(...);
public:
  static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
};

template <typename Tree, typename FunctorInternal>
static void vtkSMPTools2_Impl_Traverse(int level, vtkIdType bf, const Tree* t, FunctorInternal& fi)
{
  TaskTraverse<Tree,FunctorInternal>* task = new(tbb::task::allocate_root())
    TaskTraverse<Tree,FunctorInternal>(t, fi, level, 0, bf);
  tbb::task::spawn_root_and_wait(*task);
}

template <typename T, typename Functor>
class vtkSMPTools2_Has_Traverse_Node_const
{
  typedef char (&no_type)[1];
  typedef char (&yes_type)[2];
  template <typename U, int (U::*)(vtkIdType, int, Functor&) const> struct V {};
  template <typename U> static yes_type check(V<U, &U::TraverseNode>*);
  template <typename U> static no_type check(...);
public:
  static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
};

template <typename Tree, bool Init>
struct vtkSMPTools2_FunctorTraverse;

template <typename Tree>
struct vtkSMPTools2_FunctorTraverse<Tree, true>
{
  public:
    typedef const Tree* actual_type;
};

template <typename T, typename Functor>
class vtkSMPTools2_Lookup_Traverse
{
  static bool const init = vtkSMPTools2_Has_Traverse_Node_const<T, Functor>::value;
public:
  typedef vtkSMPTools2_FunctorTraverse<T, init> type;
};

template <typename Functor, bool Init>
struct vtkSMPTools2_FunctorInternal;

template <typename Functor>
struct vtkSMPTools2_FunctorInternal<Functor, false>
{
  Functor& F;
  vtkSMPTools2_FunctorInternal(Functor& f): F(f) {}
  void Execute(vtkIdType first, vtkIdType last)
  {
    this->F(first, last);
  }
  void TestInit()
  {
  }
  template <typename Tree>
  void Traverse(int level, vtkIdType bf, typename vtkSMPTools2_Lookup_Traverse<Tree const, Functor>::type::actual_type t)
  {
    vtk::detail::smp::vtkSMPTools2_Impl_Traverse(level, bf, t, *this);
  }
  vtkSMPTools2_FunctorInternal<Functor, false>& operator=(
    const vtkSMPTools2_FunctorInternal<Functor, false>&);
  vtkSMPTools2_FunctorInternal<Functor, false>(
    const vtkSMPTools2_FunctorInternal<Functor, false>&);
};

template <typename Functor>
struct vtkSMPTools2_FunctorInternal<Functor, true>
{
  Functor& F;
  vtkSMPThreadLocal<unsigned char> Initialized;
  vtkSMPTools2_FunctorInternal(Functor& f): F(f), Initialized(0) {}
  void Execute(vtkIdType first, vtkIdType last)
  {
    this->TestInit();
    this->F(first, last);
  }
  void TestInit()
  {
    unsigned char& inited = this->Initialized.Local();
    if (!inited)
      {
      this->F.Initialize();
      inited = 1;
      }
  }
  template <typename Tree>
  void Traverse(int level, vtkIdType bf, typename vtkSMPTools2_Lookup_Traverse<Tree const, Functor>::type::actual_type t)
  {
    vtk::detail::smp::vtkSMPTools2_Impl_Traverse(level, bf, t, *this);
    this->F.Reduce();
  }
  vtkSMPTools2_FunctorInternal<Functor, true>& operator=(
    const vtkSMPTools2_FunctorInternal<Functor, true>&);
  vtkSMPTools2_FunctorInternal<Functor, true>(
    const vtkSMPTools2_FunctorInternal<Functor, true>&);
};

template <typename Functor>
class vtkSMPTools2_Lookup_For
{
  static bool const init = vtkSMPTools2_Has_Initialize<Functor>::value;
public:
  typedef vtkSMPTools2_FunctorInternal<Functor, init> type;
};

template <typename Functor>
class vtkSMPTools2_Lookup_For<Functor const>
{
  static bool const init = vtkSMPTools2_Has_Initialize_const<Functor>::value;
public:
  typedef vtkSMPTools2_FunctorInternal<Functor const, init> type;
};
} // namespace smp
} // namespace detail
} // namespace vtk
#endif // __WRAP__
#endif // DOXYGEN_SHOULD_SKIP_THIS

class VTKCOMMONCORE_EXPORT vtkSMPTools2
{
public:

  // Description:
  // Traverse a tree in parallel. The tree has to be parallel aware.
  template <typename Tree, typename Functor>
  static void Traverse(int level, vtkIdType bf, const Tree* t, Functor& f)
  {
    typename vtk::detail::smp::vtkSMPTools2_Lookup_For<Functor>::type fi(f);
    fi.template Traverse<Tree const>(level, bf, t);
  }

  template <typename Tree, typename Functor>
  static void Traverse(int level, vtkIdType bf, const Tree* t, Functor const& f)
  {
    typename vtk::detail::smp::vtkSMPTools2_Lookup_For<Functor const>::type fi(f);
    fi.template Traverse<Tree>(level, bf, t);
  }
};

#endif
// VTK-HeaderTest-Exclude: vtkSMPTools2.h