Open3D (C++ API)  0.12.0
DeviceHashmap.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 #pragma once
28 
29 #include "open3d/core/CUDAUtils.h"
31 #include "open3d/core/Tensor.h"
33 
34 namespace open3d {
35 namespace core {
36 
37 class DefaultHash {
38  // Default constructor is required, since we need a struct instead of its
39  // pointer as a member in a hash table for CUDA kernel launches.
40  // Must set key_size_ before calling operator(), otherwise the behavior will
41  // be undefined.
42 public:
44  DefaultHash(int64_t key_size) : key_size_in_int_(key_size / sizeof(int)) {
45  if (key_size % 4 != 0 || key_size_in_int_ == 0) {
47  "[DefaultHash] Only support keys whose byte size is "
48  "multiples of sizeof(int)");
49  }
50  }
51 
52  uint64_t OPEN3D_HOST_DEVICE operator()(const void* key_ptr) const {
53  uint64_t hash = UINT64_C(14695981039346656037);
54 
55  auto cast_key_ptr = static_cast<const int*>(key_ptr);
56  for (int64_t i = 0; i < key_size_in_int_; ++i) {
57  hash ^= cast_key_ptr[i];
58  hash *= UINT64_C(1099511628211);
59  }
60  return hash;
61  }
62 
64 };
65 
66 class DefaultKeyEq {
67  // Default constructor is required, since we need a struct instead of its
68  // pointer as a member in a hash table for CUDA kernel launches.
69  // Must set key_size_ before calling operator(), otherwise the behavior will
70  // be undefined.
71 public:
73  DefaultKeyEq(int64_t key_size) : key_size_in_int_(key_size / sizeof(int)) {
74  if (key_size % 4 != 0 || key_size_in_int_ == 0) {
76  "[DefaultKeyEq] Only support keys whose byte size is "
77  "multiples of sizeof(int)");
78  }
79  }
80 
81  bool OPEN3D_HOST_DEVICE operator()(const void* lhs, const void* rhs) const {
82  if (lhs == nullptr || rhs == nullptr) {
83  return false;
84  }
85 
86  auto lhs_key_ptr = static_cast<const int*>(lhs);
87  auto rhs_key_ptr = static_cast<const int*>(rhs);
88 
89  bool is_eq = true;
90  for (int64_t i = 0; i < key_size_in_int_; ++i) {
91  is_eq = is_eq && (lhs_key_ptr[i] == rhs_key_ptr[i]);
92  }
93  return is_eq;
94  }
95 
97 };
98 
100 template <typename Hash, typename KeyEq>
102 public:
104  DeviceHashmap(int64_t init_buckets,
105  int64_t init_capacity,
106  int64_t dsize_key,
107  int64_t dsize_value,
108  const Device& device)
109  : bucket_count_(init_buckets),
110  capacity_(init_capacity),
111  dsize_key_(dsize_key),
112  dsize_value_(dsize_value),
113  device_(device) {}
114  virtual ~DeviceHashmap() {}
115 
122  virtual void Rehash(int64_t buckets) = 0;
123 
125  virtual void Insert(const void* input_keys,
126  const void* input_values,
127  addr_t* output_iterators,
128  bool* output_masks,
129  int64_t count) = 0;
130 
134  virtual void Activate(const void* input_keys,
135  addr_t* output_iterators,
136  bool* output_masks,
137  int64_t count) = 0;
138 
140  virtual void Find(const void* input_keys,
141  addr_t* output_iterators,
142  bool* output_masks,
143  int64_t count) = 0;
144 
146  virtual void Erase(const void* input_keys,
147  bool* output_masks,
148  int64_t count) = 0;
149 
151  virtual int64_t GetActiveIndices(addr_t* output_indices) = 0;
152 
153  virtual int64_t Size() const = 0;
154 
155  int64_t GetCapacity() const { return capacity_; }
156  int64_t GetBucketCount() const { return bucket_count_; }
157  Device GetDevice() const { return device_; }
158  int64_t GetKeyBytesize() const { return dsize_key_; }
159  int64_t GetValueBytesize() const { return dsize_value_; }
160 
161  Tensor& GetKeyBuffer() { return buffer_->GetKeyBuffer(); }
162  Tensor& GetValueBuffer() { return buffer_->GetValueBuffer(); }
163 
166  virtual std::vector<int64_t> BucketSizes() const = 0;
167 
169  virtual float LoadFactor() const = 0;
170 
171 public:
172  int64_t bucket_count_;
173  int64_t capacity_;
174  int64_t dsize_key_;
175  int64_t dsize_value_;
176 
178 
179  std::shared_ptr<HashmapBuffer> buffer_;
180 
182  return float(capacity_) / float(bucket_count_);
183  }
184 };
185 
190 
195 
196 std::shared_ptr<DefaultDeviceHashmap> CreateDefaultDeviceHashmap(
197  int64_t init_buckets,
198  int64_t init_capacity,
199  int64_t dsize_key,
200  int64_t dsize_value,
201  const Device& device);
202 
203 std::shared_ptr<DefaultDeviceHashmap> CreateDefaultCPUHashmap(
204  int64_t init_buckets,
205  int64_t init_capacity,
206  int64_t dsize_key,
207  int64_t dsize_value,
208  const Device& device);
209 
210 std::shared_ptr<DefaultDeviceHashmap> CreateDefaultCUDAHashmap(
211  int64_t init_buckets,
212  int64_t init_capacity,
213  int64_t dsize_key,
214  int64_t dsize_value,
215  const Device& device);
216 
217 } // namespace core
218 } // namespace open3d
DefaultKeyEq()
Definition: DeviceHashmap.h:72
int64_t GetKeyBytesize() const
Definition: DeviceHashmap.h:158
Tensor & GetKeyBuffer()
Definition: DeviceHashmap.h:161
int64_t dsize_value_
Definition: DeviceHashmap.h:175
bool OPEN3D_HOST_DEVICE operator()(const void *lhs, const void *rhs) const
Definition: DeviceHashmap.h:81
int64_t GetCapacity() const
Definition: DeviceHashmap.h:155
float avg_capacity_bucket_ratio()
Definition: DeviceHashmap.h:181
DeviceHashmap< DefaultHash, DefaultKeyEq > DefaultDeviceHashmap
Definition: DeviceHashmap.h:194
DefaultKeyEq(int64_t key_size)
Definition: DeviceHashmap.h:73
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
Tensor & GetValueBuffer()
Definition: DeviceHashmap.h:162
DefaultHash(int64_t key_size)
Definition: DeviceHashmap.h:44
Definition: DeviceHashmap.h:37
DeviceHashmap(int64_t init_buckets, int64_t init_capacity, int64_t dsize_key, int64_t dsize_value, const Device &device)
Comprehensive constructor for the developer.
Definition: DeviceHashmap.h:104
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample uint64_t
Definition: K4aPlugin.cpp:352
Base class: shared interface.
Definition: DeviceHashmap.h:101
int64_t capacity_
Definition: DeviceHashmap.h:173
Definition: DeviceHashmap.h:66
std::shared_ptr< DefaultDeviceHashmap > CreateDefaultDeviceHashmap(int64_t init_buckets, int64_t init_capacity, int64_t dsize_key, int64_t dsize_value, const Device &device)
Definition: DeviceHashmap.cpp:41
#define OPEN3D_HOST_DEVICE
Definition: CUDAUtils.h:54
std::shared_ptr< DefaultDeviceHashmap > CreateDefaultCPUHashmap(int64_t init_buckets, int64_t init_capacity, int64_t dsize_key, int64_t dsize_value, const Device &device)
Non-templated factory.
Definition: DefaultHashmapCPU.cpp:33
std::shared_ptr< HashmapBuffer > buffer_
Definition: DeviceHashmap.h:179
int64_t GetValueBytesize() const
Definition: DeviceHashmap.h:159
Definition: Device.h:39
Device GetDevice() const
Definition: DeviceHashmap.h:157
int64_t key_size_in_int_
Definition: DeviceHashmap.h:63
int64_t dsize_key_
Definition: DeviceHashmap.h:174
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c int
Definition: K4aPlugin.cpp:479
int count
Definition: FilePCD.cpp:61
DefaultHash()
Definition: DeviceHashmap.h:43
Device device_
Definition: DeviceHashmap.h:177
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Tensor.h:48
std::shared_ptr< DefaultDeviceHashmap > CreateDefaultCUDAHashmap(int64_t init_buckets, int64_t init_capacity, int64_t dsize_key, int64_t dsize_value, const Device &device)
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle float
Definition: K4aPlugin.cpp:465
virtual ~DeviceHashmap()
Definition: DeviceHashmap.h:114
uint32_t addr_t
Definition: HashmapBuffer.h:58
uint64_t OPEN3D_HOST_DEVICE operator()(const void *key_ptr) const
Definition: DeviceHashmap.h:52
Common CUDA utilities.
int64_t GetBucketCount() const
Definition: DeviceHashmap.h:156
int64_t key_size_in_int_
Definition: DeviceHashmap.h:96
int64_t bucket_count_
Definition: DeviceHashmap.h:172