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
cloudViewer
Generic file read and write utility for python interface.
Definition: CloudViewerConfig.cpp:12
cloudViewer::core::HashBackendBuffer::GetValueBuffers
std::vector< Tensor > GetValueBuffers() const
Return the value buffer tensors.
Definition: HashBackendBuffer.cpp:112
cloudViewer::core::CPUHashBackendBufferAccessor::GetKeyPtr
void * GetKeyPtr(buf_index_t buf_index)
Definition: CPUHashBackendBufferAccessor.hpp:45
cloudViewer::core::HashBackendBuffer::GetHeapTop
HeapTop & GetHeapTop()
Definition: HashBackendBuffer.cpp:99
cloudViewer::core::Tensor::GetLength
int64_t GetLength() const
Definition: Tensor.h:1125
cloudViewer::core::CPUHashBackendBufferAccessor
Definition: CPUHashBackendBufferAccessor.hpp:21
cloudViewer::core::HashBackendBuffer
Definition: HashBackendBuffer.h:46
cloudViewer::core::CPUHashBackendBufferAccessor::CPUHashBackendBufferAccessor
CPUHashBackendBufferAccessor(HashBackendBuffer &hashmap_buffer)
Must initialize from a non-const buffer to grab the heap top.
Definition: CPUHashBackendBufferAccessor.hpp:24
cloudViewer::core::Tensor
Definition: Tensor.h:32
cloudViewer::core::CPUHashBackendBufferAccessor::key_buffer_ptr_
uint8_t * key_buffer_ptr_
Definition: CPUHashBackendBufferAccessor.hpp:61
cloudViewer::core::CPUHashBackendBufferAccessor::capacity_
int64_t capacity_
Definition: CPUHashBackendBufferAccessor.hpp:54
Parallel.h
cloudViewer::core::CPUHashBackendBufferAccessor::DeviceFree
void DeviceFree(buf_index_t buf_index)
Definition: CPUHashBackendBufferAccessor.hpp:41
cloudViewer::core::CPUResetHeap
void CPUResetHeap(Tensor &heap)
Definition: CPUHashBackendBuffer.cpp:14
cloudViewer::core::CPUHashBackendBufferAccessor::key_dsize_
int64_t key_dsize_
Definition: CPUHashBackendBufferAccessor.hpp:55
HashBackendBuffer.h
cloudViewer::core::CPUHashBackendBufferAccessor::value_dsizes_
std::vector< int64_t > value_dsizes_
Definition: CPUHashBackendBufferAccessor.hpp:56
cloudViewer::core::CPUHashBackendBufferAccessor::GetValuePtr
void * GetValuePtr(buf_index_t buf_index, int value_idx=0)
Definition: CPUHashBackendBufferAccessor.hpp:48
cloudViewer::core::CPUHashBackendBufferAccessor::value_buffer_ptrs_
std::vector< uint8_t * > value_buffer_ptrs_
Definition: CPUHashBackendBufferAccessor.hpp:62
core
cloudViewer::core::CPUHashBackendBufferAccessor::heap_top_
std::atomic< int > * heap_top_
Definition: CPUHashBackendBufferAccessor.hpp:59
cloudViewer::core::buf_index_t
uint32_t buf_index_t
Definition: HashBackendBuffer.h:44
cloudViewer::core::CPUHashBackendBufferAccessor::heap_
buf_index_t * heap_
Definition: CPUHashBackendBufferAccessor.hpp:58
cloudViewer::core::CPUHashBackendBufferAccessor::DeviceAllocate
buf_index_t DeviceAllocate()
Definition: CPUHashBackendBufferAccessor.hpp:40
cloudViewer::core::HashBackendBuffer::HeapTop::cpu
std::atomic< int > cpu
Definition: HashBackendBuffer.h:50
cloudViewer::core::Tensor::GetDataPtr
T * GetDataPtr()
Definition: Tensor.h:1144