Distributed Ranges
Loading...
Searching...
No Matches
sycl_support.hpp
1// SPDX-FileCopyrightText: Intel Corporation
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#pragma once
6
7// file for helper functions working only if compiled with SYCL, assert
8// otherwise
9
10#ifdef SYCL_LANGUAGE_VERSION
11
12namespace dr::mp {
13
14sycl::queue &sycl_queue();
15
16} // namespace dr::mp
17
18namespace dr::mp::__detail {
19
20// sometimes we only want to dereference iterator inside SYCL
21template <typename T> auto sycl_get_deref(T v) {
22 using deref_type = std::remove_reference<decltype(*v)>::type;
23 deref_type temp;
24 {
25 sycl::buffer<deref_type> buff(&temp, 1);
26 sycl_queue()
27 .submit([&](auto &&h) {
28 sycl::accessor access(buff, h, sycl::write_only, sycl::no_init);
29 h.single_task([=](auto i) { access[0] = *v; });
30 })
31 .wait();
32 }
33 return temp;
34}
35
36template <typename T> T sycl_get(T &v) {
37 T temp;
38 sycl_queue().memcpy(&temp, &v, sizeof(v)).wait();
39 return temp;
40}
41
42template <typename T> auto sycl_get(T &v1, T &v2) {
43 std::pair<T, T> temp;
44 auto ev1 = sycl_queue().memcpy(&temp.first, &v1, sizeof(v1));
45 auto ev2 = sycl_queue().memcpy(&temp.second, &v2, sizeof(v2));
46 ev1.wait();
47 ev2.wait();
48 return temp;
49}
50
51template <typename T> void sycl_copy(T const *begin, T const *end, T *dst) {
52 sycl_queue().memcpy(dst, begin, (end - begin) * sizeof(T)).wait();
53}
54
55template <typename T, std::size_t Alignment>
56using shared_base_allocator =
57 sycl::usm_allocator<T, sycl::usm::alloc::shared, Alignment>;
58
59}; // namespace dr::mp::__detail
60
61namespace dr::mp {
62
63template <typename T, std::size_t Alignment = 0>
64class sycl_shared_allocator
65 : public __detail::shared_base_allocator<T, Alignment> {
66public:
67 sycl_shared_allocator(sycl::queue q = sycl_queue())
68 : __detail::shared_base_allocator<T, Alignment>(q) {}
69};
70
71struct device_policy {
72 device_policy(sycl::queue q = sycl_queue()) : queue(q), dpl_policy(q) {}
73
74 sycl::queue queue;
75 decltype(oneapi::dpl::execution::make_device_policy(queue)) dpl_policy;
76};
77
78} // namespace dr::mp
79
80#else // !SYCL_LANGUAGE_VERSION
81
82namespace dr::mp {
83
84struct device_policy {};
85
86} // namespace dr::mp
87
88namespace dr::mp::__detail {
89
90// define here to avoid ifdefs where it is called
91template <typename T> T sycl_get(T &v) {
92 assert(false);
93 return v;
94}
95
96// define here to avoid ifdefs where it is called
97template <typename T> auto sycl_get(T &v1, T &v2) {
98 assert(false);
99 return std::pair<T, T>{v1, v2};
100}
101
102template <typename T> void sycl_copy(T const *begin, T const *end, T *dst) {
103 assert(false);
104}
105
106} // namespace dr::mp::__detail
107
108#endif // SYCL_LANGUAGE_VERSION
109
110namespace dr::mp::__detail {
111
112template <typename T>
113void sycl_copy(T const *src, T *dst, std::size_t size = 1) {
114 sycl_copy(src, src + size, dst);
115}
116
117} // namespace dr::mp::__detail
Definition: sycl_support.hpp:84