29 #include <tbb/concurrent_unordered_map.h> 31 #include <unordered_map> 38 template <
typename Hash,
typename KeyEq>
42 int64_t init_capacity,
49 void Rehash(int64_t buckets)
override;
51 void Insert(
const void* input_keys,
52 const void* input_values,
55 int64_t
count)
override;
57 void Activate(
const void* input_keys,
60 int64_t count)
override;
62 void Find(
const void* input_keys,
65 int64_t count)
override;
67 void Erase(
const void* input_keys,
69 int64_t count)
override;
73 int64_t
Size()
const override;
79 std::shared_ptr<tbb::concurrent_unordered_map<void*, addr_t, Hash, KeyEq>>
85 const void* input_values,
90 void Allocate(int64_t capacity, int64_t buckets);
93 template <
typename Hash,
typename KeyEq>
95 int64_t init_capacity,
106 Allocate(init_capacity, init_buckets);
109 template <
typename Hash,
typename KeyEq>
112 template <
typename Hash,
typename KeyEq>
114 return impl_->size();
117 template <
typename Hash,
typename KeyEq>
119 const void* input_values,
125 float avg_capacity_per_bucket =
127 int64_t expected_buckets = std::max(
129 int64_t(
std::ceil(new_size / avg_capacity_per_bucket)));
132 InsertImpl(input_keys, input_values, output_addrs, output_masks, count);
135 template <
typename Hash,
typename KeyEq>
142 float avg_capacity_per_bucket =
144 int64_t expected_buckets = std::max(
146 int64_t(
std::ceil(new_size / avg_capacity_per_bucket)));
149 InsertImpl(input_keys,
nullptr, output_addrs, output_masks, count);
152 template <
typename Hash,
typename KeyEq>
157 #pragma omp parallel for 158 for (int64_t i = 0; i <
count; ++i) {
159 uint8_t* key =
const_cast<uint8_t*
>(
160 static_cast<const uint8_t*
>(input_keys) + this->
dsize_key_ * i);
162 auto iter =
impl_->find(key);
163 bool flag = (iter !=
impl_->end());
164 output_masks[i] = flag;
165 output_addrs[i] = flag ? iter->second : 0;
169 template <
typename Hash,
typename KeyEq>
173 for (int64_t i = 0; i <
count; ++i) {
174 uint8_t* key =
const_cast<uint8_t*
>(
175 static_cast<const uint8_t*
>(input_keys) + this->
dsize_key_ * i);
177 auto iter =
impl_->find(key);
178 bool flag = (iter !=
impl_->end());
179 output_masks[i] = flag;
182 impl_->unsafe_erase(iter);
188 template <
typename Hash,
typename KeyEq>
192 for (
auto iter =
impl_->begin(); iter !=
impl_->end(); ++iter, ++i) {
193 output_indices[i] =
static_cast<int64_t
>(iter->second);
199 template <
typename Hash,
typename KeyEq>
201 int64_t iterator_count =
Size();
206 if (iterator_count > 0) {
215 float avg_capacity_per_bucket =
218 int64_t new_capacity =
219 int64_t(
std::ceil(buckets * avg_capacity_per_bucket));
222 if (iterator_count > 0) {
227 static_cast<addr_t*
>(output_addrs.GetDataPtr()),
228 static_cast<bool*>(output_masks.GetDataPtr()),
232 impl_->rehash(buckets);
236 template <
typename Hash,
typename KeyEq>
238 int64_t bucket_count =
impl_->unsafe_bucket_count();
239 std::vector<int64_t> ret;
240 for (int64_t i = 0; i < bucket_count; ++i) {
241 ret.push_back(
impl_->unsafe_bucket_size(i));
246 template <
typename Hash,
typename KeyEq>
248 return impl_->load_factor();
251 template <
typename Hash,
typename KeyEq>
253 const void* input_values,
257 #pragma omp parallel for 258 for (int64_t i = 0; i <
count; ++i) {
259 const uint8_t* src_key =
260 static_cast<const uint8_t*
>(input_keys) + this->
dsize_key_ * i;
263 auto dst_kv_iter =
buffer_ctx_->ExtractIterator(dst_kv_addr);
265 uint8_t* dst_key =
static_cast<uint8_t*
>(dst_kv_iter.first);
266 uint8_t* dst_value =
static_cast<uint8_t*
>(dst_kv_iter.second);
267 std::memcpy(dst_key, src_key, this->
dsize_key_);
269 if (input_values !=
nullptr) {
270 const uint8_t* src_value =
271 static_cast<const uint8_t*
>(input_values) +
279 auto res =
impl_->insert({dst_key, dst_kv_addr});
281 output_addrs[i] = dst_kv_addr;
282 output_masks[i] = res.second;
285 #pragma omp parallel for 286 for (int64_t i = 0; i <
count; ++i) {
287 if (!output_masks[i]) {
295 template <
typename Hash,
typename KeyEq>
303 buffer_ctx_ = std::make_shared<CPUHashmapBufferContext>(
307 buffer_ctx_->Reset();
309 impl_ = std::make_shared<
310 tbb::concurrent_unordered_map<void*, addr_t, Hash, KeyEq>>(
311 buckets, Hash(this->dsize_key_), KeyEq(this->dsize_key_));
int64_t Size() const override
Definition: HashmapCPU.h:113
~CPUHashmap()
Definition: HashmapCPU.h:110
Tensor & GetKeyBuffer()
Definition: DeviceHashmap.h:161
int64_t dsize_value_
Definition: DeviceHashmap.h:175
void * GetDataPtr()
Definition: Tensor.h:961
void Allocate(int64_t capacity, int64_t buckets)
Definition: HashmapCPU.h:296
void Insert(const void *input_keys, const void *input_values, addr_t *output_addrs, bool *output_masks, int64_t count) override
Parallel insert contiguous arrays of keys and values.
Definition: HashmapCPU.h:118
Tensor & GetValueBuffer()
Definition: DeviceHashmap.h:162
std::shared_ptr< CPUHashmapBufferContext > buffer_ctx_
Definition: HashmapCPU.h:82
float LoadFactor() const override
Return size / bucket_count.
Definition: HashmapCPU.h:247
FN_SPECIFIERS MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:108
Base class: shared interface.
Definition: DeviceHashmap.h:101
int64_t capacity_
Definition: DeviceHashmap.h:173
void InsertImpl(const void *input_keys, const void *input_values, addr_t *output_addrs, bool *output_masks, int64_t count)
Definition: HashmapCPU.h:252
static const Dtype Int32
Definition: Dtype.h:44
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter.
Definition: Tensor.cpp:689
std::shared_ptr< HashmapBuffer > buffer_
Definition: DeviceHashmap.h:179
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:453
void Activate(const void *input_keys, addr_t *output_addrs, bool *output_masks, int64_t count) override
Definition: HashmapCPU.h:136
int64_t dsize_key_
Definition: DeviceHashmap.h:174
int count
Definition: FilePCD.cpp:61
std::vector< int64_t > BucketSizes() const override
Definition: HashmapCPU.h:237
Device device_
Definition: DeviceHashmap.h:177
static const Dtype Int64
Definition: Dtype.h:45
Definition: PinholeCameraIntrinsic.cpp:35
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
uint32_t addr_t
Definition: HashmapBuffer.h:58
void Find(const void *input_keys, addr_t *output_addrs, bool *output_masks, int64_t count) override
Parallel find a contiguous array of keys.
Definition: HashmapCPU.h:153
int64_t GetActiveIndices(addr_t *output_indices) override
Parallel collect all iterators in the hash table.
Definition: HashmapCPU.h:189
void Erase(const void *input_keys, bool *output_masks, int64_t count) override
Parallel erase a contiguous array of keys.
Definition: HashmapCPU.h:170
Definition: HashmapCPU.h:39
static const Dtype Bool
Definition: Dtype.h:48
CPUHashmap(int64_t init_buckets, int64_t init_capacity, int64_t dsize_key, int64_t dsize_value, const Device &device)
Definition: HashmapCPU.h:94
void Rehash(int64_t buckets) override
Definition: HashmapCPU.h:200
int64_t bucket_count_
Definition: DeviceHashmap.h:172
std::shared_ptr< tbb::concurrent_unordered_map< void *, addr_t, Hash, KeyEq > > impl_
Definition: HashmapCPU.h:80