TensorDeviceSycl.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Mehdi Goli    Codeplay Software Ltd.
00005 // Ralph Potter  Codeplay Software Ltd.
00006 // Luke Iwanski  Codeplay Software Ltd.
00007 // Contact: <eigen@codeplay.com>
00008 // Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.com>
00009 
00010 //
00011 // This Source Code Form is subject to the terms of the Mozilla
00012 // Public License v. 2.0. If a copy of the MPL was not distributed
00013 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00014 
00015 #if defined(EIGEN_USE_SYCL) && !defined(EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H)
00016 #define EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H
00017 
00018 namespace Eigen {
00019 struct SyclDevice {
00022   mutable cl::sycl::queue m_queue;
00026   mutable std::map<const void *, std::shared_ptr<void>> buffer_map;
00028   template<typename dev_Selector> SyclDevice(dev_Selector s)
00029   :
00030 #ifdef EIGEN_EXCEPTIONS
00031   m_queue(cl::sycl::queue(s, [=](cl::sycl::exception_list l) {
00032     for (const auto& e : l) {
00033       try {
00034         std::rethrow_exception(e);
00035       } catch (cl::sycl::exception e) {
00036           std::cout << e.what() << std::endl;
00037         }
00038     }
00039   }))
00040 #else
00041   m_queue(cl::sycl::queue(s))
00042 #endif
00043   {}
00044   // destructor
00045   ~SyclDevice() { deallocate_all(); }
00046 
00047   template <typename T> void deallocate(T *p) const {
00048     auto it = buffer_map.find(p);
00049     if (it != buffer_map.end()) {
00050       buffer_map.erase(it);
00051       internal::aligned_free(p);
00052     }
00053   }
00054   void deallocate_all() const {
00055     std::map<const void *, std::shared_ptr<void>>::iterator it=buffer_map.begin();
00056     while (it!=buffer_map.end()) {
00057       auto p=it->first;
00058       buffer_map.erase(it);
00059       internal::aligned_free(const_cast<void*>(p));
00060       it=buffer_map.begin();
00061     }
00062     buffer_map.clear();
00063   }
00064 
00068   template <cl::sycl::access::mode AcMd, typename T> inline cl::sycl::accessor<T, 1, AcMd, cl::sycl::access::target::global_buffer>
00069   get_sycl_accessor(size_t num_bytes, cl::sycl::handler &cgh, const T * ptr) const {
00070     return (get_sycl_buffer<T>(num_bytes, ptr)->template get_access<AcMd, cl::sycl::access::target::global_buffer>(cgh));
00071   }
00072 
00073   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 {
00074     using Type = cl::sycl::buffer<T, 1>;
00075     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)),
00076       [](void *dataMem) { delete static_cast<Type*>(dataMem); })));
00077     (static_cast<Type*>(buffer_map.at(ptr).get()))->set_final_data(nullptr);
00078     return ret;
00079   }
00080 
00081   template <typename T> inline cl::sycl::buffer<T, 1>* get_sycl_buffer(size_t num_bytes,const T * ptr) const {
00082     return static_cast<cl::sycl::buffer<T, 1>*>(add_sycl_buffer(ptr, num_bytes).first->second.get());
00083   }
00084 
00086   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void *allocate(size_t) const {
00087     return internal::aligned_malloc(8);
00088   }
00089 
00090   // some runtime conditions that can be applied here
00091   bool isDeviceSuitable() const { return true; }
00092 
00093   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpy(void *dst, const void *src, size_t n) const {
00094     ::memcpy(dst, src, n);
00095   }
00096 
00097   template<typename T> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyHostToDevice(T *dst, const T *src, size_t n) const {
00098     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>();
00099     memcpy(host_acc.get_pointer(), src, n);
00100   }
00102   template<typename T> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyDeviceToHost(T *dst, const T *src, size_t n) const {
00103     auto it = buffer_map.find(src);
00104     if (it != buffer_map.end()) {
00105       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>();
00106       memcpy(dst,host_acc.get_pointer(),  n);
00107     } else{
00108       eigen_assert("no device memory found. The memory might be destroyed before creation");
00109     }
00110   }
00111 
00112   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memset(void *buffer, int c, size_t n) const {
00113     ::memset(buffer, c, n);
00114   }
00115   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE int majorDeviceVersion() const {
00116   return 1;
00117   }
00118 };
00119 
00120 }  // end namespace Eigen
00121 
00122 #endif  // EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H
 All Classes Functions Variables Typedefs Enumerator