From 622805a0c5d216141eca3090e80d58c159e175ee Mon Sep 17 00:00:00 2001 From: Mehdi Goli Date: Fri, 18 Nov 2016 16:20:42 +0000 Subject: Modifying TensorDeviceSycl.h to always create buffer of type uint8_t and convert them to the actual type at the execution on the device; adding the queue interface class to separate the lifespan of sycl queue and buffers,created for that queue, from Eigen::SyclDevice; modifying sycl tests to support the evaluation of the results for both row major and column major data layout on all different devices that are supported by Sycl{CPU; GPU; and Host}. --- .../Eigen/CXX11/src/Tensor/TensorDeviceSycl.h | 266 ++++++++++----------- 1 file changed, 126 insertions(+), 140 deletions(-) (limited to 'unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h') diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h b/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h index fe8452d79..d6d127153 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h @@ -12,37 +12,34 @@ // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. -#include - #if defined(EIGEN_USE_SYCL) && !defined(EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H) #define EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H namespace Eigen { -struct SyclDevice { - /// class members: - bool exception_caught_ = false; - - /// sycl queue - mutable cl::sycl::queue m_queue; +#define ConvertToActualTypeSycl(T, buf_acc) reinterpret_cast::pointer_t>((&(*buf_acc.get_pointer()))) + +struct QueueInterface { + /// class members: /// std::map is the container used to make sure that we create only one buffer /// per pointer. The lifespan of the buffer now depends on the lifespan of SyclDevice. /// If a non-read-only pointer is needed to be accessed on the host we should manually deallocate it. - mutable std::map> buffer_map; - + mutable std::map> buffer_map; + /// sycl queue + mutable cl::sycl::queue m_queue; /// creating device by using selector - template explicit SyclDevice(dev_Selector s): + /// SyclStreamDevice is not owned. it is the caller's responsibility to destroy it. + template explicit QueueInterface(dev_Selector s): #ifdef EIGEN_EXCEPTIONS m_queue(cl::sycl::queue(s, [=](cl::sycl::exception_list l) { for (const auto& e : l) { try { - if (e) { - exception_caught_ = true; + if(e){ std::rethrow_exception(e); } - } catch (const cl::sycl::exception& e) { - std::cerr << e.what() << std::endl; - } + } catch (cl::sycl::exception e) { + std::cerr << e.what() << std::endl; + } } })) #else @@ -50,63 +47,92 @@ struct SyclDevice { #endif {} - // destructor - ~SyclDevice() { deallocate_all(); } + /// Allocating device pointer. This pointer is actually an 8 bytes host pointer used as key to access the sycl device buffer. + /// The reason is that we cannot use device buffer as a pointer as a m_data in Eigen leafNode expressions. So we create a key + /// pointer to be used in Eigen expression construction. When we convert the Eigen construction into the sycl construction we + /// use this pointer as a key in our buffer_map and we make sure that we dedicate only one buffer only for this pointer. + /// The device pointer would be deleted by calling deallocate function. + EIGEN_STRONG_INLINE void* allocate(size_t num_bytes) const { + auto buf = cl::sycl::buffer(cl::sycl::range<1>(num_bytes)); + auto ptr =buf.get_access().get_pointer(); + buf.set_final_data(nullptr); + buffer_map.insert(std::pair>(ptr,buf)); + return static_cast(ptr); + } /// This is used to deallocate the device pointer. p is used as a key inside /// the map to find the device buffer and delete it. - template EIGEN_STRONG_INLINE void deallocate(T *p) const { - auto it = buffer_map.find(p); + EIGEN_STRONG_INLINE void deallocate(const void *p) const { + auto it = buffer_map.find(static_cast(p)); if (it != buffer_map.end()) { buffer_map.erase(it); - internal::aligned_free(p); } } - /// This is called by the SyclDevice destructor to release all allocated memory if the user didn't already do so. - /// We also free the host pointer that we have dedicated as a key to accessing the device buffer. - EIGEN_STRONG_INLINE void deallocate_all() const { - std::map>::iterator it=buffer_map.begin(); - while (it!=buffer_map.end()) { - auto p=it->first; - buffer_map.erase(it); - internal::aligned_free(const_cast(p)); - it=buffer_map.begin(); + EIGEN_STRONG_INLINE std::map>::iterator find_buffer(const void* ptr) const { + auto it1 = buffer_map.find(static_cast(ptr)); + if (it1 != buffer_map.end()){ + return it1; + } + else{ + for(std::map>::iterator it=buffer_map.begin(); it!=buffer_map.end(); ++it){ + auto size = it->second.get_size(); + if((it->first < (static_cast(ptr))) && ((static_cast(ptr)) < (it->first + size)) ) return it; + } + } + //eigen_assert("No sycl buffer found. Make sure that you have allocated memory for your buffer by calling allocate function in SyclDevice"); + std::cerr << "No sycl buffer found. Make sure that you have allocated memory for your buffer by calling allocate function in SyclDevice"<< std::endl; + abort(); + //return buffer_map.end(); + } + + // destructor + ~QueueInterface() { buffer_map.clear(); } +}; + +template class MemCopyFunctor { + public: + typedef cl::sycl::accessor read_accessor; + typedef cl::sycl::accessor write_accessor; + MemCopyFunctor(read_accessor src_acc, write_accessor dst_acc, size_t rng, size_t i, size_t offset): m_src_acc(src_acc), m_dst_acc(dst_acc), m_rng(rng), m_i(i), m_offset(offset) {} + void operator()(cl::sycl::nd_item<1> itemID) { + auto src_ptr = ConvertToActualTypeSycl(T, m_src_acc); + auto dst_ptr = ConvertToActualTypeSycl(T, m_dst_acc); + auto globalid = itemID.get_global_linear_id(); + if (globalid< m_rng) { + dst_ptr[globalid + m_i] = src_ptr[globalid + m_offset]; } - buffer_map.clear(); } + private: + read_accessor m_src_acc; + write_accessor m_dst_acc; + size_t m_rng; + size_t m_i; + size_t m_offset; +}; + +struct SyclDevice { + // class member. + QueueInterface* m_queu_stream; + /// QueueInterface is not owned. it is the caller's responsibility to destroy it. + explicit SyclDevice(QueueInterface* queu_stream):m_queu_stream(queu_stream){} /// Creation of sycl accessor for a buffer. This function first tries to find /// the buffer in the buffer_map. If found it gets the accessor from it, if not, /// the function then adds an entry by creating a sycl buffer for that particular pointer. - template EIGEN_STRONG_INLINE cl::sycl::accessor - get_sycl_accessor(size_t num_bytes, cl::sycl::handler &cgh, const T * ptr) const { - return (get_sycl_buffer(num_bytes, ptr)->template get_access(cgh)); - } - - /// Inserting a new sycl buffer. For every allocated device pointer only one buffer would be created. The buffer type is a device- only buffer. - /// The key pointer used to access the device buffer(the device pointer(ptr) ) must be initialised by the allocate function. - template EIGEN_STRONG_INLINE std::pair>::iterator,bool> add_sycl_buffer(size_t num_bytes, const T *ptr) const { - using Type = cl::sycl::buffer; - std::pair>::iterator,bool> ret; - if(ptr!=nullptr){ - ret= buffer_map.insert(std::pair>(ptr, std::shared_ptr(new Type(cl::sycl::range<1>(num_bytes)), - [](void *dataMem) { delete static_cast(dataMem); }))); - (static_cast(ret.first->second.get()))->set_final_data(nullptr); - } else { - eigen_assert("The device memory is not allocated. Please call allocate on the device!!"); - } - return ret; + template EIGEN_STRONG_INLINE cl::sycl::accessor + get_sycl_accessor(size_t num_bytes, cl::sycl::handler &cgh, const void* ptr) const { + return (get_sycl_buffer(num_bytes, ptr).template get_access(cgh)); } /// Accessing the created sycl device buffer for the device pointer - template EIGEN_STRONG_INLINE cl::sycl::buffer* get_sycl_buffer(size_t num_bytes,const T * ptr) const { - return static_cast*>(add_sycl_buffer(num_bytes, ptr).first->second.get()); + EIGEN_STRONG_INLINE cl::sycl::buffer& get_sycl_buffer(size_t , const void * ptr) const { + return m_queu_stream->find_buffer(ptr)->second; } /// This is used to prepare the number of threads and also the number of threads per block for sycl kernels EIGEN_STRONG_INLINE void parallel_for_setup(size_t n, size_t &tileSize, size_t &rng, size_t &GRange) const { - tileSize =m_queue.get_device(). template get_info()/2; + tileSize =sycl_queue().get_device(). template get_info()/2; rng = n; if (rng==0) rng=1; GRange=rng; @@ -116,57 +142,35 @@ struct SyclDevice { if (xMode != 0) GRange += (tileSize - xMode); } } - - /// Allocating device pointer. This pointer is actually an 8 bytes host pointer used as key to access the sycl device buffer. - /// The reason is that we cannot use device buffer as a pointer as a m_data in Eigen leafNode expressions. So we create a key - /// pointer to be used in Eigen expression construction. When we convert the Eigen construction into the sycl construction we - /// use this pointer as a key in our buffer_map and we make sure that we dedicate only one buffer only for this pointer. - /// The device pointer would be deleted by calling deallocate function. - EIGEN_STRONG_INLINE void *allocate(size_t) const { - return internal::aligned_malloc(8); + /// allocate device memory + EIGEN_STRONG_INLINE void *allocate(size_t num_bytes) const { + return m_queu_stream->allocate(num_bytes); } + /// deallocate device memory + EIGEN_STRONG_INLINE void deallocate(const void *p) const { + m_queu_stream->deallocate(p); + } // some runtime conditions that can be applied here EIGEN_STRONG_INLINE bool isDeviceSuitable() const { return true; } - template EIGEN_STRONG_INLINE std::map>::iterator find_nearest(const T* ptr) const { - auto it1 = buffer_map.find(ptr); - if (it1 != buffer_map.end()){ - return it1; - } - else{ - for(std::map>::iterator it=buffer_map.begin(); it!=buffer_map.end(); ++it){ - auto size = ((cl::sycl::buffer*)it->second.get())->get_size(); - if((static_cast(it->first) < ptr) && (ptr < (static_cast(it->first)) + size)) return it; - } - } - return buffer_map.end(); - } /// the memcpy function template EIGEN_STRONG_INLINE void memcpy(void *dst, const T *src, size_t n) const { - auto it1 = find_nearest(src); - auto it2 = find_nearest(static_cast(dst)); - if ((it1 != buffer_map.end()) && (it2!=buffer_map.end())) { - auto offset= (src - (static_cast(it1->first))); - auto i= ((static_cast(dst)) - const_cast((static_cast(it2->first)))); - size_t rng, GRange, tileSize; - parallel_for_setup(n/sizeof(T), tileSize, rng, GRange); - m_queue.submit([&](cl::sycl::handler &cgh) { - auto src_acc =((cl::sycl::buffer*)it1->second.get())-> template get_access(cgh); - auto dst_acc =((cl::sycl::buffer*)it2->second.get())-> template get_access(cgh); - typedef decltype(src_acc) DevToDev; - cgh.parallel_for( cl::sycl::nd_range<1>(cl::sycl::range<1>(GRange), cl::sycl::range<1>(tileSize)), [=](cl::sycl::nd_item<1> itemID) { - auto globalid=itemID.get_global_linear_id(); - if (globalid< rng) { - dst_acc[globalid+i ]=src_acc[globalid+offset]; - } - }); - }); - m_queue.throw_asynchronous(); - } else { - eigen_assert("no source or destination device memory found."); - } + auto it1 = m_queu_stream->find_buffer((void*)src); + auto it2 = m_queu_stream->find_buffer(dst); + auto offset= (static_cast(static_cast(src))) - it1->first; + auto i= (static_cast(dst)) - it2->first; + offset/=sizeof(T); + i/=sizeof(T); + size_t rng, GRange, tileSize; + parallel_for_setup(n/sizeof(T), tileSize, rng, GRange); + sycl_queue().submit([&](cl::sycl::handler &cgh) { + auto src_acc =it1->second.template get_access(cgh); + auto dst_acc =it2->second.template get_access(cgh); + cgh.parallel_for(cl::sycl::nd_range<1>(cl::sycl::range<1>(GRange), cl::sycl::range<1>(tileSize)), MemCopyFunctor(src_acc, dst_acc, rng, 0, offset)); + }); + sycl_queue().throw_asynchronous(); } /// The memcpyHostToDevice is used to copy the device only pointer to a host pointer. Using the device @@ -175,8 +179,7 @@ struct SyclDevice { /// buffer to host. Then we use the memcpy to copy the data to the host accessor. The first time that /// this buffer is accessed, the data will be copied to the device. template EIGEN_STRONG_INLINE void memcpyHostToDevice(T *dst, const T *src, size_t n) const { - - auto host_acc= get_sycl_buffer(n, dst)-> template get_access(); + auto host_acc= get_sycl_buffer(n, dst). template get_access(); ::memcpy(host_acc.get_pointer(), src, n); } /// The memcpyDeviceToHost is used to copy the data from host to device. Here, in order to avoid double copying the data. We create a sycl @@ -185,61 +188,44 @@ struct SyclDevice { /// buffer with map_allocator on the gpu in parallel. At the end of the function call the destination buffer would be destroyed and the data /// would be available on the dst pointer using fast copy technique (map_allocator). In this case we can make sure that we copy the data back /// to the cpu only once per function call. - template EIGEN_STRONG_INLINE void memcpyDeviceToHost(T *dst, const T *src, size_t n) const { - auto it = find_nearest(src); - auto offset = src- (static_cast(it->first)); - if (it != buffer_map.end()) { + template EIGEN_STRONG_INLINE void memcpyDeviceToHost(void *dst, const T *src, size_t n) const { + auto it = m_queu_stream->find_buffer(src); + auto offset =static_cast(static_cast(src))- it->first; + offset/=sizeof(T); size_t rng, GRange, tileSize; parallel_for_setup(n/sizeof(T), tileSize, rng, GRange); // Assuming that the dst is the start of the destination pointer - auto dest_buf = cl::sycl::buffer>(dst, cl::sycl::range<1>(rng)); - typedef decltype(dest_buf) SYCLDTOH; - m_queue.submit([&](cl::sycl::handler &cgh) { - auto src_acc= (static_cast*>(it->second.get()))-> template get_access(cgh); + auto dest_buf = cl::sycl::buffer >(static_cast(dst), cl::sycl::range<1>(rng*sizeof(T))); + sycl_queue().submit([&](cl::sycl::handler &cgh) { + auto src_acc= it->second.template get_access(cgh); auto dst_acc =dest_buf.template get_access(cgh); - cgh.parallel_for( cl::sycl::nd_range<1>(cl::sycl::range<1>(GRange), cl::sycl::range<1>(tileSize)), [=](cl::sycl::nd_item<1> itemID) { - auto globalid=itemID.get_global_linear_id(); - if (globalid< dst_acc.get_size()) { - dst_acc[globalid] = src_acc[globalid + offset]; - } - }); + cgh.parallel_for( cl::sycl::nd_range<1>(cl::sycl::range<1>(GRange), cl::sycl::range<1>(tileSize)), MemCopyFunctor(src_acc, dst_acc, rng, 0, offset)); }); - m_queue.throw_asynchronous(); - - } else { - eigen_assert("no device memory found. The memory might be destroyed before creation"); - } + sycl_queue().throw_asynchronous(); } - + /// returning the sycl queue + EIGEN_STRONG_INLINE cl::sycl::queue& sycl_queue() const { return m_queu_stream->m_queue;} /// Here is the implementation of memset function on sycl. template EIGEN_STRONG_INLINE void memset(T *buff, int c, size_t n) const { - size_t rng, GRange, tileSize; - parallel_for_setup(n/sizeof(T), tileSize, rng, GRange); - m_queue.submit([&](cl::sycl::handler &cgh) { - auto buf_acc =get_sycl_buffer(n, buff)-> template get_access(cgh); - cgh.parallel_for( cl::sycl::nd_range<1>(cl::sycl::range<1>(GRange), cl::sycl::range<1>(tileSize)), [=](cl::sycl::nd_item<1> itemID) { - auto globalid=itemID.get_global_linear_id(); - auto buf_ptr= reinterpret_cast::pointer_t>((&(*buf_acc.get_pointer()))); - if (globalid< buf_acc.get_size()) { - for(size_t i=0; i(static_cast(buff))). template get_access(cgh); + cgh.parallel_for( cl::sycl::nd_range<1>(cl::sycl::range<1>(GRange), cl::sycl::range<1>(tileSize)), [=](cl::sycl::nd_item<1> itemID) { + auto globalid=itemID.get_global_linear_id(); + if (globalid< buf_acc.get_size()) { + for(size_t i=0; i