Open3D (C++ API)  0.11.0
InternalKvPairManager.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <assert.h>
30 
31 #include <memory>
32 #include <vector>
33 
34 #include "open3d/core/CUDAUtils.h"
38 
39 namespace open3d {
40 namespace core {
41 
45 public:
46  uint8_t *keys_; /* [N] * sizeof(Key) */
47  uint8_t *values_; /* [N] * sizeof(Value) */
48  addr_t *heap_; /* [N] */
49  int *heap_counter_; /* [1] */
50 
51 public:
54  int capacity_;
55 
56 public:
57  // The value_ array's size is FIXED.
58  // The heap_ array stores the addresses of the values.
59  // Only the unallocated part is maintained.
60  // (ONLY care about the heap above the heap counter. Below is
61  // meaningless.)
62  // During Allocate, ptr is extracted from the heap;
63  // During Free, ptr is put back to the top of the heap.
64  // ---------------------------------------------------------------------
65  // heap ---Malloc--> heap ---Malloc--> heap ---Free(0)--> heap
66  // N-1 N-1 N-1 N-1 |
67  // . . . . |
68  // . . . . |
69  // . . . . |
70  // 3 3 3 3 |
71  // 2 2 2 <- 2 |
72  // 1 1 <- 1 0 <- |
73  // 0 <- heap_counter 0 0 0
74 
75  __device__ addr_t Allocate() {
76  int index = atomicAdd(heap_counter_, 1);
77  return heap_[index];
78  }
79 
80  __device__ void Free(addr_t ptr) {
81  int index = atomicSub(heap_counter_, 1);
82  heap_[index - 1] = ptr;
83  }
84 
86  return iterator_t(keys_ + ptr * dsize_key_,
87  values_ + ptr * dsize_value_);
88  }
89 };
90 
93  const int i = blockIdx.x * blockDim.x + threadIdx.x;
94  if (i < ctx.capacity_) {
95  ctx.heap_[i] = i;
96  }
97 }
98 
100 public:
103 
104 public:
105  InternalKvPairManager(int capacity,
106  int dsize_key,
107  int dsize_value,
108  const Device &device)
109  : device_(device) {
110  gpu_context_.capacity_ = capacity;
111  gpu_context_.dsize_key_ = dsize_key;
112  gpu_context_.dsize_value_ = dsize_value;
113 
114  gpu_context_.heap_counter_ =
115  static_cast<int *>(MemoryManager::Malloc(sizeof(int), device_));
116  gpu_context_.heap_ = static_cast<addr_t *>(
117  MemoryManager::Malloc(capacity * sizeof(addr_t), device_));
118  gpu_context_.keys_ = static_cast<uint8_t *>(
119  MemoryManager::Malloc(capacity * dsize_key, device_));
120  gpu_context_.values_ = static_cast<uint8_t *>(
121  MemoryManager::Malloc(capacity * dsize_value, device_));
122 
123  const int blocks = (capacity + kThreadsPerBlock - 1) / kThreadsPerBlock;
124  ResetInternalKvPairManagerKernel<<<blocks, kThreadsPerBlock>>>(
125  gpu_context_);
126  OPEN3D_CUDA_CHECK(cudaDeviceSynchronize());
127  OPEN3D_CUDA_CHECK(cudaGetLastError());
128 
129  int heap_counter = 0;
131  cudaMemset(gpu_context_.values_, 0, capacity * dsize_value));
132  MemoryManager::Memcpy(gpu_context_.heap_counter_, device_,
133  &heap_counter, Device("CPU:0"), sizeof(int));
134  }
135 
137  MemoryManager::Free(gpu_context_.heap_counter_, device_);
138  MemoryManager::Free(gpu_context_.heap_, device_);
139  MemoryManager::Free(gpu_context_.keys_, device_);
140  MemoryManager::Free(gpu_context_.values_, device_);
141  }
142 
143  std::vector<int> DownloadHeap() {
144  std::vector<int> ret;
145  ret.resize(gpu_context_.capacity_);
146  MemoryManager::Memcpy(ret.data(), Device("CPU:0"), gpu_context_.heap_,
147  device_, sizeof(int) * gpu_context_.capacity_);
148  return ret;
149  }
150 
151  int heap_counter() {
152  int heap_counter;
153  MemoryManager::Memcpy(&heap_counter, Device("CPU:0"),
154  gpu_context_.heap_counter_, device_, sizeof(int));
155  return heap_counter;
156  }
157 };
158 } // namespace core
159 } // namespace open3d
int * heap_counter_
Definition: InternalKvPairManager.h:49
__global__ void ResetInternalKvPairManagerKernel(InternalKvPairManagerContext ctx)
Definition: InternalKvPairManager.h:91
__device__ void Free(addr_t ptr)
Definition: InternalKvPairManager.h:80
__device__ addr_t Allocate()
Definition: InternalKvPairManager.h:75
static void Memcpy(void *dst_ptr, const Device &dst_device, const void *src_ptr, const Device &src_device, size_t num_bytes)
Definition: MemoryManager.cpp:48
uint8_t * values_
Definition: InternalKvPairManager.h:47
__device__ iterator_t extract_iterator(addr_t ptr)
Definition: InternalKvPairManager.h:85
static void Free(void *ptr, const Device &device)
Definition: MemoryManager.cpp:44
int heap_counter()
Definition: InternalKvPairManager.h:151
#define OPEN3D_CUDA_CHECK(err)
Definition: CUDAUtils.h:57
Definition: InternalKvPairManager.h:99
static void * Malloc(size_t byte_size, const Device &device)
Definition: MemoryManager.cpp:40
int dsize_key_
Definition: InternalKvPairManager.h:52
InternalKvPairManagerContext gpu_context_
Definition: InternalKvPairManager.h:101
InternalKvPairManager(int capacity, int dsize_key, int dsize_value, const Device &device)
Definition: InternalKvPairManager.h:105
Definition: Device.h:39
Device device_
Definition: InternalKvPairManager.h:102
std::vector< int > DownloadHeap()
Definition: InternalKvPairManager.h:143
int capacity_
Definition: InternalKvPairManager.h:54
Definition: Traits.h:51
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 int
Definition: K4aPlugin.cpp:476
Definition: PinholeCameraIntrinsic.cpp:35
~InternalKvPairManager()
Definition: InternalKvPairManager.h:136
uint8_t * keys_
Definition: InternalKvPairManager.h:46
int dsize_value_
Definition: InternalKvPairManager.h:53
uint32_t addr_t
Definition: Traits.h:49
addr_t * heap_
Definition: InternalKvPairManager.h:48
Definition: InternalKvPairManager.h:44
Common CUDA utilities.