Open3D (C++ API)  0.12.0
HashmapBufferCPU.hpp
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 <atomic>
32 #include <memory>
33 #include <vector>
34 
36 
37 namespace open3d {
38 namespace core {
39 
43 public:
44  CPUHashmapBufferContext(int64_t capacity,
45  int64_t dsize_key,
46  int64_t dsize_value,
47  Tensor &keys,
48  Tensor &values,
49  Tensor &heap)
50  : capacity_(capacity),
51  dsize_key_(dsize_key),
52  dsize_value_(dsize_value),
53  keys_(static_cast<uint8_t *>(keys.GetDataPtr())),
54  values_(static_cast<uint8_t *>(values.GetDataPtr())),
55  heap_(static_cast<addr_t *>(heap.GetDataPtr())) {
56  std::memset(values_, 0, capacity_ * dsize_value_);
57  }
58 
59  void Reset() {
60 #pragma omp parallel for
61  for (int i = 0; i < capacity_; ++i) {
62  heap_[i] = i;
63  }
64 
65  heap_counter_ = 0;
66  }
67 
68  addr_t DeviceAllocate() { return heap_[heap_counter_.fetch_add(1)]; }
69 
70  void DeviceFree(addr_t ptr) { heap_[heap_counter_.fetch_sub(1) - 1] = ptr; }
71 
72  int HeapCounter() const { return heap_counter_.load(); }
73 
74  std::pair<void *, void *> ExtractIterator(addr_t ptr) {
75  return std::make_pair(keys_ + ptr * dsize_key_,
76  values_ + ptr * dsize_value_);
77  }
78 
79 public:
80  int64_t capacity_;
81  int64_t dsize_key_;
82  int64_t dsize_value_;
83 
84  uint8_t *keys_; /* [N] * sizeof(Key) */
85  uint8_t *values_; /* [N] * sizeof(Value) */
86  addr_t *heap_; /* [N] */
87  std::atomic<int> heap_counter_; /* [1] */
88 };
89 
90 } // namespace core
91 } // namespace open3d
Definition: HashmapBufferCPU.hpp:42
addr_t DeviceAllocate()
Definition: HashmapBufferCPU.hpp:68
int64_t dsize_value_
Definition: HashmapBufferCPU.hpp:82
uint8_t * keys_
Definition: HashmapBufferCPU.hpp:84
addr_t * heap_
Definition: HashmapBufferCPU.hpp:86
uint8_t * values_
Definition: HashmapBufferCPU.hpp:85
int64_t dsize_key_
Definition: HashmapBufferCPU.hpp:81
CPUHashmapBufferContext(int64_t capacity, int64_t dsize_key, int64_t dsize_value, Tensor &keys, Tensor &values, Tensor &heap)
Definition: HashmapBufferCPU.hpp:44
int HeapCounter() const
Definition: HashmapBufferCPU.hpp:72
int64_t capacity_
Definition: HashmapBufferCPU.hpp:80
std::pair< void *, void * > ExtractIterator(addr_t ptr)
Definition: HashmapBufferCPU.hpp:74
void DeviceFree(addr_t ptr)
Definition: HashmapBufferCPU.hpp:70
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Tensor.h:48
void Reset()
Definition: HashmapBufferCPU.hpp:59
uint32_t addr_t
Definition: HashmapBuffer.h:58
std::atomic< int > heap_counter_
Definition: HashmapBufferCPU.hpp:87
OPEN3D_HOST_DEVICE Pair< First, Second > make_pair(const First &_first, const Second &_second)
Definition: Traits.h:68