ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
HashBackendBuffer.h
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 
18 
19 namespace cloudViewer {
20 namespace core {
21 
22 void CPUResetHeap(Tensor &heap);
23 
24 #ifdef BUILD_CUDA_MODULE
25 void CUDAResetHeap(Tensor &heap);
26 #endif
27 
28 // The heap array stores the indices of the key/values buffers. It is not
29 // injective.
30 // During Allocate, an buffer index (buf_index) is extracted from the
31 // heap; During Free, a buf_index is put back to the top of the heap.
32 // ---------------------------------------------------------------------
33 // heap ---Malloc--> heap ---Malloc--> heap ---Free(0)--> heap
34 // N-1 N-1 N-1 N-1 |
35 // . . . . |
36 // . . . . |
37 // . . . . |
38 // 3 3 3 3 |
39 // 2 2 2 <- 2 |
40 // 1 1 <- 1 0 <- |
41 // 0 <- heap_top 0 0 0
42 
43 // Buffer index type for the internal heap.
44 using buf_index_t = uint32_t;
45 
47 public:
48  struct HeapTop {
50  std::atomic<int> cpu = {0};
51  };
52 
53  HashBackendBuffer(int64_t capacity,
54  int64_t key_dsize,
55  std::vector<int64_t> value_dsizes,
56  const Device &device);
57 
59  void ResetHeap();
60 
62  Device GetDevice() const;
63 
65  int64_t GetCapacity() const;
66 
68  int64_t GetKeyDsize() const;
69 
71  std::vector<int64_t> GetValueDsizes() const;
72 
74  int64_t GetCommonBlockSize() const;
75 
77  std::vector<int64_t> GetValueBlocksPerElement() const;
78 
80  Tensor GetIndexHeap() const;
81 
84  HeapTop &GetHeapTop();
85 
87  int GetHeapTopIndex() const;
88 
90  Tensor GetKeyBuffer() const;
91 
93  std::vector<Tensor> GetValueBuffers() const;
94 
96  Tensor GetValueBuffer(size_t i = 0) const;
97 
98 protected:
101 
103  std::vector<Tensor> value_buffers_;
104 
106  std::vector<int64_t> blocks_per_element_;
107 };
108 } // namespace core
109 } // namespace cloudViewer
cloudViewer
Generic file read and write utility for python interface.
Definition: CloudViewerConfig.cpp:12
cloudViewer::core::Tensor::GetDtype
Dtype GetDtype() const
Definition: Tensor.h:1164
cloudViewer::core::HashBackendBuffer::key_buffer_
Tensor key_buffer_
Definition: HashBackendBuffer.h:102
cloudViewer::core::HashBackendBuffer::GetValueBuffers
std::vector< Tensor > GetValueBuffers() const
Return the value buffer tensors.
Definition: HashBackendBuffer.cpp:112
CUDAUtils.h
Common CUDA utilities.
cloudViewer::core::HashBackendBuffer::HeapTop
Definition: HashBackendBuffer.h:48
cloudViewer::core::HashBackendBuffer::GetHeapTop
HeapTop & GetHeapTop()
Definition: HashBackendBuffer.cpp:99
cloudViewer::core::Tensor::GetLength
int64_t GetLength() const
Definition: Tensor.h:1125
cloudViewer::core::HashBackendBuffer::HeapTop::cuda
Tensor cuda
Definition: HashBackendBuffer.h:49
cloudViewer::core::Tensor::Fill
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
cloudViewer::core::UInt32
const Dtype UInt32
Definition: Dtype.cpp:50
cloudViewer::core::IsDevice::IsCUDA
bool IsCUDA() const
Definition: Device.h:99
cloudViewer::core::Dtype::ByteSize
int64_t ByteSize() const
Definition: Dtype.h:59
cloudViewer::core::HashBackendBuffer
Definition: HashBackendBuffer.h:46
cloudViewer::core::HashBackendBuffer::GetValueBlocksPerElement
std::vector< int64_t > GetValueBlocksPerElement() const
Return value's data sizes in the unit of common block size divisor.
Definition: HashBackendBuffer.cpp:93
cloudViewer::core::Tensor
Definition: Tensor.h:32
cloudViewer::core::HashBackendBuffer::common_block_size_
int64_t common_block_size_
Definition: HashBackendBuffer.h:105
cloudViewer::core::Dtype
Definition: Dtype.h:21
cloudViewer::core::Device::IsCUDA
bool IsCUDA() const
Returns true iff device type is CUDA.
Definition: Device.h:49
cloudViewer::core::HashBackendBuffer::heap_
Tensor heap_
Definition: HashBackendBuffer.h:99
CUDA_CALL
#define CUDA_CALL(cuda_function,...)
Definition: CUDAUtils.h:49
cloudViewer::core::HashBackendBuffer::heap_top_
HeapTop heap_top_
Definition: HashBackendBuffer.h:100
cloudViewer::core::HashBackendBuffer::GetHeapTopIndex
int GetHeapTopIndex() const
Return the current heap top.
Definition: HashBackendBuffer.cpp:103
cloudViewer::core::CPUResetHeap
void CPUResetHeap(Tensor &heap)
Definition: CPUHashBackendBuffer.cpp:14
HashBackendBuffer.h
cloudViewer::core::Device
Definition: Device.h:18
cloudViewer::core::HashBackendBuffer::GetValueBuffer
Tensor GetValueBuffer(size_t i=0) const
Return the selected value buffer tensor at index i.
Definition: HashBackendBuffer.cpp:116
cloudViewer::core::HashBackendBuffer::GetKeyDsize
int64_t GetKeyDsize() const
Return key's data size in bytes.
Definition: HashBackendBuffer.cpp:77
core
cloudViewer::core::Tensor::GetDevice
Device GetDevice() const override
Definition: Tensor.cpp:1435
cloudViewer::core::buf_index_t
uint32_t buf_index_t
Definition: HashBackendBuffer.h:44
cloudViewer::core::HashBackendBuffer::GetIndexHeap
Tensor GetIndexHeap() const
Return the index heap tensor.
Definition: HashBackendBuffer.cpp:97
cloudViewer::core::HashBackendBuffer::value_buffers_
std::vector< Tensor > value_buffers_
Definition: HashBackendBuffer.h:103
LogError
#define LogError(...)
Definition: Logging.h:60
cloudViewer::core::HashBackendBuffer::ResetHeap
void ResetHeap()
Reset the heap and heap top.
Definition: HashBackendBuffer.cpp:60
cloudViewer::core::Device::IsCPU
bool IsCPU() const
Returns true iff device type is CPU.
Definition: Device.h:46
cloudViewer::core::Tensor::Item
T Item() const
Definition: Tensor.h:618
cloudViewer::core::HashBackendBuffer::GetValueDsizes
std::vector< int64_t > GetValueDsizes() const
Return value's data sizes in bytes.
Definition: HashBackendBuffer.cpp:81
cloudViewer::core::HashBackendBuffer::GetDevice
Device GetDevice() const
Return device of the buffer.
Definition: HashBackendBuffer.cpp:73
cloudViewer::core::Dtype::DtypeCode::Object
@ Object
Tensor.h
cloudViewer::core::HashBackendBuffer::HashBackendBuffer
HashBackendBuffer(int64_t capacity, int64_t key_dsize, std::vector< int64_t > value_dsizes, const Device &device)
Definition: HashBackendBuffer.cpp:15
cloudViewer::core::HashBackendBuffer::blocks_per_element_
std::vector< int64_t > blocks_per_element_
Definition: HashBackendBuffer.h:106
cloudViewer::core::HashBackendBuffer::GetCapacity
int64_t GetCapacity() const
Return capacity of the buffer.
Definition: HashBackendBuffer.cpp:75
cloudViewer::core::HashBackendBuffer::GetKeyBuffer
Tensor GetKeyBuffer() const
Return the key buffer tensor.
Definition: HashBackendBuffer.cpp:110
cloudViewer::core::HashBackendBuffer::GetCommonBlockSize
int64_t GetCommonBlockSize() const
Get the common block size divisor of all values types.
Definition: HashBackendBuffer.cpp:89
cloudViewer::core::HashBackendBuffer::HeapTop::cpu
std::atomic< int > cpu
Definition: HashBackendBuffer.h:50
cloudViewer::core::Dtype::Int32
static const Dtype Int32
Definition: Dtype.h:28
patch::to_string
std::string to_string(const T &n)
Definition: Common.h:20
MemoryManager.h