VTK-m  2.2
TaskStrided.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_exec_cuda_internal_TaskStrided_h
11 #define vtk_m_exec_cuda_internal_TaskStrided_h
12 
13 #include <vtkm/exec/TaskBase.h>
14 
16 
17 //Todo: rename this header to TaskInvokeWorkletDetail.h
19 
20 namespace vtkm
21 {
22 namespace exec
23 {
24 namespace cuda
25 {
26 namespace internal
27 {
28 
29 template <typename WType>
30 void TaskStridedSetErrorBuffer(void* w, const vtkm::exec::internal::ErrorMessageBuffer& buffer)
31 {
32  using WorkletType = typename std::remove_cv<WType>::type;
33  WorkletType* const worklet = static_cast<WorkletType*>(w);
34  worklet->SetErrorMessageBuffer(buffer);
35 }
36 
37 class TaskStrided : public vtkm::exec::TaskBase
38 {
39 public:
40  void SetErrorMessageBuffer(const vtkm::exec::internal::ErrorMessageBuffer& buffer)
41  {
42  (void)buffer;
43  this->SetErrorBufferFunction(this->WPtr, buffer);
44  }
45 
46 protected:
47  void* WPtr = nullptr;
48 
49  using SetErrorBufferSignature = void (*)(void*, const vtkm::exec::internal::ErrorMessageBuffer&);
50  SetErrorBufferSignature SetErrorBufferFunction = nullptr;
51 };
52 
53 template <typename WType, typename IType, typename Hints>
54 class TaskStrided1D : public TaskStrided
55 {
56  VTKM_IS_HINT_LIST(Hints);
57 
58 public:
59  TaskStrided1D(const WType& worklet, const IType& invocation)
60  : TaskStrided()
61  , Worklet(worklet)
62  , Invocation(invocation)
63  {
64  this->SetErrorBufferFunction = &TaskStridedSetErrorBuffer<WType>;
65  //Bind the Worklet to void*
66  this->WPtr = (void*)&this->Worklet;
67  }
68 
69  VTKM_EXEC
70  void operator()(vtkm::Id start, vtkm::Id end, vtkm::Id inc) const
71  {
72  for (vtkm::Id index = start; index < end; index += inc)
73  {
74  //Todo: rename this function to DoTaskInvokeWorklet
75  vtkm::exec::internal::detail::DoWorkletInvokeFunctor(
76  this->Worklet,
77  this->Invocation,
78  this->Worklet.GetThreadIndices(index,
79  this->Invocation.OutputToInputMap,
80  this->Invocation.VisitArray,
81  this->Invocation.ThreadToOutputMap,
82  this->Invocation.GetInputDomain()));
83  }
84  }
85 
86 private:
87  typename std::remove_const<WType>::type Worklet;
88  // This is held by by value so that when we transfer the invocation object
89  // over to CUDA it gets properly copied to the device. While we want to
90  // hold by reference to reduce the number of copies, it is not possible
91  // currently.
92  const IType Invocation;
93 };
94 
95 template <typename WType, typename Hints>
96 class TaskStrided1D<WType, vtkm::internal::NullType, Hints> : public TaskStrided
97 {
98  VTKM_IS_HINT_LIST(Hints);
99 
100 public:
101  TaskStrided1D(WType& worklet)
102  : TaskStrided()
103  , Worklet(worklet)
104  {
105  this->SetErrorBufferFunction = &TaskStridedSetErrorBuffer<WType>;
106  //Bind the Worklet to void*
107  this->WPtr = (void*)&this->Worklet;
108  }
109 
110  VTKM_EXEC
111  void operator()(vtkm::Id start, vtkm::Id end, vtkm::Id inc) const
112  {
113  for (vtkm::Id index = start; index < end; index += inc)
114  {
115  this->Worklet(index);
116  }
117  }
118 
119 private:
120  typename std::remove_const<WType>::type Worklet;
121 };
122 
123 template <typename WType, typename IType, typename Hints>
124 class TaskStrided3D : public TaskStrided
125 {
126  VTKM_IS_HINT_LIST(Hints);
127 
128 public:
129  TaskStrided3D(const WType& worklet, const IType& invocation)
130  : TaskStrided()
131  , Worklet(worklet)
132  , Invocation(invocation)
133  {
134  this->SetErrorBufferFunction = &TaskStridedSetErrorBuffer<WType>;
135  //Bind the Worklet to void*
136  this->WPtr = (void*)&this->Worklet;
137  }
138 
139  VTKM_EXEC
140  void operator()(const vtkm::Id3& size,
141  vtkm::Id start,
142  vtkm::Id end,
143  vtkm::Id inc,
144  vtkm::Id j,
145  vtkm::Id k) const
146  {
147  vtkm::Id3 index(start, j, k);
148  auto threadIndex1D = index[0] + size[0] * (index[1] + size[1] * index[2]);
149  for (vtkm::Id i = start; i < end; i += inc, threadIndex1D += inc)
150  {
151  index[0] = i;
152  //Todo: rename this function to DoTaskInvokeWorklet
153  vtkm::exec::internal::detail::DoWorkletInvokeFunctor(
154  this->Worklet,
155  this->Invocation,
156  this->Worklet.GetThreadIndices(threadIndex1D,
157  index,
158  this->Invocation.OutputToInputMap,
159  this->Invocation.VisitArray,
160  this->Invocation.ThreadToOutputMap,
161  this->Invocation.GetInputDomain()));
162  }
163  }
164 
165 private:
166  typename std::remove_const<WType>::type Worklet;
167  // This is held by by value so that when we transfer the invocation object
168  // over to CUDA it gets properly copied to the device. While we want to
169  // hold by reference to reduce the number of copies, it is not possible
170  // currently.
171  const IType Invocation;
172 };
173 
174 template <typename WType, typename Hints>
175 class TaskStrided3D<WType, vtkm::internal::NullType, Hints> : public TaskStrided
176 {
177  VTKM_IS_HINT_LIST(Hints);
178 
179 public:
180  TaskStrided3D(WType& worklet)
181  : TaskStrided()
182  , Worklet(worklet)
183  {
184  this->SetErrorBufferFunction = &TaskStridedSetErrorBuffer<WType>;
185  //Bind the Worklet to void*
186  this->WPtr = (void*)&this->Worklet;
187  }
188 
189  VTKM_EXEC
190  void operator()(const vtkm::Id3& size,
191  vtkm::Id start,
192  vtkm::Id end,
193  vtkm::Id inc,
194  vtkm::Id j,
195  vtkm::Id k) const
196  {
197  vtkm::Id3 index(start, j, k);
198  for (vtkm::Id i = start; i < end; i += inc)
199  {
200  index[0] = i;
201  this->Worklet(index);
202  }
203  }
204 
205 private:
206  typename std::remove_const<WType>::type Worklet;
207 };
208 }
209 }
210 }
211 } // vtkm::exec::cuda::internal
212 
213 #endif //vtk_m_exec_cuda_internal_TaskStrided_h
WorkletInvokeFunctorDetail.h
CudaAllocator.h
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::exec::TaskBase
Base class for all classes that are used to marshal data from the invocation parameters to the user w...
Definition: TaskBase.h:26
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
VTKM_IS_HINT_LIST
#define VTKM_IS_HINT_LIST(T)
Performs a static assert that the given object is a hint list.
Definition: Hints.h:85
vtkm::Vec< vtkm::Id, 3 >
TaskBase.h