44 #ifndef KOKKOS_VECTOR_HPP
45 #define KOKKOS_VECTOR_HPP
47 #include <Kokkos_Core_fwd.hpp>
56 template<
class Scalar,
class Arg1Type =
void>
57 class vector :
public DualView<Scalar*,LayoutLeft,Arg1Type> {
60 typedef Scalar value_type;
61 typedef Scalar* pointer;
62 typedef const Scalar* const_pointer;
63 typedef Scalar& reference;
64 typedef const Scalar& const_reference;
65 typedef Scalar* iterator;
66 typedef const Scalar* const_iterator;
70 typedef size_t size_type;
72 typedef DualView<Scalar*,LayoutLeft,Arg1Type> DV;
76 #ifdef KOKKOS_ENABLE_CUDA_UVM
77 KOKKOS_INLINE_FUNCTION reference operator() (
int i)
const {
return DV::h_view(i);};
78 KOKKOS_INLINE_FUNCTION reference operator[] (
int i)
const {
return DV::h_view(i);};
80 inline reference operator() (
int i)
const {
return DV::h_view(i);};
81 inline reference operator[] (
int i)
const {
return DV::h_view(i);};
92 vector(
int n, Scalar val=Scalar()):DualView<Scalar*,LayoutLeft,Arg1Type>(
"Vector",size_t(n*(1.1))) {
95 DV::modified_flags(0) = 1;
107 void resize(
size_t n,
const Scalar& val) {
111 void assign (
size_t n,
const Scalar& val) {
121 if( DV::template need_sync<typename DV::t_dev::device_type>() ) {
122 set_functor_host f(DV::h_view,val);
124 DV::t_host::execution_space::fence();
125 DV::template modify<typename DV::t_host::device_type>();
127 set_functor f(DV::d_view,val);
129 DV::t_dev::execution_space::fence();
130 DV::template modify<typename DV::t_dev::device_type>();
134 void reserve(
size_t n) {
138 void push_back(Scalar val) {
139 DV::template sync<typename DV::t_host::device_type>();
140 DV::template modify<typename DV::t_host::device_type>();
141 if(_size == span()) {
142 size_t new_size = _size*_extra_storage;
143 if(new_size == _size) new_size++;
147 DV::h_view(_size) = val;
160 size_type size()
const {
return _size;}
161 size_type max_size()
const {
return 2000000000;}
162 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
163 size_type capacity()
const {
return DV::capacity();}
165 size_type span()
const {
return DV::span();}
166 bool empty()
const {
return _size==0;}
168 iterator begin()
const {
return &DV::h_view(0);}
170 iterator end()
const {
return &DV::h_view(_size);}
172 reference front() {
return DV::h_view(0);}
174 reference back() {
return DV::h_view(_size - 1);}
176 const_reference front()
const {
return DV::h_view(0);}
178 const_reference back()
const {
return DV::h_view(_size - 1);}
183 lower_bound (
const size_t& start,
184 const size_t& theEnd,
185 const Scalar& comp_val)
const
188 int upper = _size > theEnd? theEnd : _size-1;
189 if (upper <= lower) {
193 Scalar lower_val = DV::h_view(lower);
194 Scalar upper_val = DV::h_view(upper);
195 size_t idx = (upper+lower)/2;
196 Scalar val = DV::h_view(idx);
197 if(val>upper_val)
return upper;
198 if(val<lower_val)
return start;
206 idx = (upper+lower)/2;
207 val = DV::h_view(idx);
213 for(
int i=0;i<_size-1;i++) {
214 if(DV::h_view(i)>DV::h_view(i+1))
return false;
219 iterator find(Scalar val)
const {
220 if(_size == 0)
return end();
222 int upper,lower,current;
227 if((val<DV::h_view(0)) || (val>DV::h_view(_size-1)) )
return end();
231 if(val>DV::h_view(current)) lower = current+1;
232 else upper = current;
233 current = (upper+lower)/2;
236 if(val==DV::h_view(current))
return &DV::h_view(current);
242 void device_to_host(){
245 void host_to_device()
const {
250 DV::template modify<typename DV::t_host::device_type>();
253 DV::template modify<typename DV::t_dev::device_type>();
256 void set_overallocation(
float extra) {
257 _extra_storage = 1.0 + extra;
263 typedef typename DV::t_dev::execution_space execution_space;
264 typename DV::t_dev _data;
267 set_functor(
typename DV::t_dev data, Scalar val) :
268 _data(data),_val(val) {}
270 KOKKOS_INLINE_FUNCTION
271 void operator() (
const int &i)
const {
276 struct set_functor_host {
277 typedef typename DV::t_host::execution_space execution_space;
278 typename DV::t_host _data;
281 set_functor_host(
typename DV::t_host data, Scalar val) :
282 _data(data),_val(val) {}
284 KOKKOS_INLINE_FUNCTION
285 void operator() (
const int &i)
const {
Declaration and definition of Kokkos::DualView.
void parallel_for(const ExecPolicy &policy, const FunctorType &functor, const std::string &str="", typename Impl::enable_if< Kokkos::Impl::is_execution_policy< ExecPolicy >::value >::type *=0)
Execute functor in parallel according to the execution policy.
std::enable_if< std::is_same< typename Kokkos::View< T, P...>::array_layout, Kokkos::LayoutLeft >::value||std::is_same< typename Kokkos::View< T, P...>::array_layout, Kokkos::LayoutRight >::value >::type resize(Kokkos::View< T, P...> &v, const size_t n0=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n1=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n2=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n3=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n4=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n5=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n6=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n7=KOKKOS_IMPL_CTOR_DEFAULT_ARG)
Resize a view with copying old data to new data at the corresponding indices.
void deep_copy(const View< DT, DP...> &dst, typename ViewTraits< DT, DP...>::const_value_type &value, typename std::enable_if< std::is_same< typename ViewTraits< DT, DP...>::specialize, void >::value >::type *=0)
Deep copy a value from Host memory into a view.
void resize(DynRankView< T, P...> &v, const size_t n0=KOKKOS_INVALID_INDEX, const size_t n1=KOKKOS_INVALID_INDEX, const size_t n2=KOKKOS_INVALID_INDEX, const size_t n3=KOKKOS_INVALID_INDEX, const size_t n4=KOKKOS_INVALID_INDEX, const size_t n5=KOKKOS_INVALID_INDEX, const size_t n6=KOKKOS_INVALID_INDEX, const size_t n7=KOKKOS_INVALID_INDEX)
Resize a view with copying old data to new data at the corresponding indices.