Open3D (C++ API)  0.11.0
InternalNodeManager.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 
65 class Slab {
66 public:
67  addr_t kv_pair_ptrs[kWarpSize - 1];
69 };
70 
72 public:
74  : super_blocks_(nullptr),
75  hash_coef_(0),
76  num_attempts_(0),
77  memory_block_index_(0),
78  super_block_index_(0) {}
79 
80  __device__ __forceinline__ uint32_t* get_unit_ptr_from_slab(
81  const addr_t& next_slab_ptr, const uint32_t& lane_id) {
82  return super_blocks_ + addressDecoder(next_slab_ptr) + lane_id;
83  }
84  __device__ __forceinline__ uint32_t* get_ptr_for_bitmap(
85  const uint32_t super_block_idx, const uint32_t bitmap_idx) {
86  return super_blocks_ + super_block_idx * kUIntsPerSuperBlock +
87  bitmap_idx;
88  }
89 
90  // Objective: each warp selects its own memory_block warp allocator.
91  __device__ void Init(uint32_t& tid, uint32_t& lane_id) {
92  // Hashing the memory block to be used.
93  createMemBlockIndex(tid >> 5);
94 
95  // Loading the assigned memory block.
96  memory_block_bitmap_ =
97  super_blocks_[super_block_index_ * kUIntsPerSuperBlock +
98  memory_block_index_ * kSlabsPerBlock + lane_id];
99  }
100 
101  __device__ uint32_t WarpAllocate(const uint32_t& lane_id) {
102  // Try and allocate a new memory units within the memory_block memory
103  // block if it returns 0xFFFFFFFF, then there was not any empty memory
104  // unit a new memory_block block should be chosen, and repeat again
105  // allocated result: 5 bits: super_block_index
106  // 17 bits: memory block index
107  // 5 bits: memory unit index (hi-bits of 10bit)
108  // 5 bits: memory unit index (lo-bits of 10bit)
109  int empty_lane = -1;
110  uint32_t free_lane;
111  uint32_t read_bitmap = memory_block_bitmap_;
112  uint32_t allocated_result = kNotFoundFlag;
113  // Works as long as <31 bit are used in the allocated_result
114  // in other words, if there are 32 super blocks and at most 64k blocks
115  // per super block.
116 
117  while (allocated_result == kNotFoundFlag) {
118  empty_lane = __ffs(~memory_block_bitmap_) - 1;
119  free_lane = __ballot_sync(kSyncLanesMask, empty_lane >= 0);
120  if (free_lane == 0) {
121  // all bitmaps are full: need to be rehashed again.
122  updateMemBlockIndex((threadIdx.x + blockIdx.x * blockDim.x) >>
123  5);
124  read_bitmap = memory_block_bitmap_;
125  continue;
126  }
127  uint32_t src_lane = __ffs(free_lane) - 1;
128  if (src_lane == lane_id) {
129  read_bitmap = atomicCAS(
130  super_blocks_ +
131  super_block_index_ * kUIntsPerSuperBlock +
132  memory_block_index_ * kSlabsPerBlock + lane_id,
133  memory_block_bitmap_,
134  memory_block_bitmap_ | (1 << empty_lane));
135  if (read_bitmap == memory_block_bitmap_) {
136  // Successful attempt.
137  memory_block_bitmap_ |= (1 << empty_lane);
138  allocated_result =
139  (super_block_index_ << kSuperBlockMaskBits) |
140  (memory_block_index_ << kBlockMaskBits) |
141  (lane_id << kSlabMaskBits) | empty_lane;
142  } else {
143  // Not successful: updating the current bitmap.
144  memory_block_bitmap_ = read_bitmap;
145  }
146  }
147  // Asking for the allocated result.
148  allocated_result =
149  __shfl_sync(kSyncLanesMask, allocated_result, src_lane);
150  }
151  return allocated_result;
152  }
153 
154  // This function, frees a recently allocated memory unit by a single thread.
155  // Since it is untouched, there shouldn't be any worries for the actual
156  // memory contents to be reset again.
157  __device__ void FreeUntouched(addr_t ptr) {
158  atomicAnd(super_blocks_ +
159  getSuperBlockIndex(ptr) * kUIntsPerSuperBlock +
160  getMemBlockIndex(ptr) * kSlabsPerBlock +
161  (getMemUnitIndex(ptr) >> 5),
162  ~(1 << (getMemUnitIndex(ptr) & 0x1F)));
163  }
164 
165 private:
166  __device__ __host__ __forceinline__ uint32_t
167  getSuperBlockIndex(addr_t address) const {
168  return address >> kSuperBlockMaskBits;
169  }
170  __device__ __host__ __forceinline__ uint32_t
171  getMemBlockIndex(addr_t address) const {
172  return ((address >> kBlockMaskBits) & 0x1FFFF);
173  }
174  __device__ __host__ __forceinline__ addr_t
175  getMemBlockAddress(addr_t address) const {
176  return (kBitmapsPerSuperBlock +
177  getMemBlockIndex(address) * kUIntsPerBlock);
178  }
179  __device__ __host__ __forceinline__ uint32_t
180  getMemUnitIndex(addr_t address) const {
181  return address & 0x3FF;
182  }
183  __device__ __host__ __forceinline__ addr_t
184  getMemUnitAddress(addr_t address) {
185  return getMemUnitIndex(address) * kWarpSize;
186  }
187 
188  // Called at the beginning of the kernel.
189  __device__ void createMemBlockIndex(uint32_t global_warp_id) {
190  super_block_index_ = global_warp_id % kSuperBlocks;
191  memory_block_index_ = (hash_coef_ * global_warp_id) >>
192  (32 - kBlocksPerSuperBlockInBits);
193  }
194 
195  // Called when the allocator fails to find an empty unit to allocate.
196  __device__ void updateMemBlockIndex(uint32_t global_warp_id) {
197  num_attempts_++;
198  super_block_index_++;
199  super_block_index_ =
200  (super_block_index_ == kSuperBlocks) ? 0 : super_block_index_;
201  memory_block_index_ = (hash_coef_ * (global_warp_id + num_attempts_)) >>
202  (32 - kBlocksPerSuperBlockInBits);
203  // Loading the assigned memory block.
204  memory_block_bitmap_ =
205  *((super_blocks_ + super_block_index_ * kUIntsPerSuperBlock) +
206  memory_block_index_ * kSlabsPerBlock + (threadIdx.x & 0x1f));
207  }
208 
209  __host__ __device__ addr_t addressDecoder(addr_t address_ptr_index) {
210  return getSuperBlockIndex(address_ptr_index) * kUIntsPerSuperBlock +
211  getMemBlockAddress(address_ptr_index) +
212  getMemUnitIndex(address_ptr_index) * kWarpSize;
213  }
214 
215  __host__ __device__ void print_address(addr_t address_ptr_index) {
216  printf("Super block Index: %d, Memory block index: %d, Memory unit "
217  "index: "
218  "%d\n",
219  getSuperBlockIndex(address_ptr_index),
220  getMemBlockIndex(address_ptr_index),
221  getMemUnitIndex(address_ptr_index));
222  }
223 
224 public:
228  uint32_t hash_coef_; // A random 32-bit.
229 
230 private:
232  uint32_t num_attempts_;
233  uint32_t memory_block_index_;
234  uint32_t memory_block_bitmap_;
235  uint32_t super_block_index_;
236 };
237 
238 __global__ void CountSlabsPerSuperblockKernel(
239  InternalNodeManagerContext context, uint32_t* slabs_per_superblock);
240 
242 public:
243  InternalNodeManager(const Device& device) : device_(device) {
245  std::mt19937 rng(time(0));
246  gpu_context_.hash_coef_ = rng();
247 
250  gpu_context_.super_blocks_ =
251  static_cast<uint32_t*>(MemoryManager::Malloc(
252  kUIntsPerSuperBlock * kSuperBlocks * sizeof(uint32_t),
253  device_));
254 
255  OPEN3D_CUDA_CHECK(cudaMemset(
256  gpu_context_.super_blocks_, 0xFF,
257  kUIntsPerSuperBlock * kSuperBlocks * sizeof(uint32_t)));
258 
259  for (uint32_t i = 0; i < kSuperBlocks; i++) {
260  // setting bitmaps into zeros:
261  OPEN3D_CUDA_CHECK(cudaMemset(
262  gpu_context_.super_blocks_ + i * kUIntsPerSuperBlock, 0x00,
263  kBlocksPerSuperBlock * kSlabsPerBlock * sizeof(uint32_t)));
264  }
265  }
266 
268  MemoryManager::Free(gpu_context_.super_blocks_, device_);
269  }
270 
271  std::vector<int> CountSlabsPerSuperblock() {
272  const uint32_t num_super_blocks = kSuperBlocks;
273 
274  thrust::device_vector<uint32_t> slabs_per_superblock(kSuperBlocks);
275  thrust::fill(slabs_per_superblock.begin(), slabs_per_superblock.end(),
276  0);
277 
278  // Counting total number of allocated memory units.
279  int num_mem_units = kBlocksPerSuperBlock * 32;
280  int num_cuda_blocks =
281  (num_mem_units + kThreadsPerBlock - 1) / kThreadsPerBlock;
282  CountSlabsPerSuperblockKernel<<<num_cuda_blocks, kThreadsPerBlock>>>(
283  gpu_context_,
284  thrust::raw_pointer_cast(slabs_per_superblock.data()));
285  OPEN3D_CUDA_CHECK(cudaDeviceSynchronize());
286  OPEN3D_CUDA_CHECK(cudaGetLastError());
287 
288  std::vector<int> result(num_super_blocks);
289  thrust::copy(slabs_per_superblock.begin(), slabs_per_superblock.end(),
290  result.begin());
291 
292  return std::move(result);
293  }
294 
295 public:
298 };
299 
301  InternalNodeManagerContext context, uint32_t* slabs_per_superblock) {
302  uint32_t tid = threadIdx.x + blockIdx.x * blockDim.x;
303 
304  int num_bitmaps = kBlocksPerSuperBlock * 32;
305  if (tid >= num_bitmaps) {
306  return;
307  }
308 
309  for (uint32_t i = 0; i < kSuperBlocks; i++) {
310  uint32_t read_bitmap = *(context.get_ptr_for_bitmap(i, tid));
311  atomicAdd(&slabs_per_superblock[i], __popc(read_bitmap));
312  }
313 }
314 } // namespace core
315 } // namespace open3d
uint32_t * super_blocks_
A pointer to each super-block.
Definition: InternalNodeManager.h:226
__device__ uint32_t WarpAllocate(const uint32_t &lane_id)
Definition: InternalNodeManager.h:101
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:554
static void Free(void *ptr, const Device &device)
Definition: MemoryManager.cpp:44
#define OPEN3D_CUDA_CHECK(err)
Definition: CUDAUtils.h:57
static void * Malloc(size_t byte_size, const Device &device)
Definition: MemoryManager.cpp:40
__device__ void FreeUntouched(addr_t ptr)
Definition: InternalNodeManager.h:157
Device device_
Definition: InternalNodeManager.h:297
addr_t next_slab_ptr
Definition: InternalNodeManager.h:68
std::vector< int > CountSlabsPerSuperblock()
Definition: InternalNodeManager.h:271
InternalNodeManager(const Device &device)
Definition: InternalNodeManager.h:243
__device__ void Init(uint32_t &tid, uint32_t &lane_id)
Definition: InternalNodeManager.h:91
Definition: Device.h:39
Definition: InternalNodeManager.h:71
__device__ __forceinline__ uint32_t * get_ptr_for_bitmap(const uint32_t super_block_idx, const uint32_t bitmap_idx)
Definition: InternalNodeManager.h:84
uint32_t hash_coef_
hash_coef (register): used as (16 bits, 16 bits) for hashing.
Definition: InternalNodeManager.h:228
InternalNodeManagerContext gpu_context_
Definition: InternalNodeManager.h:296
Definition: PinholeCameraIntrinsic.cpp:35
uint32_t addr_t
Definition: Traits.h:49
__device__ __forceinline__ uint32_t * get_unit_ptr_from_slab(const addr_t &next_slab_ptr, const uint32_t &lane_id)
Definition: InternalNodeManager.h:80
Definition: InternalNodeManager.h:65
Common CUDA utilities.
~InternalNodeManager()
Definition: InternalNodeManager.h:267
addr_t kv_pair_ptrs[kWarpSize - 1]
Definition: InternalNodeManager.h:67
InternalNodeManagerContext()
Definition: InternalNodeManager.h:73
__global__ void CountSlabsPerSuperblockKernel(InternalNodeManagerContext context, uint32_t *slabs_per_superblock)
Definition: InternalNodeManager.h:300
Definition: InternalNodeManager.h:241