VTK-m  2.2
ArrayGetValues.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_cont_ArrayGetValues_h
11 #define vtk_m_cont_ArrayGetValues_h
12 
14 
15 #include <vtkm/cont/ArrayHandle.h>
17 
18 #include <initializer_list>
19 #include <vector>
20 
21 namespace vtkm
22 {
23 namespace cont
24 {
25 
26 // Work around circular dependancy with UnknownArrayHandle.
27 class UnknownArrayHandle;
28 
29 namespace internal
30 {
31 
32 VTKM_CONT_EXPORT void ArrayGetValuesImpl(const vtkm::cont::UnknownArrayHandle& ids,
34  const vtkm::cont::UnknownArrayHandle& output,
35  std::false_type extractComponentInefficient);
36 
37 template <typename IdsArrayHandle, typename DataArrayHandle, typename OutputArrayHandle>
38 void ArrayGetValuesImpl(const IdsArrayHandle& ids,
39  const DataArrayHandle& data,
40  const OutputArrayHandle& output,
41  std::true_type vtkmNotUsed(extractComponentInefficient))
42 {
43  // Fallback implementation. Using UnknownArrayHandle to extract the data would be more
44  // inefficient than simply getting the ReadPortal (which could potentially copy everything
45  // form device to host), so we do that here. The only other alternative would be to write
46  // a custom worklet, but that would require a device compiler, and we are avoiding that for
47  // this header.
48  vtkm::Id outputSize = ids.GetNumberOfValues();
49  output.Allocate(outputSize);
50  auto idsPortal = ids.ReadPortal();
51  auto dataPortal = data.ReadPortal();
52  auto outputPortal = output.WritePortal();
53  for (vtkm::Id index = 0; index < outputSize; ++index)
54  {
55  outputPortal.Set(index, dataPortal.Get(idsPortal.Get(index)));
56  }
57 }
58 
59 } // namespace internal
60 
118 template <typename SIds, typename T, typename SData, typename SOut>
122 {
123  using DataArrayHandle = vtkm::cont::ArrayHandle<T, SData>;
124  using InefficientExtract =
125  vtkm::cont::internal::ArrayExtractComponentIsInefficient<DataArrayHandle>;
126  internal::ArrayGetValuesImpl(ids, data, output, InefficientExtract{});
127 }
128 
131 template <typename SIds, typename TIn, typename SData, typename TOut, typename SOut>
136 {
137  // In this specialization, we extract the values from the cast array's source array and
138  // then cast and copy to output.
141  ArrayGetValues(ids, castArray.GetSourceArray(), tempOutput);
142 
143  vtkm::Id numExtracted = tempOutput.GetNumberOfValues();
144  output.Allocate(numExtracted);
145  auto inp = tempOutput.ReadPortal();
146  auto outp = output.WritePortal();
147  for (vtkm::Id i = 0; i < numExtracted; ++i)
148  {
149  outp.Set(i, static_cast<TOut>(inp.Get(i)));
150  }
151 }
152 
153 template <typename SIds, typename T, typename SData, typename Alloc>
156  std::vector<T, Alloc>& output)
157 {
158  const std::size_t numVals = static_cast<std::size_t>(ids.GetNumberOfValues());
159 
160  // Allocate the vector and toss its data pointer into the array handle.
161  output.resize(numVals);
162  auto result = vtkm::cont::make_ArrayHandle(output, vtkm::CopyFlag::Off);
163  vtkm::cont::ArrayGetValues(ids, data, result);
164  // Make sure to pull the data back to control before we dealloc the handle
165  // that wraps the vec memory:
166  result.SyncControlArray();
167 }
168 
169 template <typename SIds, typename T, typename SData>
172 {
173  std::vector<T> result;
174  vtkm::cont::ArrayGetValues(ids, data, result);
175  return result;
176 }
177 
178 template <typename T, typename Alloc, typename SData, typename SOut>
179 VTKM_CONT void ArrayGetValues(const std::vector<vtkm::Id, Alloc>& ids,
182 {
183  const auto idsAH = vtkm::cont::make_ArrayHandle(ids, vtkm::CopyFlag::Off);
184  ArrayGetValues(idsAH, data, output);
185 }
186 
187 template <typename T, typename AllocId, typename SData, typename AllocOut>
188 VTKM_CONT void ArrayGetValues(const std::vector<vtkm::Id, AllocId>& ids,
190  std::vector<T, AllocOut>& output)
191 {
192  const auto idsAH = vtkm::cont::make_ArrayHandle(ids, vtkm::CopyFlag::Off);
193  ArrayGetValues(idsAH, data, output);
194 }
195 
196 template <typename T, typename Alloc, typename SData>
197 VTKM_CONT std::vector<T> ArrayGetValues(const std::vector<vtkm::Id, Alloc>& ids,
199 {
200  const auto idsAH = vtkm::cont::make_ArrayHandle(ids, vtkm::CopyFlag::Off);
201  return ArrayGetValues(idsAH, data);
202 }
203 
204 template <typename T, typename SData, typename SOut>
205 VTKM_CONT void ArrayGetValues(const std::initializer_list<vtkm::Id>& ids,
208 {
209  const auto idsAH = vtkm::cont::make_ArrayHandle(
210  ids.begin(), static_cast<vtkm::Id>(ids.size()), vtkm::CopyFlag::Off);
211  ArrayGetValues(idsAH, data, output);
212 }
213 
214 template <typename T, typename SData, typename Alloc>
215 VTKM_CONT void ArrayGetValues(const std::initializer_list<vtkm::Id>& ids,
217  std::vector<T, Alloc>& output)
218 {
219  const auto idsAH = vtkm::cont::make_ArrayHandle(
220  ids.begin(), static_cast<vtkm::Id>(ids.size()), vtkm::CopyFlag::Off);
221  ArrayGetValues(idsAH, data, output);
222 }
223 template <typename T, typename SData>
224 VTKM_CONT std::vector<T> ArrayGetValues(const std::initializer_list<vtkm::Id>& ids,
226 {
227  const auto idsAH = vtkm::cont::make_ArrayHandle(
228  ids.begin(), static_cast<vtkm::Id>(ids.size()), vtkm::CopyFlag::Off);
229  return ArrayGetValues(idsAH, data);
230 }
231 
232 template <typename T, typename SData, typename SOut>
234  const vtkm::Id numIds,
237 {
238  const auto idsAH = vtkm::cont::make_ArrayHandle(ids, numIds, vtkm::CopyFlag::Off);
239  ArrayGetValues(idsAH, data, output);
240 }
241 
242 template <typename T, typename SData, typename Alloc>
244  const vtkm::Id numIds,
246  std::vector<T, Alloc>& output)
247 {
248  const auto idsAH = vtkm::cont::make_ArrayHandle(ids, numIds, vtkm::CopyFlag::Off);
249  ArrayGetValues(idsAH, data, output);
250 }
251 template <typename T, typename SData>
252 VTKM_CONT std::vector<T> ArrayGetValues(const vtkm::Id* ids,
253  const vtkm::Id numIds,
255 {
256  const auto idsAH = vtkm::cont::make_ArrayHandle(ids, numIds, vtkm::CopyFlag::Off);
257  return ArrayGetValues(idsAH, data);
258 }
259 
260 template <typename T, typename S>
262 {
263  const auto idAH = vtkm::cont::make_ArrayHandle(&id, 1, vtkm::CopyFlag::Off);
264  auto result = vtkm::cont::ArrayGetValues(idAH, data);
265  return result[0];
266 }
267 
268 template <typename T, typename S>
270 {
271  val = ArrayGetValue(id, data);
272 }
274 }
275 } // namespace vtkm::cont
276 
277 #endif //vtk_m_cont_ArrayGetValues_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayHandle.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::UnknownArrayHandle
An ArrayHandle of an unknown value type and storage.
Definition: UnknownArrayHandle.h:430
vtkm::cont::ArrayHandle< T, vtkm::cont::StorageTagBasic >::GetNumberOfValues
vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:468
vtkm::cont::StorageTagCast
Definition: ArrayHandleCast.h:28
UnknownArrayHandle.h
vtkm::cont::ArrayHandleCast::GetSourceArray
ArrayHandleType GetSourceArray() const
Returns the ArrayHandle that is being transformed.
Definition: ArrayHandleCast.h:169
VTKM_CONT_EXPORT
#define VTKM_CONT_EXPORT
Definition: vtkm_cont_export.h:44
vtkm_cont_export.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
vtkm::cont::ArrayHandle< T, vtkm::cont::StorageTagBasic >::ReadPortal
ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:433
vtkm::cont::ArrayGetValues
void ArrayGetValues(const vtkm::cont::ArrayHandle< vtkm::Id, SIds > &ids, const vtkm::cont::ArrayHandle< T, SData > &data, vtkm::cont::ArrayHandle< T, SOut > &output)
Obtain a small set of values from an ArrayHandle with minimal device transfers.
Definition: ArrayGetValues.h:119
vtkm::CopyFlag::Off
@ Off
vtkm::cont::ArrayHandleCast
Cast the values of an array to the specified type, on demand.
Definition: ArrayHandleCast.h:141
vtkm::cont::ArrayHandle::Allocate
void Allocate(vtkm::Id numberOfValues, vtkm::CopyFlag preserve, vtkm::cont::Token &token) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:490
vtkm::cont::ArrayHandleBasic
Basic array storage for an array handle.
Definition: ArrayHandleBasic.h:111
vtkm::cont::make_ArrayHandle
vtkm::cont::ArrayHandleBasic< T > make_ArrayHandle(const T *array, vtkm::Id numberOfValues, vtkm::CopyFlag copy)
A convenience function for creating an ArrayHandle from a standard C array.
Definition: ArrayHandleBasic.h:270
vtkm::cont::ArrayHandle::WritePortal
WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:454
vtkm::cont::ArrayGetValue
T ArrayGetValue(vtkm::Id id, const vtkm::cont::ArrayHandle< T, S > &data)
Obtain a small set of values from an ArrayHandle with minimal device transfers.
Definition: ArrayGetValues.h:261