Open3D (C++ API)  0.11.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 
31 
32 namespace open3d {
33 namespace core {
34 
35 struct DefaultHash {
36  // Default constructor is required, since we need a struct instead of its
37  // pointer as a member in a hash table for CUDA kernel launches.
38  // Must set key_size_ before calling operator(), otherwise the behavior will
39  // be undefined.
41  DefaultHash(size_t key_size) : key_size_in_int_(key_size / sizeof(int)) {
42  if (key_size_in_int_ == 0) {
44  "[DefaultHash] Only support keys whose byte size is "
45  "multiples of sizeof(int)");
46  }
47  }
48 
49  uint64_t OPEN3D_HOST_DEVICE operator()(const void* key_ptr) const {
50  uint64_t hash = UINT64_C(14695981039346656037);
51 
52  auto cast_key_ptr = static_cast<const int*>(key_ptr);
53  for (size_t i = 0; i < key_size_in_int_; ++i) {
54  hash ^= cast_key_ptr[i];
55  hash *= UINT64_C(1099511628211);
56  }
57  return hash;
58  }
59 
61 };
62 
63 struct DefaultKeyEq {
64  // Default constructor is required, since we need a struct instead of its
65  // pointer as a member in a hash table for CUDA kernel launches.
66  // Must set key_size_ before calling operator(), otherwise the behavior will
67  // be undefined.
69  DefaultKeyEq(size_t key_size) : key_size_in_int_(key_size / sizeof(int)) {}
70 
71  bool OPEN3D_HOST_DEVICE operator()(const void* lhs, const void* rhs) const {
72  if (lhs == nullptr || rhs == nullptr) {
73  return false;
74  }
75 
76  auto lhs_key_ptr = static_cast<const int*>(lhs);
77  auto rhs_key_ptr = static_cast<const int*>(rhs);
78 
79  bool is_eq = true;
80  for (size_t i = 0; i < key_size_in_int_; ++i) {
81  is_eq = is_eq && (lhs_key_ptr[i] == rhs_key_ptr[i]);
82  }
83  return is_eq;
84  }
85 
87 };
88 
90 template <typename Hash, typename KeyEq>
92 public:
94  DeviceHashmap(size_t init_buckets,
95  size_t init_capacity,
96  size_t dsize_key,
97  size_t dsize_value,
98  const Device& device)
99  : bucket_count_(init_buckets),
100  capacity_(init_capacity),
101  dsize_key_(dsize_key),
102  dsize_value_(dsize_value),
103  device_(device) {}
104  virtual ~DeviceHashmap() {}
105 
112  virtual void Rehash(size_t buckets) = 0;
113 
115  virtual void Insert(const void* input_keys,
116  const void* input_values,
117  iterator_t* output_iterators,
118  bool* output_masks,
119  size_t count) = 0;
120 
124  virtual void Activate(const void* input_keys,
125  iterator_t* output_iterators,
126  bool* output_masks,
127  size_t count) = 0;
128 
130  virtual void Find(const void* input_keys,
131  iterator_t* output_iterators,
132  bool* output_masks,
133  size_t count) = 0;
134 
136  virtual void Erase(const void* input_keys,
137  bool* output_masks,
138  size_t count) = 0;
139 
141  virtual size_t GetIterators(iterator_t* output_iterators) = 0;
142 
144  virtual void UnpackIterators(const iterator_t* input_iterators,
145  const bool* input_masks,
146  void* output_keys,
147  void* output_values,
148  size_t count) = 0;
149 
151  virtual void AssignIterators(iterator_t* input_iterators,
152  const bool* input_masks,
153  const void* input_values,
154  size_t count) = 0;
155 
156  virtual size_t Size() const = 0;
157 
160  virtual std::vector<size_t> BucketSizes() const = 0;
161 
163  virtual float LoadFactor() const = 0;
164 
165  int GetBucketCount() const { return bucket_count_; }
166  int GetCapacity() const { return capacity_; }
167  int GetKeyBytesize() const { return dsize_key_; }
168  int GetValueBytesize() const { return dsize_value_; }
169  Device GetDevice() const { return device_; }
170 
171 public:
173  size_t capacity_;
174  size_t dsize_key_;
175  size_t dsize_value_;
177 
179  return float(capacity_) / float(bucket_count_);
180  }
181 };
182 
187 
192 
193 std::shared_ptr<DefaultDeviceHashmap> CreateDefaultDeviceHashmap(
194  size_t init_buckets,
195  size_t init_capacity,
196  size_t dsize_key,
197  size_t dsize_value,
198  const Device& device);
199 
200 std::shared_ptr<DefaultDeviceHashmap> CreateDefaultCPUHashmap(
201  size_t init_buckets,
202  size_t init_capacity,
203  size_t dsize_key,
204  size_t dsize_value,
205  const Device& device);
206 
207 std::shared_ptr<DefaultDeviceHashmap> CreateDefaultCUDAHashmap(
208  size_t init_buckets,
209  size_t init_capacity,
210  size_t dsize_key,
211  size_t dsize_value,
212  const Device& device);
213 
214 } // namespace core
215 } // namespace open3d
std::shared_ptr< DefaultDeviceHashmap > CreateDefaultDeviceHashmap(size_t init_buckets, size_t init_capacity, size_t dsize_key, size_t dsize_value, const Device &device)
Definition: DeviceHashmap.cpp:43
Definition: DeviceHashmap.h:63
bool OPEN3D_HOST_DEVICE operator()(const void *lhs, const void *rhs) const
Definition: DeviceHashmap.h:71
float avg_capacity_bucket_ratio()
Definition: DeviceHashmap.h:178
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
size_t key_size_in_int_
Definition: DeviceHashmap.h:86
size_t bucket_count_
Definition: DeviceHashmap.h:172
int GetCapacity() const
Definition: DeviceHashmap.h:166
std::shared_ptr< DefaultDeviceHashmap > CreateDefaultCPUHashmap(size_t init_buckets, size_t init_capacity, size_t dsize_key, size_t dsize_value, const Device &device)
Non-templated factory.
Definition: DefaultHashmapCPU.cpp:33
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:349
Base class: shared interface.
Definition: DeviceHashmap.h:91
int GetBucketCount() const
Definition: DeviceHashmap.h:165
Definition: DeviceHashmap.h:35
#define OPEN3D_HOST_DEVICE
Definition: CUDAUtils.h:54
size_t capacity_
Definition: DeviceHashmap.h:173
std::shared_ptr< DefaultDeviceHashmap > CreateDefaultCUDAHashmap(size_t init_buckets, size_t init_capacity, size_t dsize_key, size_t dsize_value, const Device &device)
Definition: Device.h:39
uint64_t OPEN3D_HOST_DEVICE operator()(const void *key_ptr) const
Definition: DeviceHashmap.h:49
Device GetDevice() const
Definition: DeviceHashmap.h:169
Definition: Traits.h:51
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:476
int count
Definition: FilePCD.cpp:61
DefaultHash()
Definition: DeviceHashmap.h:40
DefaultHash(size_t key_size)
Definition: DeviceHashmap.h:41
DeviceHashmap< DefaultHash, DefaultKeyEq > DefaultDeviceHashmap
Definition: DeviceHashmap.h:191
Device device_
Definition: DeviceHashmap.h:176
Definition: PinholeCameraIntrinsic.cpp:35
DefaultKeyEq()
Definition: DeviceHashmap.h:68
int GetValueBytesize() const
Definition: DeviceHashmap.h:168
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:462
DeviceHashmap(size_t init_buckets, size_t init_capacity, size_t dsize_key, size_t dsize_value, const Device &device)
Comprehensive constructor for the developer.
Definition: DeviceHashmap.h:94
virtual ~DeviceHashmap()
Definition: DeviceHashmap.h:104
size_t key_size_in_int_
Definition: DeviceHashmap.h:60
size_t dsize_key_
Definition: DeviceHashmap.h:174
DefaultKeyEq(size_t key_size)
Definition: DeviceHashmap.h:69
size_t dsize_value_
Definition: DeviceHashmap.h:175
int GetKeyBytesize() const
Definition: DeviceHashmap.h:167