Open3D (C++ API)  0.19.0
Loading...
Searching...
No Matches
HashBackendBuffer.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// Copyright (c) 2018-2024 www.open3d.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#include "open3d/core/Tensor.h"
18
19namespace open3d {
20namespace core {
21
22void CPUResetHeap(Tensor &heap);
23
24#ifdef BUILD_CUDA_MODULE
25void CUDAResetHeap(Tensor &heap);
26#endif
27
28#ifdef BUILD_SYCL_MODULE
30void SYCLResetHeap(Tensor &heap);
31#endif
32
33// The heap array stores the indices of the key/values buffers. It is not
34// injective.
35// During Allocate, an buffer index (buf_index) is extracted from the
36// heap; During Free, a buf_index is put back to the top of the heap.
37// ---------------------------------------------------------------------
38// heap ---Malloc--> heap ---Malloc--> heap ---Free(0)--> heap
39// N-1 N-1 N-1 N-1 |
40// . . . . |
41// . . . . |
42// . . . . |
43// 3 3 3 3 |
44// 2 2 2 <- 2 |
45// 1 1 <- 1 0 <- |
46// 0 <- heap_top 0 0 0
47
48// Buffer index type for the internal heap.
49using buf_index_t = uint32_t;
50
52public:
53 struct HeapTop {
54 // Device-resident heap top (a single Int32 element). Despite the name,
55 // this Tensor is reused for any device backend that needs the heap top
56 // to live in device memory (currently CUDA and SYCL). The CPU backend
57 // uses the std::atomic member below instead.
59 std::atomic<int> cpu = {0};
60 };
61
62 HashBackendBuffer(int64_t capacity,
63 int64_t key_dsize,
64 std::vector<int64_t> value_dsizes,
65 const Device &device);
66
68 void ResetHeap();
69
71 Device GetDevice() const;
72
74 int64_t GetCapacity() const;
75
77 int64_t GetKeyDsize() const;
78
80 std::vector<int64_t> GetValueDsizes() const;
81
83 int64_t GetCommonBlockSize() const;
84
86 std::vector<int64_t> GetValueBlocksPerElement() const;
87
89 Tensor GetIndexHeap() const;
90
93 HeapTop &GetHeapTop();
94
96 int GetHeapTopIndex() const;
97
99 Tensor GetKeyBuffer() const;
100
102 std::vector<Tensor> GetValueBuffers() const;
103
105 Tensor GetValueBuffer(size_t i = 0) const;
106
107protected:
110
112 std::vector<Tensor> value_buffers_;
113
115 std::vector<int64_t> blocks_per_element_;
116};
117} // namespace core
118} // namespace open3d
Definition Device.h:18
Definition HashBackendBuffer.h:51
int64_t GetKeyDsize() const
Return key's data size in bytes.
Definition HashBackendBuffer.cpp:82
Tensor key_buffer_
Definition HashBackendBuffer.h:111
void ResetHeap()
Reset the heap and heap top.
Definition HashBackendBuffer.cpp:60
std::vector< Tensor > value_buffers_
Definition HashBackendBuffer.h:112
Tensor heap_
Definition HashBackendBuffer.h:108
Device GetDevice() const
Return device of the buffer.
Definition HashBackendBuffer.cpp:78
Tensor GetIndexHeap() const
Return the index heap tensor.
Definition HashBackendBuffer.cpp:102
int64_t GetCapacity() const
Return capacity of the buffer.
Definition HashBackendBuffer.cpp:80
HeapTop heap_top_
Definition HashBackendBuffer.h:109
int GetHeapTopIndex() const
Return the current heap top.
Definition HashBackendBuffer.cpp:108
int64_t common_block_size_
Definition HashBackendBuffer.h:114
HeapTop & GetHeapTop()
Definition HashBackendBuffer.cpp:104
std::vector< int64_t > blocks_per_element_
Definition HashBackendBuffer.h:115
Tensor GetKeyBuffer() const
Return the key buffer tensor.
Definition HashBackendBuffer.cpp:115
std::vector< int64_t > GetValueDsizes() const
Return value's data sizes in bytes.
Definition HashBackendBuffer.cpp:86
Tensor GetValueBuffer(size_t i=0) const
Return the selected value buffer tensor at index i.
Definition HashBackendBuffer.cpp:121
int64_t GetCommonBlockSize() const
Get the common block size divisor of all values types.
Definition HashBackendBuffer.cpp:94
std::vector< Tensor > GetValueBuffers() const
Return the value buffer tensors.
Definition HashBackendBuffer.cpp:117
std::vector< int64_t > GetValueBlocksPerElement() const
Return value's data sizes in the unit of common block size divisor.
Definition HashBackendBuffer.cpp:98
Definition Tensor.h:32
void CPUResetHeap(Tensor &heap)
Definition CPUHashBackendBuffer.cpp:13
uint32_t buf_index_t
Definition HashBackendBuffer.h:49
void SYCLResetHeap(Tensor &heap)
Definition SYCLHashBackendBuffer.cpp:21
Definition PinholeCameraIntrinsic.cpp:16
Definition HashBackendBuffer.h:53
std::atomic< int > cpu
Definition HashBackendBuffer.h:59
Tensor cuda
Definition HashBackendBuffer.h:58