10 #include <stdgpu/memory.h>
11 #include <stdgpu/utility.h>
12 #include <thrust/device_vector.h>
13 #include <thrust/transform.h>
15 #include <stdgpu/unordered_map.cuh>
16 #include <type_traits>
59 : std_allocator_(other.std_allocator_) {}
63 T* p = std_allocator_.allocate(n);
64 stdgpu::register_memory(p, n, stdgpu::dynamic_memory_type::device);
70 stdgpu::deregister_memory(p, n, stdgpu::dynamic_memory_type::device);
71 std_allocator_.deallocate(p, n);
76 return std_allocator_ == other.std_allocator_;
84 template <
typename T2>
92 template <
typename Key>
96 template <
typename Key,
typename Hash,
typename Eq>
97 using InternalStdGPUHashBackend =
98 stdgpu::unordered_map<Key,
104 template <
typename Key,
typename Hash,
typename Eq>
109 const std::vector<int64_t>& value_dsizes,
113 void Reserve(int64_t capacity)
override;
115 void Insert(
const void* input_keys,
116 const std::vector<const void*>& input_values_soa,
119 int64_t
count)
override;
121 void Find(
const void* input_keys,
124 int64_t
count)
override;
126 void Erase(
const void* input_keys,
128 int64_t
count)
override;
132 void Clear()
override;
134 int64_t
Size()
const override;
140 InternalStdGPUHashBackend<Key, Hash, Eq>
GetImpl()
const {
return impl_; }
148 InternalStdGPUHashBackend<Key, Hash, Eq>
impl_;
153 template <
typename Key,
typename Hash,
typename Eq>
155 int64_t init_capacity,
157 const std::vector<int64_t>& value_dsizes,
164 template <
typename Key,
typename Hash,
typename Eq>
170 template <
typename Key,
typename Hash,
typename Eq>
177 template <
typename Key,
typename Hash,
typename Eq>
180 const Key* input_keys,
184 uint32_t tid = threadIdx.x + blockIdx.x * blockDim.x;
185 if (tid >=
count)
return;
187 Key key = input_keys[tid];
188 auto iter = map.find(key);
189 bool flag = (iter != map.end());
190 output_masks[tid] = flag;
191 output_buf_indices[tid] = flag ? iter->second : 0;
194 template <
typename Key,
typename Hash,
typename Eq>
200 uint32_t threads = 128;
201 uint32_t blocks = (
count + threads - 1) / threads;
204 impl_, buffer_accessor_,
static_cast<const Key*
>(input_keys),
205 output_buf_indices, output_masks,
count);
210 template <
typename Key,
typename Hash,
typename Eq>
213 const Key* input_keys,
217 uint32_t tid = threadIdx.x + blockIdx.x * blockDim.x;
218 if (tid >=
count)
return;
220 Key key = input_keys[tid];
221 auto iter = map.find(key);
222 bool flag = (iter != map.end());
223 output_masks[tid] = flag;
224 output_buf_indices[tid] = flag ? iter->second : 0;
226 if (output_masks[tid]) {
227 output_masks[tid] = map.erase(key);
228 if (output_masks[tid]) {
229 buffer_accessor.
DeviceFree(output_buf_indices[tid]);
234 template <
typename Key,
typename Hash,
typename Eq>
239 uint32_t threads = 128;
240 uint32_t blocks = (
count + threads - 1) / threads;
248 impl_, buffer_accessor_,
static_cast<const Key*
>(input_keys),
249 output_buf_indices, output_masks,
count);
253 template <
typename Key>
261 template <
typename Key,
typename Hash,
typename Eq>
265 auto range = impl_.device_range();
267 thrust::transform(range.begin(), range.end(), output_indices,
273 template <
typename Key,
typename Hash,
typename Eq>
277 this->buffer_->ResetHeap();
280 template <
typename Key,
typename Hash,
typename Eq>
285 template <
typename Key,
typename Hash,
typename Eq>
288 return impl_.bucket_count();
291 template <
typename Key,
typename Hash,
typename Eq>
297 template <
typename Key,
typename Hash,
typename Eq>
300 return impl_.load_factor();
304 template <
typename Key,
typename Hash,
typename Eq,
typename block_t>
306 InternalStdGPUHashBackend<Key, Hash, Eq> map,
308 const Key* input_keys,
309 const void*
const* input_values_soa,
314 uint32_t tid = threadIdx.x + blockIdx.x * blockDim.x;
315 if (tid >=
count)
return;
317 Key key = input_keys[tid];
318 output_buf_indices[tid] = 0;
319 output_masks[tid] =
false;
322 auto res = map.emplace(key, 0);
327 auto key_ptr = buffer_accessor.
GetKeyPtr(buf_index);
331 *
static_cast<Key*
>(key_ptr) = key;
334 for (
int j = 0; j < n_values; ++j) {
335 const int64_t blocks_per_element =
338 block_t* dst_value =
static_cast<block_t*
>(
340 const block_t* src_value =
341 static_cast<const block_t*
>(input_values_soa[j]) +
342 blocks_per_element * tid;
343 for (
int b = 0; b < blocks_per_element; ++b) {
344 dst_value[b] = src_value[b];
349 res.first->second = buf_index;
352 output_buf_indices[tid] = buf_index;
353 output_masks[tid] =
true;
357 template <
typename Key,
typename Hash,
typename Eq>
359 const void* input_keys,
360 const std::vector<const void*>& input_values_soa,
365 uint32_t threads = 128;
366 uint32_t blocks = (
count + threads - 1) / threads;
368 thrust::device_vector<const void*> input_values_soa_device(
369 input_values_soa.begin(), input_values_soa.end());
371 int64_t n_values = input_values_soa.size();
372 const void*
const* ptr_input_values_soa =
373 thrust::raw_pointer_cast(input_values_soa_device.data());
375 DISPATCH_DIVISOR_SIZE_TO_BLOCK_T(
376 buffer_accessor_.common_block_size_, [&]() {
377 STDGPUInsertKernel<Key, Hash, Eq, block_t>
378 <<<blocks, threads, 0, core::cuda::GetStream()>>>(
379 impl_, buffer_accessor_,
380 static_cast<const Key*>(input_keys),
381 ptr_input_values_soa, output_buf_indices,
382 output_masks, count, n_values);
387 template <
typename Key,
typename Hash,
typename Eq>
390 this->capacity_ = capacity;
393 this->buffer_ = std::make_shared<HashBackendBuffer>(
394 this->capacity_, this->key_dsize_, this->value_dsizes_,
396 buffer_accessor_.Setup(*this->buffer_);
401 CUDAScopedStream scoped_stream(cuda::GetDefaultStream());
403 impl_ = InternalStdGPUHashBackend<Key, Hash, Eq>::createDeviceObject(
410 template <
typename Key,
typename Hash,
typename Eq>
414 buffer_accessor_.Shutdown(this->device_);
419 CUDAScopedStream scoped_stream(cuda::GetDefaultStream());
421 InternalStdGPUHashBackend<Key, Hash, Eq>::destroyDeviceObject(impl_);