ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
CUDAHashBackendBufferAccessor.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <assert.h>
11 
12 #include <memory>
13 #include <vector>
14 
20 
21 namespace cloudViewer {
22 namespace core {
23 
25 public:
26  __host__ void Setup(HashBackendBuffer &hashmap_buffer) {
27  Device device = hashmap_buffer.GetDevice();
28 
29  // Properties
30  capacity_ = hashmap_buffer.GetCapacity();
31  key_dsize_ = hashmap_buffer.GetKeyDsize();
32 
33  std::vector<int64_t> value_dsizes_host =
34  hashmap_buffer.GetValueDsizes();
35  std::vector<int64_t> value_blocks_per_element_host =
36  hashmap_buffer.GetValueBlocksPerElement();
37  n_values_ = value_blocks_per_element_host.size();
38 
39  value_dsizes_ = static_cast<int64_t *>(
40  MemoryManager::Malloc(n_values_ * sizeof(int64_t), device));
41  value_blocks_per_element_ = static_cast<int64_t *>(
42  MemoryManager::Malloc(n_values_ * sizeof(int64_t), device));
43 
45  value_dsizes_host.data(),
46  n_values_ * sizeof(int64_t));
48  value_blocks_per_element_host.data(),
49  n_values_ * sizeof(int64_t));
50 
51  common_block_size_ = hashmap_buffer.GetCommonBlockSize();
52 
53  // Pointers
54  heap_ = hashmap_buffer.GetIndexHeap().GetDataPtr<buf_index_t>();
55  keys_ = hashmap_buffer.GetKeyBuffer().GetDataPtr<uint8_t>();
56 
57  std::vector<Tensor> value_buffers = hashmap_buffer.GetValueBuffers();
58  std::vector<uint8_t *> value_ptrs(n_values_);
59  for (size_t i = 0; i < n_values_; ++i) {
60  value_ptrs[i] = value_buffers[i].GetDataPtr<uint8_t>();
61  cudaMemset(value_ptrs[i], 0, capacity_ * value_dsizes_host[i]);
62  }
63  values_ = static_cast<uint8_t **>(
64  MemoryManager::Malloc(n_values_ * sizeof(uint8_t *), device));
65  MemoryManager::MemcpyFromHost(values_, device, value_ptrs.data(),
66  n_values_ * sizeof(uint8_t *));
67 
68  heap_top_ = hashmap_buffer.GetHeapTop().cuda.GetDataPtr<int>();
70  CLOUDVIEWER_CUDA_CHECK(cudaGetLastError());
71  }
72 
73  __host__ void Shutdown(const Device &device) {
77  }
78 
79  __device__ buf_index_t DeviceAllocate() {
80  int index = atomicAdd(heap_top_, 1);
81  return heap_[index];
82  }
83  __device__ void DeviceFree(buf_index_t ptr) {
84  int index = atomicSub(heap_top_, 1);
85  heap_[index - 1] = ptr;
86  }
87 
88  __device__ void *GetKeyPtr(buf_index_t ptr) {
89  return keys_ + ptr * key_dsize_;
90  }
91  __device__ void *GetValuePtr(buf_index_t ptr, int value_idx = 0) {
92  return values_[value_idx] + ptr * value_dsizes_[value_idx];
93  }
94 
95 public:
96  buf_index_t *heap_; /* [N] */
97  int *heap_top_ = nullptr; /* [1] */
98 
99  uint8_t *keys_; /* [N] * sizeof(Key) */
100  int64_t key_dsize_;
101 
102  size_t n_values_;
103  uint8_t **values_;
104 
106 
107  int64_t *value_dsizes_;
109 
110  int64_t capacity_;
111 };
112 
113 } // namespace core
114 } // namespace cloudViewer
Common CUDA utilities.
#define CLOUDVIEWER_CUDA_CHECK(err)
Definition: CUDAUtils.h:47
__device__ void * GetValuePtr(buf_index_t ptr, int value_idx=0)
__host__ void Setup(HashBackendBuffer &hashmap_buffer)
int64_t GetKeyDsize() const
Return key's data size in bytes.
Tensor GetKeyBuffer() const
Return the key buffer tensor.
Device GetDevice() const
Return device of the buffer.
std::vector< int64_t > GetValueDsizes() const
Return value's data sizes in bytes.
std::vector< int64_t > GetValueBlocksPerElement() const
Return value's data sizes in the unit of common block size divisor.
int64_t GetCommonBlockSize() const
Get the common block size divisor of all values types.
std::vector< Tensor > GetValueBuffers() const
Return the value buffer tensors.
int64_t GetCapacity() const
Return capacity of the buffer.
Tensor GetIndexHeap() const
Return the index heap tensor.
static void MemcpyFromHost(void *dst_ptr, const Device &dst_device, const void *host_ptr, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default src_device.
static void * Malloc(size_t byte_size, const Device &device)
static void Free(void *ptr, const Device &device)
Frees previously allocated memory at address ptr on device device.
Generic file read and write utility for python interface.