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_Macros.hpp> 
   26 #if defined(KOKKOS_ENABLE_DEPRECATED_CODE_4) 
   27 #if defined(KOKKOS_ENABLE_DEPRECATION_WARNINGS) 
   29 [[deprecated(
"Deprecated <Kokkos_Vector.hpp> header is included")]] 
int 
   30 emit_warning_kokkos_vector_deprecated() {
 
   33 static auto do_not_include = emit_warning_kokkos_vector_deprecated();
 
   37 #error "Deprecated <Kokkos_Vector.hpp> header is included" 
   40 #include <Kokkos_Core_fwd.hpp> 
   50 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4 
   51 template <
class Scalar, 
class Arg1Type = 
void>
 
   52 class KOKKOS_DEPRECATED vector
 
   53     : 
public DualView<Scalar*, LayoutLeft, Arg1Type> {
 
   55   using value_type      = Scalar;
 
   56   using pointer         = Scalar*;
 
   57   using const_pointer   = 
const Scalar*;
 
   58   using reference       = Scalar&;
 
   59   using const_reference = 
const Scalar&;
 
   60   using iterator        = Scalar*;
 
   61   using const_iterator  = 
const Scalar*;
 
   62   using size_type       = size_t;
 
   67   using DV = DualView<Scalar*, LayoutLeft, Arg1Type>;
 
   70 #ifdef KOKKOS_ENABLE_CUDA_UVM 
   71   KOKKOS_INLINE_FUNCTION reference operator()(
int i)
 const {
 
   72     return DV::view_host()(i);
 
   74   KOKKOS_INLINE_FUNCTION reference operator[](
int i)
 const {
 
   75     return DV::view_host()(i);
 
   78   inline reference operator()(
int i)
 const { 
return DV::view_host()(i); }
 
   79   inline reference operator[](
int i)
 const { 
return DV::view_host()(i); }
 
   89   vector(
int n, Scalar val = Scalar())
 
   90       : DualView<Scalar*, LayoutLeft, Arg1Type>(
"Vector", size_t(n * (1.1))) {
 
   93     DV::modified_flags(0) = 1;
 
   98   void resize(
size_t n) {
 
   99     if (n >= span()) DV::resize(
size_t(n * _extra_storage));
 
  103   void resize(
size_t n, 
const Scalar& val) { assign(n, val); }
 
  105   void assign(
size_t n, 
const Scalar& val) {
 
  108     if (n > span()) DV::resize(
size_t(n * _extra_storage));
 
  113     if (DV::template need_sync<typename DV::t_dev::device_type>()) {
 
  114       set_functor_host f(DV::view_host(), val);
 
  115       parallel_for(
"Kokkos::vector::assign", n, f);
 
  116       typename DV::t_host::execution_space().fence(
 
  117           "Kokkos::vector::assign: fence after assigning values");
 
  118       DV::template modify<typename DV::t_host::device_type>();
 
  120       set_functor f(DV::view_device(), val);
 
  121       parallel_for(
"Kokkos::vector::assign", n, f);
 
  122       typename DV::t_dev::execution_space().fence(
 
  123           "Kokkos::vector::assign: fence after assigning values");
 
  124       DV::template modify<typename DV::t_dev::device_type>();
 
  128   void reserve(
size_t n) { DV::resize(
size_t(n * _extra_storage)); }
 
  130   void push_back(Scalar val) {
 
  131     if (_size == span()) {
 
  132       size_t new_size = _size * _extra_storage;
 
  133       if (new_size == _size) new_size++;
 
  134       DV::resize(new_size);
 
  138     DV::view_host()(_size) = val;
 
  143   void pop_back() { _size--; }
 
  145   void clear() { _size = 0; }
 
  147   iterator insert(iterator it, 
const value_type& val) {
 
  148     return insert(it, 1, val);
 
  151   iterator insert(iterator it, size_type count, 
const value_type& val) {
 
  152     if ((size() == 0) && (it == begin())) {
 
  159     if (std::less<>()(it, begin()) || std::less<>()(end(), it))
 
  160       Kokkos::abort(
"Kokkos::vector::insert : invalid insert iterator");
 
  161     if (count == 0) 
return it;
 
  162     ptrdiff_t start = std::distance(begin(), it);
 
  163     auto org_size   = size();
 
  164     resize(size() + count);
 
  166     std::copy_backward(begin() + start, begin() + org_size,
 
  167                        begin() + org_size + count);
 
  168     std::fill_n(begin() + start, count, val);
 
  170     return begin() + start;
 
  175   struct impl_is_input_iterator :  std::bool_constant<
 
  176                                       !std::is_convertible_v<T, size_type>> {};
 
  180   template <
typename InputIterator>
 
  181   std::enable_if_t<impl_is_input_iterator<InputIterator>::value, iterator>
 
  182   insert(iterator it, InputIterator b, InputIterator e) {
 
  183     ptrdiff_t count = std::distance(b, e);
 
  187     if (std::less<>()(it, begin()) || std::less<>()(end(), it))
 
  188       Kokkos::abort(
"Kokkos::vector::insert : invalid insert iterator");
 
  190     ptrdiff_t start = std::distance(begin(), it);
 
  191     auto org_size   = size();
 
  194     resize(size() + count);
 
  196     std::copy_backward(begin() + start, begin() + org_size,
 
  197                        begin() + org_size + count);
 
  198     std::copy(b, e, begin() + start);
 
  200     return begin() + start;
 
  203   KOKKOS_INLINE_FUNCTION constexpr 
bool is_allocated()
 const {
 
  204     return DV::is_allocated();
 
  207   size_type size()
 const { 
return _size; }
 
  208   size_type max_size()
 const { 
return 2000000000; }
 
  209   size_type span()
 const { 
return DV::span(); }
 
  210   bool empty()
 const { 
return _size == 0; }
 
  212   pointer data()
 const { 
return DV::view_host().data(); }
 
  214   iterator begin()
 const { 
return DV::view_host().data(); }
 
  216   const_iterator cbegin()
 const { 
return DV::view_host().data(); }
 
  218   iterator end()
 const {
 
  219     return _size > 0 ? DV::view_host().data() + _size : DV::view_host().data();
 
  222   const_iterator cend()
 const {
 
  223     return _size > 0 ? DV::view_host().data() + _size : DV::view_host().data();
 
  226   reference front() { 
return DV::view_host()(0); }
 
  228   reference back() { 
return DV::view_host()(_size - 1); }
 
  230   const_reference front()
 const { 
return DV::view_host()(0); }
 
  232   const_reference back()
 const { 
return DV::view_host()(_size - 1); }
 
  237   size_t lower_bound(
const size_t& start, 
const size_t& theEnd,
 
  238                      const Scalar& comp_val)
 const {
 
  244     if (upper <= lower) {
 
  248     Scalar lower_val = DV::view_host()(lower);
 
  249     Scalar upper_val = DV::view_host()(upper);
 
  250     size_t idx       = (upper + lower) / 2;
 
  251     Scalar val       = DV::view_host()(idx);
 
  252     if (val > upper_val) 
return upper;
 
  253     if (val < lower_val) 
return start;
 
  255     while (upper > lower) {
 
  256       if (comp_val > val) {
 
  261       idx = (upper + lower) / 2;
 
  262       val = DV::view_host()(idx);
 
  268     for (
int i = 0; i < _size - 1; i++) {
 
  269       if (DV::view_host()(i) > DV::view_host()(i + 1)) 
return false;
 
  274   iterator find(Scalar val)
 const {
 
  275     if (_size == 0) 
return end();
 
  277     int upper, lower, current;
 
  282     if ((val < DV::view_host()(0)) || (val > DV::view_host()(_size - 1)))
 
  285     while (upper > lower) {
 
  286       if (val > DV::view_host()(current))
 
  290       current = (upper + lower) / 2;
 
  293     if (val == DV::view_host()(current))
 
  294       return &DV::view_host()(current);
 
  301   void device_to_host() { deep_copy(DV::view_host(), DV::view_device()); }
 
  302   void host_to_device()
 const { deep_copy(DV::view_device(), DV::view_host()); }
 
  304   void on_host() { DV::template modify<typename DV::t_host::device_type>(); }
 
  305   void on_device() { DV::template modify<typename DV::t_dev::device_type>(); }
 
  307   void set_overallocation(
float extra) { _extra_storage = 1.0 + extra; }
 
  311     using execution_space = 
typename DV::t_dev::execution_space;
 
  312     typename DV::t_dev _data;
 
  315     set_functor(
typename DV::t_dev data, Scalar val) : _data(data), _val(val) {}
 
  317     KOKKOS_INLINE_FUNCTION
 
  318     void operator()(
const int& i)
 const { _data(i) = _val; }
 
  321   struct set_functor_host {
 
  322     using execution_space = 
typename DV::t_host::execution_space;
 
  323     typename DV::t_host _data;
 
  326     set_functor_host(
typename DV::t_host data, Scalar val)
 
  327         : _data(data), _val(val) {}
 
  329     KOKKOS_INLINE_FUNCTION
 
  330     void operator()(
const int& i)
 const { _data(i) = _val; }
 
  336 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_VECTOR 
  337 #undef KOKKOS_IMPL_PUBLIC_INCLUDE 
  338 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_VECTOR 
Declaration and definition of Kokkos::DualView.