-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathp3a_device_ptr.hpp
178 lines (158 loc) · 4.15 KB
/
p3a_device_ptr.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
#pragma once
#include <memory>
#include <p3a_allocator.hpp>
#include <p3a_for_each.hpp>
namespace p3a {
namespace details {
template <class T>
class device_delete_functor {
T* m_pointer;
public:
device_delete_functor(T* pointer_arg)
:m_pointer(pointer_arg)
{
}
P3A_HOST_DEVICE void operator()(int) const
{
m_pointer->~T();
}
};
template <class T>
class device_deleter {
std::size_t m_actual_size{sizeof(T)};
public:
device_deleter() = default;
device_deleter(device_deleter&&) = default;
device_deleter(device_deleter const&) = default;
device_deleter& operator=(device_deleter&&) = default;
device_deleter& operator=(device_deleter const&) = default;
template <class U>
device_deleter(device_deleter<U>&& other)
:m_actual_size(other.size())
{
}
void operator()(T* ptr) const
{
for_each(execution::par,
counting_iterator<int>(0),
counting_iterator<int>(1),
device_delete_functor<T>(ptr));
p3a::execution::par.synchronize();
device_allocator<T> allocator;
allocator.deallocate(ptr, m_actual_size);
}
std::size_t size() const { return m_actual_size; }
};
// we have to create a construct_on_device overload for each of
// a few common argument counts because CUDA does not support
// capturing a member of a parameter pack in a host/device lambda,
// nor do we have the metaprogramming skills to construct
// a good enough tuple type that can cross this boundary
template <class T>
class device_constructor0 {
T* m_pointer;
public:
device_constructor0(T* pointer_arg)
:m_pointer(pointer_arg)
{
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE void operator()(int) const
{
::new (static_cast<void*>(m_pointer)) T();
}
};
template <class T>
void construct_on_device(T* ptr)
{
for_each(execution::par,
counting_iterator<int>(0),
counting_iterator<int>(1),
device_constructor0<T>{ptr});
}
template <class T, class Arg1>
class device_constructor1 {
T* m_pointer;
Arg1 m_arg1;
public:
device_constructor1(T* pointer_arg, Arg1 arg1_arg)
:m_pointer(pointer_arg)
,m_arg1(arg1_arg)
{
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE void operator()(int) const
{
::new (static_cast<void*>(m_pointer)) T(m_arg1);
}
};
template <class T, class Arg1>
void construct_on_device(T* ptr, Arg1 arg1)
{
for_each(execution::par,
counting_iterator<int>(0),
counting_iterator<int>(1),
device_constructor1<T, Arg1>{ptr, arg1});
}
template <class T, class Arg1, class Arg2>
class device_constructor2 {
T* m_pointer;
Arg1 m_arg1;
Arg2 m_arg2;
public:
device_constructor2(T* pointer_arg, Arg1 arg1_arg, Arg2 arg2_arg)
:m_pointer(pointer_arg)
,m_arg1(arg1_arg)
,m_arg2(arg2_arg)
{
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE void operator()(int) const
{
::new (static_cast<void*>(m_pointer)) T(m_arg1, m_arg2);
}
};
template <class T, class Arg1, class Arg2>
void construct_on_device(T* ptr, Arg1 arg1, Arg2 arg2)
{
for_each(execution::par,
counting_iterator<int>(0),
counting_iterator<int>(1),
device_constructor2<T, Arg1, Arg2>{ptr, arg1, arg2});
}
template <class T, class Arg1, class Arg2, class Arg3>
class device_constructor3 {
T* m_pointer;
Arg1 m_arg1;
Arg2 m_arg2;
Arg3 m_arg3;
public:
device_constructor3(T* pointer_arg, Arg1 arg1_arg, Arg2 arg2_arg, Arg3 arg3_arg)
:m_pointer(pointer_arg)
,m_arg1(arg1_arg)
,m_arg2(arg2_arg)
,m_arg3(arg3_arg)
{
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE void operator()(int) const
{
::new (static_cast<void*>(m_pointer)) T(m_arg1, m_arg2, m_arg3);
}
};
template <class T, class Arg1, class Arg2, class Arg3>
void construct_on_device(T* ptr, Arg1 arg1, Arg2 arg2, Arg3 arg3)
{
for_each(execution::par,
counting_iterator<int>(0),
counting_iterator<int>(1),
device_constructor3<T, Arg1, Arg2, Arg3>{ptr, arg1, arg2, arg3});
}
}
template <class T>
using device_ptr = std::unique_ptr<T, details::device_deleter<T>>;
template <class T, class... Args>
device_ptr<T> make_device(Args... args)
{
device_allocator<T> allocator;
T* raw_pointer = allocator.allocate(sizeof(T));
details::construct_on_device(raw_pointer, args...);
return device_ptr<T>(raw_pointer);
}
}