1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Mehdi Goli    Codeplay Software Ltd.
5// Ralph Potter  Codeplay Software Ltd.
6// Luke Iwanski  Codeplay Software Ltd.
7// Contact: <eigen@codeplay.com>
8// Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.com>
9
10//
11// This Source Code Form is subject to the terms of the Mozilla
12// Public License v. 2.0. If a copy of the MPL was not distributed
13// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
14
15#if defined(EIGEN_USE_SYCL) && !defined(EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H)
16#define EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H
17
18namespace Eigen {
19struct SyclDevice {
20  /// class members
21  /// sycl queue
22  mutable cl::sycl::queue m_queue;
23  /// std::map is the container used to make sure that we create only one buffer
24  /// per pointer. The lifespan of the buffer now depends on the lifespan of SyclDevice.
25  /// If a non-read-only pointer is needed to be accessed on the host we should manually deallocate it.
26  mutable std::map<const void *, std::shared_ptr<void>> buffer_map;
27  /// creating device by using selector
28  template<typename dev_Selector> SyclDevice(dev_Selector s)
29  :
30#ifdef EIGEN_EXCEPTIONS
31  m_queue(cl::sycl::queue(s, [=](cl::sycl::exception_list l) {
32    for (const auto& e : l) {
33      try {
34        std::rethrow_exception(e);
35      } catch (cl::sycl::exception e) {
36          std::cout << e.what() << std::endl;
37        }
38    }
39  }))
40#else
41  m_queue(cl::sycl::queue(s))
42#endif
43  {}
44  // destructor
45  ~SyclDevice() { deallocate_all(); }
46
47  template <typename T> void deallocate(T *p) const {
48    auto it = buffer_map.find(p);
49    if (it != buffer_map.end()) {
50      buffer_map.erase(it);
51      internal::aligned_free(p);
52    }
53  }
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()) {
57      auto p=it->first;
58      buffer_map.erase(it);
59      internal::aligned_free(const_cast<void*>(p));
60      it=buffer_map.begin();
61    }
62    buffer_map.clear();
63  }
64
65  /// creation of sycl accessor for a buffer. This function first tries to find
66  /// the buffer in the buffer_map. If found it gets the accessor from it, if not,
67  ///the function then adds an entry by creating a sycl buffer for that particular pointer.
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));
71  }
72
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);
78    return ret;
79  }
80
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());
83  }
84
85  /// allocating memory on the cpu
86  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void *allocate(size_t) const {
87    return internal::aligned_malloc(8);
88  }
89
90  // some runtime conditions that can be applied here
91  bool isDeviceSuitable() const { return true; }
92
93  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpy(void *dst, const void *src, size_t n) const {
94    ::memcpy(dst, src, n);
95  }
96
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);
100  }
101 /// whith the current implementation of sycl, the data is copied twice from device to host. This will be fixed soon.
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);
107    } else{
108      eigen_assert("no device memory found. The memory might be destroyed before creation");
109    }
110  }
111
112  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memset(void *buffer, int c, size_t n) const {
113    ::memset(buffer, c, n);
114  }
115  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE int majorDeviceVersion() const {
116  return 1;
117  }
118};
119
120}  // end namespace Eigen
121
122#endif  // EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H
123