VTK-m  2.0
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>
54 class TaskStrided1D : public TaskStrided
55 {
56 public:
57  TaskStrided1D(const WType& worklet, const IType& invocation)
58  : TaskStrided()
59  , Worklet(worklet)
60  , Invocation(invocation)
61  {
62  this->SetErrorBufferFunction = &TaskStridedSetErrorBuffer<WType>;
63  //Bind the Worklet to void*
64  this->WPtr = (void*)&this->Worklet;
65  }
66 
67  VTKM_EXEC
68  void operator()(vtkm::Id start, vtkm::Id end, vtkm::Id inc) const
69  {
70  for (vtkm::Id index = start; index < end; index += inc)
71  {
72  //Todo: rename this function to DoTaskInvokeWorklet
73  vtkm::exec::internal::detail::DoWorkletInvokeFunctor(
74  this->Worklet,
75  this->Invocation,
76  this->Worklet.GetThreadIndices(index,
77  this->Invocation.OutputToInputMap,
78  this->Invocation.VisitArray,
79  this->Invocation.ThreadToOutputMap,
80  this->Invocation.GetInputDomain()));
81  }
82  }
83 
84 private:
85  typename std::remove_const<WType>::type Worklet;
86  // This is held by by value so that when we transfer the invocation object
87  // over to CUDA it gets properly copied to the device. While we want to
88  // hold by reference to reduce the number of copies, it is not possible
89  // currently.
90  const IType Invocation;
91 };
92 
93 template <typename WType>
94 class TaskStrided1D<WType, vtkm::internal::NullType> : public TaskStrided
95 {
96 public:
97  TaskStrided1D(WType& worklet)
98  : TaskStrided()
99  , Worklet(worklet)
100  {
101  this->SetErrorBufferFunction = &TaskStridedSetErrorBuffer<WType>;
102  //Bind the Worklet to void*
103  this->WPtr = (void*)&this->Worklet;
104  }
105 
106  VTKM_EXEC
107  void operator()(vtkm::Id start, vtkm::Id end, vtkm::Id inc) const
108  {
109  for (vtkm::Id index = start; index < end; index += inc)
110  {
111  this->Worklet(index);
112  }
113  }
114 
115 private:
116  typename std::remove_const<WType>::type Worklet;
117 };
118 
119 template <typename WType, typename IType>
120 class TaskStrided3D : public TaskStrided
121 {
122 public:
123  TaskStrided3D(const WType& worklet, const IType& invocation)
124  : TaskStrided()
125  , Worklet(worklet)
126  , Invocation(invocation)
127  {
128  this->SetErrorBufferFunction = &TaskStridedSetErrorBuffer<WType>;
129  //Bind the Worklet to void*
130  this->WPtr = (void*)&this->Worklet;
131  }
132 
133  VTKM_EXEC
134  void operator()(const vtkm::Id3& size,
135  vtkm::Id start,
136  vtkm::Id end,
137  vtkm::Id inc,
138  vtkm::Id j,
139  vtkm::Id k) const
140  {
141  vtkm::Id3 index(start, j, k);
142  auto threadIndex1D = index[0] + size[0] * (index[1] + size[1] * index[2]);
143  for (vtkm::Id i = start; i < end; i += inc, threadIndex1D += inc)
144  {
145  index[0] = i;
146  //Todo: rename this function to DoTaskInvokeWorklet
147  vtkm::exec::internal::detail::DoWorkletInvokeFunctor(
148  this->Worklet,
149  this->Invocation,
150  this->Worklet.GetThreadIndices(threadIndex1D,
151  index,
152  this->Invocation.OutputToInputMap,
153  this->Invocation.VisitArray,
154  this->Invocation.ThreadToOutputMap,
155  this->Invocation.GetInputDomain()));
156  }
157  }
158 
159 private:
160  typename std::remove_const<WType>::type Worklet;
161  // This is held by by value so that when we transfer the invocation object
162  // over to CUDA it gets properly copied to the device. While we want to
163  // hold by reference to reduce the number of copies, it is not possible
164  // currently.
165  const IType Invocation;
166 };
167 
168 template <typename WType>
169 class TaskStrided3D<WType, vtkm::internal::NullType> : public TaskStrided
170 {
171 public:
172  TaskStrided3D(WType& worklet)
173  : TaskStrided()
174  , Worklet(worklet)
175  {
176  this->SetErrorBufferFunction = &TaskStridedSetErrorBuffer<WType>;
177  //Bind the Worklet to void*
178  this->WPtr = (void*)&this->Worklet;
179  }
180 
181  VTKM_EXEC
182  void operator()(const vtkm::Id3& size,
183  vtkm::Id start,
184  vtkm::Id end,
185  vtkm::Id inc,
186  vtkm::Id j,
187  vtkm::Id k) const
188  {
189  vtkm::Id3 index(start, j, k);
190  for (vtkm::Id i = start; i < end; i += inc)
191  {
192  index[0] = i;
193  this->Worklet(index);
194  }
195  }
196 
197 private:
198  typename std::remove_const<WType>::type Worklet;
199 };
200 }
201 }
202 }
203 } // vtkm::exec::cuda::internal
204 
205 #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::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
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:24
vtkm::Vec< vtkm::Id, 3 >
TaskBase.h