VTK-m  2.2
ArrayHandleDiscard.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_ArrayHandleDiscard_h
11 #define vtk_m_cont_ArrayHandleDiscard_h
12 
13 #include <vtkm/TypeTraits.h>
14 #include <vtkm/cont/ArrayHandle.h>
16 
17 #include <type_traits>
18 
19 namespace vtkm
20 {
21 namespace exec
22 {
23 namespace internal
24 {
25 
28 template <typename ValueType_>
29 class ArrayPortalDiscard
30 {
31 public:
32  using ValueType = ValueType_;
33 
36  ArrayPortalDiscard()
37  : NumberOfValues(0)
38  {
39  } // needs to be host and device so that cuda can create lvalue of these
40 
41  VTKM_CONT
42  explicit ArrayPortalDiscard(vtkm::Id numValues)
43  : NumberOfValues(numValues)
44  {
45  }
46 
51  template <class OtherV>
52  VTKM_CONT ArrayPortalDiscard(const ArrayPortalDiscard<OtherV>& src)
53  : NumberOfValues(src.NumberOfValues)
54  {
55  }
56 
58  vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; }
59 
60  ValueType Get(vtkm::Id) const
61  {
62  VTKM_UNREACHABLE("Cannot read from ArrayHandleDiscard.");
64  }
65 
66  VTKM_EXEC
67  void Set(vtkm::Id index, const ValueType&) const
68  {
69  VTKM_ASSERT(index < this->GetNumberOfValues());
70  (void)index;
71  // no-op
72  }
73 
74 private:
75  vtkm::Id NumberOfValues;
76 };
77 
78 } // end namespace internal
79 } // end namespace exec
80 
81 namespace cont
82 {
83 
84 namespace internal
85 {
86 
87 struct VTKM_ALWAYS_EXPORT StorageTagDiscard
88 {
89 };
90 
91 struct VTKM_ALWAYS_EXPORT DiscardMetaData
92 {
93  vtkm::Id NumberOfValues = 0;
94 };
95 
96 template <typename ValueType>
97 class Storage<ValueType, StorageTagDiscard>
98 {
99 public:
100  using WritePortalType = vtkm::exec::internal::ArrayPortalDiscard<ValueType>;
101 
102  // Note that this portal is write-only, so you will probably run into problems if
103  // you actually try to use this read portal.
104  using ReadPortalType = vtkm::exec::internal::ArrayPortalDiscard<ValueType>;
105 
106  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
107  {
108  DiscardMetaData metaData;
109  metaData.NumberOfValues = 0;
110  return vtkm::cont::internal::CreateBuffers(metaData);
111  }
112 
113  VTKM_CONT static void ResizeBuffers(vtkm::Id numValues,
114  const std::vector<vtkm::cont::internal::Buffer>& buffers,
117  {
118  VTKM_ASSERT(numValues >= 0);
119  buffers[0].GetMetaData<DiscardMetaData>().NumberOfValues = numValues;
120  }
121 
122  VTKM_CONT static vtkm::IdComponent GetNumberOfComponentsFlat(
123  const std::vector<vtkm::cont::internal::Buffer>&)
124  {
126  }
127 
128  VTKM_CONT static vtkm::Id GetNumberOfValues(
129  const std::vector<vtkm::cont::internal::Buffer>& buffers)
130  {
131  return buffers[0].GetMetaData<DiscardMetaData>().NumberOfValues;
132  }
133 
134  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>&,
135  const ValueType&,
136  vtkm::Id,
137  vtkm::Id,
139  {
140  // Fill is a NO-OP.
141  }
142 
143  VTKM_CONT static ReadPortalType CreateReadPortal(const std::vector<vtkm::cont::internal::Buffer>&,
146  {
147  throw vtkm::cont::ErrorBadValue("Cannot read from ArrayHandleDiscard.");
148  }
149 
150  VTKM_CONT static WritePortalType CreateWritePortal(
151  const std::vector<vtkm::cont::internal::Buffer>& buffers,
154  {
155  return WritePortalType(GetNumberOfValues(buffers));
156  }
157 };
158 
159 template <typename ValueType_>
160 struct ArrayHandleDiscardTraits
161 {
162  using ValueType = ValueType_;
163  using StorageTag = StorageTagDiscard;
165 };
166 
167 } // end namespace internal
168 
172 template <typename ValueType_>
173 class ArrayHandleDiscard : public internal::ArrayHandleDiscardTraits<ValueType_>::Superclass
174 {
175 public:
179 };
180 
182 template <typename T>
183 struct IsArrayHandleDiscard : std::false_type
184 {
185 };
186 
187 template <typename T>
188 struct IsArrayHandleDiscard<ArrayHandle<T, internal::StorageTagDiscard>> : std::true_type
189 {
190 };
191 
192 } // end namespace cont
193 } // end namespace vtkm
194 
195 #endif // vtk_m_cont_ArrayHandleDiscard_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayHandle.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::Get
auto Get(const vtkm::Tuple< Ts... > &tuple)
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:81
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:243
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::cont::ArrayHandleDiscard
ArrayHandleDiscard is a write-only array that discards all data written to it.
Definition: ArrayHandleDiscard.h:173
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
TypeTraits.h
vtkm::cont::ArrayHandleDiscard::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(typename internal::ArrayHandleDiscardTraits< ValueType_ >::Superclass) >::type Superclass
Definition: ArrayHandleDiscard.h:178
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
vtkm::VecFlat
Treat a Vec or Vec-like object as a flat Vec.
Definition: VecFlat.h:224
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
VTKM_UNREACHABLE
#define VTKM_UNREACHABLE(msg)
VTKM_UNREACHABLE is similar to VTK_ASSUME, with the significant difference that it is not conditional...
Definition: Unreachable.h:31
vtkm::cont::IsArrayHandleDiscard
Helper to determine if an ArrayHandle type is an ArrayHandleDiscard.
Definition: ArrayHandleDiscard.h:183
vtkm::cont::ErrorBadValue
This class is thrown when a VTKm function or method encounters an invalid value that inhibits progres...
Definition: ErrorBadValue.h:25
vtkm::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:17
vtkm::TypeTraits::ZeroInitialization
static T ZeroInitialization()
A static function that returns 0 (or the closest equivalent to it) for the given type.
Definition: TypeTraits.h:77
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
VTKM_SUPPRESS_EXEC_WARNINGS
#define VTKM_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:53
Unreachable.h