Graphite  0.5.0
GPU-accelerated graph optimization framework
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1
2#pragma once
3#include <cuda_runtime.h>
4#include <new>
5#include <thrust/device_ptr.h>
6#include <thrust/universal_vector.h>
7namespace graphite {
8
9// https://github.com/NVIDIA/cccl/issues/810
10
11template <typename T>
12struct uninitialized_allocator : thrust::cuda::universal_allocator<T> {
13 __host__ uninitialized_allocator() {}
15 : thrust::cuda::universal_allocator<T>(other) {}
16 __host__ ~uninitialized_allocator() {}
17
18 uninitialized_allocator &operator=(const uninitialized_allocator &) = default;
19
20 template <typename U> struct rebind {
21 typedef uninitialized_allocator<U> other;
22 };
23
24 __host__ __device__ void construct(T *) {}
25
26#ifdef DEVICE_JETSON
27 // Integrated Tegra parts report concurrentManagedAccess == 0: the host may not
28 // touch managed memory while any kernel is running, and such an access
29 // segfaults on the spot rather than returning an error. managed_vector is
30 // dereferenced from the host all over the graph-building code while the other
31 // Nitro-SLAM threads keep kernels in flight, so managed memory is not usable
32 // here. Pinned mapped memory is physically shared on Tegra, reachable from the
33 // device through the same address (unifiedAddressing == 1), and has no such
34 // restriction.
35 __host__ thrust::universal_ptr<T> allocate(std::size_t n) {
36 void *raw = nullptr;
37 if (n != 0 &&
38 cudaHostAlloc(&raw, n * sizeof(T), cudaHostAllocMapped) != cudaSuccess) {
39 cudaGetLastError(); // do not leave the failure latched for the next launch
40 throw std::bad_alloc();
41 }
42 return thrust::universal_ptr<T>(static_cast<T *>(raw));
43 }
44
45 __host__ void deallocate(thrust::universal_ptr<T> p, std::size_t) {
46 cudaFreeHost(thrust::raw_pointer_cast(p));
47 }
48#endif
49};
50
51template <typename T> class managed_vector {
52
53private:
54 size_t m_capacity;
55 size_t m_size;
56
57 thrust::universal_ptr<T> m_data;
58 uninitialized_allocator<T> alloc;
59 // thrust::cuda::universal_allocator<T> alloc;
60
61 void deallocate() {
62 if (m_capacity > 0) {
63 for (size_t i = 0; i < m_size; i++) {
64 m_data[i].~T();
65 }
66 alloc.deallocate(m_data, m_capacity);
67 m_data = nullptr;
68 }
69 }
70
71public:
72 managed_vector() : m_capacity(0), m_size(0), m_data(nullptr) {}
73 managed_vector(size_t size)
74 : m_capacity(size), m_size(size), m_data(alloc.allocate(size)) {}
75
76 managed_vector(const managed_vector &) = delete;
77 managed_vector &operator=(const managed_vector &) = delete;
78 managed_vector(managed_vector &&) = delete;
79 managed_vector &operator=(managed_vector &&) = delete;
80
81 ~managed_vector() { deallocate(); }
82
83 size_t capacity() const { return m_capacity; }
84
85 size_t size() const { return m_size; }
86
87 void reserve(size_t size) {
88 if (size > m_capacity) {
89
90 thrust::universal_ptr<T> new_data = alloc.allocate(size);
91
92 if (m_size > 0) {
93 for (size_t i = 0; i < m_size; i++) {
94 new (new_data.get() + i) T(m_data[i]);
95 }
96 }
97
98 deallocate();
99
100 m_data = new_data;
101 m_capacity = size;
102 }
103 }
104
105 void resize(size_t size) {
106 if (size < m_size) {
107 for (size_t i = size; i < m_size; i++) {
108 m_data[i].~T();
109 }
110 } else if (size > m_capacity) {
111 reserve(size);
112 }
113 m_size = size;
114 }
115
116 void push_back(const T &value) {
117 if (m_size == m_capacity) {
118 reserve(m_capacity == 0 ? 1 : m_capacity * 2);
119 }
120 new (m_data.get() + m_size) T(value);
121 m_size++;
122 }
123
124 void pop_back() {
125 if (m_size > 0) {
126 m_data[m_size - 1].~T();
127 m_size--;
128 }
129 }
130
131 T &operator[](size_t index) { return m_data[index]; }
132 const T &operator[](size_t index) const { return m_data[index]; }
133
134 T &back() { return m_data[m_size - 1]; }
135
136 const T &back() const { return m_data[m_size - 1]; }
137
138 T *begin() { return m_data.get(); }
139
140 T *end() { return m_data.get() + m_size; }
141
142 thrust::universal_ptr<T> data() { return m_data; }
143
144 const thrust::universal_ptr<T> data() const { return m_data; }
145
146 void clear() {
147 for (size_t i = 0; i < m_size; i++) {
148 m_data[i].~T();
149 }
150 m_size = 0;
151 }
152};
153
154} // namespace graphite
Definition vector.hpp:51
The top-level namespace for Graphite.
Definition eigen_solver.cpp:4
Definition vector.hpp:12