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