diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h b/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h index 7c039890e..e767d8965 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h @@ -72,9 +72,14 @@ struct SyclDevice { template inline std::pair>::iterator,bool> add_sycl_buffer(const T *ptr, size_t num_bytes) const { using Type = cl::sycl::buffer; - std::pair>::iterator,bool> 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(buffer_map.at(ptr).get()))->set_final_data(nullptr); + 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 is not initialised!!") + } return ret; } @@ -83,36 +88,77 @@ struct SyclDevice { } /// allocating memory on the cpu - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void *allocate(size_t) const { + void *allocate(size_t) const { return internal::aligned_malloc(8); } // some runtime conditions that can be applied here bool isDeviceSuitable() const { return true; } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpy(void *dst, const void *src, size_t n) const { + void memcpy(void *dst, const void *src, size_t n) const { ::memcpy(dst, src, n); } - template EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyHostToDevice(T *dst, const T *src, size_t n) const { + template void memcpyHostToDevice(T *dst, const T *src, size_t n) const { auto host_acc= (static_cast*>(add_sycl_buffer(dst, n).first->second.get()))-> template get_access(); memcpy(host_acc.get_pointer(), src, n); } - /// whith the current implementation of sycl, the data is copied twice from device to host. This will be fixed soon. - template EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyDeviceToHost(T *dst, const T *src, size_t n) const { + + 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; + rng = n; + if (rng==0) rng=1; + GRange=rng; + if (tileSize>GRange) tileSize=GRange; + else if(GRange>tileSize){ + size_t xMode = GRange % tileSize; + if (xMode != 0) GRange += (tileSize - xMode); + } + } + + template void memcpyDeviceToHost(T *dst, const T *src, size_t n) const { auto it = buffer_map.find(src); if (it != buffer_map.end()) { - auto host_acc= (static_cast*>(it->second.get()))-> template get_access(); - memcpy(dst,host_acc.get_pointer(), n); + size_t rng, GRange, tileSize; + parallel_for_setup(n/sizeof(T), tileSize, rng, GRange); + + 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 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]; + } + }); + }); + m_queue.throw_asynchronous(); + } else{ eigen_assert("no device memory found. The memory might be destroyed before creation"); } } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memset(void *buffer, int c, size_t n) const { - ::memset(buffer, c, n); + template 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 =(static_cast*>(add_sycl_buffer(buff, n).first->second.get()))-> 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 { typedef const typename Self::ChildType HostExpr; /// this is the child of reduction typedef typename TensorSycl::internal::createPlaceHolderExpression::Type PlaceHolderExpr; auto functors = TensorSycl::internal::extractFunctors(self.impl()); - - size_t tileSize =dev.m_queue.get_device(). template get_info()/2; - - size_t GRange=num_coeffs_to_preserve; - if (tileSize>GRange) tileSize=GRange; - else if(GRange>tileSize){ - size_t xMode = GRange % tileSize; - if (xMode != 0) GRange += (tileSize - xMode); - } + size_t range, GRange, tileSize; + dev.parallel_for_setup(num_coeffs_to_preserve, tileSize, range, GRange); // getting final out buffer at the moment the created buffer is true because there is no need for assign /// creating the shared memory for calculating reduction. /// This one is used to collect all the reduced value of shared memory as we dont have global barrier on GPU. Once it is saved we can @@ -223,7 +216,7 @@ struct InnerReducer { auto device_self_evaluator = Eigen::TensorEvaluator(device_self_expr, Eigen::DefaultDevice()); /// const cast added as a naive solution to solve the qualifier drop error auto globalid=itemID.get_global_linear_id(); - if (globalid< static_cast(num_coeffs_to_preserve)) { + if (globalid< range) { typename DeiceSelf::CoeffReturnType accum = functor.initialize(); GenericDimReducer::reduce(device_self_evaluator, device_self_evaluator.firstInput(globalid),const_cast(functor), &accum); functor.finalize(accum); diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorSyclRun.h b/unsupported/Eigen/CXX11/src/Tensor/TensorSyclRun.h index 7914b6fad..724eebd83 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorSyclRun.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorSyclRun.h @@ -37,18 +37,12 @@ void run(Expr &expr, Dev &dev) { typedef typename internal::createPlaceHolderExpression::Type PlaceHolderExpr; auto functors = internal::extractFunctors(evaluator); - size_t tileSize =dev.m_queue.get_device(). template get_info()/2; dev.m_queue.submit([&](cl::sycl::handler &cgh) { - // create a tuple of accessors from Evaluator auto tuple_of_accessors = internal::createTupleOfAccessors(cgh, evaluator); - const auto range = utility::tuple::get<0>(tuple_of_accessors).get_range()[0]; - size_t GRange=range; - if (tileSize>GRange) tileSize=GRange; - else if(GRange>tileSize){ - size_t xMode = GRange % tileSize; - if (xMode != 0) GRange += (tileSize - xMode); - } + size_t range, GRange, tileSize; + dev.parallel_for_setup(utility::tuple::get<0>(tuple_of_accessors).get_range()[0], tileSize, range, GRange); + // run the kernel cgh.parallel_for( cl::sycl::nd_range<1>(cl::sycl::range<1>(GRange), cl::sycl::range<1>(tileSize)), [=](cl::sycl::nd_item<1> itemID) { typedef typename internal::ConvertToDeviceExpression::Type DevExpr; diff --git a/unsupported/test/cxx11_tensor_device_sycl.cpp b/unsupported/test/cxx11_tensor_device_sycl.cpp index 7f79753c5..820bc88d0 100644 --- a/unsupported/test/cxx11_tensor_device_sycl.cpp +++ b/unsupported/test/cxx11_tensor_device_sycl.cpp @@ -19,10 +19,23 @@ #include "main.h" #include +#include void test_device_sycl(const Eigen::SyclDevice &sycl_device) { std::cout <<"Helo from ComputeCpp: the requested device exists and the device name is : " << sycl_device.m_queue.get_device(). template get_info() < tensorRange = {{sizeDim1}}; + Tensor in(tensorRange); + Tensor in1(tensorRange); + memset(in1.data(), 1,in1.dimensions().TotalSize()*sizeof(int)); + int * gpu_in_data = static_cast(sycl_device.allocate(in.dimensions().TotalSize()*sizeof(int))); + sycl_device.memset(gpu_in_data, 1,in.dimensions().TotalSize()*sizeof(int) ); + sycl_device.memcpyDeviceToHost(in.data(), gpu_in_data, in.dimensions().TotalSize()*sizeof(int) ); + for (int i=0; i