Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Stokhos_ViewStorage.hpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Stokhos Package
5// Copyright (2009) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38//
39// ***********************************************************************
40// @HEADER
41
42#ifndef STOKHOS_VIEW_STORAGE_HPP
43#define STOKHOS_VIEW_STORAGE_HPP
44
46
47#include "Kokkos_Macros.hpp"
48#include "impl/Kokkos_Traits.hpp"
49
50#include "Sacado_Traits.hpp"
52#include <sstream>
53
54namespace Stokhos {
55
58
60 template < typename ordinal_t ,
61 typename value_t ,
62 unsigned static_length ,
63 unsigned static_stride ,
64 typename device_t >
66 private:
67 // Enumerated flag so logic is evaluated at compile-time
68 enum { stride_one = 1 == static_stride };
69 public:
70
71 static const bool is_static = static_length != 0 ;
72 static const int static_size = static_length ;
73 static const bool supports_reset = false ;
74 static const bool supports_resize = ! is_static ;
75 static const bool supports_view = true ;
76
77 typedef ordinal_t ordinal_type;
78 typedef value_t value_type;
79 typedef device_t execution_space;
81 typedef const value_type & const_reference;
83 typedef const value_type * const_pointer;
84 // typedef Stokhos::DynArrayTraits<value_type,execution_space> ds;
85
87 template <typename ord_t, typename val_t = value_t , typename dev_t = device_t >
88 struct apply {
89 typedef ViewStorage<ord_t,val_t,static_length,static_stride,dev_t> type;
90 };
91
93 KOKKOS_INLINE_FUNCTION
96 const value_type& x = value_type(0) );
97
99 KOKKOS_INLINE_FUNCTION
101 const ordinal_type & arg_size = 0 ,
102 const ordinal_type & arg_stride = 0 ) :
103 coeff_(v), size_(arg_size), stride_(arg_stride) {}
104
106 KOKKOS_INLINE_FUNCTION
107 ViewStorage(const ordinal_type& sz, const value_type* x) {}
108
110 KOKKOS_INLINE_FUNCTION
111 ViewStorage(const ordinal_type& sz, pointer v, bool owned) :
112 coeff_(v), size_(sz) {}
113
115 KOKKOS_INLINE_FUNCTION
118
119 KOKKOS_INLINE_FUNCTION
120 ViewStorage( const ViewStorage& s , const ordinal_type & arg_begin , const ordinal_type & arg_end )
121 : coeff_( ( ordinal_type(size_.value) < arg_end ) ? pointer(0) : s.coeff_ + arg_begin * ordinal_type(s.stride_.value) )
122 , size_( ( ordinal_type(size_.value) < arg_end ) ? ordinal_type(0) : arg_end - arg_begin )
123 , stride_( s.stride_ )
124 {}
125
127 KOKKOS_INLINE_FUNCTION
129
130private:
132 KOKKOS_INLINE_FUNCTION
134public:
135
137 KOKKOS_INLINE_FUNCTION
139 if ( stride_one ) {
140 for ( ordinal_type i = 0 ; i < size_.value ; ++i ) { coeff_[i] = v ; }
141 }
142 else {
143 for ( ordinal_type i = 0 ; i < size_.value * stride_.value ; i += stride_.value ) {
144 coeff_[i] = v ;
145 }
146 }
147 }
148
150 KOKKOS_INLINE_FUNCTION
151 void init(const_pointer v, const ordinal_type& sz = 0) {
152 const ordinal_type n = sz ? sz : size_.value ;
153 if ( stride_one ) {
154 for ( ordinal_type i = 0 ; i < n ; ++i ) { coeff_[i] = v[i] ; }
155 }
156 else {
157 for ( ordinal_type i = 0 ; i < n ; ++i ) { coeff_[i*stride_.value] = v[i] ; }
158 }
159 }
160
162 KOKKOS_INLINE_FUNCTION
163 void load(pointer v) {
164 if ( stride_one ) {
165 for ( ordinal_type i = 0 ; i < size_.value ; ++i ) { coeff_[i] = v[i] ; }
166 }
167 else {
168 for ( ordinal_type i = 0 ; i < size_.value ; ++i ) { coeff_[i*stride_.value] = v[i] ; }
169 }
170 }
171
173 KOKKOS_INLINE_FUNCTION
174 void resize( ordinal_type s ) {}
175 //void resize( const error_storage_type_is_not_resizeable & );
176
178 KOKKOS_INLINE_FUNCTION
182 const bool owned );
183
185 KOKKOS_INLINE_FUNCTION
186 ordinal_type size() const { return size_.value ; }
187
189 KOKKOS_INLINE_FUNCTION
191 {
192 return coeff_[ stride_one ? i : i * stride_.value ];
193 }
194
195 template <int i>
196 KOKKOS_INLINE_FUNCTION
197 reference getCoeff() { return coeff_[ stride_one ? i : i * stride_.value ]; }
198
199 template <int i>
200 KOKKOS_INLINE_FUNCTION
201 const_reference getCoeff() const { return coeff_[ stride_one ? i : i * stride_.value ]; }
202
204 KOKKOS_INLINE_FUNCTION
205 const_pointer coeff() const { return coeff_; }
206
208 KOKKOS_INLINE_FUNCTION
209 pointer coeff() { return coeff_; }
210
211 private:
212
215
217 const Kokkos::Impl::integral_nonzero_constant< ordinal_t , static_length > size_ ;
218
220 const Kokkos::Impl::integral_nonzero_constant< ordinal_t , static_stride > stride_ ;
221 };
222
223 template< class Storage >
224 struct is_ViewStorage { enum { value = false }; };
225
226 template < typename ordinal_t ,
227 typename value_t ,
228 unsigned static_length ,
229 unsigned static_stride ,
230 typename device_t >
231 struct is_ViewStorage< ViewStorage< ordinal_t , value_t , static_length , static_stride , device_t > >
232 { enum { value = true }; };
233
234
235} /* namespace Stokhos */
236
237namespace Sacado {
238 template <typename ordinal_t, typename value_t, unsigned static_length,
239 unsigned static_stride, typename device_t>
240 struct StringName< Stokhos::ViewStorage<ordinal_t,
241 value_t,
242 static_length,
243 static_stride,
244 device_t> > {
245 static std::string eval() {
246 std::stringstream ss;
247 ss << "Stokhos::ViewStorage<"
248 << StringName<ordinal_t>::eval() << ","
249 << StringName<value_t>::eval() << ","
250 << static_length << ","
251 << static_stride << ","
252 << StringName<device_t>::eval() << ">";
253 return ss.str();
254 }
255 };
256}
257
258#endif /* #ifndef STOKHOS_VIEW_STORAGE_HPP */
Dynamic storage with view semantics and contiguous access.
KOKKOS_INLINE_FUNCTION ~ViewStorage()
Destructor.
KOKKOS_INLINE_FUNCTION void load(pointer v)
Load values to an array of values.
KOKKOS_INLINE_FUNCTION reference getCoeff()
KOKKOS_INLINE_FUNCTION pointer coeff()
Get coefficients.
const value_type & const_reference
static const bool supports_view
const Kokkos::Impl::integral_nonzero_constant< ordinal_t, static_length > size_
Size of array.
static const bool supports_reset
const pointer coeff_
Coefficient values.
KOKKOS_INLINE_FUNCTION void resize(ordinal_type s)
Resize function disabled.
KOKKOS_INLINE_FUNCTION reference operator[](const ordinal_type &i) const
Coefficient access (avoid if possible)
static const bool is_static
KOKKOS_INLINE_FUNCTION const_reference getCoeff() const
KOKKOS_INLINE_FUNCTION const_pointer coeff() const
Get coefficients.
KOKKOS_INLINE_FUNCTION ViewStorage(const ViewStorage &s)
Constructor.
const value_type * const_pointer
KOKKOS_INLINE_FUNCTION void init(const_pointer v, const ordinal_type &sz=0)
Initialize values to an array of values.
KOKKOS_INLINE_FUNCTION ViewStorage(pointer v, const ordinal_type &arg_size=0, const ordinal_type &arg_stride=0)
Constructor.
KOKKOS_INLINE_FUNCTION void shallowReset(pointer v, const error_storage_type_is_not_resizeable &, const error_storage_type_is_not_resizeable &, const bool owned)
Reset function disabled.
KOKKOS_INLINE_FUNCTION void init(const_reference v)
Initialize values to a constant value.
KOKKOS_INLINE_FUNCTION ViewStorage(const error_storage_type_is_not_allocateable &z=error_storage_type_is_not_allocateable(), const value_type &x=value_type(0))
Constructor to satisfy Sacado::MP::Vector, disabled via error type.
KOKKOS_INLINE_FUNCTION ordinal_type size() const
Return size.
const Kokkos::Impl::integral_nonzero_constant< ordinal_t, static_stride > stride_
Stride of array.
KOKKOS_INLINE_FUNCTION ViewStorage(const ordinal_type &sz, pointer v, bool owned)
Constructor for creating a view.
KOKKOS_INLINE_FUNCTION ViewStorage(const ordinal_type &sz, const value_type *x)
Constructor from array.
KOKKOS_INLINE_FUNCTION ViewStorage & operator=(const ViewStorage &)
Assignment operator.
KOKKOS_INLINE_FUNCTION ViewStorage(const ViewStorage &s, const ordinal_type &arg_begin, const ordinal_type &arg_end)
static const bool supports_resize
Top-level namespace for Stokhos classes and functions.
Turn ViewStorage into a meta-function class usable with mpl::apply.
ViewStorage< ord_t, val_t, static_length, static_stride, dev_t > type