DPC++ Runtime
Runtime libraries for oneAPI DPC++
usm.hpp
Go to the documentation of this file.
1 //==---------------- usm.hpp - SYCL USM ------------------------*- C++ -*---==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 // ===--------------------------------------------------------------------=== //
8 #pragma once
9 
14 
15 #include <cstddef>
16 
18 namespace sycl {
20 // Explicit USM
22 __SYCL_EXPORT void *malloc_device(
23  size_t size, const device &dev, const context &ctxt,
24  const detail::code_location CodeLoc = detail::code_location::current());
25 __SYCL_EXPORT void *malloc_device(
26  size_t size, const device &dev, const context &ctxt,
27  const property_list &propList,
28  const detail::code_location CodeLoc = detail::code_location::current());
29 __SYCL_EXPORT void *malloc_device(
30  size_t size, const queue &q,
31  const detail::code_location CodeLoc = detail::code_location::current());
32 __SYCL_EXPORT void *malloc_device(
33  size_t size, const queue &q, const property_list &propList,
34  const detail::code_location CodeLoc = detail::code_location::current());
35 
36 __SYCL_EXPORT void *aligned_alloc_device(
37  size_t alignment, size_t size, const device &dev, const context &ctxt,
38  const detail::code_location CodeLoc = detail::code_location::current());
39 __SYCL_EXPORT void *aligned_alloc_device(
40  size_t alignment, size_t size, const device &dev, const context &ctxt,
41  const property_list &propList,
42  const detail::code_location CodeLoc = detail::code_location::current());
43 __SYCL_EXPORT void *aligned_alloc_device(
44  size_t alignment, size_t size, const queue &q,
45  const detail::code_location CodeLoc = detail::code_location::current());
46 __SYCL_EXPORT void *aligned_alloc_device(
47  size_t alignment, size_t size, const queue &q,
48  const property_list &propList,
49  const detail::code_location CodeLoc = detail::code_location::current());
50 
51 __SYCL_EXPORT void
52 free(void *ptr, const context &ctxt,
53  const detail::code_location CodeLoc = detail::code_location::current());
54 __SYCL_EXPORT void
55 free(void *ptr, const queue &q,
56  const detail::code_location CodeLoc = detail::code_location::current());
57 
59 // Restricted USM
61 __SYCL_EXPORT void *malloc_host(
62  size_t size, const context &ctxt,
63  const detail::code_location CodeLoc = detail::code_location::current());
64 __SYCL_EXPORT void *malloc_host(
65  size_t size, const context &ctxt, const property_list &propList,
66  const detail::code_location CodeLoc = detail::code_location::current());
67 __SYCL_EXPORT void *malloc_host(
68  size_t size, const queue &q,
69  const detail::code_location CodeLoc = detail::code_location::current());
70 __SYCL_EXPORT void *malloc_host(
71  size_t size, const queue &q, const property_list &propList,
72  const detail::code_location CodeLoc = detail::code_location::current());
73 
74 __SYCL_EXPORT void *malloc_shared(
75  size_t size, const device &dev, const context &ctxt,
76  const detail::code_location CodeLoc = detail::code_location::current());
77 __SYCL_EXPORT void *malloc_shared(
78  size_t size, const device &dev, const context &ctxt,
79  const property_list &propList,
80  const detail::code_location CodeLoc = detail::code_location::current());
81 __SYCL_EXPORT void *malloc_shared(
82  size_t size, const queue &q,
83  const detail::code_location CodeLoc = detail::code_location::current());
84 __SYCL_EXPORT void *malloc_shared(
85  size_t size, const queue &q, const property_list &propList,
86  const detail::code_location CodeLoc = detail::code_location::current());
87 
88 __SYCL_EXPORT void *aligned_alloc_host(
89  size_t alignment, size_t size, const context &ctxt,
90  const detail::code_location CodeLoc = detail::code_location::current());
91 __SYCL_EXPORT void *aligned_alloc_host(
92  size_t alignment, size_t size, const context &ctxt,
93  const property_list &propList,
94  const detail::code_location CodeLoc = detail::code_location::current());
95 __SYCL_EXPORT void *aligned_alloc_host(
96  size_t alignment, size_t size, const queue &q,
97  const detail::code_location CodeLoc = detail::code_location::current());
98 __SYCL_EXPORT void *aligned_alloc_host(
99  size_t alignment, size_t size, const queue &q,
100  const property_list &propList,
101  const detail::code_location CodeLoc = detail::code_location::current());
102 
103 __SYCL_EXPORT void *aligned_alloc_shared(
104  size_t alignment, size_t size, const device &dev, const context &ctxt,
105  const detail::code_location CodeLoc = detail::code_location::current());
106 __SYCL_EXPORT void *aligned_alloc_shared(
107  size_t alignment, size_t size, const device &dev, const context &ctxt,
108  const property_list &propList,
109  const detail::code_location CodeLoc = detail::code_location::current());
110 __SYCL_EXPORT void *aligned_alloc_shared(
111  size_t alignment, size_t size, const queue &q,
112  const detail::code_location CodeLoc = detail::code_location::current());
113 __SYCL_EXPORT void *aligned_alloc_shared(
114  size_t alignment, size_t size, const queue &q,
115  const property_list &propList,
116  const detail::code_location CodeLoc = detail::code_location::current());
117 
119 // single form
121 __SYCL_EXPORT void *
122 malloc(size_t size, const device &dev, const context &ctxt, usm::alloc kind,
123  const detail::code_location CodeLoc = detail::code_location::current());
124 __SYCL_EXPORT void *
125 malloc(size_t size, const device &dev, const context &ctxt, usm::alloc kind,
126  const property_list &propList,
127  const detail::code_location CodeLoc = detail::code_location::current());
128 __SYCL_EXPORT void *
129 malloc(size_t size, const queue &q, usm::alloc kind,
130  const detail::code_location CodeLoc = detail::code_location::current());
131 __SYCL_EXPORT void *
132 malloc(size_t size, const queue &q, usm::alloc kind,
133  const property_list &propList,
134  const detail::code_location CodeLoc = detail::code_location::current());
135 
136 __SYCL_EXPORT void *aligned_alloc(
137  size_t alignment, size_t size, const device &dev, const context &ctxt,
138  usm::alloc kind,
139  const detail::code_location CodeLoc = detail::code_location::current());
140 __SYCL_EXPORT void *aligned_alloc(
141  size_t alignment, size_t size, const device &dev, const context &ctxt,
142  usm::alloc kind, const property_list &propList,
143  const detail::code_location CodeLoc = detail::code_location::current());
144 __SYCL_EXPORT void *aligned_alloc(
145  size_t alignment, size_t size, const queue &q, usm::alloc kind,
146  const detail::code_location CodeLoc = detail::code_location::current());
147 __SYCL_EXPORT void *aligned_alloc(
148  size_t alignment, size_t size, const queue &q, usm::alloc kind,
149  const property_list &propList,
150  const detail::code_location CodeLoc = detail::code_location::current());
151 
153 // Template forms
155 template <typename T>
157  size_t Count, const device &Dev, const context &Ctxt,
158  const property_list &PropList = {},
159  const detail::code_location CodeLoc = detail::code_location::current()) {
160  return static_cast<T *>(aligned_alloc_device(alignof(T), Count * sizeof(T),
161  Dev, Ctxt, PropList, CodeLoc));
162 }
163 
164 template <typename T>
166  size_t Count, const queue &Q, const property_list &PropList = {},
167  const detail::code_location CodeLoc = detail::code_location::current()) {
168  return malloc_device<T>(Count, Q.get_device(), Q.get_context(), PropList,
169  CodeLoc);
170 }
171 
172 template <typename T>
174  size_t Alignment, size_t Count, const device &Dev, const context &Ctxt,
175  const property_list &PropList = {},
176  const detail::code_location CodeLoc = detail::code_location::current()) {
177  return static_cast<T *>(aligned_alloc_device(max(Alignment, alignof(T)),
178  Count * sizeof(T), Dev, Ctxt,
179  PropList, CodeLoc));
180 }
181 
182 template <typename T>
184  size_t Alignment, size_t Count, const queue &Q,
185  const property_list &PropList = {},
186  const detail::code_location CodeLoc = detail::code_location::current()) {
187  return aligned_alloc_device<T>(Alignment, Count, Q.get_device(),
188  Q.get_context(), PropList, CodeLoc);
189 }
190 
191 template <typename T>
193  size_t Count, const context &Ctxt, const property_list &PropList = {},
194  const detail::code_location CodeLoc = detail::code_location::current()) {
195  return static_cast<T *>(aligned_alloc_host(alignof(T), Count * sizeof(T),
196  Ctxt, PropList, CodeLoc));
197 }
198 
199 template <typename T>
201  size_t Count, const queue &Q, const property_list &PropList = {},
202  const detail::code_location CodeLoc = detail::code_location::current()) {
203  return malloc_host<T>(Count, Q.get_context(), PropList, CodeLoc);
204 }
205 
206 template <typename T>
208  size_t Count, const device &Dev, const context &Ctxt,
209  const property_list &PropList = {},
210  const detail::code_location CodeLoc = detail::code_location::current()) {
211  return static_cast<T *>(aligned_alloc_shared(alignof(T), Count * sizeof(T),
212  Dev, Ctxt, PropList, CodeLoc));
213 }
214 
215 template <typename T>
217  size_t Count, const queue &Q, const property_list &PropList = {},
218  const detail::code_location CodeLoc = detail::code_location::current()) {
219  return malloc_shared<T>(Count, Q.get_device(), Q.get_context(), PropList,
220  CodeLoc);
221 }
222 
223 template <typename T>
225  size_t Alignment, size_t Count, const context &Ctxt,
226  const property_list &PropList = {},
227  const detail::code_location CodeLoc = detail::code_location::current()) {
228  return static_cast<T *>(aligned_alloc_host(std ::max(Alignment, alignof(T)),
229  Count * sizeof(T), Ctxt, PropList,
230  CodeLoc));
231 }
232 
233 template <typename T>
235  size_t Alignment, size_t Count, const queue &Q,
236  const property_list &PropList = {},
237  const detail::code_location CodeLoc = detail::code_location::current()) {
238  return aligned_alloc_host<T>(Alignment, Count, Q.get_context(), PropList,
239  CodeLoc);
240 }
241 
242 template <typename T>
244  size_t Alignment, size_t Count, const device &Dev, const context &Ctxt,
245  const property_list &PropList = {},
246  const detail::code_location CodeLoc = detail::code_location::current()) {
247  return static_cast<T *>(aligned_alloc_shared(max(Alignment, alignof(T)),
248  Count * sizeof(T), Dev, Ctxt,
249  PropList, CodeLoc));
250 }
251 
252 template <typename T>
254  size_t Alignment, size_t Count, const queue &Q,
255  const property_list &PropList = {},
256  const detail::code_location CodeLoc = detail::code_location::current()) {
257  return aligned_alloc_shared<T>(Alignment, Count, Q.get_device(),
258  Q.get_context(), PropList, CodeLoc);
259 }
260 
261 template <typename T>
263  size_t Count, const device &Dev, const context &Ctxt, usm::alloc Kind,
264  const property_list &PropList = {},
265  const detail::code_location CodeLoc = detail::code_location::current()) {
266  return static_cast<T *>(aligned_alloc(alignof(T), Count * sizeof(T), Dev,
267  Ctxt, Kind, PropList, CodeLoc));
268 }
269 
270 template <typename T>
272  size_t Count, const queue &Q, usm::alloc Kind,
273  const property_list &PropList = {},
274  const detail::code_location CodeLoc = detail::code_location::current()) {
275  return malloc<T>(Count, Q.get_device(), Q.get_context(), Kind, PropList,
276  CodeLoc);
277 }
278 
279 template <typename T>
281  size_t Alignment, size_t Count, const device &Dev, const context &Ctxt,
282  usm::alloc Kind, const property_list &PropList = {},
283  const detail::code_location CodeLoc = detail::code_location::current()) {
284  return static_cast<T *>(aligned_alloc(max(Alignment, alignof(T)),
285  Count * sizeof(T), Dev, Ctxt, Kind,
286  PropList, CodeLoc));
287 }
288 
289 template <typename T>
291  size_t Alignment, size_t Count, const queue &Q, usm::alloc Kind,
292  const property_list &PropList = {},
293  const detail::code_location CodeLoc = detail::code_location::current()) {
294  return aligned_alloc<T>(Alignment, Count, Q.get_device(), Q.get_context(),
295  Kind, PropList, CodeLoc);
296 }
297 
298 // Pointer queries
303 __SYCL_EXPORT usm::alloc get_pointer_type(const void *ptr, const context &ctxt);
304 
310 __SYCL_EXPORT device get_pointer_device(const void *ptr, const context &ctxt);
311 
312 } // namespace sycl
313 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::malloc_host
void * malloc_host(size_t size, const queue &q, const property_list &propList, const detail::code_location CodeLoc=detail::code_location::current())
Definition: usm.hpp:200
sycl
Definition: invoke_simd.hpp:68
cl::sycl::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:26
max
simd< _Tp, _Abi > max(const simd< _Tp, _Abi > &, const simd< _Tp, _Abi > &) noexcept
cl::sycl::free
void free(void *ptr, const queue &q, const detail::code_location CodeLoc=detail::code_location::current())
Definition: usm_impl.cpp:308
cl::sycl::aligned_alloc_host
void * aligned_alloc_host(size_t alignment, size_t size, const queue &q, const property_list &propList, const detail::code_location CodeLoc=detail::code_location::current())
Definition: usm.hpp:234
cl::sycl::queue
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:103
cl::sycl::info::queue
queue
Definition: info_desc.hpp:229
export.hpp
cl::sycl::malloc_shared
void * malloc_shared(size_t size, const queue &q, const property_list &propList, const detail::code_location CodeLoc=detail::code_location::current())
Definition: usm.hpp:216
cl::sycl::malloc
void * malloc(size_t size, const queue &q, usm::alloc kind, const property_list &propList, const detail::code_location CodeLoc=detail::code_location::current())
Definition: usm.hpp:271
cl::sycl::aligned_alloc_device
void * aligned_alloc_device(size_t alignment, size_t size, const queue &q, const property_list &propList, const detail::code_location CodeLoc=detail::code_location::current())
Definition: usm.hpp:183
cl::sycl::device
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:36
cl::sycl::malloc_device
void * malloc_device(size_t size, const queue &q, const property_list &propList, const detail::code_location CodeLoc=detail::code_location::current())
Definition: usm.hpp:165
usm_allocator.hpp
cl::sycl::aligned_alloc_shared
void * aligned_alloc_shared(size_t alignment, size_t size, const queue &q, const property_list &propList, const detail::code_location CodeLoc=detail::code_location::current())
Definition: usm.hpp:253
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::get_pointer_device
device get_pointer_device(const void *ptr, const context &ctxt)
Queries the device against which the pointer was allocated Throws an invalid_object_error if ptr is a...
Definition: usm_impl.cpp:527
cl::sycl::queue::get_device
device get_device() const
Definition: queue.cpp:75
cl::sycl::get_pointer_type
usm::alloc get_pointer_type(const void *ptr, const context &ctxt)
Query the allocation type from a USM pointer.
Definition: usm_impl.cpp:477
cl::sycl::queue::get_context
context get_context() const
Definition: queue.cpp:73
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:38
usm_properties.hpp
usm_enums.hpp
cl::sycl::aligned_alloc
void * aligned_alloc(size_t alignment, size_t size, const queue &q, usm::alloc kind, const property_list &propList, const detail::code_location CodeLoc=detail::code_location::current())
Definition: usm.hpp:290
cl::sycl::usm::alloc
alloc
Definition: usm_enums.hpp:14
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12