10 #include <stdgpu/memory.h>
11 #include <thrust/device_vector.h>
12 #include <thrust/transform.h>
14 #include <stdgpu/unordered_map.cuh>
15 #include <type_traits>
58 : std_allocator_(other.std_allocator_) {}
62 T* p = std_allocator_.allocate(n);
63 stdgpu::register_memory(p, n, stdgpu::dynamic_memory_type::device);
69 stdgpu::deregister_memory(p, n, stdgpu::dynamic_memory_type::device);
70 std_allocator_.deallocate(p, n);
75 return std_allocator_ == other.std_allocator_;
83 template <
typename T2>
91 template <
typename Key>
95 template <
typename Key,
typename Hash,
typename Eq>
97 stdgpu::unordered_map<Key,
103 template <
typename Key,
typename Hash,
typename Eq>
108 const std::vector<int64_t>& value_dsizes,
112 void Reserve(int64_t capacity)
override;
114 void Insert(
const void* input_keys,
115 const std::vector<const void*>& input_values_soa,
118 int64_t
count)
override;
120 void Find(
const void* input_keys,
123 int64_t
count)
override;
125 void Erase(
const void* input_keys,
127 int64_t
count)
override;
131 void Clear()
override;
133 int64_t
Size()
const override;
152 template <
typename Key,
typename Hash,
typename Eq>
154 int64_t init_capacity,
156 const std::vector<int64_t>& value_dsizes,
163 template <
typename Key,
typename Hash,
typename Eq>
169 template <
typename Key,
typename Hash,
typename Eq>
176 template <
typename Key,
typename Hash,
typename Eq>
179 const Key* input_keys,
183 uint32_t tid = threadIdx.x + blockIdx.x * blockDim.x;
184 if (tid >=
count)
return;
186 Key key = input_keys[tid];
187 auto iter = map.find(key);
188 bool flag = (iter != map.end());
189 output_masks[tid] = flag;
190 output_buf_indices[tid] = flag ? iter->second : 0;
193 template <
typename Key,
typename Hash,
typename Eq>
203 impl_, buffer_accessor_,
static_cast<const Key*
>(input_keys),
204 output_buf_indices, output_masks,
count);
209 template <
typename Key,
typename Hash,
typename Eq>
212 const Key* input_keys,
216 uint32_t tid = threadIdx.x + blockIdx.x * blockDim.x;
217 if (tid >=
count)
return;
219 Key key = input_keys[tid];
220 auto iter = map.find(key);
221 bool flag = (iter != map.end());
222 output_masks[tid] = flag;
223 output_buf_indices[tid] = flag ? iter->second : 0;
225 if (output_masks[tid]) {
226 output_masks[tid] = map.erase(key);
227 if (output_masks[tid]) {
228 buffer_accessor.
DeviceFree(output_buf_indices[tid]);
233 template <
typename Key,
typename Hash,
typename Eq>
247 impl_, buffer_accessor_,
static_cast<const Key*
>(input_keys),
248 output_buf_indices, output_masks,
count);
252 template <
typename Key>
260 template <
typename Key,
typename Hash,
typename Eq>
264 auto range = impl_.device_range();
266 thrust::transform(range.begin(), range.end(), output_indices,
272 template <
typename Key,
typename Hash,
typename Eq>
276 this->buffer_->ResetHeap();
279 template <
typename Key,
typename Hash,
typename Eq>
284 template <
typename Key,
typename Hash,
typename Eq>
287 return impl_.bucket_count();
290 template <
typename Key,
typename Hash,
typename Eq>
296 template <
typename Key,
typename Hash,
typename Eq>
299 return impl_.load_factor();
303 template <
typename Key,
typename Hash,
typename Eq,
typename block_t>
307 const Key* input_keys,
308 const void*
const* input_values_soa,
313 uint32_t tid = threadIdx.x + blockIdx.x * blockDim.x;
314 if (tid >=
count)
return;
316 Key key = input_keys[tid];
317 output_buf_indices[tid] = 0;
318 output_masks[tid] =
false;
321 auto res = map.emplace(key, 0);
326 auto key_ptr = buffer_accessor.
GetKeyPtr(buf_index);
330 *
static_cast<Key*
>(key_ptr) = key;
333 for (
int j = 0; j < n_values; ++j) {
334 const int64_t blocks_per_element =
337 block_t* dst_value =
static_cast<block_t*
>(
339 const block_t* src_value =
340 static_cast<const block_t*
>(input_values_soa[j]) +
341 blocks_per_element * tid;
342 for (
int b = 0; b < blocks_per_element; ++b) {
343 dst_value[b] = src_value[b];
348 res.first->second = buf_index;
351 output_buf_indices[tid] = buf_index;
352 output_masks[tid] =
true;
356 template <
typename Key,
typename Hash,
typename Eq>
358 const void* input_keys,
359 const std::vector<const void*>& input_values_soa,
367 thrust::device_vector<const void*> input_values_soa_device(
368 input_values_soa.begin(), input_values_soa.end());
370 int64_t n_values = input_values_soa.size();
371 const void*
const* ptr_input_values_soa =
372 thrust::raw_pointer_cast(input_values_soa_device.data());
374 DISPATCH_DIVISOR_SIZE_TO_BLOCK_T(
375 buffer_accessor_.common_block_size_, [&]() {
376 STDGPUInsertKernel<Key, Hash, Eq, block_t>
377 <<<blocks, threads, 0, core::cuda::GetStream()>>>(
378 impl_, buffer_accessor_,
379 static_cast<const Key*>(input_keys),
380 ptr_input_values_soa, output_buf_indices,
381 output_masks, count, n_values);
386 template <
typename Key,
typename Hash,
typename Eq>
389 this->capacity_ = capacity;
392 this->buffer_ = std::make_shared<HashBackendBuffer>(
393 this->capacity_, this->key_dsize_, this->value_dsizes_,
395 buffer_accessor_.Setup(*this->buffer_);
400 CUDAScopedStream scoped_stream(cuda::GetDefaultStream());
409 template <
typename Key,
typename Hash,
typename Eq>
413 buffer_accessor_.Shutdown(this->device_);
418 CUDAScopedStream scoped_stream(cuda::GetDefaultStream());
#define OPEN3D_HOST_DEVICE
Definition: CUDAUtils.h:44
#define LogError(...)
Definition: Logging.h:48
Definition: CUDAHashBackendBufferAccessor.h:24
int64_t * value_blocks_per_element_
Definition: CUDAHashBackendBufferAccessor.h:108
__device__ buf_index_t DeviceAllocate()
Definition: CUDAHashBackendBufferAccessor.h:79
__device__ void DeviceFree(buf_index_t ptr)
Definition: CUDAHashBackendBufferAccessor.h:83
__device__ void * GetKeyPtr(buf_index_t ptr)
Definition: CUDAHashBackendBufferAccessor.h:88
__device__ void * GetValuePtr(buf_index_t ptr, int value_idx=0)
Definition: CUDAHashBackendBufferAccessor.h:91
When CUDA is not enabled, this is a dummy class.
Definition: CUDAUtils.h:214
Definition: DeviceHashBackend.h:20
Device device_
Definition: DeviceHashBackend.h:100
Definition: StdAllocator.h:23
Definition: StdGPUHashBackend.h:32
T * allocate(std::size_t n)
Allocates memory of size n.
Definition: StdGPUHashBackend.h:61
StdGPUAllocator()=default
Default constructor.
StdGPUAllocator(int device_id)
Constructor from device.
Definition: StdGPUHashBackend.h:41
StdGPUAllocator(const StdGPUAllocator &)=default
Default copy constructor.
void deallocate(T *p, std::size_t n)
Deallocates memory from pointer p of size n .
Definition: StdGPUHashBackend.h:68
bool operator==(const StdGPUAllocator &other)
Returns true if the instances are equal, false otherwise.
Definition: StdGPUHashBackend.h:74
StdGPUAllocator(StdGPUAllocator &&)=default
Default move constructor.
T value_type
T.
Definition: StdGPUHashBackend.h:35
StdGPUAllocator & operator=(StdGPUAllocator &&)=default
Default move assignment operator.
StdGPUAllocator & operator=(const StdGPUAllocator &)=default
Default copy assignment operator.
bool operator!=(const StdGPUAllocator &other)
Returns true if the instances are not equal, false otherwise.
Definition: StdGPUHashBackend.h:79
StdGPUAllocator(const StdGPUAllocator< U > &other)
Rebind copy constructor.
Definition: StdGPUHashBackend.h:57
Definition: StdGPUHashBackend.h:104
StdGPUHashBackend(int64_t init_capacity, int64_t key_dsize, const std::vector< int64_t > &value_dsizes, const Device &device)
Definition: StdGPUHashBackend.h:153
void Erase(const void *input_keys, bool *output_masks, int64_t count) override
Parallel erase a contiguous array of keys.
Definition: StdGPUHashBackend.h:234
~StdGPUHashBackend()
Definition: StdGPUHashBackend.h:164
float LoadFactor() const override
Get the current load factor, defined as size / bucket count.
Definition: StdGPUHashBackend.h:297
InternalStdGPUHashBackend< Key, Hash, Eq > GetImpl() const
Definition: StdGPUHashBackend.h:139
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: StdGPUHashBackend.h:194
void Free()
Definition: StdGPUHashBackend.h:410
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: StdGPUHashBackend.h:357
std::vector< int64_t > BucketSizes() const override
Get the number of entries per bucket.
Definition: StdGPUHashBackend.h:291
InternalStdGPUHashBackend< Key, Hash, Eq > impl_
Definition: StdGPUHashBackend.h:147
void Reserve(int64_t capacity) override
Definition: StdGPUHashBackend.h:280
int64_t GetBucketCount() const override
Get the number of buckets of the hash map.
Definition: StdGPUHashBackend.h:285
int64_t GetActiveIndices(buf_index_t *output_indices) override
Parallel collect all iterators in the hash table.
Definition: StdGPUHashBackend.h:261
int64_t Size() const override
Get the size (number of valid entries) of the hash map.
Definition: StdGPUHashBackend.h:170
void Allocate(int64_t capacity)
Definition: StdGPUHashBackend.h:387
CUDAHashBackendBufferAccessor buffer_accessor_
Definition: StdGPUHashBackend.h:149
void Clear() override
Clear stored map without reallocating memory.
Definition: StdGPUHashBackend.h:273
T * GetDataPtr()
Definition: Tensor.h:1133
void Synchronize()
Definition: CUDAUtils.cpp:58
__global__ void STDGPUFindKernel(InternalStdGPUHashBackend< Key, Hash, Eq > map, CUDAHashBackendBufferAccessor buffer_accessor, const Key *input_keys, buf_index_t *output_buf_indices, bool *output_masks, int64_t count)
Definition: StdGPUHashBackend.h:177
uint32_t buf_index_t
Definition: HashBackendBuffer.h:44
__global__ void STDGPUEraseKernel(InternalStdGPUHashBackend< Key, Hash, Eq > map, CUDAHashBackendBufferAccessor buffer_accessor, const Key *input_keys, buf_index_t *output_buf_indices, bool *output_masks, int64_t count)
Definition: StdGPUHashBackend.h:210
const Dtype Int32
Definition: Dtype.cpp:46
stdgpu::unordered_map< Key, buf_index_t, Hash, Eq, InternalStdGPUHashBackendAllocator< Key > > InternalStdGPUHashBackend
Definition: StdGPUHashBackend.h:101
__global__ void STDGPUInsertKernel(InternalStdGPUHashBackend< Key, Hash, Eq > map, CUDAHashBackendBufferAccessor buffer_accessor, const Key *input_keys, const void *const *input_values_soa, buf_index_t *output_buf_indices, bool *output_masks, int64_t count, int64_t n_values)
Definition: StdGPUHashBackend.h:304
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 k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle uint32_t
Definition: K4aPlugin.cpp:548
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 k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle image_handle timestamp_usec white_balance image_handle k4a_device_configuration_t config device_handle char size_t serial_number_size bool int32_t int32_t int32_t int32_t k4a_color_control_mode_t default_mode value const const k4a_calibration_t calibration char size_t
Definition: K4aPlugin.cpp:719
Definition: PinholeCameraIntrinsic.cpp:16