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 using value_type = Scalar;
62 using pointer = Scalar*;
63 using const_pointer =
const Scalar*;
64 using reference = Scalar&;
65 using const_reference =
const Scalar&;
66 using iterator = Scalar*;
67 using const_iterator =
const Scalar*;
68 using size_type = size_t;
73 using DV = DualView<Scalar*, LayoutLeft, Arg1Type>;
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;
104 void resize(
size_t n) {
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();
168 resize(size() + count);
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 KOKKOS_INLINE_FUNCTION constexpr
bool is_allocated()
const {
216 return DV::is_allocated();
219 size_type size()
const {
return _size; }
220 size_type max_size()
const {
return 2000000000; }
221 size_type span()
const {
return DV::span(); }
222 bool empty()
const {
return _size == 0; }
224 pointer data()
const {
return DV::h_view.data(); }
226 iterator begin()
const {
return DV::h_view.data(); }
228 iterator end()
const {
229 return _size > 0 ? DV::h_view.data() + _size : DV::h_view.data();
232 reference front() {
return DV::h_view(0); }
234 reference back() {
return DV::h_view(_size - 1); }
236 const_reference front()
const {
return DV::h_view(0); }
238 const_reference back()
const {
return DV::h_view(_size - 1); }
243 size_t lower_bound(
const size_t& start,
const size_t& theEnd,
244 const Scalar& comp_val)
const {
250 if (upper <= lower) {
254 Scalar lower_val = DV::h_view(lower);
255 Scalar upper_val = DV::h_view(upper);
256 size_t idx = (upper + lower) / 2;
257 Scalar val = DV::h_view(idx);
258 if (val > upper_val)
return upper;
259 if (val < lower_val)
return start;
261 while (upper > lower) {
262 if (comp_val > val) {
267 idx = (upper + lower) / 2;
268 val = DV::h_view(idx);
274 for (
int i = 0; i < _size - 1; i++) {
275 if (DV::h_view(i) > DV::h_view(i + 1))
return false;
280 iterator find(Scalar val)
const {
281 if (_size == 0)
return end();
283 int upper, lower, current;
288 if ((val < DV::h_view(0)) || (val > DV::h_view(_size - 1)))
return end();
290 while (upper > lower) {
291 if (val > DV::h_view(current))
295 current = (upper + lower) / 2;
298 if (val == DV::h_view(current))
299 return &DV::h_view(current);
306 void device_to_host() { deep_copy(DV::h_view, DV::d_view); }
307 void host_to_device()
const { deep_copy(DV::d_view, DV::h_view); }
309 void on_host() { DV::template modify<typename DV::t_host::device_type>(); }
310 void on_device() { DV::template modify<typename DV::t_dev::device_type>(); }
312 void set_overallocation(
float extra) { _extra_storage = 1.0 + extra; }
316 using execution_space =
typename DV::t_dev::execution_space;
317 typename DV::t_dev _data;
320 set_functor(
typename DV::t_dev data, Scalar val) : _data(data), _val(val) {}
322 KOKKOS_INLINE_FUNCTION
323 void operator()(
const int& i)
const { _data(i) = _val; }
326 struct set_functor_host {
327 using execution_space =
typename DV::t_host::execution_space;
328 typename DV::t_host _data;
331 set_functor_host(
typename DV::t_host data, Scalar val)
332 : _data(data), _val(val) {}
334 KOKKOS_INLINE_FUNCTION
335 void operator()(
const int& i)
const { _data(i) = _val; }
Declaration and definition of Kokkos::DualView.
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.