Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SlabNodeManager.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 // Copyright 2019 Saman Ashkiani
28 // Rewritten by Wei Dong 2019 - 2020
29 // Licensed under the Apache License, Version 2.0 (the "License");
30 // you may not use this file except in compliance with the License.
31 // You may obtain a copy of the License at
32 //
33 // http://www.apache.org/licenses/LICENSE-2.0
34 //
35 // Unless required by applicable law or agreed to in writing, software
36 // distributed under the License is distributed on an "AS IS" BASIS,
37 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
38 // implied. See the License for the specific language governing permissions
39 // and limitations under the License.
40 
41 #pragma once
42 
43 #include <thrust/device_vector.h>
44 
45 #include <cassert>
46 #include <memory>
47 #include <random>
48 
49 #include "open3d/core/CUDAUtils.h"
53 
54 namespace open3d {
55 namespace core {
56 
59 class Slab {
60 public:
63  addr_t kv_pair_ptrs[kWarpSize - 1];
66 };
67 
69 public:
71  : super_blocks_(nullptr),
72  hash_coef_(0),
73  num_attempts_(0),
74  memory_block_index_(0),
75  super_block_index_(0) {}
76 
77  __device__ __forceinline__ uint32_t* get_unit_ptr_from_slab(
78  const addr_t& next_slab_ptr, const uint32_t& lane_id) {
79  return super_blocks_ + addressDecoder(next_slab_ptr) + lane_id;
80  }
81  __device__ __forceinline__ uint32_t* get_ptr_for_bitmap(
82  const uint32_t super_block_idx, const uint32_t bitmap_idx) {
83  return super_blocks_ + super_block_idx * kUIntsPerSuperBlock +
84  bitmap_idx;
85  }
86 
87  // Objective: each warp selects its own memory_block warp allocator.
88  __device__ void Init(uint32_t& tid, uint32_t& lane_id) {
89  // Hashing the memory block to be used.
90  createMemBlockIndex(tid >> 5);
91 
92  // Loading the assigned memory block.
93  memory_block_bitmap_ =
94  super_blocks_[super_block_index_ * kUIntsPerSuperBlock +
95  memory_block_index_ * kSlabsPerBlock + lane_id];
96  }
97 
98  __device__ uint32_t WarpAllocate(const uint32_t& lane_id) {
99  // Try and allocate a new memory units within the memory_block memory
100  // block if it returns 0xFFFFFFFF, then there was not any empty memory
101  // unit a new memory_block block should be chosen, and repeat again
102  // allocated result: 5 bits: super_block_index
103  // 17 bits: memory block index
104  // 5 bits: memory unit index (hi-bits of 10bit)
105  // 5 bits: memory unit index (lo-bits of 10bit)
106  int empty_lane = -1;
107  uint32_t free_lane;
108  uint32_t read_bitmap = memory_block_bitmap_;
109  uint32_t allocated_result = kNotFoundFlag;
110  // Works as long as <31 bit are used in the allocated_result
111  // in other words, if there are 32 super blocks and at most 64k blocks
112  // per super block.
113 
114  while (allocated_result == kNotFoundFlag) {
115  empty_lane = __ffs(~memory_block_bitmap_) - 1;
116  free_lane = __ballot_sync(kSyncLanesMask, empty_lane >= 0);
117  if (free_lane == 0) {
118  // all bitmaps are full: need to be rehashed again.
119  updateMemBlockIndex((threadIdx.x + blockIdx.x * blockDim.x) >>
120  5);
121  read_bitmap = memory_block_bitmap_;
122  continue;
123  }
124  uint32_t src_lane = __ffs(free_lane) - 1;
125  if (src_lane == lane_id) {
126  read_bitmap = atomicCAS(
127  super_blocks_ +
128  super_block_index_ * kUIntsPerSuperBlock +
129  memory_block_index_ * kSlabsPerBlock + lane_id,
130  memory_block_bitmap_,
131  memory_block_bitmap_ | (1 << empty_lane));
132  if (read_bitmap == memory_block_bitmap_) {
133  // Successful attempt.
134  memory_block_bitmap_ |= (1 << empty_lane);
135  allocated_result =
136  (super_block_index_ << kSuperBlockMaskBits) |
137  (memory_block_index_ << kBlockMaskBits) |
138  (lane_id << kSlabMaskBits) | empty_lane;
139  } else {
140  // Not successful: updating the current bitmap.
141  memory_block_bitmap_ = read_bitmap;
142  }
143  }
144  // Asking for the allocated result.
145  allocated_result =
146  __shfl_sync(kSyncLanesMask, allocated_result, src_lane);
147  }
148  return allocated_result;
149  }
150 
151  // This function, frees a recently allocated memory unit by a single thread.
152  // Since it is untouched, there shouldn't be any worries for the actual
153  // memory contents to be reset again.
154  __device__ void FreeUntouched(addr_t ptr) {
155  atomicAnd(super_blocks_ +
156  getSuperBlockIndex(ptr) * kUIntsPerSuperBlock +
157  getMemBlockIndex(ptr) * kSlabsPerBlock +
158  (getMemUnitIndex(ptr) >> 5),
159  ~(1 << (getMemUnitIndex(ptr) & 0x1F)));
160  }
161 
162 private:
163  __device__ __host__ __forceinline__ uint32_t
164  getSuperBlockIndex(addr_t address) const {
165  return address >> kSuperBlockMaskBits;
166  }
167  __device__ __host__ __forceinline__ uint32_t
168  getMemBlockIndex(addr_t address) const {
169  return ((address >> kBlockMaskBits) & 0x1FFFF);
170  }
171  __device__ __host__ __forceinline__ addr_t
172  getMemBlockAddress(addr_t address) const {
173  return (kBitmapsPerSuperBlock +
174  getMemBlockIndex(address) * kUIntsPerBlock);
175  }
176  __device__ __host__ __forceinline__ uint32_t
177  getMemUnitIndex(addr_t address) const {
178  return address & 0x3FF;
179  }
180  __device__ __host__ __forceinline__ addr_t
181  getMemUnitAddress(addr_t address) {
182  return getMemUnitIndex(address) * kWarpSize;
183  }
184 
185  // Called at the beginning of the kernel.
186  __device__ void createMemBlockIndex(uint32_t global_warp_id) {
187  super_block_index_ = global_warp_id % kSuperBlocks;
188  memory_block_index_ = (hash_coef_ * global_warp_id) >>
189  (32 - kBlocksPerSuperBlockInBits);
190  }
191 
192  // Called when the allocator fails to find an empty unit to allocate.
193  __device__ void updateMemBlockIndex(uint32_t global_warp_id) {
194  num_attempts_++;
195  super_block_index_++;
196  super_block_index_ =
197  (super_block_index_ == kSuperBlocks) ? 0 : super_block_index_;
198  memory_block_index_ = (hash_coef_ * (global_warp_id + num_attempts_)) >>
199  (32 - kBlocksPerSuperBlockInBits);
200  // Loading the assigned memory block.
201  memory_block_bitmap_ =
202  *((super_blocks_ + super_block_index_ * kUIntsPerSuperBlock) +
203  memory_block_index_ * kSlabsPerBlock + (threadIdx.x & 0x1f));
204  }
205 
206  __host__ __device__ addr_t addressDecoder(addr_t address_ptr_index) {
207  return getSuperBlockIndex(address_ptr_index) * kUIntsPerSuperBlock +
208  getMemBlockAddress(address_ptr_index) +
209  getMemUnitIndex(address_ptr_index) * kWarpSize;
210  }
211 
212  __host__ __device__ void print_address(addr_t address_ptr_index) {
213  printf("Super block Index: %d, Memory block index: %d, Memory unit "
214  "index: "
215  "%d\n",
216  getSuperBlockIndex(address_ptr_index),
217  getMemBlockIndex(address_ptr_index),
218  getMemUnitIndex(address_ptr_index));
219  }
220 
221 public:
225  uint32_t hash_coef_; // A random 32-bit.
226 
227 private:
229  uint32_t num_attempts_;
230  uint32_t memory_block_index_;
231  uint32_t memory_block_bitmap_;
232  uint32_t super_block_index_;
233 };
234 
236  uint32_t* slabs_per_superblock);
237 
239 public:
240  SlabNodeManager(const Device& device) : device_(device) {
242  std::mt19937 rng(time(0));
243  impl_.hash_coef_ = rng();
244 
247  impl_.super_blocks_ = static_cast<uint32_t*>(MemoryManager::Malloc(
248  kUIntsPerSuperBlock * kSuperBlocks * sizeof(uint32_t),
249  device_));
250  Reset();
251  }
252 
253  ~SlabNodeManager() { MemoryManager::Free(impl_.super_blocks_, device_); }
254 
255  void Reset() {
256  OPEN3D_CUDA_CHECK(cudaMemset(
257  impl_.super_blocks_, 0xFF,
258  kUIntsPerSuperBlock * kSuperBlocks * sizeof(uint32_t)));
259 
260  for (uint32_t i = 0; i < kSuperBlocks; i++) {
261  // setting bitmaps into zeros:
262  OPEN3D_CUDA_CHECK(cudaMemset(
263  impl_.super_blocks_ + i * kUIntsPerSuperBlock, 0x00,
264  kBlocksPerSuperBlock * kSlabsPerBlock * sizeof(uint32_t)));
265  }
266  OPEN3D_CUDA_CHECK(cudaDeviceSynchronize());
267  OPEN3D_CUDA_CHECK(cudaGetLastError());
268  }
269 
270  std::vector<int> CountSlabsPerSuperblock() {
271  const uint32_t num_super_blocks = kSuperBlocks;
272 
273  thrust::device_vector<uint32_t> slabs_per_superblock(kSuperBlocks);
274  thrust::fill(slabs_per_superblock.begin(), slabs_per_superblock.end(),
275  0);
276 
277  // Counting total number of allocated memory units.
278  int num_mem_units = kBlocksPerSuperBlock * 32;
279  int num_cuda_blocks =
280  (num_mem_units + kThreadsPerBlock - 1) / kThreadsPerBlock;
281  CountSlabsPerSuperblockKernel<<<num_cuda_blocks, kThreadsPerBlock>>>(
282  impl_, thrust::raw_pointer_cast(slabs_per_superblock.data()));
283  OPEN3D_CUDA_CHECK(cudaDeviceSynchronize());
284  OPEN3D_CUDA_CHECK(cudaGetLastError());
285 
286  std::vector<int> result(num_super_blocks);
287  thrust::copy(slabs_per_superblock.begin(), slabs_per_superblock.end(),
288  result.begin());
289 
290  return result;
291  }
292 
293 public:
296 };
297 } // namespace core
298 } // namespace open3d
uint32_t hash_coef_
hash_coef (register): used as (16 bits, 16 bits) for hashing.
Definition: SlabNodeManager.h:225
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:557
static void Free(void *ptr, const Device &device)
Definition: MemoryManager.cpp:44
Device device_
Definition: SlabNodeManager.h:295
#define OPEN3D_CUDA_CHECK(err)
Definition: CUDAUtils.h:59
Definition: SlabNodeManager.h:238
static void * Malloc(size_t byte_size, const Device &device)
Definition: MemoryManager.cpp:40
__device__ __forceinline__ uint32_t * get_unit_ptr_from_slab(const addr_t &next_slab_ptr, const uint32_t &lane_id)
Definition: SlabNodeManager.h:77
__global__ void CountSlabsPerSuperblockKernel(SlabNodeManagerImpl impl, uint32_t *slabs_per_superblock)
addr_t next_slab_ptr
An internal ptr managed by InternalNodeManager.
Definition: SlabNodeManager.h:65
__device__ void Init(uint32_t &tid, uint32_t &lane_id)
Definition: SlabNodeManager.h:88
SlabNodeManager(const Device &device)
Definition: SlabNodeManager.h:240
__device__ __forceinline__ uint32_t * get_ptr_for_bitmap(const uint32_t super_block_idx, const uint32_t bitmap_idx)
Definition: SlabNodeManager.h:81
Definition: Device.h:39
uint32_t * super_blocks_
A pointer to each super-block.
Definition: SlabNodeManager.h:223
Definition: SlabNodeManager.h:68
Definition: PinholeCameraIntrinsic.cpp:35
uint32_t addr_t
Definition: HashmapBuffer.h:58
SlabNodeManagerImpl()
Definition: SlabNodeManager.h:70
std::vector< int > CountSlabsPerSuperblock()
Definition: SlabNodeManager.h:270
SlabNodeManagerImpl impl_
Definition: SlabNodeManager.h:294
__device__ void FreeUntouched(addr_t ptr)
Definition: SlabNodeManager.h:154
Definition: SlabNodeManager.h:59
Common CUDA utilities.
__device__ uint32_t WarpAllocate(const uint32_t &lane_id)
Definition: SlabNodeManager.h:98
addr_t kv_pair_ptrs[kWarpSize - 1]
Definition: SlabNodeManager.h:63
~SlabNodeManager()
Definition: SlabNodeManager.h:253
void Reset()
Definition: SlabNodeManager.h:255