okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- vector.h ---------------------------------*- C++ -*----------------==// 2// 3// Copyright (C) Intel Corporation 4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 5// See https://llvm.org/LICENSE.txt for license information. 6// 7//===----------------------------------------------------------------------===// 8 9#ifndef __DPCT_VECTOR_H__ 10#define __DPCT_VECTOR_H__ 11 12#include <oneapi/dpl/algorithm> 13#include <oneapi/dpl/execution> 14#include <oneapi/dpl/memory> 15 16#include "memory.h" 17 18#include <algorithm> 19#include <iterator> 20#include <vector> 21 22namespace dpct { 23 24namespace internal { 25template < typename Iter ,typename Void = void > // for non-iterators 26struct is_iterator :std ::false_type {}; 27 28template < typename Iter > // For iterators 29struct is_iterator < 30Iter , 31typename std ::enable_if < 32 !std ::is_void < typename Iter ::iterator_category > ::value ,void > ::type > 33 :std ::true_type {}; 34 35template < typename T > // For pointers 36struct is_iterator < T *> :std ::true_type {}; 37}// end namespace internal 38 39#ifndef DPCT_USM_LEVEL_NONE 40 41// device_allocator_traits is a helper struct which makes use of custom 42// allocator constructor routines when they are specified serially on the host, 43// while enabling oneDPL pstl accelleration when such custom constructors are 44// not provided. 45template < typename _Allocator > struct device_allocator_traits { 46 47// taken from libc++ 48template < class ,class _Alloc ,class ..._Args > 49struct __has_construct_impl : ::std ::false_type {}; 50 51template < class _Alloc ,class ..._Args > 52struct __has_construct_impl < decltype ((void )std ::declval < _Alloc > (). construct ( 53std::declval < _Args > ()...)), 54_Alloc, _Args... > : ::std::true_type {}; 55 56template < class _Alloc, class... _Args > 57struct __has_construct : __has_construct_impl < void, _Alloc, _Args... > {}; 58 59template < class _Alloc, class _Pointer, class = void > 60struct __has_destroy : :: std ::false_type {}; 61 62template < class _Alloc, class _Pointer > 63struct __has_destroy < _Alloc, _Pointer, 64decltype (( void )std::declval < _Alloc > (). destroy ( 65std::declval < _Pointer > ())) > : ::std::true_type {}; 66// end of taken from libc++ 67 68template < typename T , typename Size > 69static void uninitialized_value_construct_n ( _Allocator alloc, T * p, Size n) { 70assert (p != nullptr && "value constructing null data" ); 71if constexpr (__has_construct < _Allocator, T *> ::value) { 72for ( Size i = 0 ; i < n; i ++ ) { 73:: std ::allocator_traits < _Allocator > :: construct (alloc, p + i); 74} 75} else { 76:: std :: uninitialized_value_construct_n ( 77oneapi :: dpl :: execution :: make_device_policy ( 78:: dpct :: cs :: get_default_queue ()), 79p, n); 80} 81} 82 83template < typename T , typename Size, typename Value > 84static void uninitialized_fill_n ( _Allocator alloc, T * first, Size n, 85const Value & value) { 86assert (first != nullptr && "filling null data" ); 87if constexpr (__has_construct < _Allocator, T * , const Value &> ::value) { 88for ( Size i = 0 ; i < n; i ++ ) { 89:: std ::allocator_traits < _Allocator > :: construct (alloc, first + i, value); 90} 91} else { 92:: std :: uninitialized_fill_n ( oneapi :: dpl :: execution :: make_device_policy ( 93:: dpct :: cs :: get_default_queue ()), 94first, n, value); 95} 96} 97 98template < typename Iter1, typename Size, typename T > 99static void __uninitialized_custom_copy_n ( _Allocator alloc, Iter1 first, 100Size n, T * d_first) { 101for ( Size i = 0 ; i < n; i ++ ) { 102:: std ::allocator_traits < _Allocator > :: construct (alloc, d_first + i, 103* (first + i)); 104} 105} 106 107template < typename Iter1, typename Size, typename T > 108static void uninitialized_device_copy_n ( _Allocator alloc, Iter1 first, Size n, 109T * d_first) { 110assert (d_first != nullptr && "copying into null data" ); 111if constexpr (__has_construct < _Allocator, T * , 112typename ::std::iterator_traits < 113Iter1 > ::value_type > ::value) { 114__uninitialized_custom_copy_n (alloc, first, n, d_first); 115} else { 116:: std :: uninitialized_copy_n ( oneapi :: dpl :: execution :: make_device_policy ( 117:: dpct :: cs :: get_default_queue ()), 118first, n, d_first); 119} 120} 121 122template < typename Iter1, typename Size, typename T > 123static void uninitialized_host_copy_n ( _Allocator alloc, Iter1 first, Size n, 124T * d_first) { 125assert (d_first != nullptr && "copying into null data" ); 126if constexpr (__has_construct < _Allocator, T * , 127typename ::std::iterator_traits < 128Iter1 > ::value_type > ::value) { 129__uninitialized_custom_copy_n (alloc, first, n, d_first); 130} else { 131:: std :: uninitialized_copy_n (first, n, d_first); 132} 133} 134 135template < typename T , typename Size > 136static void destroy_n ( _Allocator alloc, T * p, Size n) { 137assert (p != nullptr && "destroying null data" ); 138if constexpr (__has_destroy < _Allocator, T *> ::value) { 139for ( Size i = 0 ; i < n; i ++ ) { 140:: std ::allocator_traits < _Allocator > :: destroy (alloc, p + i); 141} 142} else { 143:: std :: destroy_n ( oneapi :: dpl :: execution :: make_device_policy ( 144:: dpct :: cs :: get_default_queue ()), 145p, n); 146} 147} 148}; 149 150template < typename T , 151typename Allocator = sycl::usm_allocator < T , sycl:: usm :: alloc ::shared>> 152class device_vector { 153public : 154using iterator = device_iterator < T > ; 155using const_iterator = const iterator; 156using reference = device_reference < T > ; 157using const_reference = const reference; 158using value_type = T ; 159using pointer = device_pointer < T > ; 160using const_pointer = device_pointer < const T > ; 161using difference_type = 162typename ::std::iterator_traits < iterator > ::difference_type; 163using size_type = ::std:: size_t ; 164using allocator_type = Allocator; 165using alloc_traits = ::std::allocator_traits < Allocator > ; 166 167private : 168Allocator _alloc; 169size_type _size; 170size_type _capacity; 171T * _storage; 172 173size_type _min_capacity () const { return size_type ( 1 ); } 174 175void _set_capacity_and_alloc () { 176_capacity = (:: std :: max )(_size * 2 , _min_capacity ()); 177_storage = alloc_traits:: allocate (_alloc, _capacity); 178} 179 180void _construct ( size_type n, size_type start_idx = 0 ) { 181if (n > 0 ) { 182device_allocator_traits < Allocator > :: uninitialized_value_construct_n ( 183_alloc, _storage + start_idx, n); 184} 185} 186 187void _construct ( size_type n, const T & value, size_type start_idx = 0 ) { 188if (n > 0 ) { 189device_allocator_traits < Allocator > :: uninitialized_fill_n ( 190_alloc, _storage + start_idx, n, value); 191} 192} 193 194template < typename Iter > 195void _construct_iter (Iter first, size_type n, size_type start_idx = 0 ) { 196if (n > 0 ) { 197device_allocator_traits < Allocator > :: uninitialized_device_copy_n ( 198_alloc, first, n, _storage + start_idx); 199} 200} 201 202template < typename Iter > 203void _construct_iter_host (Iter first, size_type n, size_type start_idx = 0 ) { 204if (n > 0 ) { 205device_allocator_traits < Allocator > :: uninitialized_host_copy_n ( 206_alloc, first, n, _storage + start_idx); 207} 208} 209 210void _destroy ( size_type n, size_type start_idx = 0 ) { 211if (n > 0 ) { 212device_allocator_traits < Allocator > :: destroy_n (_alloc, 213_storage + start_idx, n); 214} 215} 216 217void _assign_elements ( const device_vector & other) { 218if (other. size () <= _size) { 219// if incoming elements fit within existing elements, copy then destroy 220// excess 221:: std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 222:: dpct :: cs :: get_default_queue ()), 223other. begin (), other. end (), begin ()); 224resize (other. size ()); 225} else if (other. size () < _capacity) { 226// if incoming elements don't fit within existing elements but do fit 227// within total capacity 228// copy elements that fit, then use uninitialized copy to ge the rest 229// and adjust size 230std :: copy_n ( oneapi :: dpl :: execution :: make_device_policy ( 231:: dpct :: cs :: get_default_queue ()), 232other. begin (), _size, begin ()); 233device_allocator_traits < Allocator > :: uninitialized_device_copy_n ( 234_alloc, other. begin () + _size, other. size () - _size, 235_storage + _size); 236_size = other. size (); 237} else { 238// If incoming elements exceed current capacity, destroy all existing 239// elements, then allocate incoming vectors capacity, then use 240// uninitialized copy 241clear (); 242reserve (other. capacity ()); 243device_allocator_traits < Allocator > :: uninitialized_device_copy_n ( 244_alloc, other. begin (), other. size (), _storage); 245_size = other. size (); 246} 247} 248 249public : 250template < typename OtherA > operator :: std ::vector < T , OtherA > () const { 251auto __tmp = ::std::vector < T , OtherA > (this -> size ()); 252:: std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 253:: dpct :: cs :: get_default_queue ()), 254this -> begin (), this -> end (), __tmp. begin ()); 255return __tmp; 256} 257 258device_vector( 259const Allocator & alloc = Allocator (::dpct::cs:: get_default_queue ())) 260: _alloc (alloc), _size ( 0 ), _capacity ( _min_capacity ()) { 261_set_capacity_and_alloc (); 262} 263 264~ device_vector () /*= default*/ { 265clear (); 266alloc_traits :: deallocate (_alloc, _storage, _capacity); 267} 268 269explicit device_vector( size_type n, const Allocator & alloc = Allocator ( 270::dpct::cs:: get_default_queue ())) 271: _alloc (alloc), _size (n) { 272_set_capacity_and_alloc (); 273_construct (n); 274} 275 276explicit device_vector ( 277size_type n, const T & value, 278const Allocator & alloc = Allocator (:: dpct ::cs:: get_default_queue ())) 279: _alloc (alloc), _size ( n ) { 280_set_capacity_and_alloc (); 281_construct (n, value); 282} 283 284device_vector (device_vector && other) 285: _alloc ( std :: move (other. _alloc )), _size (other. size ()), 286_capacity (other. capacity ()), _storage (other. _storage ) { 287other. _size = 0 ; 288other. _capacity = 0 ; 289other. _storage = nullptr ; 290} 291 292device_vector (device_vector && other, const Allocator & alloc) 293: _alloc (alloc), _size (other. size ()), _capacity (other. capacity ()) { 294_storage = alloc_traits:: allocate (_alloc, _capacity); 295_construct_iter (other. begin (), _size); // ok to parallelize 296other. _size = 0 ; 297other. _capacity = 0 ; 298other. _storage = nullptr ; 299} 300 301template < typename InputIterator > 302device_vector ( 303InputIterator first, 304typename ::std::enable_if_t < 305dpct::internal::is_iterator < InputIterator > ::value, InputIterator > 306last, 307const Allocator & alloc = Allocator (::dpct::cs:: get_default_queue ())) 308: _alloc (alloc) { 309_size = :: std :: distance (first, last); 310_set_capacity_and_alloc (); 311// unsafe to parallelize on device as we dont know if InputIterator is 312// valid oneDPL input type 313_construct_iter_host (first, _size); 314} 315 316device_vector( const device_vector & other, const Allocator & alloc) 317: _alloc (alloc) { 318_size = other. size (); 319_capacity = other. capacity (); 320_storage = alloc_traits:: allocate (_alloc, _capacity); 321_construct_iter (other. begin (), _size); 322} 323 324device_vector( const device_vector & other) 325: device_vector ( 326other , 327alloc_traits:: select_on_container_copy_construction ( other ._alloc)) { 328} 329 330template < typename OtherAllocator > 331device_vector ( 332const device_vector < T , OtherAllocator > & other, 333const Allocator & alloc = Allocator (::dpct::cs:: get_default_queue ())) 334: _alloc (alloc) { 335_size = other. size (); 336_capacity = other. capacity (); 337_storage = alloc_traits:: allocate (_alloc, _capacity); 338_construct_iter (other. begin (), _size); 339} 340 341template < typename OtherAllocator > 342device_vector (const ::std::vector < T , OtherAllocator > & v) 343: device_vector (v. begin (), v. end ()) {} 344 345template < typename OtherAllocator > 346device_vector (const ::std::vector < T , OtherAllocator > & v, 347const Allocator & alloc) 348: device_vector (v. begin (), v. end (), alloc) {} 349 350template < typename OtherAllocator > 351device_vector & operator = ( const :: std ::vector < T , OtherAllocator > & v) { 352resize (v. size ()); 353:: std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 354:: dpct :: cs :: get_default_queue ()), 355v. begin (), v. end (), begin ()); 356return * this; 357} 358 359device_vector & operator = ( const device_vector & other) { 360// Copy assignment operator: 361if constexpr (alloc_traits::propagate_on_container_copy_assignment::value) { 362clear (); 363alloc_traits :: deallocate (_alloc, _storage, _capacity); 364_capacity = 0 ; 365_alloc = other. _alloc ; 366} 367_assign_elements (other); 368return * this; 369} 370device_vector & operator = (device_vector && other) { 371// Move assignment operator: 372if constexpr (alloc_traits::propagate_on_container_move_assignment::value) { 373clear (); 374alloc_traits :: deallocate (_alloc, _storage, _capacity); 375_alloc = :: std :: move (other. _alloc ); 376_storage = :: std :: move (other. _storage ); 377_capacity = :: std :: move (other. _capacity ); 378_size = :: std :: move (other. _size ); 379} else { 380_assign_elements (other); 381// destroy and deallocate other vector 382other. clear (); 383alloc_traits :: deallocate (other. _alloc , other. _storage , other. _capacity ); 384} 385other. _size = 0 ; 386other. _capacity = 0 ; 387other. _storage = nullptr ; 388return * this; 389} 390size_type size () const { return _size; } 391iterator begin () noexcept { return device_iterator < T > (_storage, 0 ); } 392iterator end () { return device_iterator < T > (_storage, size ()); } 393const_iterator begin () const noexcept { 394return device_iterator < T > (_storage, 0 ); 395} 396const_iterator cbegin () const noexcept { return begin (); } 397const_iterator end () const { return device_iterator < T > (_storage, size ()); } 398const_iterator cend () const { return end (); } 399T * real_begin () { return _storage; } 400const T * real_begin () const { return _storage; } 401void swap ( device_vector & v) { 402if constexpr (::std::allocator_traits < 403Allocator > ::propagate_on_container_swap::value) { 404:: std :: swap (_alloc, v. _alloc ); 405:: std :: swap (_size, v. _size ); 406:: std :: swap (_capacity, v. _capacity ); 407:: std :: swap (_storage, v. _storage ); 408} else { 409// swap all elements up to the minimum size between the two vectors 410size_type min_size = (::std::min)( size (), v. size ()); 411auto zip = oneapi::dpl:: make_zip_iterator ( begin (), v . begin ()); 412:: std :: for_each ( oneapi :: dpl :: execution :: make_device_policy ( 413:: dpct :: cs :: get_default_queue ()), 414zip, zip + min_size, [](auto zip_ele) { 415std :: swap (:: std ::get < 0 > (zip_ele), 416:: std ::get < 1 > (zip_ele)); 417}); 418// then copy the elements beyond the end of the smaller list, and resize 419if ( size () > v. size ()) { 420v. reserve ( capacity ()); 421device_allocator_traits < Allocator > :: uninitialized_device_copy_n ( 422_alloc, begin () + min_size, size () - min_size, 423v. _storage + min_size); 424v. _size = size (); 425_destroy ( size () - min_size, min_size); 426_size = min_size; 427} else if ( size () < v. size ()) { 428reserve (v. capacity ()); 429device_allocator_traits < Allocator > :: uninitialized_device_copy_n ( 430_alloc, v. begin () + min_size, v. size () - min_size, 431_storage + min_size); 432_size = v. size (); 433v. _destroy (v. size () - min_size, min_size); 434v. _size = min_size; 435} 436} 437} 438reference operator[]( size_type n) { return _storage[n]; } 439const_reference operator[]( size_type n) const { return _storage[n]; } 440void reserve ( size_type n) { 441if (n > capacity ()) { 442// allocate buffer for new size 443auto tmp = alloc_traits:: allocate ( _alloc , n ); 444// copy content (old buffer to new buffer) 445if ( capacity () > 0 ) { 446device_allocator_traits < Allocator > :: uninitialized_device_copy_n ( 447_alloc, begin (), _size, tmp); 448alloc_traits :: deallocate (_alloc, _storage, _capacity); 449} 450_storage = tmp; 451_capacity = n; 452} 453} 454void resize( size_type new_size, const T & x = T ()) { 455reserve (new_size); 456if (new_size > size ()) { 457_construct (new_size - size (), x, size ()); 458} else { 459_destroy (_size - new_size, new_size); 460} 461_size = new_size; 462} 463size_type max_size ( void ) const { 464return ::std::numeric_limits < size_type > :: max () / sizeof ( T ); 465} 466size_type capacity () const { return _capacity; } 467const_reference front () const { return * begin (); } 468reference front () { return * begin (); } 469const_reference back ( void ) const { return * ( end () - 1 ); } 470reference back ( void ) { return * ( end () - 1 ); } 471pointer data ( void ) { return pointer (_storage); } 472const_pointer data ( void ) const { return const_pointer (_storage); } 473void shrink_to_fit ( void ) { 474if (_size != capacity () && capacity () > _min_capacity ()) { 475size_type tmp_capacity = (::std::max)(_size, _min_capacity ()); 476auto tmp = alloc_traits:: allocate ( _alloc , tmp_capacity ); 477if (_size > 0 ) { 478:: std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 479:: dpct :: cs :: get_default_queue ()), 480begin (), end (), tmp); 481} 482alloc_traits :: deallocate (_alloc, _storage, _capacity); 483_storage = tmp; 484_capacity = tmp_capacity; 485} 486} 487void assign ( size_type n, const T & x) { 488resize (n); 489if (_size > 0 ) { 490:: std :: fill ( oneapi :: dpl :: execution :: make_device_policy ( 491:: dpct :: cs :: get_default_queue ()), 492begin (), begin () + n, x); 493} 494} 495template < typename InputIterator > 496void 497assign (InputIterator first, 498typename ::std::enable_if < internal::is_iterator < InputIterator > ::value, 499InputIterator > ::type last) { 500auto n = ::std:: distance ( first , last ); 501resize (n); 502if (_size > 0 ) { 503// unsafe to call on device as we don't know the InputIterator type 504:: std :: copy (first, last, begin ()); 505} 506} 507void clear ( void ) { 508_destroy (_size); 509_size = 0 ; 510} 511bool empty ( void ) const { return ( size () == 0 ); } 512void push_back ( const T & x) { insert ( end (), size_type ( 1 ), x); } 513void pop_back ( void ) { 514if (_size > 0 ) { 515_destroy ( 1 , _size - 1 ); 516-- _size; 517} 518} 519iterator erase ( iterator first, iterator last) { 520auto n = ::std:: distance ( first , last ); 521if (last == end ()) { 522_destroy (n, _size - n); 523_size = _size - n; 524return end (); 525} 526auto m = ::std:: distance ( last , end ()); 527if (m <= 0 ) { 528return end (); 529} else if (n >= m) { 530:: std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 531:: dpct :: cs :: get_default_queue ()), 532last, last + m, first); 533} else { 534auto tmp = alloc_traits:: allocate ( _alloc , m ); 535 536device_allocator_traits < Allocator > :: uninitialized_device_copy_n ( 537_alloc, last, m, tmp); 538 539std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 540:: dpct :: cs :: get_default_queue ()), 541tmp, tmp + m, first); 542device_allocator_traits < Allocator > :: destroy_n (_alloc, tmp, m); 543alloc_traits :: deallocate (_alloc, tmp, m); 544} 545// now destroy the remaining elements 546_destroy (n, size () - n); 547_size -= n; 548return begin () + first. get_idx () + n; 549} 550iterator erase ( iterator pos) { return erase (pos, pos + 1 ); } 551iterator insert ( iterator position, const T & x) { 552auto n = ::std:: distance ( begin (), position ); 553insert (position, size_type ( 1 ), x); 554return begin () + n; 555} 556void insert ( iterator position, size_type n, const T & x) { 557if (position == end ()) { 558reserve ( size () + n); 559device_allocator_traits < Allocator > :: uninitialized_fill_n ( 560_alloc, _storage + size (), n, x); 561_size += n; 562} else { 563auto i_n = ::std:: distance ( begin (), position ); 564// allocate temporary storage 565auto m = ::std:: distance ( position , end ()); 566// will throw if position is not inside active vector 567auto tmp = alloc_traits:: allocate ( _alloc , m ); 568// copy remainder 569device_allocator_traits < Allocator > :: uninitialized_device_copy_n ( 570_alloc, position, m, tmp); 571resize ( size () + n); 572// resizing might invalidate position 573position = begin () + position. get_idx (); 574:: std :: fill ( oneapi :: dpl :: execution :: make_device_policy ( 575:: dpct :: cs :: get_default_queue ()), 576position, position + n, x); 577:: std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 578:: dpct :: cs :: get_default_queue ()), 579tmp, tmp + m, position + n); 580device_allocator_traits < Allocator > :: destroy_n (_alloc, tmp, m); 581alloc_traits :: deallocate (_alloc, tmp, m); 582} 583} 584template < typename InputIterator > 585void 586insert (iterator position, InputIterator first, 587typename ::std::enable_if < internal::is_iterator < InputIterator > ::value, 588InputIterator > ::type last) { 589auto n = ::std:: distance ( first , last ); 590if (position == end ()) { 591reserve ( size () + n); 592// unsafe to call on device as we dont know the InputIterator type 593:: std :: uninitialized_copy (first, last, end ()); 594_size += n; 595} else { 596auto m = ::std:: distance ( position , end ()); 597// will throw if position is not inside active vector 598auto tmp = alloc_traits:: allocate ( _alloc , m ); 599 600device_allocator_traits < Allocator > :: uninitialized_device_copy_n ( 601_alloc, position, m, tmp); 602 603resize ( size () + n); 604// resizing might invalidate position 605position = begin () + position. get_idx (); 606// unsafe to call on device as we dont know the InputIterator type 607:: std :: copy (first, last, position); 608:: std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 609:: dpct :: cs :: get_default_queue ()), 610tmp, tmp + m, position + n); 611device_allocator_traits < Allocator > :: destroy_n (_alloc, tmp, m); 612alloc_traits :: deallocate (_alloc, tmp, m); 613} 614} 615Allocator get_allocator () const { return _alloc; } 616}; 617 618#else 619 620template < typename T , typename Allocator = detail::__buffer_allocator < T >> 621class device_vector { 622static_assert ( 623std ::is_same < Allocator, detail::__buffer_allocator < T >>::value, 624"device_vector doesn't support custom allocator when USM is not used." ); 625 626public : 627using iterator = device_iterator < T > ; 628using const_iterator = const iterator; 629using reference = device_reference < T > ; 630using const_reference = const reference; 631using value_type = T ; 632using pointer = device_pointer < T > ; 633using const_pointer = device_pointer < const T > ; 634using difference_type = 635typename std ::iterator_traits < iterator > ::difference_type; 636using size_type = std:: size_t ; 637 638private : 639using Buffer = sycl::buffer < T , 1 > ; 640using Range = sycl::range < 1 > ; 641// Using mem_mgr to handle memory allocation 642void * _storage; 643size_type _size; 644 645size_type _min_capacity () const { return size_type ( 1 ); } 646 647void * alloc_store ( size_type num_bytes) { 648return detail::mem_mgr:: instance (). mem_alloc (num_bytes); 649} 650 651public : 652template < typename OtherA > operator std ::vector < T , OtherA > () const { 653auto __tmp = std::vector < T , OtherA > (this -> size ()); 654std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, this -> begin (), this -> end (), 655__tmp. begin ()); 656return __tmp; 657} 658device_vector () 659: _storage ( alloc_store ( _min_capacity () * sizeof ( T ))), _size ( 0 ) {} 660~ device_vector () = default; 661explicit device_vector ( size_type n) : device_vector (n, T ()) {} 662explicit device_vector ( size_type n, const T & value) 663: _storage ( alloc_store ((std::max)(n, _min_capacity ()) * sizeof ( T ))), 664_size ( n ) { 665auto buf = get_buffer (); 666std :: fill ( oneapi :: dpl :: execution ::dpcpp_default, oneapi:: dpl :: begin (buf), 667oneapi:: dpl :: begin (buf) + n, T (value)); 668} 669device_vector( const device_vector & other) 670: _storage ( other ._storage), _size ( other . size ()) {} 671device_vector (device_vector && other) 672: _storage ( std :: move (other. _storage )), _size (other. size ()) {} 673 674template < typename InputIterator > 675device_vector (InputIterator first, 676typename std::enable_if < 677internal::is_iterator < InputIterator > ::value && 678!std::is_pointer < InputIterator > ::value && 679std::is_same < typename std::iterator_traits < 680InputIterator > ::iterator_category, 681std::random_access_iterator_tag > ::value, 682InputIterator > ::type last) 683: _storage(alloc_store(std::distance(first, last) * sizeof ( T ))), 684_size(std::distance(first, last)) { 685auto buf = get_buffer(); 686auto dst = oneapi::dpl:: begin ( buf ); 687std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 688:: dpct :: cs :: get_default_queue ()), 689first, last, dst); 690} 691 692template < typename InputIterator > 693device_vector (InputIterator first, 694typename std::enable_if < std::is_pointer < InputIterator > ::value, 695InputIterator > ::type last) 696: _storage(alloc_store( std :: distance ( first , last ) * sizeof ( T ))), 697_size (std:: distance (first, last)) { 698auto buf = get_buffer (); 699Buffer tmp_buf ( first , last ); 700auto start = oneapi::dpl:: begin ( tmp_buf ); 701auto end = oneapi::dpl:: end ( tmp_buf ); 702auto dst = oneapi::dpl:: begin ( buf ); 703std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 704:: dpct :: cs :: get_default_queue ()), 705start, end, dst); 706} 707 708template < typename InputIterator > 709device_vector (InputIterator first, 710typename std::enable_if < 711internal::is_iterator < InputIterator > ::value && 712!std::is_same < typename std::iterator_traits < 713InputIterator > ::iterator_category, 714std::random_access_iterator_tag > ::value, 715InputIterator > ::type last) 716: _storage(alloc_store( std :: distance ( first , last ) * sizeof ( T ))), 717_size (std:: distance (first, last)) { 718auto buf = get_buffer (); 719std ::vector < T > tmp (first, last); 720Buffer tmp_buf ( tmp ); 721auto start = oneapi::dpl:: begin ( tmp_buf ); 722auto end = oneapi::dpl:: end ( tmp_buf ); 723auto dst = oneapi::dpl:: begin ( buf ); 724std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 725:: dpct :: cs :: get_default_queue ()), 726start, end, dst); 727} 728 729template < typename OtherAllocator > 730device_vector (const device_vector < T , OtherAllocator > & v) 731: _storage ( alloc_store (v. size () * sizeof ( T ))), _size (v. size ()) { 732auto buf = get_buffer (); 733auto dst = oneapi::dpl:: begin ( buf ); 734std :: copy ( oneapi :: dpl :: execution :: make_device_policy ( 735:: dpct :: cs :: get_default_queue ()), 736v. real_begin (), v. real_begin () + v. size (), dst); 737} 738 739template < typename OtherAllocator > 740device_vector (std::vector < T , OtherAllocator > & v) 741: _storage ( alloc_store (v. size () * sizeof ( T ))), _size (v. size ()) { 742std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, v. begin (), v. end (), 743oneapi:: dpl :: begin ( get_buffer ())); 744} 745 746device_vector & operator = ( const device_vector & other) { 747// Copy assignment operator: 748_size = other. size (); 749void * tmp = alloc_store (_size * sizeof ( T )); 750auto tmp_buf = 751detail::mem_mgr:: instance () 752.translate_ptr( tmp ) 753.buffer.template reinterpret < T , 1 > (sycl::range < 1 > (_size)); 754std :: copy ( oneapi ::dpl::execution::dpcpp_default, 755oneapi ::dpl:: begin ( other . get_buffer ()), 756oneapi ::dpl:: end ( other . get_buffer ()), 757oneapi ::dpl:: begin ( tmp_buf )); 758detail ::mem_mgr:: instance (). mem_free (_storage); 759_storage = tmp; 760return * this; 761} 762device_vector & operator = (device_vector && other) { 763// Move assignment operator: 764_size = other. size (); 765this -> _storage = std:: move (other. _storage ); 766return * this; 767} 768template < typename OtherAllocator > 769device_vector & operator = ( const std ::vector < T , OtherAllocator > & v) { 770Buffer data ( v . begin (), v . end ()); 771_size = v. size (); 772void * tmp = alloc_store (_size * sizeof ( T )); 773auto tmp_buf = 774detail::mem_mgr:: instance () 775.translate_ptr( tmp ) 776.buffer.template reinterpret < T , 1 > (sycl::range < 1 > (_size)); 777std :: copy ( oneapi ::dpl::execution::dpcpp_default, oneapi ::dpl:: begin ( data ), 778oneapi ::dpl:: end ( data ), oneapi ::dpl:: begin ( tmp_buf )); 779detail ::mem_mgr:: instance (). mem_free (_storage); 780_storage = tmp; 781 782return * this; 783} 784Buffer get_buffer () const { 785return detail::mem_mgr:: instance () 786. translate_ptr (_storage) 787. buffer . template reinterpret < T , 1 > (sycl::range < 1 > ( capacity ())); 788} 789size_type size () const { return _size; } 790iterator begin () noexcept { return device_iterator < T > ( get_buffer (), 0 ); } 791iterator end () { return device_iterator < T > ( get_buffer (), _size); } 792const_iterator begin () const noexcept { 793return device_iterator < T > ( get_buffer (), 0 ); 794} 795const_iterator cbegin () const noexcept { return begin (); } 796const_iterator end () const { return device_iterator < T > ( get_buffer (), _size); } 797const_iterator cend () const { return end (); } 798T * real_begin () { 799// This code returns a pointer to a data within sycl buffer accessor which 800// is leaving scope. This relies on undefined 801// behavior and may not provide a valid pointer to data inside that buffer. 802return (detail::mem_mgr:: instance () 803. translate_ptr (_storage) 804. buffer . get_host_access ()) 805. get_pointer (); 806} 807const T * real_begin () const { 808// This code returns a pointer to a data within sycl buffer accessor which 809// is leaving scope. This relies on undefined 810// behavior and may not provide a valid pointer to data inside that buffer. 811return const_cast < device_vector *> (this) -> real_begin (); 812} 813void swap ( device_vector & v) { 814void * temp = v. _storage ; 815v. _storage = this -> _storage ; 816this -> _storage = temp; 817std :: swap (_size, v. _size ); 818} 819reference operator[]( size_type n) { return * ( begin () + n); } 820const_reference operator[]( size_type n) const { return * ( begin () + n); } 821void reserve ( size_type n) { 822if (n > capacity ()) { 823// create new buffer (allocate for new size) 824void * a = alloc_store (n * sizeof ( T )); 825 826// copy content (old buffer to new buffer) 827if (_storage != nullptr ) { 828auto tmp = detail::mem_mgr:: instance () 829.translate_ptr( a ) 830.buffer.template reinterpret < T , 1 > (sycl::range < 1 > (n)); 831auto src_buf = get_buffer (); 832std :: copy ( oneapi ::dpl::execution::dpcpp_default, 833oneapi ::dpl:: begin ( src_buf ), oneapi ::dpl:: end ( src_buf ), 834oneapi ::dpl:: begin ( tmp )); 835 836// deallocate old memory 837detail ::mem_mgr:: instance (). mem_free (_storage); 838} 839_storage = a; 840} 841} 842void resize ( size_type new_size, const T & x = T ()) { 843reserve (new_size); 844if (_size < new_size) { 845auto src_buf = get_buffer (); 846std :: fill ( oneapi :: dpl :: execution ::dpcpp_default, 847oneapi:: dpl :: begin (src_buf) + _size, 848oneapi:: dpl :: begin (src_buf) + new_size, x); 849} 850_size = new_size; 851} 852size_type max_size ( void ) const { 853return std::numeric_limits < size_type > :: max () / sizeof ( T ); 854} 855size_type capacity () const { 856return _storage != nullptr ? detail::mem_mgr:: instance () 857. translate_ptr (_storage) 858. buffer . size () / 859sizeof ( T ) 860: 0 ; 861} 862const_reference front () const { return * begin (); } 863reference front () { return * begin (); } 864const_reference back ( void ) const { return * ( end () - 1 ); } 865reference back ( void ) { return * ( end () - 1 ); } 866pointer data ( void ) { return pointer (reinterpret_cast < T *> (_storage)); } 867const_pointer data ( void ) const { 868return const_pointer (reinterpret_cast < const T *> (_storage)); 869} 870void shrink_to_fit ( void ) { 871if (_size != capacity ()) { 872void * a = alloc_store (_size * sizeof ( T )); 873auto tmp = detail::mem_mgr:: instance () 874.translate_ptr( a ) 875.buffer.template reinterpret < T , 1 > (sycl::range < 1 > (_size)); 876std :: copy ( oneapi ::dpl::execution::dpcpp_default, 877oneapi ::dpl:: begin ( get_buffer ()), 878oneapi ::dpl:: begin ( get_buffer ()) + _size, 879oneapi ::dpl:: begin ( tmp )); 880detail ::mem_mgr:: instance (). mem_free (_storage); 881_storage = a; 882} 883} 884void assign ( size_type n, const T & x) { 885resize (n); 886std :: fill ( oneapi :: dpl :: execution ::dpcpp_default, begin (), begin () + n, x); 887} 888template < typename InputIterator > 889void 890assign (InputIterator first, 891typename std::enable_if < internal::is_iterator < InputIterator > ::value, 892InputIterator > ::type last) { 893auto n = std:: distance ( first , last ); 894resize (n); 895if (internal::is_iterator < InputIterator > ::value && 896!std::is_pointer < InputIterator > ::value) 897std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, first, last, begin ()); 898else { 899Buffer tmp ( first , last ); 900std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, oneapi:: dpl :: begin (tmp), 901oneapi:: dpl :: end (tmp), begin ()); 902} 903} 904void clear ( void ) { 905_size = 0 ; 906detail :: mem_mgr :: instance (). mem_free (_storage); 907_storage = nullptr ; 908} 909bool empty ( void ) const { return ( size () == 0 ); } 910void push_back ( const T & x) { insert ( end (), size_type ( 1 ), x); } 911void pop_back ( void ) { 912if (_size > 0 ) 913-- _size; 914} 915iterator erase ( iterator first, iterator last) { 916auto n = std:: distance ( first , last ); 917if (last == end ()) { 918_size = _size - n; 919return end (); 920} 921Buffer tmp{Range( std :: distance ( last , end ()))}; 922// copy remainder to temporary buffer. 923std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, last, end (), 924oneapi:: dpl :: begin (tmp)); 925// override (erase) subsequence in storage. 926std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, oneapi:: dpl :: begin (tmp), 927oneapi:: dpl :: end (tmp), first); 928resize (_size - n); 929return begin () + first. get_idx () + n; 930} 931iterator erase ( iterator pos) { return erase (pos, pos + 1 ); } 932iterator insert ( iterator position, const T & x) { 933auto n = std:: distance ( begin (), position ); 934insert (position, size_type ( 1 ), x); 935return begin () + n; 936} 937void insert ( iterator position, size_type n, const T & x) { 938if (position == end ()) { 939resize ( size () + n); 940std :: fill ( oneapi :: dpl :: execution ::dpcpp_default, end () - n, end (), x); 941} else { 942auto i_n = std:: distance ( begin (), position ); 943// allocate temporary storage 944Buffer tmp{Range( std :: distance ( position , end ()))}; 945// copy remainder 946std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, position, end (), 947oneapi:: dpl :: begin (tmp)); 948 949resize ( size () + n); 950// resizing might invalidate position 951position = begin () + position. get_idx (); 952 953std :: fill ( oneapi :: dpl :: execution ::dpcpp_default, position, position + n, 954x); 955 956std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, oneapi:: dpl :: begin (tmp), 957oneapi:: dpl :: end (tmp), position + n); 958} 959} 960template < typename InputIterator > 961void 962insert (iterator position, InputIterator first, 963typename std::enable_if < internal::is_iterator < InputIterator > ::value, 964InputIterator > ::type last) { 965auto n = std:: distance ( first , last ); 966if (position == end ()) { 967resize ( size () + n); 968std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, first, last, end ()); 969} else { 970Buffer tmp{Range( std :: distance ( position , end ()))}; 971 972std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, position, end (), 973oneapi:: dpl :: begin (tmp)); 974 975resize ( size () + n); 976// resizing might invalidate position 977position = begin () + position. get_idx (); 978 979std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, first, last, position); 980std :: copy ( oneapi :: dpl :: execution ::dpcpp_default, oneapi:: dpl :: begin (tmp), 981oneapi:: dpl :: end (tmp), position + n); 982} 983} 984}; 985 986#endif 987 988} // end namespace dpct 989 990#endif