ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
HashBackendBuffer.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
11 
12 namespace cloudViewer {
13 namespace core {
14 
16  int64_t key_dsize,
17  std::vector<int64_t> value_dsizes,
18  const Device &device) {
19  // First compute common bytesize divisor for fast copying values.
20  const std::vector<int64_t> kDivisors = {16, 12, 8, 4, 2, 1};
21 
22  for (const auto &divisor : kDivisors) {
23  bool valid = true;
24  blocks_per_element_.clear();
25  for (size_t i = 0; i < value_dsizes.size(); ++i) {
26  int64_t bytesize = value_dsizes[i];
27  valid = valid && (bytesize % divisor == 0);
28  blocks_per_element_.push_back(bytesize / divisor);
29  }
30  if (valid) {
31  common_block_size_ = divisor;
32  break;
33  }
34  }
35 
36  heap_ = Tensor({capacity}, core::UInt32, device);
37 
38  key_buffer_ = Tensor({capacity},
39  Dtype(Dtype::DtypeCode::Object, key_dsize, "_hash_k"),
40  device);
41 
42  value_buffers_.clear();
43  for (size_t i = 0; i < value_dsizes.size(); ++i) {
44  int64_t dsize_value = value_dsizes[i];
45  Tensor value_buffer_i({capacity},
46  Dtype(Dtype::DtypeCode::Object, dsize_value,
47  "_hash_v_" + std::to_string(i)),
48  device);
49  value_buffers_.push_back(value_buffer_i);
50  }
51 
52  // Heap top is device specific
53  if (device.IsCUDA()) {
54  heap_top_.cuda = Tensor({1}, Dtype::Int32, device);
55  }
56 
57  ResetHeap();
58 }
59 
61  Device device = GetDevice();
62 
63  Tensor heap = GetIndexHeap();
64  if (device.IsCPU()) {
65  CPUResetHeap(heap);
66  heap_top_.cpu = 0;
67  } else if (device.IsCUDA()) {
68  CUDA_CALL(CUDAResetHeap, heap);
69  heap_top_.cuda.Fill<int>(0);
70  }
71 }
72 
74 
75 int64_t HashBackendBuffer::GetCapacity() const { return heap_.GetLength(); }
76 
78  return key_buffer_.GetDtype().ByteSize();
79 }
80 
81 std::vector<int64_t> HashBackendBuffer::GetValueDsizes() const {
82  std::vector<int64_t> value_dsizes;
83  for (auto &value_buffer : value_buffers_) {
84  value_dsizes.push_back(value_buffer.GetDtype().ByteSize());
85  }
86  return value_dsizes;
87 }
88 
90  return common_block_size_;
91 }
92 
93 std::vector<int64_t> HashBackendBuffer::GetValueBlocksPerElement() const {
94  return blocks_per_element_;
95 }
96 
98 
100  return heap_top_;
101 }
102 
104  if (heap_.IsCUDA()) {
105  return heap_top_.cuda[0].Item<int>();
106  }
107  return heap_top_.cpu.load();
108 }
109 
111 
112 std::vector<Tensor> HashBackendBuffer::GetValueBuffers() const {
113  return value_buffers_;
114 }
115 
117  if (i >= value_buffers_.size()) {
118  utility::LogError("Value buffer index out-of-bound ({} >= {}).", i,
119  value_buffers_.size());
120  }
121  return value_buffers_[i];
122 }
123 
124 } // namespace core
125 } // namespace cloudViewer
Common CUDA utilities.
#define CUDA_CALL(cuda_function,...)
Definition: CUDAUtils.h:49
bool IsCUDA() const
Returns true iff device type is CUDA.
Definition: Device.h:49
bool IsCPU() const
Returns true iff device type is CPU.
Definition: Device.h:46
int64_t ByteSize() const
Definition: Dtype.h:59
static const Dtype Int32
Definition: Dtype.h:28
int64_t GetKeyDsize() const
Return key's data size in bytes.
Tensor GetKeyBuffer() const
Return the key buffer tensor.
HashBackendBuffer(int64_t capacity, int64_t key_dsize, std::vector< int64_t > value_dsizes, const Device &device)
Device GetDevice() const
Return device of the buffer.
int GetHeapTopIndex() const
Return the current heap top.
std::vector< int64_t > GetValueDsizes() const
Return value's data sizes in bytes.
std::vector< int64_t > GetValueBlocksPerElement() const
Return value's data sizes in the unit of common block size divisor.
Tensor GetValueBuffer(size_t i=0) const
Return the selected value buffer tensor at index i.
int64_t GetCommonBlockSize() const
Get the common block size divisor of all values types.
std::vector< Tensor > GetValueBuffers() const
Return the value buffer tensors.
std::vector< int64_t > blocks_per_element_
int64_t GetCapacity() const
Return capacity of the buffer.
void ResetHeap()
Reset the heap and heap top.
Tensor GetIndexHeap() const
Return the index heap tensor.
bool IsCUDA() const
Definition: Device.h:99
int64_t GetLength() const
Definition: Tensor.h:1125
Dtype GetDtype() const
Definition: Tensor.h:1164
Device GetDevice() const override
Definition: Tensor.cpp:1435
void Fill(S v)
Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor's Dtype.
Definition: Tensor.h:1400
#define LogError(...)
Definition: Logging.h:60
void CPUResetHeap(Tensor &heap)
const Dtype UInt32
Definition: Dtype.cpp:50
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20