diff options
author | Mehdi Goli <mehdi.goli@codeplay.com> | 2016-11-08 17:08:02 +0000 |
---|---|---|
committer | Mehdi Goli <mehdi.goli@codeplay.com> | 2016-11-08 17:08:02 +0000 |
commit | d57430dd73ab2f88aa5e45c370f6ab91103ff18a (patch) | |
tree | d3d46d788686c38b1da1cb696807d51334829e5a /unsupported | |
parent | dad177be010b45ba42425ab04af6dde6c479453b (diff) |
Converting all sycl buffers to uninitialised device only buffers; adding memcpyHostToDevice and memcpyDeviceToHost on syclDevice; modifying all examples to obey the new rules; moving sycl queue creating to the device based on Benoit suggestion; removing the sycl specefic condition for returning m_result in TensorReduction.h according to Benoit suggestion.
Diffstat (limited to 'unsupported')
-rw-r--r-- | unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h | 115 | ||||
-rw-r--r-- | unsupported/Eigen/CXX11/src/Tensor/TensorReduction.h | 8 | ||||
-rw-r--r-- | unsupported/Eigen/CXX11/src/Tensor/TensorReductionSycl.h | 10 | ||||
-rw-r--r-- | unsupported/Eigen/CXX11/src/Tensor/TensorSyclExtractAccessor.h | 22 | ||||
-rw-r--r-- | unsupported/test/cxx11_tensor_broadcast_sycl.cpp | 79 | ||||
-rw-r--r-- | unsupported/test/cxx11_tensor_device_sycl.cpp | 20 | ||||
-rw-r--r-- | unsupported/test/cxx11_tensor_forced_eval_sycl.cpp | 44 | ||||
-rw-r--r-- | unsupported/test/cxx11_tensor_reduction_sycl.cpp | 147 | ||||
-rw-r--r-- | unsupported/test/cxx11_tensor_sycl.cpp | 67 |
9 files changed, 241 insertions, 271 deletions
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h b/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h index 4231a11ff..8333301ea 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h @@ -16,95 +16,93 @@ #define EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H namespace Eigen { -/// \struct BufferT is used to specialise add_sycl_buffer function for -// two types of buffer we have. When the MapAllocator is true, we create the -// sycl buffer with MapAllocator. -/// We have to const_cast the input pointer in order to work around the fact -/// that sycl does not accept map allocator for const pointer. -template <typename T, bool MapAllocator> -struct BufferT { - using Type = cl::sycl::buffer<T, 1, cl::sycl::map_allocator<T>>; - static inline void add_sycl_buffer(const T *ptr, size_t num_bytes,std::map<const void *, std::shared_ptr<void>> &buffer_map) { - buffer_map.insert(std::pair<const void *, std::shared_ptr<void>>(ptr, std::shared_ptr<void>(std::make_shared<Type>(Type(const_cast<T *>(ptr), cl::sycl::range<1>(num_bytes)))))); - } -}; - -/// specialisation of the \ref BufferT when the MapAllocator is false. In this -/// case we only create the device-only buffer. -template <typename T> -struct BufferT<T, false> { - using Type = cl::sycl::buffer<T, 1>; - static inline void add_sycl_buffer(const T *ptr, size_t num_bytes, std::map<const void *, std::shared_ptr<void>> &buffer_map) { - buffer_map.insert(std::pair<const void *, std::shared_ptr<void>>(ptr, std::shared_ptr<void>(std::make_shared<Type>(Type(cl::sycl::range<1>(num_bytes)))))); - } -}; - struct SyclDevice { /// class members /// sycl queue - cl::sycl::queue &m_queue; + mutable cl::sycl::queue m_queue; /// 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. + /// 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<const void *, std::shared_ptr<void>> buffer_map; - - SyclDevice(cl::sycl::queue &q) : m_queue(q) {} + /// creating device by using selector + template<typename dev_Selector> SyclDevice(dev_Selector s) + :m_queue(cl::sycl::queue(s, [=](cl::sycl::exception_list l) { + for (const auto& e : l) { + try { + std::rethrow_exception(e); + } catch (cl::sycl::exception e) { + std::cout << e.what() << std::endl; + } + } + })) {} // destructor ~SyclDevice() { deallocate_all(); } - template <typename T> - void deallocate(const T *p) const { + template <typename T> void deallocate(T *p) const { auto it = buffer_map.find(p); if (it != buffer_map.end()) { buffer_map.erase(it); + internal::aligned_free(p); + } + } + void deallocate_all() const { + std::map<const void *, std::shared_ptr<void>>::iterator it=buffer_map.begin(); + while (it!=buffer_map.end()) { + auto p=it->first; + buffer_map.erase(it); + internal::aligned_free(const_cast<void*>(p)); + it=buffer_map.begin(); } + buffer_map.clear(); } - void deallocate_all() const { buffer_map.clear(); } /// 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 <cl::sycl::access::mode AcMd, bool MapAllocator, typename T> - inline cl::sycl::accessor<T, 1, AcMd, cl::sycl::access::target::global_buffer> + /// 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 <cl::sycl::access::mode AcMd, typename T> inline cl::sycl::accessor<T, 1, AcMd, cl::sycl::access::target::global_buffer> get_sycl_accessor(size_t num_bytes, cl::sycl::handler &cgh, const T * ptr) const { - return (get_sycl_buffer<MapAllocator,T>(num_bytes, ptr).template get_access<AcMd, cl::sycl::access::target::global_buffer>(cgh)); + return (get_sycl_buffer<T>(num_bytes, ptr)->template get_access<AcMd, cl::sycl::access::target::global_buffer>(cgh)); } -template <bool MapAllocator, typename T> - inline typename BufferT<T, MapAllocator>::Type - get_sycl_buffer(size_t num_bytes,const T * ptr) const { - if(MapAllocator && !ptr){ - eigen_assert("pointer with map_Allocator cannot be null. Please initialise the input pointer"); } - auto it = buffer_map.find(ptr); - if (it == buffer_map.end()) { - BufferT<T, MapAllocator>::add_sycl_buffer(ptr, num_bytes, buffer_map); - } - return (*((typename BufferT<T, MapAllocator>::Type*)((buffer_map.at(ptr).get())))); + 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 { + using Type = cl::sycl::buffer<T, 1>; + 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)), + [](void *dataMem) { delete static_cast<Type*>(dataMem); }))); + (static_cast<Type*>(buffer_map.at(ptr).get()))->set_final_data(nullptr); + return ret; + } + + template <typename T> inline cl::sycl::buffer<T, 1>* get_sycl_buffer(size_t num_bytes,const T * ptr) const { + return static_cast<cl::sycl::buffer<T, 1>*>(add_sycl_buffer(ptr, num_bytes).first->second.get()); } /// allocating memory on the cpu - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void *allocate(size_t num_bytes) const { - return internal::aligned_malloc(num_bytes); + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 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 deallocate(void *buffer) const { - internal::aligned_free(buffer); - } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpy(void *dst, const void *src, size_t n) const { ::memcpy(dst, src, n); } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyHostToDevice(void *dst, const void *src, size_t n) const { - memcpy(dst, src, n); + + template<typename T> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyHostToDevice(T *dst, const T *src, size_t n) const { + 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>(); + memcpy(host_acc.get_pointer(), src, n); } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void memcpyDeviceToHost(void *dst, const void *src, size_t n) const { - memcpy(dst, src, n); + /// whith the current implementation of sycl, the data is copied twice from device to host. This will be fixed soon. + template<typename T> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 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<cl::sycl::buffer<T, 1>*>(it->second.get()))-> template get_access<cl::sycl::access::mode::read, cl::sycl::access::target::host_buffer>(); + memcpy(dst,host_acc.get_pointer(), n); + } 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); } @@ -112,6 +110,7 @@ template <bool MapAllocator, typename T> return 1; } }; + } // end namespace Eigen #endif // EIGEN_CXX11_TENSOR_TENSOR_DEVICE_SYCL_H diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorReduction.h b/unsupported/Eigen/CXX11/src/Tensor/TensorReduction.h index 367bccf63..f731bf17e 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorReduction.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorReduction.h @@ -662,13 +662,7 @@ struct TensorEvaluator<const TensorReductionOp<Op, Dims, ArgType, MakePointer_>, } } - /// required by sycl in order to extract the output accessor -#ifndef EIGEN_USE_SYCL - EIGEN_DEVICE_FUNC typename MakePointer_<Scalar>::Type data() const { return NULL; } -#else - EIGEN_DEVICE_FUNC typename MakePointer_<Scalar>::Type data() const { - return m_result; } -#endif + EIGEN_DEVICE_FUNC typename MakePointer_<Scalar>::Type data() const { return m_result; } /// required by sycl in order to extract the accessor const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; } /// added for sycl in order to construct the buffer from the sycl device diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorReductionSycl.h b/unsupported/Eigen/CXX11/src/Tensor/TensorReductionSycl.h index 1c89132db..3daecb045 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorReductionSycl.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorReductionSycl.h @@ -27,9 +27,9 @@ namespace internal { template<typename CoeffReturnType, typename KernelName> struct syclGenericBufferReducer{ template<typename BufferTOut, typename BufferTIn> -static void run(BufferTOut& bufOut, BufferTIn& bufI, const Eigen::SyclDevice& dev, size_t length, size_t local){ +static void run(BufferTOut* bufOut, BufferTIn& bufI, const Eigen::SyclDevice& dev, size_t length, size_t local){ do { - auto f = [length, local, &bufOut, &bufI](cl::sycl::handler& h) mutable { + auto f = [length, local, bufOut, &bufI](cl::sycl::handler& h) mutable { cl::sycl::nd_range<1> r{cl::sycl::range<1>{std::max(length, local)}, cl::sycl::range<1>{std::min(length, local)}}; /* Two accessors are used: one to the buffer that is being reduced, @@ -37,7 +37,7 @@ static void run(BufferTOut& bufOut, BufferTIn& bufI, const Eigen::SyclDevice& de auto aI = bufI.template get_access<cl::sycl::access::mode::read_write>(h); auto aOut = - bufOut.template get_access<cl::sycl::access::mode::discard_write>(h); + bufOut->template get_access<cl::sycl::access::mode::discard_write>(h); cl::sycl::accessor<CoeffReturnType, 1, cl::sycl::access::mode::read_write, cl::sycl::access::target::local> scratch(cl::sycl::range<1>(local), h); @@ -134,7 +134,7 @@ struct FullReducer<Self, Op, const Eigen::SyclDevice, Vectorizable> { /// if the shared memory is less than the GRange, we set shared_mem size to the TotalSize and in this case one kernel would be created for recursion to reduce all to one. if (GRange < outTileSize) outTileSize=GRange; // getting final out buffer at the moment the created buffer is true because there is no need for assign - auto out_buffer =dev.template get_sycl_buffer<true, typename Eigen::internal::remove_all<CoeffReturnType>::type>(self.dimensions().TotalSize(), output); + auto out_buffer =dev.template get_sycl_buffer<typename Eigen::internal::remove_all<CoeffReturnType>::type>(self.dimensions().TotalSize(), output); /// 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 /// recursively apply reduction on it in order to reduce the whole. @@ -208,7 +208,7 @@ struct InnerReducer<Self, Op, const Eigen::SyclDevice> { dev.m_queue.submit([&](cl::sycl::handler &cgh) { // create a tuple of accessors from Evaluator auto tuple_of_accessors = TensorSycl::internal::createTupleOfAccessors(cgh, self.impl()); - auto output_accessor = dev.template get_sycl_accessor<cl::sycl::access::mode::discard_write, true>(num_coeffs_to_preserve,cgh, output); + auto output_accessor = dev.template get_sycl_accessor<cl::sycl::access::mode::discard_write>(num_coeffs_to_preserve,cgh, output); cgh.parallel_for<Self>( cl::sycl::nd_range<1>(cl::sycl::range<1>(GRange), cl::sycl::range<1>(tileSize)), [=](cl::sycl::nd_item<1> itemID) { typedef typename TensorSycl::internal::ConvertToDeviceExpression<const HostExpr>::Type DevExpr; diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorSyclExtractAccessor.h b/unsupported/Eigen/CXX11/src/Tensor/TensorSyclExtractAccessor.h index 3af5f8cfc..b1da6858e 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorSyclExtractAccessor.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorSyclExtractAccessor.h @@ -56,10 +56,10 @@ struct AccessorConstructor{ -> decltype(utility::tuple::append(ExtractAccessor<Arg1>::getTuple(cgh, eval1),utility::tuple::append(ExtractAccessor<Arg2>::getTuple(cgh, eval2), ExtractAccessor<Arg3>::getTuple(cgh, eval3)))) { return utility::tuple::append(ExtractAccessor<Arg1>::getTuple(cgh, eval1),utility::tuple::append(ExtractAccessor<Arg2>::getTuple(cgh, eval2), ExtractAccessor<Arg3>::getTuple(cgh, eval3))); } - template< cl::sycl::access::mode AcM, bool MapAllocator, typename Arg> static inline auto getAccessor(cl::sycl::handler& cgh, Arg eval) - -> decltype(utility::tuple::make_tuple( eval.device().template get_sycl_accessor<AcM, MapAllocator, + template< cl::sycl::access::mode AcM, typename Arg> static inline auto getAccessor(cl::sycl::handler& cgh, Arg eval) + -> decltype(utility::tuple::make_tuple( eval.device().template get_sycl_accessor<AcM, typename Eigen::internal::remove_all<typename Arg::CoeffReturnType>::type>(eval.dimensions().TotalSize(), cgh,eval.data()))){ - return utility::tuple::make_tuple(eval.device().template get_sycl_accessor<AcM, MapAllocator, typename Eigen::internal::remove_all<typename Arg::CoeffReturnType>::type>(eval.dimensions().TotalSize(), cgh,eval.data())); + return utility::tuple::make_tuple(eval.device().template get_sycl_accessor<AcM, typename Eigen::internal::remove_all<typename Arg::CoeffReturnType>::type>(eval.dimensions().TotalSize(), cgh,eval.data())); } }; @@ -141,8 +141,8 @@ struct ExtractAccessor<TensorEvaluator<TensorAssignOp<LHSExpr, RHSExpr>, Dev> > template <typename PlainObjectType, int Options_, typename Dev>\ struct ExtractAccessor<TensorEvaluator<CVQual TensorMap<PlainObjectType, Options_>, Dev> > {\ static inline auto getTuple(cl::sycl::handler& cgh,const TensorEvaluator<CVQual TensorMap<PlainObjectType, Options_>, Dev> eval)\ - -> decltype(AccessorConstructor::template getAccessor<ACCType, true>(cgh, eval)){\ - return AccessorConstructor::template getAccessor<ACCType, true>(cgh, eval);\ + -> decltype(AccessorConstructor::template getAccessor<ACCType>(cgh, eval)){\ + return AccessorConstructor::template getAccessor<ACCType>(cgh, eval);\ }\ }; TENSORMAPEXPR(const, cl::sycl::access::mode::read) @@ -153,8 +153,8 @@ TENSORMAPEXPR(, cl::sycl::access::mode::read_write) template <typename Expr, typename Dev> struct ExtractAccessor<TensorEvaluator<const TensorForcedEvalOp<Expr>, Dev> > { static inline auto getTuple(cl::sycl::handler& cgh, const TensorEvaluator<const TensorForcedEvalOp<Expr>, Dev> eval) - -> decltype(AccessorConstructor::template getAccessor<cl::sycl::access::mode::read, false>(cgh, eval)){ - return AccessorConstructor::template getAccessor<cl::sycl::access::mode::read, false>(cgh, eval); + -> decltype(AccessorConstructor::template getAccessor<cl::sycl::access::mode::read>(cgh, eval)){ + return AccessorConstructor::template getAccessor<cl::sycl::access::mode::read>(cgh, eval); } }; @@ -167,8 +167,8 @@ struct ExtractAccessor<TensorEvaluator<TensorForcedEvalOp<Expr>, Dev> > template <typename Expr, typename Dev> struct ExtractAccessor<TensorEvaluator<const TensorEvalToOp<Expr>, Dev> > { static inline auto getTuple(cl::sycl::handler& cgh,const TensorEvaluator<const TensorEvalToOp<Expr>, Dev> eval) - -> decltype(utility::tuple::append(AccessorConstructor::template getAccessor<cl::sycl::access::mode::write, false>(cgh, eval), AccessorConstructor::getTuple(cgh, eval.impl()))){ - return utility::tuple::append(AccessorConstructor::template getAccessor<cl::sycl::access::mode::write, false>(cgh, eval), AccessorConstructor::getTuple(cgh, eval.impl())); + -> decltype(utility::tuple::append(AccessorConstructor::template getAccessor<cl::sycl::access::mode::write>(cgh, eval), AccessorConstructor::getTuple(cgh, eval.impl()))){ + return utility::tuple::append(AccessorConstructor::template getAccessor<cl::sycl::access::mode::write>(cgh, eval), AccessorConstructor::getTuple(cgh, eval.impl())); } }; @@ -181,8 +181,8 @@ struct ExtractAccessor<TensorEvaluator<TensorEvalToOp<Expr>, Dev> > template <typename OP, typename Dim, typename Expr, typename Dev> struct ExtractAccessor<TensorEvaluator<const TensorReductionOp<OP, Dim, Expr>, Dev> > { static inline auto getTuple(cl::sycl::handler& cgh, const TensorEvaluator<const TensorReductionOp<OP, Dim, Expr>, Dev> eval) - -> decltype(AccessorConstructor::template getAccessor<cl::sycl::access::mode::read, false>(cgh, eval)){ - return AccessorConstructor::template getAccessor<cl::sycl::access::mode::read, false>(cgh, eval); + -> decltype(AccessorConstructor::template getAccessor<cl::sycl::access::mode::read>(cgh, eval)){ + return AccessorConstructor::template getAccessor<cl::sycl::access::mode::read>(cgh, eval); } }; diff --git a/unsupported/test/cxx11_tensor_broadcast_sycl.cpp b/unsupported/test/cxx11_tensor_broadcast_sycl.cpp index ecebf7d68..7201bfe37 100644 --- a/unsupported/test/cxx11_tensor_broadcast_sycl.cpp +++ b/unsupported/test/cxx11_tensor_broadcast_sycl.cpp @@ -25,55 +25,50 @@ using Eigen::SyclDevice; using Eigen::Tensor; using Eigen::TensorMap; -// Types used in tests: -using TestTensor = Tensor<float, 3>; -using TestTensorMap = TensorMap<Tensor<float, 3>>; -static void test_broadcast_sycl(){ +static void test_broadcast_sycl(const Eigen::SyclDevice &sycl_device){ - cl::sycl::gpu_selector s; - cl::sycl::queue q(s, [=](cl::sycl::exception_list l) { - for (const auto& e : l) { - try { - std::rethrow_exception(e); - } catch (cl::sycl::exception e) { - std::cout << e.what() << std::endl; - } - } - }); - SyclDevice sycl_device(q); - // BROADCAST test: - array<int, 4> in_range = {{2, 3, 5, 7}}; - array<int, in_range.size()> broadcasts = {{2, 3, 1, 4}}; - array<int, in_range.size()> out_range; // = in_range * broadcasts - for (size_t i = 0; i < out_range.size(); ++i) - out_range[i] = in_range[i] * broadcasts[i]; + // BROADCAST test: + array<int, 4> in_range = {{2, 3, 5, 7}}; + array<int, 4> broadcasts = {{2, 3, 1, 4}}; + array<int, 4> out_range; // = in_range * broadcasts + for (size_t i = 0; i < out_range.size(); ++i) + out_range[i] = in_range[i] * broadcasts[i]; + + Tensor<float, 4> input(in_range); + Tensor<float, 4> out(out_range); - Tensor<float, in_range.size()> input(in_range); - Tensor<float, out_range.size()> output(out_range); + for (size_t i = 0; i < in_range.size(); ++i) + VERIFY_IS_EQUAL(out.dimension(i), out_range[i]); - for (int i = 0; i < input.size(); ++i) - input(i) = static_cast<float>(i); - TensorMap<decltype(input)> gpu_in(input.data(), in_range); - TensorMap<decltype(output)> gpu_out(output.data(), out_range); - gpu_out.device(sycl_device) = gpu_in.broadcast(broadcasts); - sycl_device.deallocate(output.data()); + for (int i = 0; i < input.size(); ++i) + input(i) = static_cast<float>(i); - for (size_t i = 0; i < in_range.size(); ++i) - VERIFY_IS_EQUAL(output.dimension(i), out_range[i]); + float * gpu_in_data = static_cast<float*>(sycl_device.allocate(input.dimensions().TotalSize()*sizeof(float))); + float * gpu_out_data = static_cast<float*>(sycl_device.allocate(out.dimensions().TotalSize()*sizeof(float))); - for (int i = 0; i < 4; ++i) { - for (int j = 0; j < 9; ++j) { - for (int k = 0; k < 5; ++k) { - for (int l = 0; l < 28; ++l) { - VERIFY_IS_APPROX(input(i%2,j%3,k%5,l%7), output(i,j,k,l)); - } - } - } - } - printf("Broadcast Test Passed\n"); + TensorMap<Tensor<float, 4>> gpu_in(gpu_in_data, in_range); + TensorMap<Tensor<float, 4>> gpu_out(gpu_out_data, out_range); + sycl_device.memcpyHostToDevice(gpu_in_data, input.data(),(input.dimensions().TotalSize())*sizeof(float)); + gpu_out.device(sycl_device) = gpu_in.broadcast(broadcasts); + sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.dimensions().TotalSize())*sizeof(float)); + + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 9; ++j) { + for (int k = 0; k < 5; ++k) { + for (int l = 0; l < 28; ++l) { + VERIFY_IS_APPROX(input(i%2,j%3,k%5,l%7), out(i,j,k,l)); + } + } + } + } + printf("Broadcast Test Passed\n"); + sycl_device.deallocate(gpu_in_data); + sycl_device.deallocate(gpu_out_data); } void test_cxx11_tensor_broadcast_sycl() { - CALL_SUBTEST(test_broadcast_sycl()); + cl::sycl::gpu_selector s; + Eigen::SyclDevice sycl_device(s); + CALL_SUBTEST(test_broadcast_sycl(sycl_device)); } diff --git a/unsupported/test/cxx11_tensor_device_sycl.cpp b/unsupported/test/cxx11_tensor_device_sycl.cpp index f54fc8786..7f79753c5 100644 --- a/unsupported/test/cxx11_tensor_device_sycl.cpp +++ b/unsupported/test/cxx11_tensor_device_sycl.cpp @@ -20,20 +20,12 @@ #include "main.h" #include <unsupported/Eigen/CXX11/Tensor> -void test_device_sycl() { - cl::sycl::gpu_selector s; - cl::sycl::queue q(s, [=](cl::sycl::exception_list l) { - for (const auto& e : l) { - try { - std::rethrow_exception(e); - } catch (cl::sycl::exception e) { - std::cout << e.what() << std::endl; - } - } - }); - Eigen::SyclDevice sycl_device(q); - printf("Helo from ComputeCpp: Device Exists\n"); +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<cl::sycl::info::device::name>() <<std::endl;; } void test_cxx11_tensor_device_sycl() { - CALL_SUBTEST(test_device_sycl()); + cl::sycl::gpu_selector s; + Eigen::SyclDevice sycl_device(s); + CALL_SUBTEST(test_device_sycl(sycl_device)); } diff --git a/unsupported/test/cxx11_tensor_forced_eval_sycl.cpp b/unsupported/test/cxx11_tensor_forced_eval_sycl.cpp index 182ec7fa8..5690da723 100644 --- a/unsupported/test/cxx11_tensor_forced_eval_sycl.cpp +++ b/unsupported/test/cxx11_tensor_forced_eval_sycl.cpp @@ -22,18 +22,7 @@ using Eigen::Tensor; -void test_forced_eval_sycl() { - cl::sycl::gpu_selector s; - cl::sycl::queue q(s, [=](cl::sycl::exception_list l) { - for (const auto& e : l) { - try { - std::rethrow_exception(e); - } catch (cl::sycl::exception e) { - std::cout << e.what() << std::endl; - } - } - }); - SyclDevice sycl_device(q); +void test_forced_eval_sycl(const Eigen::SyclDevice &sycl_device) { int sizeDim1 = 100; int sizeDim2 = 200; @@ -43,17 +32,22 @@ void test_forced_eval_sycl() { Eigen::Tensor<float, 3> in2(tensorRange); Eigen::Tensor<float, 3> out(tensorRange); + float * gpu_in1_data = static_cast<float*>(sycl_device.allocate(in1.dimensions().TotalSize()*sizeof(float))); + float * gpu_in2_data = static_cast<float*>(sycl_device.allocate(in2.dimensions().TotalSize()*sizeof(float))); + float * gpu_out_data = static_cast<float*>(sycl_device.allocate(out.dimensions().TotalSize()*sizeof(float))); + in1 = in1.random() + in1.constant(10.0f); in2 = in2.random() + in2.constant(10.0f); - // creating TensorMap from tensor - Eigen::TensorMap<Eigen::Tensor<float, 3>> gpu_in1(in1.data(), tensorRange); - Eigen::TensorMap<Eigen::Tensor<float, 3>> gpu_in2(in2.data(), tensorRange); - Eigen::TensorMap<Eigen::Tensor<float, 3>> gpu_out(out.data(), tensorRange); - + // creating TensorMap from tensor + Eigen::TensorMap<Eigen::Tensor<float, 3>> gpu_in1(gpu_in1_data, tensorRange); + Eigen::TensorMap<Eigen::Tensor<float, 3>> gpu_in2(gpu_in2_data, tensorRange); + Eigen::TensorMap<Eigen::Tensor<float, 3>> gpu_out(gpu_out_data, tensorRange); + sycl_device.memcpyHostToDevice(gpu_in1_data, in1.data(),(in1.dimensions().TotalSize())*sizeof(float)); + sycl_device.memcpyHostToDevice(gpu_in2_data, in2.data(),(in1.dimensions().TotalSize())*sizeof(float)); /// c=(a+b)*b - gpu_out.device(sycl_device) =(gpu_in1 + gpu_in2).eval() * gpu_in2; - sycl_device.deallocate(out.data()); + gpu_out.device(sycl_device) =(gpu_in1 + gpu_in2).eval() * gpu_in2; + sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.dimensions().TotalSize())*sizeof(float)); for (int i = 0; i < sizeDim1; ++i) { for (int j = 0; j < sizeDim2; ++j) { for (int k = 0; k < sizeDim3; ++k) { @@ -62,7 +56,15 @@ void test_forced_eval_sycl() { } } } - printf("(a+b)*b Test Passed\n"); + printf("(a+b)*b Test Passed\n"); + sycl_device.deallocate(gpu_in1_data); + sycl_device.deallocate(gpu_in2_data); + sycl_device.deallocate(gpu_out_data); + } -void test_cxx11_tensor_forced_eval_sycl() { CALL_SUBTEST(test_forced_eval_sycl()); } +void test_cxx11_tensor_forced_eval_sycl() { + cl::sycl::gpu_selector s; + Eigen::SyclDevice sycl_device(s); + CALL_SUBTEST(test_forced_eval_sycl(sycl_device)); +} diff --git a/unsupported/test/cxx11_tensor_reduction_sycl.cpp b/unsupported/test/cxx11_tensor_reduction_sycl.cpp index bd09744a6..a9ef82907 100644 --- a/unsupported/test/cxx11_tensor_reduction_sycl.cpp +++ b/unsupported/test/cxx11_tensor_reduction_sycl.cpp @@ -22,126 +22,117 @@ -static void test_full_reductions_sycl() { - - - cl::sycl::gpu_selector s; - cl::sycl::queue q(s, [=](cl::sycl::exception_list l) { - for (const auto& e : l) { - try { - std::rethrow_exception(e); - } catch (cl::sycl::exception e) { - std::cout << e.what() << std::endl; - } - } - }); - Eigen::SyclDevice sycl_device(q); +static void test_full_reductions_sycl(const Eigen::SyclDevice& sycl_device) { const int num_rows = 452; const int num_cols = 765; array<int, 2> tensorRange = {{num_rows, num_cols}}; Tensor<float, 2> in(tensorRange); + Tensor<float, 0> full_redux; + Tensor<float, 0> full_redux_gpu; + in.setRandom(); - Tensor<float, 0> full_redux; - Tensor<float, 0> full_redux_g; full_redux = in.sum(); - float* out_data = (float*)sycl_device.allocate(sizeof(float)); - TensorMap<Tensor<float, 2> > in_gpu(in.data(), tensorRange); - TensorMap<Tensor<float, 0> > full_redux_gpu(out_data); - full_redux_gpu.device(sycl_device) = in_gpu.sum(); - sycl_device.deallocate(out_data); - // Check that the CPU and GPU reductions return the same result. - VERIFY_IS_APPROX(full_redux_gpu(), full_redux()); -} + float* gpu_in_data = static_cast<float*>(sycl_device.allocate(in.dimensions().TotalSize()*sizeof(float))); + float* gpu_out_data =(float*)sycl_device.allocate(sizeof(float)); + TensorMap<Tensor<float, 2> > in_gpu(gpu_in_data, tensorRange); + TensorMap<Tensor<float, 0> > out_gpu(gpu_out_data); -static void test_first_dim_reductions_sycl() { + sycl_device.memcpyHostToDevice(gpu_in_data, in.data(),(in.dimensions().TotalSize())*sizeof(float)); + out_gpu.device(sycl_device) = in_gpu.sum(); + sycl_device.memcpyDeviceToHost(full_redux_gpu.data(), gpu_out_data, sizeof(float)); + // Check that the CPU and GPU reductions return the same result. + VERIFY_IS_APPROX(full_redux_gpu(), full_redux()); + sycl_device.deallocate(gpu_in_data); + sycl_device.deallocate(gpu_out_data); +} - cl::sycl::gpu_selector s; - cl::sycl::queue q(s, [=](cl::sycl::exception_list l) { - for (const auto& e : l) { - try { - std::rethrow_exception(e); - } catch (cl::sycl::exception e) { - std::cout << e.what() << std::endl; - } - } - }); - Eigen::SyclDevice sycl_device(q); +static void test_first_dim_reductions_sycl(const Eigen::SyclDevice& sycl_device) { int dim_x = 145; int dim_y = 1; int dim_z = 67; array<int, 3> tensorRange = {{dim_x, dim_y, dim_z}}; - - Tensor<float, 3> in(tensorRange); - in.setRandom(); Eigen::array<int, 1> red_axis; red_axis[0] = 0; - Tensor<float, 2> redux = in.sum(red_axis); array<int, 2> reduced_tensorRange = {{dim_y, dim_z}}; - Tensor<float, 2> redux_g(reduced_tensorRange); - TensorMap<Tensor<float, 3> > in_gpu(in.data(), tensorRange); - float* out_data = (float*)sycl_device.allocate(dim_y*dim_z*sizeof(float)); - TensorMap<Tensor<float, 2> > redux_gpu(out_data, dim_y, dim_z ); - redux_gpu.device(sycl_device) = in_gpu.sum(red_axis); - sycl_device.deallocate(out_data); - // Check that the CPU and GPU reductions return the same result. - for(int j=0; j<dim_y; j++ ) - for(int k=0; k<dim_z; k++ ) - VERIFY_IS_APPROX(redux_gpu(j,k), redux(j,k)); -} + Tensor<float, 3> in(tensorRange); + Tensor<float, 2> redux(reduced_tensorRange); + Tensor<float, 2> redux_gpu(reduced_tensorRange); + + in.setRandom(); + redux= in.sum(red_axis); -static void test_last_dim_reductions_sycl() { + float* gpu_in_data = static_cast<float*>(sycl_device.allocate(in.dimensions().TotalSize()*sizeof(float))); + float* gpu_out_data = static_cast<float*>(sycl_device.allocate(redux_gpu.dimensions().TotalSize()*sizeof(float))); + TensorMap<Tensor<float, 3> > in_gpu(gpu_in_data, tensorRange); + TensorMap<Tensor<float, 2> > out_gpu(gpu_out_data, reduced_tensorRange); - cl::sycl::gpu_selector s; - cl::sycl::queue q(s, [=](cl::sycl::exception_list l) { - for (const auto& e : l) { - try { - std::rethrow_exception(e); - } catch (cl::sycl::exception e) { - std::cout << e.what() << std::endl; - } - } - }); - Eigen::SyclDevice sycl_device(q); + sycl_device.memcpyHostToDevice(gpu_in_data, in.data(),(in.dimensions().TotalSize())*sizeof(float)); + out_gpu.device(sycl_device) = in_gpu.sum(red_axis); + sycl_device.memcpyDeviceToHost(redux_gpu.data(), gpu_out_data, redux_gpu.dimensions().TotalSize()*sizeof(float)); + + // Check that the CPU and GPU reductions return the same result. + for(int j=0; j<reduced_tensorRange[0]; j++ ) + for(int k=0; k<reduced_tensorRange[1]; k++ ) + VERIFY_IS_APPROX(redux_gpu(j,k), redux(j,k)); + + sycl_device.deallocate(gpu_in_data); + sycl_device.deallocate(gpu_out_data); +} + +static void test_last_dim_reductions_sycl(const Eigen::SyclDevice &sycl_device) { int dim_x = 567; int dim_y = 1; int dim_z = 47; array<int, 3> tensorRange = {{dim_x, dim_y, dim_z}}; - - Tensor<float, 3> in(tensorRange); - in.setRandom(); Eigen::array<int, 1> red_axis; red_axis[0] = 2; - Tensor<float, 2> redux = in.sum(red_axis); array<int, 2> reduced_tensorRange = {{dim_x, dim_y}}; - Tensor<float, 2> redux_g(reduced_tensorRange); - TensorMap<Tensor<float, 3> > in_gpu(in.data(), tensorRange); - float* out_data = (float*)sycl_device.allocate(dim_x*dim_y*sizeof(float)); - TensorMap<Tensor<float, 2> > redux_gpu(out_data, dim_x, dim_y ); - redux_gpu.device(sycl_device) = in_gpu.sum(red_axis); - sycl_device.deallocate(out_data); + Tensor<float, 3> in(tensorRange); + Tensor<float, 2> redux(reduced_tensorRange); + Tensor<float, 2> redux_gpu(reduced_tensorRange); + + in.setRandom(); + + redux= in.sum(red_axis); + + float* gpu_in_data = static_cast<float*>(sycl_device.allocate(in.dimensions().TotalSize()*sizeof(float))); + float* gpu_out_data = static_cast<float*>(sycl_device.allocate(redux_gpu.dimensions().TotalSize()*sizeof(float))); + + TensorMap<Tensor<float, 3> > in_gpu(gpu_in_data, tensorRange); + TensorMap<Tensor<float, 2> > out_gpu(gpu_out_data, reduced_tensorRange); + + sycl_device.memcpyHostToDevice(gpu_in_data, in.data(),(in.dimensions().TotalSize())*sizeof(float)); + out_gpu.device(sycl_device) = in_gpu.sum(red_axis); + sycl_device.memcpyDeviceToHost(redux_gpu.data(), gpu_out_data, redux_gpu.dimensions().TotalSize()*sizeof(float)); // Check that the CPU and GPU reductions return the same result. - for(int j=0; j<dim_x; j++ ) - for(int k=0; k<dim_y; k++ ) + for(int j=0; j<reduced_tensorRange[0]; j++ ) + for(int k=0; k<reduced_tensorRange[1]; k++ ) VERIFY_IS_APPROX(redux_gpu(j,k), redux(j,k)); + + sycl_device.deallocate(gpu_in_data); + sycl_device.deallocate(gpu_out_data); + } void test_cxx11_tensor_reduction_sycl() { - CALL_SUBTEST((test_full_reductions_sycl())); - CALL_SUBTEST((test_first_dim_reductions_sycl())); - CALL_SUBTEST((test_last_dim_reductions_sycl())); + cl::sycl::gpu_selector s; + Eigen::SyclDevice sycl_device(s); + CALL_SUBTEST((test_full_reductions_sycl(sycl_device))); + CALL_SUBTEST((test_first_dim_reductions_sycl(sycl_device))); + CALL_SUBTEST((test_last_dim_reductions_sycl(sycl_device))); } diff --git a/unsupported/test/cxx11_tensor_sycl.cpp b/unsupported/test/cxx11_tensor_sycl.cpp index 0f66cd8f0..6a9c33422 100644 --- a/unsupported/test/cxx11_tensor_sycl.cpp +++ b/unsupported/test/cxx11_tensor_sycl.cpp @@ -27,42 +27,33 @@ using Eigen::SyclDevice; using Eigen::Tensor; using Eigen::TensorMap; -// Types used in tests: -using TestTensor = Tensor<float, 3>; -using TestTensorMap = TensorMap<Tensor<float, 3>>; - -void test_sycl_cpu() { - cl::sycl::gpu_selector s; - cl::sycl::queue q(s, [=](cl::sycl::exception_list l) { - for (const auto& e : l) { - try { - std::rethrow_exception(e); - } catch (cl::sycl::exception e) { - std::cout << e.what() << std::endl; - } - } - }); - SyclDevice sycl_device(q); +void test_sycl_cpu(const Eigen::SyclDevice &sycl_device) { int sizeDim1 = 100; int sizeDim2 = 100; int sizeDim3 = 100; array<int, 3> tensorRange = {{sizeDim1, sizeDim2, sizeDim3}}; - TestTensor in1(tensorRange); - TestTensor in2(tensorRange); - TestTensor in3(tensorRange); - TestTensor out(tensorRange); - in1 = in1.random(); + Tensor<float, 3> in1(tensorRange); + Tensor<float, 3> in2(tensorRange); + Tensor<float, 3> in3(tensorRange); + Tensor<float, 3> out(tensorRange); + in2 = in2.random(); in3 = in3.random(); - TestTensorMap gpu_in1(in1.data(), tensorRange); - TestTensorMap gpu_in2(in2.data(), tensorRange); - TestTensorMap gpu_in3(in3.data(), tensorRange); - TestTensorMap gpu_out(out.data(), tensorRange); + + float * gpu_in1_data = static_cast<float*>(sycl_device.allocate(in1.dimensions().TotalSize()*sizeof(float))); + float * gpu_in2_data = static_cast<float*>(sycl_device.allocate(in2.dimensions().TotalSize()*sizeof(float))); + float * gpu_in3_data = static_cast<float*>(sycl_device.allocate(in3.dimensions().TotalSize()*sizeof(float))); + float * gpu_out_data = static_cast<float*>(sycl_device.allocate(out.dimensions().TotalSize()*sizeof(float))); + + TensorMap<Tensor<float, 3>> gpu_in1(gpu_in1_data, tensorRange); + TensorMap<Tensor<float, 3>> gpu_in2(gpu_in2_data, tensorRange); + TensorMap<Tensor<float, 3>> gpu_in3(gpu_in3_data, tensorRange); + TensorMap<Tensor<float, 3>> gpu_out(gpu_out_data, tensorRange); /// a=1.2f gpu_in1.device(sycl_device) = gpu_in1.constant(1.2f); - sycl_device.deallocate(in1.data()); + sycl_device.memcpyDeviceToHost(in1.data(), gpu_in1_data ,(in1.dimensions().TotalSize())*sizeof(float)); for (int i = 0; i < sizeDim1; ++i) { for (int j = 0; j < sizeDim2; ++j) { for (int k = 0; k < sizeDim3; ++k) { @@ -74,7 +65,7 @@ void test_sycl_cpu() { /// a=b*1.2f gpu_out.device(sycl_device) = gpu_in1 * 1.2f; - sycl_device.deallocate(out.data()); + sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data ,(out.dimensions().TotalSize())*sizeof(float)); for (int i = 0; i < sizeDim1; ++i) { for (int j = 0; j < sizeDim2; ++j) { for (int k = 0; k < sizeDim3; ++k) { @@ -86,8 +77,9 @@ void test_sycl_cpu() { printf("a=b*1.2f Test Passed\n"); /// c=a*b + sycl_device.memcpyHostToDevice(gpu_in2_data, in2.data(),(in2.dimensions().TotalSize())*sizeof(float)); gpu_out.device(sycl_device) = gpu_in1 * gpu_in2; - sycl_device.deallocate(out.data()); + sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.dimensions().TotalSize())*sizeof(float)); for (int i = 0; i < sizeDim1; ++i) { for (int j = 0; j < sizeDim2; ++j) { for (int k = 0; k < sizeDim3; ++k) { @@ -101,7 +93,7 @@ void test_sycl_cpu() { /// c=a+b gpu_out.device(sycl_device) = gpu_in1 + gpu_in2; - sycl_device.deallocate(out.data()); + sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.dimensions().TotalSize())*sizeof(float)); for (int i = 0; i < sizeDim1; ++i) { for (int j = 0; j < sizeDim2; ++j) { for (int k = 0; k < sizeDim3; ++k) { @@ -115,7 +107,7 @@ void test_sycl_cpu() { /// c=a*a gpu_out.device(sycl_device) = gpu_in1 * gpu_in1; - sycl_device.deallocate(out.data()); + sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.dimensions().TotalSize())*sizeof(float)); for (int i = 0; i < sizeDim1; ++i) { for (int j = 0; j < sizeDim2; ++j) { for (int k = 0; k < sizeDim3; ++k) { @@ -125,12 +117,11 @@ void test_sycl_cpu() { } } } - printf("c= a*a Test Passed\n"); //a*3.14f + b*2.7f gpu_out.device(sycl_device) = gpu_in1 * gpu_in1.constant(3.14f) + gpu_in2 * gpu_in2.constant(2.7f); - sycl_device.deallocate(out.data()); + sycl_device.memcpyDeviceToHost(out.data(),gpu_out_data,(out.dimensions().TotalSize())*sizeof(float)); for (int i = 0; i < sizeDim1; ++i) { for (int j = 0; j < sizeDim2; ++j) { for (int k = 0; k < sizeDim3; ++k) { @@ -143,8 +134,9 @@ void test_sycl_cpu() { printf("a*3.14f + b*2.7f Test Passed\n"); ///d= (a>0.5? b:c) + sycl_device.memcpyHostToDevice(gpu_in3_data, in3.data(),(in3.dimensions().TotalSize())*sizeof(float)); gpu_out.device(sycl_device) =(gpu_in1 > gpu_in1.constant(0.5f)).select(gpu_in2, gpu_in3); - sycl_device.deallocate(out.data()); + sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.dimensions().TotalSize())*sizeof(float)); for (int i = 0; i < sizeDim1; ++i) { for (int j = 0; j < sizeDim2; ++j) { for (int k = 0; k < sizeDim3; ++k) { @@ -155,8 +147,13 @@ void test_sycl_cpu() { } } printf("d= (a>0.5? b:c) Test Passed\n"); - + sycl_device.deallocate(gpu_in1_data); + sycl_device.deallocate(gpu_in2_data); + sycl_device.deallocate(gpu_in3_data); + sycl_device.deallocate(gpu_out_data); } void test_cxx11_tensor_sycl() { - CALL_SUBTEST(test_sycl_cpu()); + cl::sycl::gpu_selector s; + Eigen::SyclDevice sycl_device(s); + CALL_SUBTEST(test_sycl_cpu(sycl_device)); } |