43 #include <thrust/device_vector.h> 74 : super_blocks_(nullptr),
77 memory_block_index_(0),
78 super_block_index_(0) {}
82 return super_blocks_ + addressDecoder(next_slab_ptr) + lane_id;
86 return super_blocks_ + super_block_idx * kUIntsPerSuperBlock +
93 createMemBlockIndex(tid >> 5);
96 memory_block_bitmap_ =
97 super_blocks_[super_block_index_ * kUIntsPerSuperBlock +
98 memory_block_index_ * kSlabsPerBlock + lane_id];
111 uint32_t read_bitmap = memory_block_bitmap_;
112 uint32_t allocated_result = kNotFoundFlag;
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) {
122 updateMemBlockIndex((threadIdx.x + blockIdx.x * blockDim.x) >>
124 read_bitmap = memory_block_bitmap_;
127 uint32_t src_lane = __ffs(free_lane) - 1;
128 if (src_lane == lane_id) {
129 read_bitmap = atomicCAS(
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_) {
137 memory_block_bitmap_ |= (1 << empty_lane);
139 (super_block_index_ << kSuperBlockMaskBits) |
140 (memory_block_index_ << kBlockMaskBits) |
141 (lane_id << kSlabMaskBits) | empty_lane;
144 memory_block_bitmap_ = read_bitmap;
149 __shfl_sync(kSyncLanesMask, allocated_result, src_lane);
151 return allocated_result;
158 atomicAnd(super_blocks_ +
159 getSuperBlockIndex(ptr) * kUIntsPerSuperBlock +
160 getMemBlockIndex(ptr) * kSlabsPerBlock +
161 (getMemUnitIndex(ptr) >> 5),
162 ~(1 << (getMemUnitIndex(ptr) & 0x1F)));
166 __device__ __host__ __forceinline__
uint32_t 167 getSuperBlockIndex(
addr_t address)
const {
168 return address >> kSuperBlockMaskBits;
170 __device__ __host__ __forceinline__
uint32_t 171 getMemBlockIndex(
addr_t address)
const {
172 return ((address >> kBlockMaskBits) & 0x1FFFF);
174 __device__ __host__ __forceinline__
addr_t 175 getMemBlockAddress(
addr_t address)
const {
176 return (kBitmapsPerSuperBlock +
177 getMemBlockIndex(address) * kUIntsPerBlock);
179 __device__ __host__ __forceinline__
uint32_t 180 getMemUnitIndex(
addr_t address)
const {
181 return address & 0x3FF;
183 __device__ __host__ __forceinline__
addr_t 184 getMemUnitAddress(
addr_t address) {
185 return getMemUnitIndex(address) * kWarpSize;
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);
196 __device__
void updateMemBlockIndex(
uint32_t global_warp_id) {
198 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);
204 memory_block_bitmap_ =
205 *((super_blocks_ + super_block_index_ * kUIntsPerSuperBlock) +
206 memory_block_index_ * kSlabsPerBlock + (threadIdx.x & 0x1f));
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;
215 __host__ __device__
void print_address(
addr_t address_ptr_index) {
216 printf(
"Super block Index: %d, Memory block index: %d, Memory unit " 219 getSuperBlockIndex(address_ptr_index),
220 getMemBlockIndex(address_ptr_index),
221 getMemUnitIndex(address_ptr_index));
245 std::mt19937 rng(time(0));
246 gpu_context_.hash_coef_ = rng();
250 gpu_context_.super_blocks_ =
252 kUIntsPerSuperBlock * kSuperBlocks *
sizeof(
uint32_t),
256 gpu_context_.super_blocks_, 0xFF,
257 kUIntsPerSuperBlock * kSuperBlocks *
sizeof(
uint32_t)));
259 for (
uint32_t i = 0; i < kSuperBlocks; i++) {
262 gpu_context_.super_blocks_ + i * kUIntsPerSuperBlock, 0x00,
263 kBlocksPerSuperBlock * kSlabsPerBlock *
sizeof(
uint32_t)));
272 const uint32_t num_super_blocks = kSuperBlocks;
274 thrust::device_vector<uint32_t> slabs_per_superblock(kSuperBlocks);
275 thrust::fill(slabs_per_superblock.begin(), slabs_per_superblock.end(),
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>>>(
284 thrust::raw_pointer_cast(slabs_per_superblock.data()));
288 std::vector<int> result(num_super_blocks);
289 thrust::copy(slabs_per_superblock.begin(), slabs_per_superblock.end(),
292 return std::move(result);
302 uint32_t tid = threadIdx.x + blockIdx.x * blockDim.x;
304 int num_bitmaps = kBlocksPerSuperBlock * 32;
305 if (tid >= num_bitmaps) {
309 for (
uint32_t i = 0; i < kSuperBlocks; i++) {
311 atomicAdd(&slabs_per_superblock[i], __popc(read_bitmap));
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: 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
~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