1 #include <thrust/mr/allocator.h>
2 #include <thrust/mr/new.h>
3 #include <thrust/mr/pool.h>
4 #include <thrust/mr/disjoint_pool.h>
5
6 #include <cassert>
7
8 template<typename Vec>
do_stuff_with_vector(typename Vec::allocator_type alloc)9 void do_stuff_with_vector(typename Vec::allocator_type alloc)
10 {
11 Vec v1(alloc);
12 v1.push_back(1);
13 assert(v1.back() == 1);
14
15 Vec v2(alloc);
16 v2 = v1;
17
18 v1.swap(v2);
19
20 v1.clear();
21 v1.resize(2);
22 assert(v1.size() == 2);
23 }
24
main()25 int main()
26 {
27 thrust::mr::new_delete_resource memres;
28
29 {
30 // no virtual calls will be issued
31 typedef thrust::mr::allocator<int, thrust::mr::new_delete_resource> Alloc;
32 Alloc alloc(&memres);
33
34 do_stuff_with_vector<thrust::host_vector<int, Alloc> >(alloc);
35 }
36
37 {
38 // virtual calls will be issued - wrapping in a polymorphic wrapper
39 thrust::mr::polymorphic_adaptor_resource<void *> adaptor(&memres);
40 typedef thrust::mr::polymorphic_allocator<int, void *> Alloc;
41 Alloc alloc(&adaptor);
42
43 do_stuff_with_vector<thrust::host_vector<int, Alloc> >(alloc);
44 }
45
46 typedef thrust::mr::unsynchronized_pool_resource<
47 thrust::mr::new_delete_resource
48 > Pool;
49 Pool pool(&memres);
50 {
51 typedef thrust::mr::allocator<int, Pool> Alloc;
52 Alloc alloc(&pool);
53
54 do_stuff_with_vector<thrust::host_vector<int, Alloc> >(alloc);
55 }
56
57 typedef thrust::mr::disjoint_unsynchronized_pool_resource<
58 thrust::mr::new_delete_resource,
59 thrust::mr::new_delete_resource
60 > DisjointPool;
61 DisjointPool disjoint_pool(&memres, &memres);
62 {
63 typedef thrust::mr::allocator<int, DisjointPool> Alloc;
64 Alloc alloc(&disjoint_pool);
65
66 do_stuff_with_vector<thrust::host_vector<int, Alloc> >(alloc);
67 }
68 }
69