diff --git a/include/oneapi/dpl/pstl/omp/util.h b/include/oneapi/dpl/pstl/omp/util.h index e7c4e3cbc40..b5fb399e2b5 100644 --- a/include/oneapi/dpl/pstl/omp/util.h +++ b/include/oneapi/dpl/pstl/omp/util.h @@ -58,33 +58,7 @@ __cancel_execution(oneapi::dpl::__internal::__omp_backend_tag) //------------------------------------------------------------------------ template -class __buffer_impl -{ - std::allocator<_Tp> __allocator_; - _Tp* __ptr_; - const std::size_t __buf_size_; - __buffer_impl(const __buffer_impl&) = delete; - void - operator=(const __buffer_impl&) = delete; - - public: - __buffer_impl(_ExecutionPolicy /*__exec*/, std::size_t __n) - : __allocator_(), __ptr_(__allocator_.allocate(__n)), __buf_size_(__n) - { - } - - operator bool() const { return __ptr_ != nullptr; } - - _Tp* - get() const - { - return __ptr_; - } - ~__buffer_impl() { __allocator_.deallocate(__ptr_, __buf_size_); } -}; - -template -using __buffer = __buffer_impl<::std::decay_t<_ExecutionPolicy>, _Tp>; +using __buffer = oneapi::dpl::__utils::__buffer_impl, _Tp, std::allocator>; // Preliminary size of each chunk: requires further discussion constexpr std::size_t __default_chunk_size = 2048; diff --git a/include/oneapi/dpl/pstl/parallel_backend_serial.h b/include/oneapi/dpl/pstl/parallel_backend_serial.h index edd6652d359..6acd4b617f9 100644 --- a/include/oneapi/dpl/pstl/parallel_backend_serial.h +++ b/include/oneapi/dpl/pstl/parallel_backend_serial.h @@ -25,6 +25,8 @@ #include #include +#include "parallel_backend_utils.h" + namespace oneapi { namespace dpl @@ -33,32 +35,7 @@ namespace __serial_backend { template -class __buffer_impl -{ - ::std::allocator<_Tp> __allocator_; - _Tp* __ptr_; - const ::std::size_t __buf_size_; - __buffer_impl(const __buffer_impl&) = delete; - void - operator=(const __buffer_impl&) = delete; - - public: - __buffer_impl(_ExecutionPolicy /*__exec*/, ::std::size_t __n) - : __allocator_(), __ptr_(__allocator_.allocate(__n)), __buf_size_(__n) - { - } - - operator bool() const { return __ptr_ != nullptr; } - _Tp* - get() const - { - return __ptr_; - } - ~__buffer_impl() { __allocator_.deallocate(__ptr_, __buf_size_); } -}; - -template -using __buffer = __buffer_impl<::std::decay_t<_ExecutionPolicy>, _Tp>; +using __buffer = oneapi::dpl::__utils::__buffer_impl, _Tp, std::allocator>; inline void __cancel_execution(oneapi::dpl::__internal::__serial_backend_tag) diff --git a/include/oneapi/dpl/pstl/parallel_backend_tbb.h b/include/oneapi/dpl/pstl/parallel_backend_tbb.h index 2ddfa61007a..e7b1fa2d646 100644 --- a/include/oneapi/dpl/pstl/parallel_backend_tbb.h +++ b/include/oneapi/dpl/pstl/parallel_backend_tbb.h @@ -55,35 +55,7 @@ not an initialize array, because initialization/destruction would make the span be at least O(N). */ // tbb::allocator can improve performance in some cases. template -class __buffer_impl -{ - tbb::tbb_allocator<_Tp> _M_allocator; - _Tp* _M_ptr; - const ::std::size_t _M_buf_size; - __buffer_impl(const __buffer_impl&) = delete; - void - operator=(const __buffer_impl&) = delete; - - public: - //! Try to obtain buffer of given size to store objects of _Tp type - __buffer_impl(_ExecutionPolicy /*__exec*/, const ::std::size_t __n) - : _M_allocator(), _M_ptr(_M_allocator.allocate(__n)), _M_buf_size(__n) - { - } - //! True if buffer was successfully obtained, zero otherwise. - operator bool() const { return _M_ptr != nullptr; } - //! Return pointer to buffer, or nullptr if buffer could not be obtained. - _Tp* - get() const - { - return _M_ptr; - } - //! Destroy buffer - ~__buffer_impl() { _M_allocator.deallocate(_M_ptr, _M_buf_size); } -}; - -template -using __buffer = __buffer_impl<::std::decay_t<_ExecutionPolicy>, _Tp>; +using __buffer = oneapi::dpl::__utils::__buffer_impl, _Tp, tbb::tbb_allocator>; // Wrapper for tbb::task inline void diff --git a/include/oneapi/dpl/pstl/parallel_backend_utils.h b/include/oneapi/dpl/pstl/parallel_backend_utils.h index c539d0f7e4e..502c5b92bdd 100644 --- a/include/oneapi/dpl/pstl/parallel_backend_utils.h +++ b/include/oneapi/dpl/pstl/parallel_backend_utils.h @@ -25,10 +25,42 @@ namespace oneapi { namespace dpl { - namespace __utils { +//------------------------------------------------------------------------ +// raw buffer (with specified _TAllocator) +//------------------------------------------------------------------------ + +template typename _TAllocator> +class __buffer_impl +{ + _TAllocator<_Tp> _M_allocator; + _Tp* _M_ptr = nullptr; + const ::std::size_t _M_buf_size = 0; + + __buffer_impl(const __buffer_impl&) = delete; + void + operator=(const __buffer_impl&) = delete; + + public: + //! Try to obtain buffer of given size to store objects of _Tp type + __buffer_impl(_ExecutionPolicy /*__exec*/, const ::std::size_t __n) + : _M_allocator(), _M_ptr(_M_allocator.allocate(__n)), _M_buf_size(__n) + { + } + //! True if buffer was successfully obtained, zero otherwise. + operator bool() const { return _M_ptr != nullptr; } + //! Return pointer to buffer, or nullptr if buffer could not be obtained. + _Tp* + get() const + { + return _M_ptr; + } + //! Destroy buffer + ~__buffer_impl() { _M_allocator.deallocate(_M_ptr, _M_buf_size); } +}; + //! Destroy sequence [xs,xe) struct __serial_destroy {