Viskores  1.0
ArrayGetValues.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_cont_ArrayGetValues_h
19 #define viskores_cont_ArrayGetValues_h
20 
22 
25 
26 #include <initializer_list>
27 #include <vector>
28 
29 namespace viskores
30 {
31 namespace cont
32 {
33 
34 // Work around circular dependancy with UnknownArrayHandle.
35 class UnknownArrayHandle;
36 
37 namespace internal
38 {
39 
40 VISKORES_CONT_EXPORT void ArrayGetValuesImpl(const viskores::cont::UnknownArrayHandle& ids,
43  std::false_type extractComponentInefficient);
44 
45 template <typename IdsArrayHandle, typename DataArrayHandle, typename OutputArrayHandle>
46 void ArrayGetValuesImpl(const IdsArrayHandle& ids,
47  const DataArrayHandle& data,
48  const OutputArrayHandle& output,
49  std::true_type viskoresNotUsed(extractComponentInefficient))
50 {
51  // Fallback implementation. Using UnknownArrayHandle to extract the data would be more
52  // inefficient than simply getting the ReadPortal (which could potentially copy everything
53  // form device to host), so we do that here. The only other alternative would be to write
54  // a custom worklet, but that would require a device compiler, and we are avoiding that for
55  // this header.
56  viskores::Id outputSize = ids.GetNumberOfValues();
57  output.Allocate(outputSize);
58  auto idsPortal = ids.ReadPortal();
59  auto dataPortal = data.ReadPortal();
60  auto outputPortal = output.WritePortal();
61  for (viskores::Id index = 0; index < outputSize; ++index)
62  {
63  outputPortal.Set(index, dataPortal.Get(idsPortal.Get(index)));
64  }
65 }
66 
67 } // namespace internal
68 
126 template <typename SIds, typename T, typename SData, typename SOut>
130 {
131  using DataArrayHandle = viskores::cont::ArrayHandle<T, SData>;
132  using InefficientExtract =
133  viskores::cont::internal::ArrayExtractComponentIsInefficient<DataArrayHandle>;
134  internal::ArrayGetValuesImpl(ids, data, output, InefficientExtract{});
135 }
136 
139 template <typename SIds, typename TIn, typename SData, typename TOut, typename SOut>
144 {
145  // In this specialization, we extract the values from the cast array's source array and
146  // then cast and copy to output.
149  ArrayGetValues(ids, castArray.GetSourceArray(), tempOutput);
150 
151  viskores::Id numExtracted = tempOutput.GetNumberOfValues();
152  output.Allocate(numExtracted);
153  auto inp = tempOutput.ReadPortal();
154  auto outp = output.WritePortal();
155  for (viskores::Id i = 0; i < numExtracted; ++i)
156  {
157  outp.Set(i, static_cast<TOut>(inp.Get(i)));
158  }
159 }
160 
161 template <typename SIds, typename T, typename SData, typename Alloc>
164  std::vector<T, Alloc>& output)
165 {
166  const std::size_t numVals = static_cast<std::size_t>(ids.GetNumberOfValues());
167 
168  // Allocate the vector and toss its data pointer into the array handle.
169  output.resize(numVals);
171  viskores::cont::ArrayGetValues(ids, data, result);
172  // Make sure to pull the data back to control before we dealloc the handle
173  // that wraps the vec memory:
174  result.SyncControlArray();
175 }
176 
177 template <typename SIds, typename T, typename SData>
181 {
182  std::vector<T> result;
183  viskores::cont::ArrayGetValues(ids, data, result);
184  return result;
185 }
186 
187 template <typename T, typename Alloc, typename SData, typename SOut>
188 VISKORES_CONT void ArrayGetValues(const std::vector<viskores::Id, Alloc>& ids,
191 {
193  ArrayGetValues(idsAH, data, output);
194 }
195 
196 template <typename T, typename AllocId, typename SData, typename AllocOut>
197 VISKORES_CONT void ArrayGetValues(const std::vector<viskores::Id, AllocId>& ids,
199  std::vector<T, AllocOut>& output)
200 {
202  ArrayGetValues(idsAH, data, output);
203 }
204 
205 template <typename T, typename Alloc, typename SData>
206 VISKORES_CONT std::vector<T> ArrayGetValues(const std::vector<viskores::Id, Alloc>& ids,
208 {
210  return ArrayGetValues(idsAH, data);
211 }
212 
213 template <typename T, typename SData, typename SOut>
214 VISKORES_CONT void ArrayGetValues(const std::initializer_list<viskores::Id>& ids,
217 {
218  const auto idsAH = viskores::cont::make_ArrayHandle(
219  ids.begin(), static_cast<viskores::Id>(ids.size()), viskores::CopyFlag::Off);
220  ArrayGetValues(idsAH, data, output);
221 }
222 
223 template <typename T, typename SData, typename Alloc>
224 VISKORES_CONT void ArrayGetValues(const std::initializer_list<viskores::Id>& ids,
226  std::vector<T, Alloc>& output)
227 {
228  const auto idsAH = viskores::cont::make_ArrayHandle(
229  ids.begin(), static_cast<viskores::Id>(ids.size()), viskores::CopyFlag::Off);
230  ArrayGetValues(idsAH, data, output);
231 }
232 template <typename T, typename SData>
233 VISKORES_CONT std::vector<T> ArrayGetValues(const std::initializer_list<viskores::Id>& ids,
235 {
236  const auto idsAH = viskores::cont::make_ArrayHandle(
237  ids.begin(), static_cast<viskores::Id>(ids.size()), viskores::CopyFlag::Off);
238  return ArrayGetValues(idsAH, data);
239 }
240 
241 template <typename T, typename SData, typename SOut>
243  const viskores::Id numIds,
246 {
247  const auto idsAH = viskores::cont::make_ArrayHandle(ids, numIds, viskores::CopyFlag::Off);
248  ArrayGetValues(idsAH, data, output);
249 }
250 
251 template <typename T, typename SData, typename Alloc>
253  const viskores::Id numIds,
255  std::vector<T, Alloc>& output)
256 {
257  const auto idsAH = viskores::cont::make_ArrayHandle(ids, numIds, viskores::CopyFlag::Off);
258  ArrayGetValues(idsAH, data, output);
259 }
260 template <typename T, typename SData>
261 VISKORES_CONT std::vector<T> ArrayGetValues(const viskores::Id* ids,
262  const viskores::Id numIds,
264 {
265  const auto idsAH = viskores::cont::make_ArrayHandle(ids, numIds, viskores::CopyFlag::Off);
266  return ArrayGetValues(idsAH, data);
267 }
268 
269 template <typename T, typename S>
271 {
273  auto result = viskores::cont::ArrayGetValues(idAH, data);
274  return result[0];
275 }
276 
277 template <typename T, typename S>
280  T& val)
281 {
282  val = ArrayGetValue(id, data);
283 }
285 }
286 } // namespace viskores::cont
287 
288 #endif //viskores_cont_ArrayGetValues_h
ArrayHandle.h
viskores::cont::ArrayHandle< T, viskores::cont::StorageTagBasic >::ReadPortal
ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:447
viskores::cont::StorageTagCast
Definition: ArrayHandleCast.h:36
viskores::CopyFlag::Off
@ Off
viskores::cont::ArrayHandleCast
Cast the values of an array to the specified type, on demand.
Definition: ArrayHandleCast.h:151
viskoresNotUsed
#define viskoresNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:136
viskores::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:313
viskores::cont::ArrayGetValue
T ArrayGetValue(viskores::Id id, const viskores::cont::ArrayHandle< T, S > &data)
Obtain a small set of values from an ArrayHandle with minimal device transfers.
Definition: ArrayGetValues.h:270
viskores::cont::ArrayHandleCast::GetSourceArray
ArrayHandleType GetSourceArray() const
Returns the ArrayHandle that is being transformed.
Definition: ArrayHandleCast.h:179
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
UnknownArrayHandle.h
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::cont::ArrayHandle::Allocate
void Allocate(viskores::Id numberOfValues, viskores::CopyFlag preserve, viskores::cont::Token &token) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:504
viskores::cont::ArrayHandleBasic
Basic array storage for an array handle.
Definition: ArrayHandleBasic.h:120
viskores::cont::ArrayHandle< T, viskores::cont::StorageTagBasic >::GetNumberOfValues
viskores::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:482
viskores::cont::ArrayHandle::WritePortal
WritePortalType WritePortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:468
viskores::cont::ArrayGetValues
void ArrayGetValues(const viskores::cont::ArrayHandle< viskores::Id, SIds > &ids, const viskores::cont::ArrayHandle< T, SData > &data, viskores::cont::ArrayHandle< T, SOut > &output)
Obtain a small set of values from an ArrayHandle with minimal device transfers.
Definition: ArrayGetValues.h:127
viskores::cont::UnknownArrayHandle
An ArrayHandle of an unknown value type and storage.
Definition: UnknownArrayHandle.h:451
viskores::cont::make_ArrayHandle
viskores::cont::ArrayHandleBasic< T > make_ArrayHandle(const T *array, viskores::Id numberOfValues, viskores::CopyFlag copy)
A convenience function for creating an ArrayHandle from a standard C array.
Definition: ArrayHandleBasic.h:285
viskores_cont_export.h