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
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.
void CPUResetHeap(Tensor &heap)
Generic file read and write utility for python interface.