ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
CPUHashBackendBufferAccessor.hpp
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 
8 #pragma once
9 
10 #include <assert.h>
11 
12 #include <atomic>
13 #include <memory>
14 #include <vector>
15 
17 
18 namespace cloudViewer {
19 namespace core {
20 
22 public:
25  : capacity_(hashmap_buffer.GetCapacity()),
26  key_dsize_(hashmap_buffer.GetKeyDsize()),
27  value_dsizes_(hashmap_buffer.GetValueDsizes()),
28  heap_(hashmap_buffer.GetIndexHeap().GetDataPtr<buf_index_t>()),
29  key_buffer_ptr_(hashmap_buffer.GetKeyBuffer().GetDataPtr<uint8_t>()) {
30  std::vector<Tensor> value_buffers = hashmap_buffer.GetValueBuffers();
31  for (size_t i = 0; i < value_buffers.size(); ++i) {
32  void *value_buffer_ptr = value_buffers[i].GetDataPtr();
33  std::memset(value_buffer_ptr, 0, capacity_ * value_dsizes_[i]);
34  value_buffer_ptrs_.push_back(
35  static_cast<uint8_t *>(value_buffer_ptr));
36  }
37  heap_top_ = &(hashmap_buffer.GetHeapTop().cpu);
38  }
39 
40  buf_index_t DeviceAllocate() { return heap_[(*heap_top_).fetch_add(1)]; }
41  void DeviceFree(buf_index_t buf_index) {
42  heap_[(*heap_top_).fetch_sub(1) - 1] = buf_index;
43  }
44 
45  void *GetKeyPtr(buf_index_t buf_index) {
46  return key_buffer_ptr_ + buf_index * key_dsize_;
47  }
48  void *GetValuePtr(buf_index_t buf_index, int value_idx = 0) {
49  return value_buffer_ptrs_[value_idx] +
50  buf_index * value_dsizes_[value_idx];
51  }
52 
53 public:
54  int64_t capacity_;
55  int64_t key_dsize_;
56  std::vector<int64_t> value_dsizes_;
57 
58  buf_index_t *heap_; /* [N] */
59  std::atomic<int> *heap_top_; /* [1] */
60 
61  uint8_t *key_buffer_ptr_; /* [N] * sizeof(Key) */
62  std::vector<uint8_t *> value_buffer_ptrs_; /* [N] * sizeof(Value) */
63 };
64 
65 } // namespace core
66 } // namespace cloudViewer
void * GetValuePtr(buf_index_t buf_index, int value_idx=0)
CPUHashBackendBufferAccessor(HashBackendBuffer &hashmap_buffer)
Must initialize from a non-const buffer to grab the heap top.
std::vector< Tensor > GetValueBuffers() const
Return the value buffer tensors.
Generic file read and write utility for python interface.