15 #if defined(EIGEN_USE_SYCL) && !defined(EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H) 16 #define EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H 22 mutable cl::sycl::queue m_queue;
26 mutable std::map<const void *, std::shared_ptr<void>> buffer_map;
28 template<
typename dev_Selector> SyclDevice(dev_Selector s)
30 #ifdef EIGEN_EXCEPTIONS
31 m_queue(cl::sycl::queue(s, [=](cl::sycl::exception_list l) {
32 for (
const auto& e : l) {
34 std::rethrow_exception(e);
35 }
catch (cl::sycl::exception e) {
36 std::cout << e.what() << std::endl;
41 m_queue(cl::sycl::queue(s))
45 ~SyclDevice() { deallocate_all(); }
47 template <
typename T>
void deallocate(T *p)
const {
48 auto it = buffer_map.find(p);
49 if (it != buffer_map.end()) {
54 void deallocate_all()
const {
55 std::map<const void *, std::shared_ptr<void>>::iterator it=buffer_map.begin();
56 while (it!=buffer_map.end()) {
60 it=buffer_map.begin();
68 template <cl::sycl::access::mode AcMd,
typename T>
inline cl::sycl::accessor<T, 1, AcMd, cl::sycl::access::target::global_buffer>
69 get_sycl_accessor(
size_t num_bytes, cl::sycl::handler &cgh,
const T * ptr)
const {
70 return (get_sycl_buffer<T>(num_bytes, ptr)->
template get_access<AcMd, cl::sycl::access::target::global_buffer>(cgh));
73 template<
typename T>
inline std::pair<std::map<const void *, std::shared_ptr<void>>::iterator,
bool> add_sycl_buffer(
const T *ptr,
size_t num_bytes)
const {
74 using Type = cl::sycl::buffer<T, 1>;
75 std::pair<std::map<const void *, std::shared_ptr<void>>::iterator,
bool> ret = buffer_map.insert(std::pair<
const void *, std::shared_ptr<void>>(ptr, std::shared_ptr<void>(
new Type(cl::sycl::range<1>(num_bytes)),
76 [](
void *dataMem) {
delete static_cast<Type*
>(dataMem); })));
77 (
static_cast<Type*
>(buffer_map.at(ptr).get()))->set_final_data(
nullptr);
81 template <
typename T>
inline cl::sycl::buffer<T, 1>* get_sycl_buffer(
size_t num_bytes,
const T * ptr)
const {
82 return static_cast<cl::sycl::buffer<T, 1>*
>(add_sycl_buffer(ptr, num_bytes).first->second.get());
91 bool isDeviceSuitable()
const {
return true; }
93 EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE void memcpy(
void *dst,
const void *src,
size_t n)
const {
94 ::memcpy(dst, src, n);
97 template<
typename T> EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE void memcpyHostToDevice(T *dst,
const T *src,
size_t n)
const {
98 auto host_acc= (
static_cast<cl::sycl::buffer<T, 1>*
>(add_sycl_buffer(dst, n).first->second.get()))->
template get_access<cl::sycl::access::mode::discard_write, cl::sycl::access::target::host_buffer>();
99 memcpy(host_acc.get_pointer(), src, n);
102 template<
typename T> EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE void memcpyDeviceToHost(T *dst,
const T *src,
size_t n)
const {
103 auto it = buffer_map.find(src);
104 if (it != buffer_map.end()) {
105 auto host_acc= (
static_cast<cl::sycl::buffer<T, 1>*
>(it->second.get()))->
template get_access<cl::sycl::access::mode::read, cl::sycl::access::target::host_buffer>();
106 memcpy(dst,host_acc.get_pointer(), n);
108 eigen_assert(
"no device memory found. The memory might be destroyed before creation");
113 ::memset(buffer, c, n);
122 #endif // EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H #define EIGEN_STRONG_INLINE
EIGEN_DEVICE_FUNC void * aligned_malloc(std::size_t size)
EIGEN_DEVICE_FUNC void aligned_free(void *ptr)