39 #ifndef ECUDA_VECTOR_HPP
40 #define ECUDA_VECTOR_HPP
47 #ifdef ECUDA_CPP11_AVAILABLE
48 #include <initializer_list>
66 template<
typename T,
class Alloc>
class vector_kernel_argument;
95 template<
typename T,
class Alloc=device_allocator<T>,
class P=shared_ptr<T> >
96 class vector :
private model::device_contiguous_sequence< T, P > {
99 typedef model::device_contiguous_sequence< T, P > base_type;
106 #ifdef ECUDA_CPP11_AVAILABLE
107 typedef typename base_type::reference
reference;
109 typedef typename std::allocator_traits<Alloc>::pointer
pointer;
110 typedef typename std::allocator_traits<Alloc>::const_pointer
const_pointer;
130 template<
typename U,
class Alloc2,
class Q>
friend class vector;
145 base_type::get_pointer() = unmanaged_cast(other.get_pointer());
147 allocator = other.allocator;
162 template<
class Iterator>
163 __HOST__ inline void init( Iterator first, Iterator last, ecuda::false_type )
186 init( n, value, ecuda::true_type() );
203 template<
class Iterator>
206 typedef typename ecuda::is_integral<Iterator>::type _Integral;
207 init( first, last, _Integral() );
230 allocator = src.allocator;
235 #ifdef ECUDA_CPP11_AVAILABLE
264 base_type::operator=(std::move(src));
265 n = std::move(src.n);
344 #ifdef ECUDA_CPP11_AVAILABLE
377 if(
size() == newSize )
return;
378 if(
size() > newSize ) { n = newSize;
return; }
379 growMemory( newSize );
380 std::vector< value_type, host_allocator<value_type> > v( newSize-n, value );
422 if( !(index <
size()) ) {
424 throw std::out_of_range(
EXCEPTION_MSG(
"ecuda::vector::at() index parameter is out of range") );
433 return base_type::operator[](index);
447 if( !(index <
size()) ) {
449 throw std::out_of_range(
EXCEPTION_MSG(
"ecuda::vector::at() index parameter is out of range") );
458 return base_type::operator[](index);
569 template<
class Iterator>
572 typename std::iterator_traits<Iterator>::difference_type len =
::ecuda::distance(first,last);
578 #ifdef ECUDA_CPP11_AVAILABLE
579 __HOST__ inline void assign( std::initializer_list<value_type> il )
588 assign( il.begin(), il.end() );
630 std::vector< value_type, host_allocator<value_type> > v( ::
ecuda::distance(position,
end())+1 );
634 growMemory(
size()+1);
657 std::vector< value_type, host_allocator<value_type> > v( ::
ecuda::distance(position,
end())+count, value );
660 growMemory(
size()+count);
679 template<
class InputIterator>
682 const std::vector< value_type, host_allocator<value_type> > v( first, last );
687 growMemory(
size()+len);
694 #ifdef ECUDA_CPP11_AVAILABLE
710 return insert( position, il.begin(), il.end() );
816 if(
size() != other.
size() )
return false;
820 for( ; iter1 !=
end(); ++iter1, ++iter2 )
if( !( *iter1 == *iter2 ) )
return false;
849 std::vector< value_type, host_allocator<value_type> > v1(
size() ), v2(
size() );
867 std::vector< value_type, host_allocator<value_type> > v1(
size() ), v2(
size() );
892 template<
typename T,
class Alloc,
class P>
893 __HOST__ void vector<T,Alloc,P>::growMemory( size_type minimum )
895 if( base_type::size() >= minimum )
return;
896 size_type m2 = base_type::size();
898 while( m2 < minimum ) m2 <<= 1;
900 shared_ptr<value_type> newMemory( get_allocator().allocate( m2 ) );
901 model::device_contiguous_sequence< value_type, shared_ptr<value_type> > newSequence( newMemory, m2 );
902 ecuda::copy( begin(), end(), newSequence.begin() );
918 template<
typename T,
class Alloc>
919 class vector_kernel_argument :
public vector<T,Alloc,typename ecuda::add_pointer<T>::type>
923 typedef vector<T,Alloc,typename ecuda::add_pointer<T>::type> base_type;
927 __HOST__ vector_kernel_argument(
const vector<T,Alloc,P>& src ) : base_type( src, ecuda::true_type() ) {}
928 __HOST__ __DEVICE__ vector_kernel_argument(
const vector_kernel_argument& src ) : base_type( src, ecuda::true_type() ) {}
930 __HOST__ vector_kernel_argument& operator=(
const vector<T,Alloc,P>& src )
932 vector<T,Alloc>::shallow_assign( src );
936 #ifdef ECUDA_CPP11_AVAILABLE
937 vector_kernel_argument( vector_kernel_argument&& src ) : base_type(std::move(src)) {}
939 vector_kernel_argument& operator=( vector_kernel_argument&& src )
941 base_type::operator=(std::move(src));
A smart pointer that retains shared ownership of an object in device memory.
__HOST__ __DEVICE__ OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
Replacement for std::copy.
__DEVICE__ const_reference operator()(const size_type index) const
Returns a reference to the element at specified location index. No bounds checking is performed...
__HOST__ void shrink_to_fit()
Requests the removal of unused capacity.
__HOST__ void insert(const_iterator position, InputIterator first, InputIterator last)
Inserts elements from range [first,last) before position.
__HOST__ __DEVICE__ const_pointer data() const __NOEXCEPT__
Returns pointer to the underlying array serving as element storage.
__HOST__ __DEVICE__ const_reverse_iterator rend() const __NOEXCEPT__
Returns a reverse iterator to the element following the last element of the reversed container...
__DEVICE__ const_reference at(size_type index) const
Returns a reference to the element at specified location index, with bounds checking.
__DEVICE__ const_reference front() const
Returns a reference to the first element in the container.
__HOST__ allocator_type get_allocator() const
Returns the allocator associated with the container.
__HOST__ __DEVICE__ iterator end() __NOEXCEPT__
Returns an iterator to the element following the last element of the container.
__HOST__ __DEVICE__ void swap(vector &other)
Exchanges the contents of the container with those of the other.
__HOST__ __DEVICE__ const_iterator end() const __NOEXCEPT__
Returns an iterator to the element following the last element of the container.
Alloc allocator_type
allocator type
const impl::vector_kernel_argument< T, Alloc > const_kernel_argument
const kernel argument type
base_type::size_type size_type
unsigned integral type
__HOST__ iterator erase(const_iterator first, const_iterator last)
Removes the elements in the range [first,last).
__DEVICE__ reference front()
Returns a reference to the first element in the container.
__HOST__ __DEVICE__ bool operator>(const vector &other) const
Compares the contents of two vectors lexicographically.
base_type::difference_type difference_type
signed integral type
__HOST__ __DEVICE__ bool empty() const __NOEXCEPT__
Checks if the container has no elements.
__HOST__ __DEVICE__ bool operator!=(const vector &other) const
Checks if the contents of two arrays are not equal.
__HOST__ __DEVICE__ reverse_iterator rend() __NOEXCEPT__
Returns a reverse iterator to the element following the last element of the reversed container...
__HOST__ vector(Iterator first, Iterator last, const allocator_type &allocator=allocator_type())
Constructs the container with the contents of the range [begin,end).
__HOST__ void reserve(size_type newCapacity)
Increase the capacity of the container to a value that's greater or equal to newCapacity.
__HOST__ vector & operator=(const vector &src)
__HOST__ __DEVICE__ bool operator<(const vector &other) const
Compares the contents of two vectors lexicographically.
__DEVICE__ reference operator[](const size_type index)
Returns a reference to the element at specified location index. No bounds checking is performed...
__HOST__ __DEVICE__ void swap(T &a, T &b) __NOEXCEPT__
__HOST__ void assign(Iterator first, Iterator last)
Replaces the contents of the container with copies of those in the range [first,last).
impl::vector_kernel_argument< T, Alloc > kernel_argument
kernel argument type
__HOST__ __DEVICE__ bool operator<=(const vector &other) const
Compares the contents of two vectors lexicographically.
Alloc::pointer pointer
cell pointer type
__DEVICE__ reference operator()(const size_type index)
Returns a reference to the element at specified location index. No bounds checking is performed...
Alloc::const_reference const_reference
cell const reference type
__HOST__ __DEVICE__ const_iterator begin() const __NOEXCEPT__
Returns an iterator to the first element of the container.
__HOST__ __DEVICE__ void pop_back()
Removes the last element of the container.
__HOST__ __DEVICE__ size_type size() const __NOEXCEPT__
Returns the number of elements in the container.
__HOST__ __DEVICE__ __CONSTEXPR__ size_type max_size() const __NOEXCEPT__
Returns the maximum number of elements the container is able to hold due to system or library impleme...
base_type::const_iterator const_iterator
const iterator type
__HOST__ void push_back(const value_type &value)
Appends the given element value to the end of the container.
__HOST__ __DEVICE__ pointer data() __NOEXCEPT__
Returns pointer to the underlying array serving as element storage.
__HOST__ void resize(size_type newSize, const value_type &value=value_type())
Resizes the container to contain newSize elements.
base_type::reverse_iterator reverse_iterator
reverse iterator type
__DEVICE__ reference at(size_type index)
Returns a reference to the element at specified location index, with bounds checking.
__HOST__ void assign(size_type newSize, const value_type &value=value_type())
Replaces the contents of the container.
__DEVICE__ const_reference back() const
Returns a reference to the last element in the container.
__HOST__ __DEVICE__ const_reverse_iterator rbegin() const __NOEXCEPT__
Returns a reverse iterator to the first element of the reversed container.
base_type::const_reverse_iterator const_reverse_iterator
const reverse iterator type
A resizable vector stored in device memory.
__HOST__ __DEVICE__ bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
Replacement for std::equal.
__HOST__ vector(size_type n, const value_type &value, const allocator_type &allocator=allocator_type())
Constructs the container with n copies of elements with value value.
__HOST__ iterator insert(const_iterator position, const size_type count, const value_type &value)
Inserts count copies of the value before position.
Alloc::const_pointer const_pointer
cell const pointer type
__DEVICE__ void threadfence()
__HOST__ __DEVICE__ const T & max(const T &a, const T &b)
__HOST__ __DEVICE__ bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
__HOST__ __DEVICE__ iterator begin() __NOEXCEPT__
Returns an iterator to the first element of the container.
__HOST__ vector(const vector &src)
Copy constructor.
__DEVICE__ reference back()
Returns a reference to the last element in the container.
base_type::value_type value_type
cell data type
__HOST__ vector(size_type n)
Constructs the container with n default-inserted instances of T. No copies are made.
__HOST__ iterator insert(const_iterator position, const value_type &value)
Inserts value before position.
__HOST__ __DEVICE__ void clear()
Removes all elements from the container.
__HOST__ __DEVICE__ bool operator>=(const vector &other) const
Compares the contents of two vectors lexicographically.
ECUDA_SUPPRESS_HD_WARNINGS __HOST__ __DEVICE__ ecuda::iterator_traits< InputIterator >::difference_type count(InputIterator first, InputIterator last, const T &value)
__DEVICE__ const_reference operator[](const size_type index) const
Returns a reference to the element at specified location index. No bounds checking is performed...
__HOST__ __DEVICE__ bool operator==(const vector &other) const
Checks if the contents of two vectors are equal.
Alloc::reference reference
cell reference type
base_type::iterator iterator
iterator type
__HOST__ __DEVICE__ reverse_iterator rbegin() __NOEXCEPT__
Returns a reverse iterator to the first element of the reversed container.
__HOST__ iterator erase(const_iterator position)
Removes the element at position.
ECUDA_SUPPRESS_HD_WARNINGS __HOST__ __DEVICE__ std::iterator_traits< Iterator >::difference_type distance(const Iterator &first, const Iterator &last)
__HOST__ vector(const allocator_type &allocator=allocator_type())
Default constructor. Constructs empty container.
__HOST__ __DEVICE__ size_type capacity() const __NOEXCEPT__
Returns the number of elements that the container has currently allocated space for.
__HOST__ __DEVICE__ void fill(ForwardIterator first, ForwardIterator last, const T &val)