15 : thrust::cuda::universal_allocator<T>(other) {}
20 template <
typename U>
struct rebind {
21 typedef uninitialized_allocator<U> other;
24 __host__ __device__
void construct(T *) {}
35 __host__ thrust::universal_ptr<T> allocate(std::size_t n) {
38 cudaHostAlloc(&raw, n *
sizeof(T), cudaHostAllocMapped) != cudaSuccess) {
40 throw std::bad_alloc();
42 return thrust::universal_ptr<T>(
static_cast<T *
>(raw));
45 __host__
void deallocate(thrust::universal_ptr<T> p, std::size_t) {
46 cudaFreeHost(thrust::raw_pointer_cast(p));
57 thrust::universal_ptr<T> m_data;
58 uninitialized_allocator<T> alloc;
63 for (
size_t i = 0; i < m_size; i++) {
66 alloc.deallocate(m_data, m_capacity);
74 : m_capacity(size), m_size(size), m_data(alloc.allocate(size)) {}
83 size_t capacity()
const {
return m_capacity; }
85 size_t size()
const {
return m_size; }
87 void reserve(
size_t size) {
88 if (size > m_capacity) {
90 thrust::universal_ptr<T> new_data = alloc.allocate(size);
93 for (
size_t i = 0; i < m_size; i++) {
94 new (new_data.get() + i) T(m_data[i]);
105 void resize(
size_t size) {
107 for (
size_t i = size; i < m_size; i++) {
110 }
else if (size > m_capacity) {
116 void push_back(
const T &value) {
117 if (m_size == m_capacity) {
118 reserve(m_capacity == 0 ? 1 : m_capacity * 2);
120 new (m_data.get() + m_size) T(value);
126 m_data[m_size - 1].~T();
131 T &operator[](
size_t index) {
return m_data[index]; }
132 const T &operator[](
size_t index)
const {
return m_data[index]; }
134 T &back() {
return m_data[m_size - 1]; }
136 const T &back()
const {
return m_data[m_size - 1]; }
138 T *begin() {
return m_data.get(); }
140 T *end() {
return m_data.get() + m_size; }
142 thrust::universal_ptr<T> data() {
return m_data; }
144 const thrust::universal_ptr<T> data()
const {
return m_data; }
147 for (
size_t i = 0; i < m_size; i++) {