10 #include <tbb/concurrent_unordered_map.h>
13 #include <unordered_map>
21 template <
typename Key,
typename Hash,
typename Eq>
26 const std::vector<int64_t>& value_dsizes,
30 void Reserve(int64_t capacity)
override;
32 void Insert(
const void* input_keys,
33 const std::vector<const void*>& input_values_soa,
36 int64_t
count)
override;
38 void Find(
const void* input_keys,
41 int64_t
count)
override;
43 void Erase(
const void* input_keys,
45 int64_t
count)
override;
49 void Clear()
override;
51 int64_t
Size()
const override;
56 std::shared_ptr<tbb::concurrent_unordered_map<Key, buf_index_t, Hash, Eq>>
61 void Allocate(int64_t capacity)
override;
65 std::shared_ptr<tbb::concurrent_unordered_map<Key, buf_index_t, Hash, Eq>>
71 template <
typename Key,
typename Hash,
typename Eq>
73 int64_t init_capacity,
75 const std::vector<int64_t>& value_dsizes,
81 template <
typename Key,
typename Hash,
typename Eq>
84 template <
typename Key,
typename Hash,
typename Eq>
89 template <
typename Key,
typename Hash,
typename Eq>
94 const Key* input_keys_templated =
static_cast<const Key*
>(input_keys);
96 #pragma omp parallel for num_threads(utility::EstimateMaxThreads())
97 for (int64_t i = 0; i <
count; ++i) {
98 const Key& key = input_keys_templated[i];
100 auto iter = impl_->find(key);
101 bool flag = (iter != impl_->end());
102 output_masks[i] = flag;
103 output_buf_indices[i] = flag ? iter->second : 0;
107 template <
typename Key,
typename Hash,
typename Eq>
111 const Key* input_keys_templated =
static_cast<const Key*
>(input_keys);
113 for (int64_t i = 0; i <
count; ++i) {
114 const Key& key = input_keys_templated[i];
116 auto iter = impl_->find(key);
117 bool flag = (iter != impl_->end());
118 output_masks[i] = flag;
120 buffer_accessor_->DeviceFree(iter->second);
121 impl_->unsafe_erase(iter);
126 template <
typename Key,
typename Hash,
typename Eq>
129 int64_t
count = impl_->size();
131 for (
auto iter = impl_->begin(); iter != impl_->end(); ++iter, ++i) {
132 output_buf_indices[i] =
static_cast<int64_t
>(iter->second);
138 template <
typename Key,
typename Hash,
typename Eq>
141 this->buffer_->ResetHeap();
144 template <
typename Key,
typename Hash,
typename Eq>
146 impl_->rehash(
std::ceil(capacity / impl_->max_load_factor()));
149 template <
typename Key,
typename Hash,
typename Eq>
151 return impl_->unsafe_bucket_count();
154 template <
typename Key,
typename Hash,
typename Eq>
156 int64_t bucket_count = impl_->unsafe_bucket_count();
157 std::vector<int64_t> ret;
158 for (int64_t i = 0; i < bucket_count; ++i) {
159 ret.push_back(impl_->unsafe_bucket_size(i));
164 template <
typename Key,
typename Hash,
typename Eq>
166 return impl_->load_factor();
169 template <
typename Key,
typename Hash,
typename Eq>
171 const void* input_keys,
172 const std::vector<const void*>& input_values_soa,
176 const Key* input_keys_templated =
static_cast<const Key*
>(input_keys);
178 size_t n_values = input_values_soa.size();
180 #pragma omp parallel for num_threads(utility::EstimateMaxThreads())
181 for (int64_t i = 0; i <
count; ++i) {
182 output_buf_indices[i] = 0;
183 output_masks[i] =
false;
185 const Key& key = input_keys_templated[i];
188 auto res = impl_->insert({key, 0});
192 buf_index_t buf_index = buffer_accessor_->DeviceAllocate();
193 void* key_ptr = buffer_accessor_->GetKeyPtr(buf_index);
196 *
static_cast<Key*
>(key_ptr) = key;
199 for (
size_t j = 0; j < n_values; ++j) {
200 uint8_t* dst_value =
static_cast<uint8_t*
>(
201 buffer_accessor_->GetValuePtr(buf_index, j));
203 const uint8_t* src_value =
204 static_cast<const uint8_t*
>(input_values_soa[j]) +
205 this->value_dsizes_[j] * i;
206 std::memcpy(dst_value, src_value, this->value_dsizes_[j]);
210 res.first->second = buf_index;
213 output_buf_indices[i] = buf_index;
214 output_masks[i] =
true;
219 template <
typename Key,
typename Hash,
typename Eq>
221 this->capacity_ = capacity;
223 this->buffer_ = std::make_shared<HashBackendBuffer>(
224 this->capacity_, this->key_dsize_, this->value_dsizes_,
228 std::make_shared<CPUHashBackendBufferAccessor>(*this->buffer_);
230 impl_ = std::make_shared<
231 tbb::concurrent_unordered_map<Key, buf_index_t, Hash, Eq>>(
232 capacity, Hash(), Eq());
Definition: DeviceHashBackend.h:20
Definition: TBBHashBackend.h:22
void Insert(const void *input_keys, const std::vector< const void * > &input_values_soa, buf_index_t *output_buf_indices, bool *output_masks, int64_t count) override
Parallel insert contiguous arrays of keys and values.
Definition: TBBHashBackend.h:170
void Find(const void *input_keys, buf_index_t *output_buf_indices, bool *output_masks, int64_t count) override
Parallel find a contiguous array of keys.
Definition: TBBHashBackend.h:90
void Erase(const void *input_keys, bool *output_masks, int64_t count) override
Parallel erase a contiguous array of keys.
Definition: TBBHashBackend.h:108
TBBHashBackend(int64_t init_capacity, int64_t key_dsize, const std::vector< int64_t > &value_dsizes, const Device &device)
Definition: TBBHashBackend.h:72
std::shared_ptr< CPUHashBackendBufferAccessor > buffer_accessor_
Definition: TBBHashBackend.h:68
std::vector< int64_t > BucketSizes() const override
Get the number of entries per bucket.
Definition: TBBHashBackend.h:155
std::shared_ptr< tbb::concurrent_unordered_map< Key, buf_index_t, Hash, Eq > > GetImpl() const
Definition: TBBHashBackend.h:57
void Reserve(int64_t capacity) override
Definition: TBBHashBackend.h:145
~TBBHashBackend()
Definition: TBBHashBackend.h:82
int64_t GetActiveIndices(buf_index_t *output_indices) override
Parallel collect all iterators in the hash table.
Definition: TBBHashBackend.h:127
void Clear() override
Clear stored map without reallocating memory.
Definition: TBBHashBackend.h:139
std::shared_ptr< tbb::concurrent_unordered_map< Key, buf_index_t, Hash, Eq > > impl_
Definition: TBBHashBackend.h:62
int64_t GetBucketCount() const override
Get the number of buckets of the hash map.
Definition: TBBHashBackend.h:150
void Free() override
Definition: TBBHashBackend.h:62
void Allocate(int64_t capacity) override
Definition: TBBHashBackend.h:220
int64_t Size() const override
Get the size (number of valid entries) of the hash map.
Definition: TBBHashBackend.h:85
float LoadFactor() const override
Get the current load factor, defined as size / bucket count.
Definition: TBBHashBackend.h:165
uint32_t buf_index_t
Definition: HashBackendBuffer.h:44
FN_SPECIFIERS MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:89
Definition: PinholeCameraIntrinsic.cpp:16