forked from NVIDIA/thrust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocator.cu
255 lines (214 loc) · 5.71 KB
/
allocator.cu
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <unittest/unittest.h>
#include <thrust/detail/config.h>
#include <thrust/device_malloc_allocator.h>
#include <thrust/system/cpp/vector.h>
#include <memory>
template <typename T>
struct my_allocator_with_custom_construct1
: thrust::device_malloc_allocator<T>
{
__host__ __device__
my_allocator_with_custom_construct1()
{}
__host__ __device__
void construct(T *p)
{
*p = 13;
}
};
template <typename T>
void TestAllocatorCustomDefaultConstruct(size_t n)
{
thrust::device_vector<T> ref(n, 13);
thrust::device_vector<T, my_allocator_with_custom_construct1<T> > vec(n);
ASSERT_EQUAL_QUIET(ref, vec);
}
DECLARE_VARIABLE_UNITTEST(TestAllocatorCustomDefaultConstruct);
template <typename T>
struct my_allocator_with_custom_construct2
: thrust::device_malloc_allocator<T>
{
__host__ __device__
my_allocator_with_custom_construct2()
{}
template <typename Arg>
__host__ __device__
void construct(T *p, const Arg &)
{
*p = 13;
}
};
template <typename T>
void TestAllocatorCustomCopyConstruct(size_t n)
{
thrust::device_vector<T> ref(n, 13);
thrust::device_vector<T> copy_from(n, 7);
thrust::device_vector<T, my_allocator_with_custom_construct2<T> >
vec(copy_from.begin(), copy_from.end());
ASSERT_EQUAL_QUIET(ref, vec);
}
DECLARE_VARIABLE_UNITTEST(TestAllocatorCustomCopyConstruct);
template <typename T>
struct my_allocator_with_custom_destroy
{
typedef T value_type;
typedef T & reference;
typedef const T & const_reference;
static bool g_state;
__host__
my_allocator_with_custom_destroy(){}
__host__
my_allocator_with_custom_destroy(const my_allocator_with_custom_destroy &other)
: use_me_to_alloc(other.use_me_to_alloc)
{}
__host__
~my_allocator_with_custom_destroy(){}
__host__ __device__
void destroy(T *)
{
#if !__CUDA_ARCH__
g_state = true;
#endif
}
value_type *allocate(std::ptrdiff_t n)
{
return use_me_to_alloc.allocate(n);
}
void deallocate(value_type *ptr, std::ptrdiff_t n)
{
use_me_to_alloc.deallocate(ptr,n);
}
bool operator==(const my_allocator_with_custom_destroy &) const
{
return true;
}
bool operator!=(const my_allocator_with_custom_destroy &other) const
{
return !(*this == other);
}
typedef thrust::detail::true_type is_always_equal;
// use composition rather than inheritance
// to avoid inheriting std::allocator's member
// function destroy
std::allocator<T> use_me_to_alloc;
};
template <typename T>
bool my_allocator_with_custom_destroy<T>::g_state = false;
template <typename T>
void TestAllocatorCustomDestroy(size_t n)
{
{
thrust::cpp::vector<T, my_allocator_with_custom_destroy<T> > vec(n);
} // destroy everything
if (0 < n)
ASSERT_EQUAL(true, my_allocator_with_custom_destroy<T>::g_state);
}
DECLARE_VARIABLE_UNITTEST(TestAllocatorCustomDestroy);
template <typename T>
struct my_minimal_allocator
{
typedef T value_type;
// XXX ideally, we shouldn't require
// these two typedefs
typedef T & reference;
typedef const T & const_reference;
__host__
my_minimal_allocator(){}
__host__
my_minimal_allocator(const my_minimal_allocator &other)
: use_me_to_alloc(other.use_me_to_alloc)
{}
__host__
~my_minimal_allocator(){}
value_type *allocate(std::ptrdiff_t n)
{
return use_me_to_alloc.allocate(n);
}
void deallocate(value_type *ptr, std::ptrdiff_t n)
{
use_me_to_alloc.deallocate(ptr,n);
}
std::allocator<T> use_me_to_alloc;
};
template <typename T>
void TestAllocatorMinimal(size_t n)
{
thrust::cpp::vector<int, my_minimal_allocator<int> > vec(n, 13);
// XXX copy to h_vec because ASSERT_EQUAL doesn't know about cpp::vector
thrust::host_vector<int> h_vec(vec.begin(), vec.end());
thrust::host_vector<int> ref(n, 13);
ASSERT_EQUAL(ref, h_vec);
}
DECLARE_VARIABLE_UNITTEST(TestAllocatorMinimal);
void TestAllocatorTraitsRebind()
{
ASSERT_EQUAL(
(thrust::detail::is_same<
typename thrust::detail::allocator_traits<
thrust::device_malloc_allocator<int>
>::template rebind_traits<float>::other,
typename thrust::detail::allocator_traits<
thrust::device_malloc_allocator<float>
>
>::value),
true
);
ASSERT_EQUAL(
(thrust::detail::is_same<
typename thrust::detail::allocator_traits<
my_minimal_allocator<int>
>::template rebind_traits<float>::other,
typename thrust::detail::allocator_traits<
my_minimal_allocator<float>
>
>::value),
true
);
}
DECLARE_UNITTEST(TestAllocatorTraitsRebind);
#if THRUST_CPP_DIALECT >= 2011
void TestAllocatorTraitsRebindCpp11()
{
ASSERT_EQUAL(
(thrust::detail::is_same<
typename thrust::detail::allocator_traits<
thrust::device_malloc_allocator<int>
>::template rebind_alloc<float>,
thrust::device_malloc_allocator<float>
>::value),
true
);
ASSERT_EQUAL(
(thrust::detail::is_same<
typename thrust::detail::allocator_traits<
my_minimal_allocator<int>
>::template rebind_alloc<float>,
my_minimal_allocator<float>
>::value),
true
);
ASSERT_EQUAL(
(thrust::detail::is_same<
typename thrust::detail::allocator_traits<
thrust::device_malloc_allocator<int>
>::template rebind_traits<float>,
typename thrust::detail::allocator_traits<
thrust::device_malloc_allocator<float>
>
>::value),
true
);
ASSERT_EQUAL(
(thrust::detail::is_same<
typename thrust::detail::allocator_traits<
my_minimal_allocator<int>
>::template rebind_traits<float>,
typename thrust::detail::allocator_traits<
my_minimal_allocator<float>
>
>::value),
true
);
}
DECLARE_UNITTEST(TestAllocatorTraitsRebindCpp11);
#endif // C++11