45 #ifndef KOKKOS_VECTOR_HPP
46 #define KOKKOS_VECTOR_HPP
48 #include <Kokkos_Core_fwd.hpp>
58 template <
class Scalar,
class Arg1Type =
void>
59 class vector :
public DualView<Scalar*, LayoutLeft, Arg1Type> {
61 typedef Scalar value_type;
62 typedef Scalar* pointer;
63 typedef const Scalar* const_pointer;
64 typedef Scalar& reference;
65 typedef const Scalar& const_reference;
66 typedef Scalar* iterator;
67 typedef const Scalar* const_iterator;
68 typedef size_t size_type;
73 typedef DualView<Scalar*, LayoutLeft, Arg1Type> DV;
76 #ifdef KOKKOS_ENABLE_CUDA_UVM
77 KOKKOS_INLINE_FUNCTION reference operator()(
int i)
const {
80 KOKKOS_INLINE_FUNCTION reference operator[](
int i)
const {
84 inline reference operator()(
int i)
const {
return DV::h_view(i); };
85 inline reference operator[](
int i)
const {
return DV::h_view(i); };
95 vector(
int n, Scalar val = Scalar())
96 : DualView<Scalar*, LayoutLeft, Arg1Type>(
"Vector", size_t(n * (1.1))) {
99 DV::modified_flags(0) = 1;
105 if (n >= span()) DV::resize(
size_t(n * _extra_storage));
109 void resize(
size_t n,
const Scalar& val) { assign(n, val); }
111 void assign(
size_t n,
const Scalar& val) {
114 if (n > span()) DV::resize(
size_t(n * _extra_storage));
119 if (DV::template need_sync<typename DV::t_dev::device_type>()) {
120 set_functor_host f(DV::h_view, val);
122 typename DV::t_host::execution_space().fence();
123 DV::template modify<typename DV::t_host::device_type>();
125 set_functor f(DV::d_view, val);
127 typename DV::t_dev::execution_space().fence();
128 DV::template modify<typename DV::t_dev::device_type>();
132 void reserve(
size_t n) { DV::resize(
size_t(n * _extra_storage)); }
134 void push_back(Scalar val) {
135 DV::template sync<typename DV::t_host::device_type>();
136 DV::template modify<typename DV::t_host::device_type>();
137 if (_size == span()) {
138 size_t new_size = _size * _extra_storage;
139 if (new_size == _size) new_size++;
140 DV::resize(new_size);
143 DV::h_view(_size) = val;
147 void pop_back() { _size--; }
149 void clear() { _size = 0; }
151 iterator insert(iterator it,
const value_type& val) {
152 return insert(it, 1, val);
155 iterator insert(iterator it, size_type count,
const value_type& val) {
156 if ((size() == 0) && (it == begin())) {
163 if (it < begin() || it > end())
164 Kokkos::abort(
"Kokkos::vector::insert : invalid insert iterator");
165 if (count == 0)
return it;
166 ptrdiff_t start = std::distance(begin(), it);
167 auto org_size = size();
170 std::copy_backward(begin() + start, begin() + org_size,
171 begin() + org_size + count);
172 std::fill_n(begin() + start, count, val);
174 return begin() + start;
179 struct impl_is_input_iterator
180 : std::integral_constant<
181 bool, !std::is_convertible<T, size_type>::value> {};
185 template <
typename InputIterator>
186 typename std::enable_if<impl_is_input_iterator<InputIterator>::value,
188 insert(iterator it, InputIterator b, InputIterator e) {
189 ptrdiff_t count = std::distance(b, e);
190 if (count == 0)
return it;
194 if (it < begin() || it > end())
195 Kokkos::abort(
"Kokkos::vector::insert : invalid insert iterator");
197 bool resized =
false;
198 if ((size() == 0) && (it == begin())) {
203 ptrdiff_t start = std::distance(begin(), it);
204 auto org_size = size();
205 if (!resized)
resize(size() + count);
206 it = begin() + start;
208 std::copy_backward(begin() + start, begin() + org_size,
209 begin() + org_size + count);
212 return begin() + start;
215 size_type size()
const {
return _size; }
216 size_type max_size()
const {
return 2000000000; }
217 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
218 size_type capacity()
const {
return DV::capacity(); }
220 size_type span()
const {
return DV::span(); }
221 bool empty()
const {
return _size == 0; }
223 iterator begin()
const {
return DV::h_view.data(); }
225 iterator end()
const {
226 return _size > 0 ? DV::h_view.data() + _size : DV::h_view.data();
229 reference front() {
return DV::h_view(0); }
231 reference back() {
return DV::h_view(_size - 1); }
233 const_reference front()
const {
return DV::h_view(0); }
235 const_reference back()
const {
return DV::h_view(_size - 1); }
240 size_t lower_bound(
const size_t& start,
const size_t& theEnd,
241 const Scalar& comp_val)
const {
247 if (upper <= lower) {
251 Scalar lower_val = DV::h_view(lower);
252 Scalar upper_val = DV::h_view(upper);
253 size_t idx = (upper + lower) / 2;
254 Scalar val = DV::h_view(idx);
255 if (val > upper_val)
return upper;
256 if (val < lower_val)
return start;
258 while (upper > lower) {
259 if (comp_val > val) {
264 idx = (upper + lower) / 2;
265 val = DV::h_view(idx);
271 for (
int i = 0; i < _size - 1; i++) {
272 if (DV::h_view(i) > DV::h_view(i + 1))
return false;
277 iterator find(Scalar val)
const {
278 if (_size == 0)
return end();
280 int upper, lower, current;
285 if ((val < DV::h_view(0)) || (val > DV::h_view(_size - 1)))
return end();
287 while (upper > lower) {
288 if (val > DV::h_view(current))
292 current = (upper + lower) / 2;
295 if (val == DV::h_view(current))
296 return &DV::h_view(current);
303 void device_to_host() {
deep_copy(DV::h_view, DV::d_view); }
304 void host_to_device()
const {
deep_copy(DV::d_view, DV::h_view); }
306 void on_host() { DV::template modify<typename DV::t_host::device_type>(); }
307 void on_device() { DV::template modify<typename DV::t_dev::device_type>(); }
309 void set_overallocation(
float extra) { _extra_storage = 1.0 + extra; }
313 typedef typename DV::t_dev::execution_space execution_space;
314 typename DV::t_dev _data;
317 set_functor(
typename DV::t_dev data, Scalar val) : _data(data), _val(val) {}
319 KOKKOS_INLINE_FUNCTION
320 void operator()(
const int& i)
const { _data(i) = _val; }
323 struct set_functor_host {
324 typedef typename DV::t_host::execution_space execution_space;
325 typename DV::t_host _data;
328 set_functor_host(
typename DV::t_host data, Scalar val)
329 : _data(data), _val(val) {}
331 KOKKOS_INLINE_FUNCTION
332 void operator()(
const int& i)
const { _data(i) = _val; }
Declaration and definition of Kokkos::DualView.
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 *=nullptr)
Deep copy a value from Host memory into a view.
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 parallel_for(const ExecPolicy &policy, const FunctorType &functor, const std::string &str="", typename std::enable_if< Kokkos::Impl::is_execution_policy< ExecPolicy >::value >::type *=nullptr)
Execute functor in parallel according to the execution policy.