Open3D (C++ API)  0.16.0
HashMap.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// The MIT License (MIT)
5//
6// Copyright (c) 2018-2021 www.open3d.org
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in
16// all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24// IN THE SOFTWARE.
25// ----------------------------------------------------------------------------
26
27#pragma once
28
29#include "open3d/core/Device.h"
30#include "open3d/core/Dtype.h"
31#include "open3d/core/Tensor.h"
33
34namespace open3d {
35namespace core {
36
37class DeviceHashBackend;
38
40
41class HashMap : public IsDevice {
42public:
44 HashMap(int64_t init_capacity,
45 const Dtype& key_dtype,
46 const SizeVector& key_element_shape,
47 const Dtype& value_dtype,
48 const SizeVector& value_element_shapes,
49 const Device& device,
51
55 HashMap(int64_t init_capacity,
56 const Dtype& key_dtype,
57 const SizeVector& key_element_shape,
58 const std::vector<Dtype>& dtypes_value,
59 const std::vector<SizeVector>& element_shapes_value,
60 const Device& device,
62
64 ~HashMap() = default;
65
67 void Reserve(int64_t capacity);
68
77 std::pair<Tensor, Tensor> Insert(const Tensor& input_keys,
78 const Tensor& input_values);
79
84 std::pair<Tensor, Tensor> Insert(
85 const Tensor& input_keys,
86 const std::vector<Tensor>& input_values_soa);
87
93 std::pair<Tensor, Tensor> Activate(const Tensor& input_keys);
94
99 std::pair<Tensor, Tensor> Find(const Tensor& input_keys);
100
104 Tensor Erase(const Tensor& input_keys);
105
109 Tensor GetActiveIndices() const;
110
114 void Insert(const Tensor& input_keys,
115 const Tensor& input_values,
116 Tensor& output_buf_indices,
117 Tensor& output_masks);
118
122 void Insert(const Tensor& input_keys,
123 const std::vector<Tensor>& input_values_soa,
124 Tensor& output_buf_indices,
125 Tensor& output_masks);
126
130 void Activate(const Tensor& input_keys,
131 Tensor& output_buf_indices,
132 Tensor& output_masks);
133
137 void Find(const Tensor& input_keys,
138 Tensor& output_buf_indices,
139 Tensor& output_masks);
140
143 void Erase(const Tensor& input_keys, Tensor& output_masks);
144
147 void GetActiveIndices(Tensor& output_buf_indices) const;
148
150 void Clear();
151
156 void Save(const std::string& file_name);
157
160 static HashMap Load(const std::string& file_name);
161
163 HashMap Clone() const;
164
166 HashMap To(const Device& device, bool copy = false) const;
167
169 int64_t Size() const;
170
172 int64_t GetCapacity() const;
173
175 int64_t GetBucketCount() const;
176
178 Device GetDevice() const override;
179
183 Tensor GetKeyTensor() const;
184
188 std::vector<Tensor> GetValueTensors() const;
189
193 Tensor GetValueTensor(size_t index = 0) const;
194
196 std::vector<int64_t> BucketSizes() const;
197
199 float LoadFactor() const;
200
202 std::shared_ptr<DeviceHashBackend> GetDeviceHashBackend() const {
203 return device_hashmap_;
204 }
205
206protected:
207 void Init(int64_t init_capacity,
208 const Device& device,
209 const HashBackendType& backend);
210
211 void InsertImpl(const Tensor& input_keys,
212 const std::vector<Tensor>& input_values_soa,
213 Tensor& output_buf_indices,
214 Tensor& output_masks,
215 bool is_activate_op = false);
216
217 void CheckKeyLength(const Tensor& input_keys) const;
219 const Tensor& input_keys,
220 const std::vector<Tensor>& input_values_soa) const;
221 void CheckKeyCompatibility(const Tensor& input_keys) const;
223 const std::vector<Tensor>& input_values_soa) const;
224
225 void PrepareIndicesOutput(Tensor& output_buf_indices, int64_t length) const;
226 void PrepareMasksOutput(Tensor& output_masks, int64_t length) const;
227
228 std::pair<int64_t, std::vector<int64_t>> GetCommonValueSizeDivisor();
229
230private:
231 std::shared_ptr<DeviceHashBackend> device_hashmap_;
232
233 Dtype key_dtype_;
234 SizeVector key_element_shape_;
235
236 std::vector<Dtype> dtypes_value_;
237 std::vector<SizeVector> element_shapes_value_;
238};
239
240} // namespace core
241} // namespace open3d
bool copy
Definition: VtkUtils.cpp:89
Definition: Device.h:37
Definition: Dtype.h:39
Definition: HashMap.h:41
HashMap Clone() const
Clone the hash map with buffers.
Definition: HashMap.cpp:245
std::vector< Tensor > GetValueTensors() const
Definition: HashMap.cpp:293
void CheckKeyLength(const Tensor &input_keys) const
Definition: HashMap.cpp:375
void CheckKeyCompatibility(const Tensor &input_keys) const
Definition: HashMap.cpp:401
void CheckValueCompatibility(const std::vector< Tensor > &input_values_soa) const
Definition: HashMap.cpp:416
void PrepareIndicesOutput(Tensor &output_buf_indices, int64_t length) const
Definition: HashMap.cpp:447
~HashMap()=default
Default destructor.
int64_t GetCapacity() const
Get the capacity of the hash map.
Definition: HashMap.cpp:276
float LoadFactor() const
Return size / bucket_count.
Definition: HashMap.cpp:335
void Save(const std::string &file_name)
Definition: HashMap.cpp:237
static HashMap Load(const std::string &file_name)
Definition: HashMap.cpp:241
int64_t GetBucketCount() const
Get the number of buckets of the internal hash map.
Definition: HashMap.cpp:278
Tensor GetKeyTensor() const
Definition: HashMap.cpp:284
void PrepareMasksOutput(Tensor &output_masks, int64_t length) const
Definition: HashMap.cpp:456
HashMap(int64_t init_capacity, const Dtype &key_dtype, const SizeVector &key_element_shape, const Dtype &value_dtype, const SizeVector &value_element_shapes, const Device &device, const HashBackendType &backend=HashBackendType::Default)
Initialize a hash map given a key and a value dtype and element shape.
Definition: HashMap.cpp:38
Device GetDevice() const override
Get the device of the hash map.
Definition: HashMap.cpp:282
std::pair< Tensor, Tensor > Insert(const Tensor &input_keys, const Tensor &input_values)
Definition: HashMap.cpp:98
std::pair< Tensor, Tensor > Find(const Tensor &input_keys)
Definition: HashMap.cpp:118
std::pair< int64_t, std::vector< int64_t > > GetCommonValueSizeDivisor()
std::pair< Tensor, Tensor > Activate(const Tensor &input_keys)
Definition: HashMap.cpp:112
Tensor GetValueTensor(size_t index=0) const
Definition: HashMap.cpp:312
void Init(int64_t init_capacity, const Device &device, const HashBackendType &backend)
Definition: HashMap.cpp:337
int64_t Size() const
Get the size (number of active entries) of the hash map.
Definition: HashMap.cpp:274
void CheckKeyValueLengthCompatibility(const Tensor &input_keys, const std::vector< Tensor > &input_values_soa) const
Definition: HashMap.cpp:382
Tensor Erase(const Tensor &input_keys)
Definition: HashMap.cpp:124
HashMap To(const Device &device, bool copy=false) const
Convert the hash map to another device.
Definition: HashMap.cpp:247
void Reserve(int64_t capacity)
Reserve the internal hash map with the given capacity by rehashing.
Definition: HashMap.cpp:66
void InsertImpl(const Tensor &input_keys, const std::vector< Tensor > &input_values_soa, Tensor &output_buf_indices, Tensor &output_masks, bool is_activate_op=false)
Definition: HashMap.cpp:136
std::vector< int64_t > BucketSizes() const
Return number of elements per bucket.
Definition: HashMap.cpp:331
std::shared_ptr< DeviceHashBackend > GetDeviceHashBackend() const
Return the implementation of the device hash backend.
Definition: HashMap.h:202
void Clear()
Clear stored map without reallocating the buffers.
Definition: HashMap.cpp:235
Tensor GetActiveIndices() const
Definition: HashMap.cpp:130
Definition: Device.h:107
Definition: SizeVector.h:88
Definition: SlabNodeManager.h:58
Definition: Tensor.h:51
HashBackendType
Definition: HashMap.h:39
Definition: PinholeCameraIntrinsic.cpp:35