10 #ifndef vtk_m_cont_ArrayHandleBitField_h
11 #define vtk_m_cont_ArrayHandleBitField_h
25 template <
typename BitPortalType>
26 class ArrayPortalBitField
29 using ValueType = bool;
32 explicit ArrayPortalBitField(
const BitPortalType& portal) noexcept
38 explicit ArrayPortalBitField(BitPortalType&& portal) noexcept
39 : BitPortal{ std::move(portal) }
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;
50 vtkm::
Id GetNumberOfValues() const noexcept {
return this->BitPortal.GetNumberOfBits(); }
53 ValueType
Get(
vtkm::Id index)
const noexcept {
return this->BitPortal.GetBit(index); }
56 void Set(
vtkm::Id index, ValueType value)
const
60 this->BitPortal.SetBitAtomic(index, value);
64 BitPortalType BitPortal;
72 class Storage<bool, StorageTagBitField>
74 using BitPortalType = vtkm::cont::detail::BitPortal;
75 using BitPortalConstType = vtkm::cont::detail::BitPortalConst;
78 static constexpr
vtkm::Id BlockSize = vtkm::cont::detail::BitFieldTraits::BlockSize;
82 using ReadPortalType = vtkm::cont::internal::ArrayPortalBitField<BitPortalConstType>;
83 using WritePortalType = vtkm::cont::internal::ArrayPortalBitField<BitPortalType>;
85 VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
87 return std::vector<vtkm::cont::internal::Buffer>(1);
91 const std::vector<vtkm::cont::internal::Buffer>& buffers,
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;
100 "BitField Allocation: %llu bits, blocked up to %s bytes.",
101 static_cast<unsigned long long>(numberOfBits),
104 buffers[0].SetNumberOfBytes(numBytes, preserve, token);
105 buffers[0].GetMetaData<vtkm::cont::internal::BitFieldMetaData>().NumberOfBits = numberOfBits;
109 const std::vector<vtkm::cont::internal::Buffer>& buffers)
113 buffers[0].GetMetaData<vtkm::cont::internal::BitFieldMetaData>().NumberOfBits;
114 VTKM_ASSERT((buffers[0].GetNumberOfBytes() * CHAR_BIT) >= numberOfBits);
118 VTKM_CONT static void Fill(
const std::vector<vtkm::cont::internal::Buffer>& buffers,
129 vtkm::Id totalBitsInArray = GetNumberOfValues(buffers);
130 if (endBit >= totalBitsInArray)
132 endBit = ((totalBitsInArray + (wordNumBits - 1)) / wordNumBits) * wordNumBits;
134 if (((startBit % wordNumBits) == 0) && ((endBit % wordNumBits) == 0))
136 WordType fillWord = (fillValue ? ~WordType{ 0 } : WordType{ 0 });
137 buffers[0].Fill(&fillWord, wordTypeSize, startBit / CHAR_BIT, endBit / CHAR_BIT, token);
139 else if (((startBit % CHAR_BIT) == 0) && ((endBit % CHAR_BIT) == 0))
142 buffers[0].Fill(&fillWord, 1, startBit / CHAR_BIT, endBit / CHAR_BIT, token);
150 VTKM_CONT static ReadPortalType CreateReadPortal(
151 const std::vector<vtkm::cont::internal::Buffer>& buffers,
156 vtkm::Id numberOfBits = GetNumberOfValues(buffers);
157 VTKM_ASSERT((buffers[0].GetNumberOfBytes() * CHAR_BIT) >= numberOfBits);
159 return ReadPortalType(
160 BitPortalConstType(buffers[0].ReadPointerDevice(device, token), numberOfBits));
163 VTKM_CONT static WritePortalType CreateWritePortal(
164 const std::vector<vtkm::cont::internal::Buffer>& buffers,
169 vtkm::Id numberOfBits = GetNumberOfValues(buffers);
170 VTKM_ASSERT((buffers[0].GetNumberOfBytes() * CHAR_BIT) >= numberOfBits);
172 return WritePortalType(
173 BitPortalType(buffers[0].WritePointerDevice(device, token), numberOfBits));
191 : Superclass(std::vector<
vtkm::cont::internal::Buffer>(1, bitField.GetBuffer()))
210 #endif // vtk_m_cont_ArrayHandleBitField_h