Distributed Ranges
Loading...
Searching...
No Matches
device_ref.hpp
1// SPDX-FileCopyrightText: Intel Corporation
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#pragma once
6
7#include <dr/sp/init.hpp>
8#include <sycl/sycl.hpp>
9#include <type_traits>
10
11namespace dr::sp {
12
13template <typename T>
14 requires(std::is_trivially_copyable_v<T> || std::is_void_v<T>)
16public:
17 device_ref() = delete;
18 ~device_ref() = default;
19 device_ref(const device_ref &) = default;
20
21 device_ref(T *pointer) : pointer_(pointer) {}
22
23 operator T() const {
24#ifdef __SYCL_DEVICE_ONLY__
25 return *pointer_;
26#else
27 auto &&q = dr::sp::__detail::default_queue();
28 char buffer[sizeof(T)] __attribute__((aligned(sizeof(T))));
29 q.memcpy(reinterpret_cast<T *>(buffer), pointer_, sizeof(T)).wait();
30 return *reinterpret_cast<T *>(buffer);
31#endif
32 }
33
34 device_ref operator=(const T &value) const
35 requires(!std::is_const_v<T>)
36 {
37#ifdef __SYCL_DEVICE_ONLY__
38 *pointer_ = value;
39#else
40 auto &&q = dr::sp::__detail::default_queue();
41 q.memcpy(pointer_, &value, sizeof(T)).wait();
42#endif
43 return *this;
44 }
45
46 device_ref operator=(const device_ref &other) const {
47#ifdef __SYCL_DEVICE_ONLY__
48 *pointer_ = *other.pointer_;
49#else
50 T value = other;
51 *this = value;
52#endif
53 return *this;
54 }
55
56private:
57 T *pointer_;
58};
59
60} // namespace dr::sp
Definition: device_ref.hpp:15