VTK-m  2.0
ArrayHandleBitField.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_ArrayHandleBitField_h
11 #define vtk_m_cont_ArrayHandleBitField_h
12 
13 #include <vtkm/cont/ArrayHandle.h>
14 #include <vtkm/cont/BitField.h>
15 #include <vtkm/cont/Storage.h>
16 
17 namespace vtkm
18 {
19 namespace cont
20 {
21 
22 namespace internal
23 {
24 
25 template <typename BitPortalType>
26 class ArrayPortalBitField
27 {
28 public:
29  using ValueType = bool;
30 
32  explicit ArrayPortalBitField(const BitPortalType& portal) noexcept
33  : BitPortal{ portal }
34  {
35  }
36 
38  explicit ArrayPortalBitField(BitPortalType&& portal) noexcept
39  : BitPortal{ std::move(portal) }
40  {
41  }
42 
43  ArrayPortalBitField() noexcept = default;
44  ArrayPortalBitField(const ArrayPortalBitField&) noexcept = default;
45  ArrayPortalBitField(ArrayPortalBitField&&) noexcept = default;
46  ArrayPortalBitField& operator=(const ArrayPortalBitField&) noexcept = default;
47  ArrayPortalBitField& operator=(ArrayPortalBitField&&) noexcept = default;
48 
50  vtkm::Id GetNumberOfValues() const noexcept { return this->BitPortal.GetNumberOfBits(); }
51 
53  ValueType Get(vtkm::Id index) const noexcept { return this->BitPortal.GetBit(index); }
54 
56  void Set(vtkm::Id index, ValueType value) const
57  {
58  // Use an atomic set so we don't clash with other threads writing nearby
59  // bits.
60  this->BitPortal.SetBitAtomic(index, value);
61  }
62 
63 private:
64  BitPortalType BitPortal;
65 };
66 
67 struct VTKM_ALWAYS_EXPORT StorageTagBitField
68 {
69 };
70 
71 template <>
72 class Storage<bool, StorageTagBitField>
73 {
74  using BitPortalType = vtkm::cont::detail::BitPortal;
75  using BitPortalConstType = vtkm::cont::detail::BitPortalConst;
76 
77  using WordType = vtkm::WordTypeDefault;
78  static constexpr vtkm::Id BlockSize = vtkm::cont::detail::BitFieldTraits::BlockSize;
79  VTKM_STATIC_ASSERT(BlockSize >= static_cast<vtkm::Id>(sizeof(WordType)));
80 
81 public:
82  using ReadPortalType = vtkm::cont::internal::ArrayPortalBitField<BitPortalConstType>;
83  using WritePortalType = vtkm::cont::internal::ArrayPortalBitField<BitPortalType>;
84 
85  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
86  {
87  return std::vector<vtkm::cont::internal::Buffer>(1);
88  }
89 
90  VTKM_CONT static void ResizeBuffers(vtkm::Id numberOfBits,
91  const std::vector<vtkm::cont::internal::Buffer>& buffers,
92  vtkm::CopyFlag preserve,
93  vtkm::cont::Token& token)
94  {
95  const vtkm::Id bytesNeeded = (numberOfBits + CHAR_BIT - 1) / CHAR_BIT;
96  const vtkm::Id blocksNeeded = (bytesNeeded + BlockSize - 1) / BlockSize;
97  const vtkm::Id numBytes = blocksNeeded * BlockSize;
98 
100  "BitField Allocation: %llu bits, blocked up to %s bytes.",
101  static_cast<unsigned long long>(numberOfBits),
102  vtkm::cont::GetSizeString(static_cast<vtkm::UInt64>(numBytes)).c_str());
103 
104  buffers[0].SetNumberOfBytes(numBytes, preserve, token);
105  buffers[0].GetMetaData<vtkm::cont::internal::BitFieldMetaData>().NumberOfBits = numberOfBits;
106  }
107 
108  VTKM_CONT static vtkm::Id GetNumberOfValues(
109  const std::vector<vtkm::cont::internal::Buffer>& buffers)
110  {
111  VTKM_ASSERT(buffers.size() == 1);
112  vtkm::Id numberOfBits =
113  buffers[0].GetMetaData<vtkm::cont::internal::BitFieldMetaData>().NumberOfBits;
114  VTKM_ASSERT((buffers[0].GetNumberOfBytes() * CHAR_BIT) >= numberOfBits);
115  return numberOfBits;
116  }
117 
118  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>& buffers,
119  bool fillValue,
120  vtkm::Id startBit,
121  vtkm::Id endBit,
122  vtkm::cont::Token& token)
123  {
124  VTKM_ASSERT(buffers.size() == 1);
125  constexpr vtkm::BufferSizeType wordTypeSize =
126  static_cast<vtkm::BufferSizeType>(sizeof(WordType));
127  constexpr vtkm::BufferSizeType wordNumBits = wordTypeSize * CHAR_BIT;
128  // Special case where filling to end of array.
129  vtkm::Id totalBitsInArray = GetNumberOfValues(buffers);
130  if (endBit >= totalBitsInArray)
131  {
132  endBit = ((totalBitsInArray + (wordNumBits - 1)) / wordNumBits) * wordNumBits;
133  }
134  if (((startBit % wordNumBits) == 0) && ((endBit % wordNumBits) == 0))
135  {
136  WordType fillWord = (fillValue ? ~WordType{ 0 } : WordType{ 0 });
137  buffers[0].Fill(&fillWord, wordTypeSize, startBit / CHAR_BIT, endBit / CHAR_BIT, token);
138  }
139  else if (((startBit % CHAR_BIT) == 0) && ((endBit % CHAR_BIT) == 0))
140  {
141  vtkm::UInt8 fillWord = (fillValue ? ~vtkm::UInt8{ 0 } : vtkm::UInt8{ 0 });
142  buffers[0].Fill(&fillWord, 1, startBit / CHAR_BIT, endBit / CHAR_BIT, token);
143  }
144  else
145  {
146  throw vtkm::cont::ErrorBadValue("Can only fill ArrayHandleBitField on 8-bit boundaries.");
147  }
148  }
149 
150  VTKM_CONT static ReadPortalType CreateReadPortal(
151  const std::vector<vtkm::cont::internal::Buffer>& buffers,
153  vtkm::cont::Token& token)
154  {
155  VTKM_ASSERT(buffers.size() == 1);
156  vtkm::Id numberOfBits = GetNumberOfValues(buffers);
157  VTKM_ASSERT((buffers[0].GetNumberOfBytes() * CHAR_BIT) >= numberOfBits);
158 
159  return ReadPortalType(
160  BitPortalConstType(buffers[0].ReadPointerDevice(device, token), numberOfBits));
161  }
162 
163  VTKM_CONT static WritePortalType CreateWritePortal(
164  const std::vector<vtkm::cont::internal::Buffer>& buffers,
166  vtkm::cont::Token& token)
167  {
168  VTKM_ASSERT(buffers.size() == 1);
169  vtkm::Id numberOfBits = GetNumberOfValues(buffers);
170  VTKM_ASSERT((buffers[0].GetNumberOfBytes() * CHAR_BIT) >= numberOfBits);
171 
172  return WritePortalType(
173  BitPortalType(buffers[0].WritePointerDevice(device, token), numberOfBits));
174  }
175 };
176 
177 } // end namespace internal
178 
179 
183 class ArrayHandleBitField : public ArrayHandle<bool, internal::StorageTagBitField>
184 {
185 public:
188 
189  VTKM_CONT
190  explicit ArrayHandleBitField(const vtkm::cont::BitField& bitField)
191  : Superclass(std::vector<vtkm::cont::internal::Buffer>(1, bitField.GetBuffer()))
192  {
193  }
194 };
195 
197  const vtkm::cont::BitField& bitField)
198 {
199  return ArrayHandleBitField{ bitField };
200 }
201 
203  vtkm::cont::BitField&& bitField) noexcept
204 {
205  return ArrayHandleBitField{ std::move(bitField) };
206 }
207 }
208 } // end namespace vtkm::cont
209 
210 #endif // vtk_m_cont_ArrayHandleBitField_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
ArrayHandle.h
vtkm::cont::GetSizeString
VTKM_CONT_EXPORT VTKM_CONT std::string GetSizeString(vtkm::UInt64 bytes, int prec=2)
Returns "%1 (%2 bytes)" where %1 is the result from GetHumanReadableSize and two is the exact number ...
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
vtkm::cont::ArrayHandleBitField::VTKM_ARRAY_HANDLE_SUBCLASS_NT
VTKM_ARRAY_HANDLE_SUBCLASS_NT(ArrayHandleBitField,(ArrayHandle< bool, internal::StorageTagBitField >))
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Get
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC_CONT auto Get(const vtkm::Tuple< Ts... > &tuple) -> decltype(tuple.template Get< Index >())
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:83
BitField.h
vtkm::WordTypeDefault
vtkm::UInt32 WordTypeDefault
The default word size used for atomic bitwise operations.
Definition: Types.h:172
Storage.h
vtkm::BufferSizeType
vtkm::Int64 BufferSizeType
Definition: DeviceAdapterMemoryManager.h:27
vtkm::cont::ArrayHandleBitField::ArrayHandleBitField
VTKM_CONT ArrayHandleBitField(const vtkm::cont::BitField &bitField)
Definition: ArrayHandleBitField.h:190
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
VTKM_STATIC_ASSERT
#define VTKM_STATIC_ASSERT(condition)
Definition: StaticAssert.h:16
VTKM_LOG_F
#define VTKM_LOG_F(level,...)
Writes a message using printf syntax to the indicated log level.
Definition: Logging.h:262
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::LogLevel::MemCont
@ MemCont
Host-side resource allocations/frees (e.g. ArrayHandle control buffers)
vtkm::cont::make_ArrayHandleBitField
VTKM_CONT vtkm::cont::ArrayHandleBitField make_ArrayHandleBitField(const vtkm::cont::BitField &bitField)
Definition: ArrayHandleBitField.h:196
vtkm::UInt8
uint8_t UInt8
Definition: Types.h:157
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::cont::BitField
Definition: BitField.h:497
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
Definition: Flags.h:16
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
vtkm::cont::ArrayHandleBitField
The ArrayHandleBitField class is a boolean-valued ArrayHandle that is backed by a BitField.
Definition: ArrayHandleBitField.h:183