-
Notifications
You must be signed in to change notification settings - Fork 13
/
hpc_memory.hpp
194 lines (174 loc) · 4.04 KB
/
hpc_memory.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#pragma once
#include <hpc_algorithm.hpp>
#include <hpc_execution.hpp>
#include <hpc_macros.hpp>
#include <memory>
#include <type_traits>
namespace hpc {
template <class T>
using host_allocator = std::allocator<T>;
#ifdef HPC_CUDA
template <class T>
class device_allocator
{
public:
using value_type = T;
using pointer = T*;
using const_pointer = T const*;
using reference = T&;
using const_reference = T const&;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
template <class U>
struct rebind
{
typedef ::hpc::device_allocator<U> other;
};
using is_always_equal = std::true_type;
constexpr bool
operator==(device_allocator const&) const noexcept
{
return true;
}
constexpr bool
operator!=(device_allocator const&) const noexcept
{
return false;
}
T*
allocate(std::size_t n)
{
auto err = cudaDeviceSynchronize();
assert(err == cudaSuccess);
void* ptr;
err = cudaMalloc(&ptr, n * sizeof(T));
if (err != cudaSuccess) {
throw std::bad_alloc();
}
return static_cast<T*>(ptr);
}
void
deallocate(T* p, std::size_t)
{
auto err = cudaDeviceSynchronize();
(void)err;
assert(err == cudaSuccess);
err = cudaFree(p);
(void)err;
assert(cudaSuccess == err);
}
};
template <class T>
class pinned_allocator
{
public:
using value_type = T;
using pointer = T*;
using const_pointer = T const*;
using reference = T&;
using const_reference = T const&;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
template <class U>
struct rebind
{
typedef ::hpc::pinned_allocator<U> other;
};
using is_always_equal = std::true_type;
constexpr bool
operator==(pinned_allocator const&) const noexcept
{
return true;
}
constexpr bool
operator!=(pinned_allocator const&) const noexcept
{
return false;
}
T*
allocate(std::size_t n)
{
auto err = cudaDeviceSynchronize();
assert(err == cudaSuccess);
void* ptr;
err = cudaMallocHost(&ptr, n * sizeof(T));
if (err != cudaSuccess) {
throw std::bad_alloc();
}
return static_cast<T*>(ptr);
}
void
deallocate(T* p, std::size_t)
{
auto err = cudaDeviceSynchronize();
(void)err;
assert(err == cudaSuccess);
err = cudaFreeHost(p);
(void)err;
assert(cudaSuccess == err);
}
};
#else
template <class T>
using pinned_allocator = std::allocator<T>;
template <class T>
using device_allocator = std::allocator<T>;
#endif
template <class Range>
HPC_NOINLINE void
uninitialized_default_construct(serial_policy, Range&& range)
{
using range_type = std::decay_t<Range>;
auto first = range.begin();
auto const last = range.end();
for (; first != last; ++first) {
::new (static_cast<void*>(std::addressof(*first))) typename range_type::value_type;
}
}
#ifdef HPC_CUDA
template <class Range>
HPC_NOINLINE void
uninitialized_default_construct(cuda_policy policy, Range&& range)
{
using range_type = std::decay_t<Range>;
using reference_type = typename range_type::reference;
auto functor = [=] HPC_DEVICE(reference_type ref) {
::new (static_cast<void*>(&ref)) typename range_type::value_type;
};
for_each(policy, range, functor);
}
#endif
template <class T>
HPC_ALWAYS_INLINE HPC_DEVICE void
device_destroy_at(T* p)
{
p->~T();
}
template <class T>
HPC_ALWAYS_INLINE void
host_destroy_at(T* p)
{
p->~T();
}
template <class Range>
HPC_NOINLINE void
destroy(serial_policy, Range&& range)
{
auto first = range.begin();
auto const last = range.end();
for (; first != last; ++first) {
::hpc::host_destroy_at(std::addressof(*first));
}
}
#ifdef HPC_CUDA
template <class Range>
HPC_NOINLINE void
destroy(cuda_policy policy, Range&& range)
{
using range_type = std::decay_t<Range>;
using reference_type = typename range_type::reference;
auto functor = [=] HPC_DEVICE(reference_type ref) { ::hpc::device_destroy_at(&ref); };
::hpc::for_each(policy, range, functor);
}
#endif
} // namespace hpc