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> {
59 typedef Scalar value_type;
60 typedef Scalar* pointer;
61 typedef const Scalar* const_pointer;
62 typedef Scalar* reference;
63 typedef const Scalar* const_reference;
64 typedef Scalar* iterator;
65 typedef const Scalar* const_iterator;
69 typedef size_t size_type;
71 typedef DualView<Scalar*,LayoutLeft,Arg1Type> DV;
75 #ifdef KOKKOS_CUDA_USE_UVM 76 KOKKOS_INLINE_FUNCTION Scalar& operator() (
int i)
const {
return DV::h_view(i);};
77 KOKKOS_INLINE_FUNCTION Scalar& operator[] (
int i)
const {
return DV::h_view(i);};
79 inline Scalar& operator() (
int i)
const {
return DV::h_view(i);};
80 inline Scalar& operator[] (
int i)
const {
return DV::h_view(i);};
88 DV::modified_host() = 1;
92 vector(
int n, Scalar val=Scalar()):DualView<Scalar*,LayoutLeft,Arg1Type>(
"Vector",size_t(n*(1.1))) {
95 DV::modified_host() = 1;
103 DV::resize(
size_t (n*_extra_storage));
107 void resize(
size_t n,
const Scalar& val) {
111 void assign (
size_t n,
const Scalar& val) {
116 DV::resize(
size_t (n*_extra_storage));
121 if( DV::modified_host() >= DV::modified_device() ) {
122 set_functor_host f(DV::h_view,val);
124 DV::t_host::execution_space::fence();
125 DV::modified_host()++;
127 set_functor f(DV::d_view,val);
129 DV::t_dev::execution_space::fence();
130 DV::modified_device()++;
134 void reserve(
size_t n) {
135 DV::resize(
size_t (n*_extra_storage));
138 void push_back(Scalar val) {
139 DV::modified_host()++;
140 if(_size == capacity()) {
141 size_t new_size = _size*_extra_storage;
142 if(new_size == _size) new_size++;
143 DV::resize(new_size);
146 DV::h_view(_size) = val;
159 size_type size()
const {
return _size;};
160 size_type max_size()
const {
return 2000000000;}
161 size_type capacity()
const {
return DV::capacity();};
162 bool empty()
const {
return _size==0;};
164 iterator begin()
const {
return &DV::h_view(0);};
166 iterator end()
const {
return &DV::h_view(_size);};
172 lower_bound (
const size_t& start,
173 const size_t& theEnd,
174 const Scalar& comp_val)
const 177 int upper = _size > theEnd? theEnd : _size-1;
178 if (upper <= lower) {
182 Scalar lower_val = DV::h_view(lower);
183 Scalar upper_val = DV::h_view(upper);
184 size_t idx = (upper+lower)/2;
185 Scalar val = DV::h_view(idx);
186 if(val>upper_val)
return upper;
187 if(val<lower_val)
return start;
195 idx = (upper+lower)/2;
196 val = DV::h_view(idx);
202 for(
int i=0;i<_size-1;i++) {
203 if(DV::h_view(i)>DV::h_view(i+1))
return false;
208 iterator find(Scalar val)
const {
209 if(_size == 0)
return end();
211 int upper,lower,current;
216 if((val<DV::h_view(0)) || (val>DV::h_view(_size-1)) )
return end();
220 if(val>DV::h_view(current)) lower = current+1;
221 else upper = current;
222 current = (upper+lower)/2;
225 if(val==DV::h_view(current))
return &DV::h_view(current);
231 void device_to_host(){
234 void host_to_device()
const {
239 DV::modified_host() = DV::modified_device() + 1;
242 DV::modified_device() = DV::modified_host() + 1;
245 void set_overallocation(
float extra) {
246 _extra_storage = 1.0 + extra;
252 typedef typename DV::t_dev::execution_space execution_space;
253 typename DV::t_dev _data;
256 set_functor(
typename DV::t_dev data, Scalar val) :
257 _data(data),_val(val) {}
259 KOKKOS_INLINE_FUNCTION
260 void operator() (
const int &i)
const {
265 struct set_functor_host {
266 typedef typename DV::t_host::execution_space execution_space;
267 typename DV::t_host _data;
270 set_functor_host(
typename DV::t_host data, Scalar val) :
271 _data(data),_val(val) {}
273 KOKKOS_INLINE_FUNCTION
274 void operator() (
const int &i)
const {
Declaration and definition of Kokkos::DualView.
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=0, const size_t n1=0, const size_t n2=0, const size_t n3=0, const size_t n4=0, const size_t n5=0, const size_t n6=0, const size_t n7=0)
Resize a view with copying old data to new data at the corresponding indices.
void parallel_for(const ExecPolicy &policy, const FunctorType &functor, const std::string &str="", typename Impl::enable_if< ! Impl::is_integral< ExecPolicy >::value >::type *=0)
Execute functor in parallel according to the execution policy.
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.