Open3D (C++ API)  0.12.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 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 #include "open3d/core/Dtype.h"
28 #include "open3d/core/Tensor.h"
30 
31 namespace open3d {
32 namespace core {
33 
34 // Forward declaration of device-dependent classes
35 class DefaultHash;
36 class DefaultKeyEq;
37 
38 template <typename Hash, typename KeyEq>
39 class DeviceHashmap;
40 typedef DeviceHashmap<DefaultHash, DefaultKeyEq> DefaultDeviceHashmap;
41 
42 class Hashmap {
43 public:
44  static constexpr int64_t kDefaultElemsPerBucket = 4;
45 
60  Hashmap(int64_t init_capacity,
61  const Dtype& dtype_key,
62  const Dtype& dtype_value,
63  const SizeVector& element_shape_key,
64  const SizeVector& element_shape_value,
65  const Device& device);
66 
67  ~Hashmap(){};
68 
74  void Rehash(int64_t buckets);
75 
81  void Insert(const Tensor& input_keys,
82  const Tensor& input_values,
83  Tensor& output_addrs,
84  Tensor& output_masks);
85 
93  void Activate(const Tensor& input_keys,
94  Tensor& output_addrs,
95  Tensor& output_masks);
96 
102  void Find(const Tensor& input_keys,
103  Tensor& output_addrs,
104  Tensor& output_masks);
105 
110  void Erase(const Tensor& input_keys, Tensor& output_masks);
111 
115  void GetActiveIndices(Tensor& output_indices);
116 
117  Hashmap Copy(const Device& device);
118  Hashmap CPU();
119  Hashmap CUDA(int device_id = 0);
120 
121  int64_t Size() const;
122 
123  int64_t GetCapacity() const;
124  int64_t GetBucketCount() const;
125  Device GetDevice() const;
126  int64_t GetKeyBytesize() const;
127  int64_t GetValueBytesize() const;
128 
129  Tensor& GetKeyBuffer();
131 
134 
137  std::vector<int64_t> BucketSizes() const;
138 
140  float LoadFactor() const;
141 
142 protected:
143  void AssertKeyDtype(const Dtype& dtype_key,
144  const SizeVector& elem_shape) const;
145  void AssertValueDtype(const Dtype& dtype_val,
146  const SizeVector& elem_shape) const;
147 
148  Dtype GetKeyDtype() const { return dtype_key_; }
149  Dtype GetValueDtype() const { return dtype_value_; }
150 
151 private:
152  std::shared_ptr<DefaultDeviceHashmap> device_hashmap_;
153 
154  Dtype dtype_key_ = Dtype::Undefined;
155  Dtype dtype_value_ = Dtype::Undefined;
156 
157  SizeVector element_shape_key_;
158  SizeVector element_shape_value_;
159 };
160 
161 } // namespace core
162 } // namespace open3d
Hashmap CPU()
Definition: Hashmap.cpp:224
Tensor & GetKeyBuffer()
Definition: Hashmap.cpp:252
int64_t Size() const
Definition: Hashmap.cpp:238
Hashmap CUDA(int device_id=0)
Definition: Hashmap.cpp:231
Hashmap(int64_t init_capacity, const Dtype &dtype_key, const Dtype &dtype_value, const SizeVector &element_shape_key, const SizeVector &element_shape_value, const Device &device)
Definition: Hashmap.cpp:43
Tensor & GetValueBuffer()
Definition: Hashmap.cpp:253
Definition: Dtype.h:39
Hashmap Copy(const Device &device)
Definition: Hashmap.cpp:206
void Find(const Tensor &input_keys, Tensor &output_addrs, Tensor &output_masks)
Definition: Hashmap.cpp:148
DeviceHashmap< DefaultHash, DefaultKeyEq > DefaultDeviceHashmap
Definition: DeviceHashmap.h:194
Device GetDevice() const
Definition: Hashmap.cpp:244
void Rehash(int64_t buckets)
Definition: Hashmap.cpp:74
void GetActiveIndices(Tensor &output_indices)
Definition: Hashmap.cpp:198
void Activate(const Tensor &input_keys, Tensor &output_addrs, Tensor &output_masks)
Definition: Hashmap.cpp:120
void Erase(const Tensor &input_keys, Tensor &output_masks)
Definition: Hashmap.cpp:175
Definition: SizeVector.h:102
Tensor GetValueTensor()
Definition: Hashmap.cpp:264
int64_t GetBucketCount() const
Definition: Hashmap.cpp:241
int64_t GetValueBytesize() const
Definition: Hashmap.cpp:248
Definition: Device.h:39
Tensor GetKeyTensor()
Definition: Hashmap.cpp:255
static const Dtype Undefined
Definition: Dtype.h:41
~Hashmap()
Definition: Hashmap.h:67
void AssertValueDtype(const Dtype &dtype_val, const SizeVector &elem_shape) const
Definition: Hashmap.cpp:296
Dtype GetKeyDtype() const
Definition: Hashmap.h:148
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Tensor.h:48
Dtype GetValueDtype() const
Definition: Hashmap.h:149
Definition: Hashmap.h:42
void Insert(const Tensor &input_keys, const Tensor &input_values, Tensor &output_addrs, Tensor &output_masks)
Definition: Hashmap.cpp:78
float LoadFactor() const
Return size / bucket_count.
Definition: Hashmap.cpp:280
int64_t GetCapacity() const
Definition: Hashmap.cpp:240
static constexpr int64_t kDefaultElemsPerBucket
Definition: Hashmap.h:44
void AssertKeyDtype(const Dtype &dtype_key, const SizeVector &elem_shape) const
Definition: Hashmap.cpp:282
std::vector< int64_t > BucketSizes() const
Definition: Hashmap.cpp:275
int64_t GetKeyBytesize() const
Definition: Hashmap.cpp:245