77// ===--------------------------------------------------------------------=== //
88
99#include < CL/sycl/context.hpp>
10+ #include < CL/sycl/detail/aligned_allocator.hpp>
11+ #include < CL/sycl/detail/os_util.hpp>
1012#include < CL/sycl/detail/pi.hpp>
1113#include < CL/sycl/device.hpp>
1214#include < CL/sycl/usm.hpp>
1315
16+ #include < cstdlib>
17+
1418namespace cl {
1519namespace sycl {
1620
@@ -21,75 +25,112 @@ namespace usm {
2125
2226void *alignedAlloc (size_t Alignment, size_t Size, const context &Ctxt,
2327 alloc Kind) {
24- std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl (Ctxt);
25- std::shared_ptr<USMDispatcher> Dispatch = CtxImpl->getUSMDispatch ();
26- pi_context C = CtxImpl->getHandleRef ();
27- pi_result Error;
2828 void *RetVal = nullptr ;
29-
30- switch (Kind) {
31- case alloc::host: {
32- RetVal = Dispatch->hostMemAlloc (C, nullptr , Size, Alignment, &Error);
33- break ;
34- }
35- case alloc::device:
36- case alloc::shared:
37- case alloc::unknown: {
38- RetVal = nullptr ;
39- Error = PI_INVALID_VALUE;
40- break ;
41- }
29+ if (Ctxt.is_host ()) {
30+ if (!Alignment) {
31+ // worst case default
32+ Alignment = 128 ;
33+ }
34+
35+ aligned_allocator<char > Alloc (Alignment);
36+ try {
37+ RetVal = Alloc.allocate (Size);
38+ } catch (const std::bad_alloc &) {
39+ // Conform with Specification behavior
40+ RetVal = nullptr ;
41+ }
42+ } else {
43+ std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl (Ctxt);
44+ std::shared_ptr<USMDispatcher> Dispatch = CtxImpl->getUSMDispatch ();
45+ pi_context C = CtxImpl->getHandleRef ();
46+ pi_result Error;
47+
48+ switch (Kind) {
49+ case alloc::host: {
50+ RetVal = Dispatch->hostMemAlloc (C, nullptr , Size, Alignment, &Error);
51+ break ;
52+ }
53+ case alloc::device:
54+ case alloc::shared:
55+ case alloc::unknown: {
56+ RetVal = nullptr ;
57+ Error = PI_INVALID_VALUE;
58+ break ;
59+ }
60+ }
61+
62+ // Error is for debugging purposes.
63+ // The spec wants a nullptr returned, not an exception.
64+ if (Error != PI_SUCCESS)
65+ return nullptr ;
4266 }
43-
44- // Error is for debugging purposes.
45- // The spec wants a nullptr returned, not an exception.
46- if (Error != PI_SUCCESS) return nullptr ;
47-
4867 return RetVal;
4968}
5069
5170void *alignedAlloc (size_t Alignment, size_t Size, const context &Ctxt,
5271 const device &Dev, alloc Kind) {
53- std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl (Ctxt);
54- std::shared_ptr<USMDispatcher> Dispatch = CtxImpl->getUSMDispatch ();
55- pi_context C = CtxImpl->getHandleRef ();
56- pi_result Error;
57- pi_device Id;
5872 void *RetVal = nullptr ;
59-
60- switch (Kind) {
61- case alloc::device: {
62- Id = detail::getSyclObjImpl (Dev)->getHandleRef ();
63- RetVal = Dispatch->deviceMemAlloc (C, Id, nullptr , Size, Alignment, &Error);
64- break ;
73+ if (Ctxt.is_host ()) {
74+ if (!Alignment) {
75+ // worst case default
76+ Alignment = 128 ;
77+ }
78+
79+ aligned_allocator<char > Alloc (Alignment);
80+ try {
81+ RetVal = Alloc.allocate (Size);
82+ } catch (const std::bad_alloc &) {
83+ // Conform with Specification behavior
84+ RetVal = nullptr ;
85+ }
86+ } else {
87+ std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl (Ctxt);
88+ std::shared_ptr<USMDispatcher> Dispatch = CtxImpl->getUSMDispatch ();
89+ pi_context C = CtxImpl->getHandleRef ();
90+ pi_result Error;
91+ pi_device Id;
92+
93+ switch (Kind) {
94+ case alloc::device: {
95+ Id = detail::getSyclObjImpl (Dev)->getHandleRef ();
96+ RetVal =
97+ Dispatch->deviceMemAlloc (C, Id, nullptr , Size, Alignment, &Error);
98+ break ;
99+ }
100+ case alloc::shared: {
101+ Id = detail::getSyclObjImpl (Dev)->getHandleRef ();
102+ RetVal =
103+ Dispatch->sharedMemAlloc (C, Id, nullptr , Size, Alignment, &Error);
104+ break ;
105+ }
106+ case alloc::host:
107+ case alloc::unknown: {
108+ RetVal = nullptr ;
109+ Error = PI_INVALID_VALUE;
110+ break ;
111+ }
112+ }
113+
114+ // Error is for debugging purposes.
115+ // The spec wants a nullptr returned, not an exception.
116+ if (Error != PI_SUCCESS)
117+ return nullptr ;
65118 }
66- case alloc::shared: {
67- Id = detail::getSyclObjImpl (Dev)->getHandleRef ();
68- RetVal = Dispatch->sharedMemAlloc (C, Id, nullptr , Size, Alignment, &Error);
69- break ;
70- }
71- case alloc::host:
72- case alloc::unknown: {
73- RetVal = nullptr ;
74- Error = PI_INVALID_VALUE;
75- break ;
76- }
77- }
78-
79- // Error is for debugging purposes.
80- // The spec wants a nullptr returned, not an exception.
81- if (Error != PI_SUCCESS) return nullptr ;
82-
83119 return RetVal;
84120}
85121
86122void free (void *Ptr, const context &Ctxt) {
87- std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl (Ctxt);
88- std::shared_ptr<USMDispatcher> Dispatch = CtxImpl->getUSMDispatch ();
89- pi_context C = CtxImpl->getHandleRef ();
90- pi_result Error = Dispatch->memFree (C, Ptr);
91-
92- PI_CHECK (Error);
123+ if (Ctxt.is_host ()) {
124+ // need to use alignedFree here for Windows
125+ detail::OSUtil::alignedFree (Ptr);
126+ } else {
127+ std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl (Ctxt);
128+ std::shared_ptr<USMDispatcher> Dispatch = CtxImpl->getUSMDispatch ();
129+ pi_context C = CtxImpl->getHandleRef ();
130+ pi_result Error = Dispatch->memFree (C, Ptr);
131+
132+ PI_CHECK (Error);
133+ }
93134}
94135
95136} // namespace usm
0 commit comments