17#ifndef KOKKOS_VECTOR_HPP
18#define KOKKOS_VECTOR_HPP
19#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
20#define KOKKOS_IMPL_PUBLIC_INCLUDE
21#define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_VECTOR
24#include <Kokkos_Core_fwd.hpp>
34template <
class Scalar,
class Arg1Type =
void>
35class vector :
public DualView<Scalar*, LayoutLeft, Arg1Type> {
37 using value_type = Scalar;
38 using pointer = Scalar*;
39 using const_pointer =
const Scalar*;
40 using reference = Scalar&;
41 using const_reference =
const Scalar&;
42 using iterator = Scalar*;
43 using const_iterator =
const Scalar*;
44 using size_type = size_t;
49 using DV = DualView<Scalar*, LayoutLeft, Arg1Type>;
52#ifdef KOKKOS_ENABLE_CUDA_UVM
53 KOKKOS_INLINE_FUNCTION reference operator()(
int i)
const {
56 KOKKOS_INLINE_FUNCTION reference operator[](
int i)
const {
60 inline reference operator()(
int i)
const {
return DV::h_view(i); };
61 inline reference operator[](
int i)
const {
return DV::h_view(i); };
71 vector(
int n, Scalar val = Scalar())
72 : DualView<Scalar*, LayoutLeft, Arg1Type>(
"Vector", size_t(n * (1.1))) {
75 DV::modified_flags(0) = 1;
80 void resize(
size_t n) {
81 if (n >= span()) DV::resize(
size_t(n * _extra_storage));
85 void resize(
size_t n,
const Scalar& val) { assign(n, val); }
87 void assign(
size_t n,
const Scalar& val) {
90 if (n > span()) DV::resize(
size_t(n * _extra_storage));
95 if (DV::template need_sync<typename DV::t_dev::device_type>()) {
96 set_functor_host f(DV::h_view, val);
97 parallel_for(
"Kokkos::vector::assign", n, f);
98 typename DV::t_host::execution_space().fence(
99 "Kokkos::vector::assign: fence after assigning values");
100 DV::template modify<typename DV::t_host::device_type>();
102 set_functor f(DV::d_view, val);
103 parallel_for(
"Kokkos::vector::assign", n, f);
104 typename DV::t_dev::execution_space().fence(
105 "Kokkos::vector::assign: fence after assigning values");
106 DV::template modify<typename DV::t_dev::device_type>();
110 void reserve(
size_t n) { DV::resize(
size_t(n * _extra_storage)); }
112 void push_back(Scalar val) {
113 DV::template sync<typename DV::t_host::device_type>();
114 DV::template modify<typename DV::t_host::device_type>();
115 if (_size == span()) {
116 size_t new_size = _size * _extra_storage;
117 if (new_size == _size) new_size++;
118 DV::resize(new_size);
121 DV::h_view(_size) = val;
125 void pop_back() { _size--; }
127 void clear() { _size = 0; }
129 iterator insert(iterator it,
const value_type& val) {
130 return insert(it, 1, val);
133 iterator insert(iterator it, size_type count,
const value_type& val) {
134 if ((size() == 0) && (it == begin())) {
141 if (std::less<>()(it, begin()) || std::less<>()(end(), it))
142 Kokkos::abort(
"Kokkos::vector::insert : invalid insert iterator");
143 if (count == 0)
return it;
144 ptrdiff_t start = std::distance(begin(), it);
145 auto org_size = size();
146 resize(size() + count);
148 std::copy_backward(begin() + start, begin() + org_size,
149 begin() + org_size + count);
150 std::fill_n(begin() + start, count, val);
152 return begin() + start;
157 struct impl_is_input_iterator
158 : std::bool_constant<
159 !std::is_convertible<T, size_type>::value> {};
163 template <
typename InputIterator>
164 std::enable_if_t<impl_is_input_iterator<InputIterator>::value, iterator>
165 insert(iterator it, InputIterator b, InputIterator e) {
166 ptrdiff_t count = std::distance(b, e);
170 if (std::less<>()(it, begin()) || std::less<>()(end(), it))
171 Kokkos::abort(
"Kokkos::vector::insert : invalid insert iterator");
173 ptrdiff_t start = std::distance(begin(), it);
174 auto org_size = size();
177 resize(size() + count);
179 std::copy_backward(begin() + start, begin() + org_size,
180 begin() + org_size + count);
181 std::copy(b, e, begin() + start);
183 return begin() + start;
186 KOKKOS_INLINE_FUNCTION
constexpr bool is_allocated()
const {
187 return DV::is_allocated();
190 size_type size()
const {
return _size; }
191 size_type max_size()
const {
return 2000000000; }
192 size_type span()
const {
return DV::span(); }
193 bool empty()
const {
return _size == 0; }
195 pointer data()
const {
return DV::h_view.data(); }
197 iterator begin()
const {
return DV::h_view.data(); }
199 iterator end()
const {
200 return _size > 0 ? DV::h_view.data() + _size : DV::h_view.data();
203 reference front() {
return DV::h_view(0); }
205 reference back() {
return DV::h_view(_size - 1); }
207 const_reference front()
const {
return DV::h_view(0); }
209 const_reference back()
const {
return DV::h_view(_size - 1); }
214 size_t lower_bound(
const size_t& start,
const size_t& theEnd,
215 const Scalar& comp_val)
const {
221 if (upper <= lower) {
225 Scalar lower_val = DV::h_view(lower);
226 Scalar upper_val = DV::h_view(upper);
227 size_t idx = (upper + lower) / 2;
228 Scalar val = DV::h_view(idx);
229 if (val > upper_val)
return upper;
230 if (val < lower_val)
return start;
232 while (upper > lower) {
233 if (comp_val > val) {
238 idx = (upper + lower) / 2;
239 val = DV::h_view(idx);
245 for (
int i = 0; i < _size - 1; i++) {
246 if (DV::h_view(i) > DV::h_view(i + 1))
return false;
251 iterator find(Scalar val)
const {
252 if (_size == 0)
return end();
254 int upper, lower, current;
259 if ((val < DV::h_view(0)) || (val > DV::h_view(_size - 1)))
return end();
261 while (upper > lower) {
262 if (val > DV::h_view(current))
266 current = (upper + lower) / 2;
269 if (val == DV::h_view(current))
270 return &DV::h_view(current);
277 void device_to_host() { deep_copy(DV::h_view, DV::d_view); }
278 void host_to_device()
const { deep_copy(DV::d_view, DV::h_view); }
280 void on_host() { DV::template modify<typename DV::t_host::device_type>(); }
281 void on_device() { DV::template modify<typename DV::t_dev::device_type>(); }
283 void set_overallocation(
float extra) { _extra_storage = 1.0 + extra; }
287 using execution_space =
typename DV::t_dev::execution_space;
288 typename DV::t_dev _data;
291 set_functor(
typename DV::t_dev data, Scalar val) : _data(data), _val(val) {}
293 KOKKOS_INLINE_FUNCTION
294 void operator()(
const int& i)
const { _data(i) = _val; }
297 struct set_functor_host {
298 using execution_space =
typename DV::t_host::execution_space;
299 typename DV::t_host _data;
302 set_functor_host(
typename DV::t_host data, Scalar val)
303 : _data(data), _val(val) {}
305 KOKKOS_INLINE_FUNCTION
306 void operator()(
const int& i)
const { _data(i) = _val; }
311#ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_VECTOR
312#undef KOKKOS_IMPL_PUBLIC_INCLUDE
313#undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_VECTOR
Declaration and definition of Kokkos::DualView.
A thread safe view to a bitset.