8
8
#pragma once
9
9
10
10
#include < CL/sycl/context.hpp>
11
- #include < CL/sycl/detail/usm_impl.hpp>
12
11
#include < CL/sycl/device.hpp>
13
12
#include < CL/sycl/exception.hpp>
14
13
#include < CL/sycl/queue.hpp>
20
19
__SYCL_INLINE namespace cl {
21
20
namespace sycl {
22
21
22
+ // Forward declarations.
23
+ void *aligned_alloc (size_t alignment, size_t size, const device &dev,
24
+ const context &ctxt, usm::alloc kind);
25
+ void free (void *ptr, const context &ctxt);
26
+
23
27
template <typename T, usm::alloc AllocKind, size_t Alignment = 0 >
24
28
class usm_allocator {
25
29
public:
@@ -36,14 +40,19 @@ class usm_allocator {
36
40
37
41
usm_allocator () = delete ;
38
42
usm_allocator (const context &Ctxt, const device &Dev)
39
- : mContext (Ctxt), mDevice (Dev) {}
43
+ : MContext (Ctxt), MDevice (Dev) {}
40
44
usm_allocator (const queue &Q)
41
- : mContext (Q.get_context()), mDevice (Q.get_device()) {}
45
+ : MContext (Q.get_context()), MDevice (Q.get_device()) {}
42
46
usm_allocator (const usm_allocator &Other)
43
- : mContext (Other.mContext ), mDevice (Other.mDevice ) {}
44
-
45
- // Construct an object
46
- // Note: AllocKind == alloc::device is not allowed
47
+ : MContext(Other.MContext), MDevice(Other.MDevice) {}
48
+
49
+ // / Constructs an object on memory pointed by Ptr.
50
+ // /
51
+ // / Note: AllocKind == alloc::device is not allowed.
52
+ // /
53
+ // / @param Ptr is a pointer to memory that will be used to construct the
54
+ // / object.
55
+ // / @param Val is a value to initialize the newly constructed object.
47
56
template <
48
57
usm::alloc AllocT = AllocKind,
49
58
typename std::enable_if<AllocT != usm::alloc::device, int >::type = 0 >
@@ -59,8 +68,11 @@ class usm_allocator {
59
68
" Device pointers do not support construct on host" );
60
69
}
61
70
62
- // Destroy an object
63
- // Note:: AllocKind == alloc::device is not allowed
71
+ // / Destroys an object.
72
+ // /
73
+ // / Note:: AllocKind == alloc::device is not allowed
74
+ // /
75
+ // / @param Ptr is a pointer to memory where the object resides.
64
76
template <
65
77
usm::alloc AllocT = AllocKind,
66
78
typename std::enable_if<AllocT != usm::alloc::device, int >::type = 0 >
@@ -76,7 +88,10 @@ class usm_allocator {
76
88
" Device pointers do not support destroy on host" );
77
89
}
78
90
79
- // Note:: AllocKind == alloc::device is not allowed
91
+ // / Note:: AllocKind == alloc::device is not allowed.
92
+ // /
93
+ // / @param Val is a reference to object.
94
+ // / @return an address of the object referenced by Val.
80
95
template <
81
96
usm::alloc AllocT = AllocKind,
82
97
typename std::enable_if<AllocT != usm::alloc::device, int >::type = 0 >
@@ -107,35 +122,27 @@ class usm_allocator {
107
122
" Device pointers do not support address on host" );
108
123
}
109
124
110
- // Allocate memory
111
- template <
112
- usm::alloc AllocT = AllocKind,
113
- typename std::enable_if<AllocT == usm::alloc::host, int >::type = 0 >
114
- pointer allocate (size_t Size) {
115
- auto Result = reinterpret_cast <pointer>(detail::usm::alignedAllocHost (
116
- getAlignment (), Size * sizeof (value_type), mContext , AllocKind));
117
- if (!Result) {
118
- throw memory_allocation_error ();
119
- }
120
- return Result;
121
- }
125
+ // / Allocates memory.
126
+ // /
127
+ // / @param NumberOfElements is a count of elements to allocate memory for.
128
+ pointer allocate (size_t NumberOfElements) {
122
129
123
- template <usm::alloc AllocT = AllocKind,
124
- typename std::enable_if<AllocT != usm::alloc::host, int >::type = 0 >
125
- pointer allocate (size_t Size) {
126
130
auto Result = reinterpret_cast <pointer>(
127
- detail::usm::alignedAlloc (getAlignment (), Size * sizeof (value_type),
128
- mContext , mDevice , AllocKind));
131
+ aligned_alloc (getAlignment (), NumberOfElements * sizeof (value_type),
132
+ MDevice, MContext , AllocKind));
129
133
if (!Result) {
130
134
throw memory_allocation_error ();
131
135
}
132
136
return Result;
133
137
}
134
138
135
- // Deallocate memory
136
- void deallocate (pointer Ptr, size_t size) {
139
+ // / Deallocates memory.
140
+ // /
141
+ // / @param Ptr is a pointer to memory being deallocated.
142
+ // / @param Size is a number of elements previously passed to allocate.
143
+ void deallocate (pointer Ptr, size_t Size) {
137
144
if (Ptr) {
138
- detail::usm:: free (Ptr, mContext );
145
+ free (Ptr, MContext );
139
146
}
140
147
}
141
148
@@ -151,8 +158,8 @@ class usm_allocator {
151
158
return Alignment;
152
159
}
153
160
154
- const context mContext ;
155
- const device mDevice ;
161
+ const context MContext ;
162
+ const device MDevice ;
156
163
};
157
164
158
165
} // namespace sycl
0 commit comments