Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_StochasticProductTensor.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef STOKHOS_STOCHASTICPRODUCTTENSOR_HPP
11 #define STOKHOS_STOCHASTICPRODUCTTENSOR_HPP
12 
13 #include <ostream>
14 
15 #include "Kokkos_Core.hpp"
16 
17 #include "Stokhos_ProductBasis.hpp"
19 
20 namespace Stokhos {
21 
22 //----------------------------------------------------------------------------
23 
45 template< typename ValueType , typename TensorType, class Device >
47 public:
48 
49  typedef Device execution_space ;
50  typedef ValueType value_type ;
51  typedef TensorType tensor_type ;
52  typedef typename tensor_type::size_type size_type ;
53 
54 private:
55 
57  Kokkos::View< size_type** , execution_space > m_degree_map ;
59 
60 public:
61 
62  inline
64 
65  inline
67  : m_tensor()
68  , m_degree_map()
69  , m_variable(0)
70  {}
71 
72  inline
74  : m_tensor( rhs.m_tensor )
75  , m_degree_map( rhs.m_degree_map )
76  , m_variable( rhs.m_variable )
77  {}
78 
79  inline
81  {
82  m_tensor = rhs.m_tensor ;
84  m_variable = rhs.m_variable ;
85  return *this ;
86  }
87 
88  KOKKOS_INLINE_FUNCTION
89  const tensor_type & tensor() const { return m_tensor ; }
90 
94  KOKKOS_INLINE_FUNCTION
95  size_type dimension() const { return m_tensor.dimension(); }
96 
99  KOKKOS_INLINE_FUNCTION
101  const bool is_cuda =
102 #if defined( KOKKOS_ENABLE_CUDA )
103  std::is_same<execution_space,Kokkos::Cuda>::value;
104 #else
105  false ;
106 #endif
107  const size_type AlignBytes = is_cuda ? 128 : 64;
108  const size_type NumAlign = AlignBytes/sizeof(value_type);
109  return (dimension() + NumAlign-1) & ~(NumAlign-1);
110  }
111 
113  KOKKOS_INLINE_FUNCTION
114  size_type variable_count() const { return m_variable ; }
115 
117  template< typename iType >
118  KOKKOS_INLINE_FUNCTION
119  size_type variable_degree( const iType & iVariable ) const
120  { return m_degree_map( 0 , iVariable ); }
121 
126  template< typename iType , typename jType >
127  KOKKOS_INLINE_FUNCTION
128  size_type bases_degree( const iType & iBasis , const jType & iVariable ) const
129  { return m_degree_map( iBasis + 1 , iVariable ); }
130 
131  void print( std::ostream & s ) const
132  {
133  for ( unsigned i = 1 ; i < m_degree_map.extent(0) ; ++i ) {
134  s << " bases[" << i - 1 << "] (" ;
135  for ( unsigned j = 0 ; j < m_degree_map.extent(1) ; ++j ) {
136  s << " " << m_degree_map(i,j);
137  }
138  s << " )" << std::endl ;
139  }
140  }
141 
142  template <typename OrdinalType, typename CijkType>
145  const CijkType& Cijk,
147  {
149 
150  // Allocate and transfer data to the device-resident object.
151 
152  typedef Kokkos::View< size_type** , execution_space > int_array_type ;
153  typedef typename int_array_type::HostMirror host_int_array_type ;
154 
155  OrdinalType basis_sz = basis.size();
156  OrdinalType basis_dim = basis.dimension();
157  Stokhos::MultiIndex<OrdinalType> max_orders = basis.getMaxOrders();
158 
159  spt.m_degree_map =
160  int_array_type( "stochastic_tensor_degree_map" ,
161  basis_sz + 1 ,
162  basis_dim );
163 
164  spt.m_variable = basis_dim ;
165 
166  // Build degree_map
167  host_int_array_type degree_map =
169  for ( OrdinalType j = 0 ; j < basis_dim ; ++j )
170  degree_map(0,j) = max_orders[j];
171  for ( OrdinalType i = 0 ; i < basis_sz ; ++i ) {
172  const Stokhos::MultiIndex<OrdinalType>& term = basis.term(i);
173  for ( OrdinalType j = 0 ; j < basis_dim ; ++j ) {
174  degree_map(i+1,j) = term[j];
175  }
176  }
177  Kokkos::deep_copy( spt.m_degree_map , degree_map );
178 
179  // Build 3 tensor
180  spt.m_tensor = tensor_type::create( basis, Cijk, params );
181 
182  return spt ;
183  }
184 };
185 
186 template< typename TensorType, typename OrdinalType , typename ValueType, typename CijkType >
187 StochasticProductTensor<ValueType, TensorType, typename TensorType::execution_space>
190  const CijkType& Cijk,
192 {
193  typedef typename TensorType::execution_space Device;
195  basis, Cijk, params);
196 }
197 
198 template < typename ValueType , typename Device, class TensorType >
199 class BlockMultiply< StochasticProductTensor< ValueType, TensorType, Device > >
200 {
201 public:
202  typedef Device execution_space ;
203  typedef typename execution_space::size_type size_type ;
205 
206  template< typename MatrixValue , typename VectorValue >
207  KOKKOS_INLINE_FUNCTION
208  static void apply( const block_type & block ,
209  const MatrixValue * a ,
210  const VectorValue * const x ,
211  VectorValue * const y )
212  {
213  typedef BlockMultiply< typename block_type::tensor_type > tensor_multiply ;
214 
215  tensor_multiply::apply( block.tensor() , a , x , y );
216  }
217 };
218 
219 //----------------------------------------------------------------------------
220 //----------------------------------------------------------------------------
221 
222 
223 
224 } // namespace Stokhos
225 
226 #endif /* #ifndef STOKHOS_STOCHASTICPRODUCTTENSOR_HPP */
KOKKOS_INLINE_FUNCTION const tensor_type & tensor() const
StochasticProductTensor & operator=(const StochasticProductTensor &rhs)
KOKKOS_INLINE_FUNCTION size_type aligned_dimension() const
Aligned dimension: length of the vector block properly aligned.
Bases defined by combinatorial product of polynomial bases.
static KOKKOS_INLINE_FUNCTION void apply(const block_type &block, const MatrixValue *a, const VectorValue *const x, VectorValue *const y)
Kokkos::DefaultExecutionSpace execution_space
static StochasticProductTensor create(const Stokhos::ProductBasis< OrdinalType, ValueType > &basis, const CijkType &Cijk, const Teuchos::ParameterList &params=Teuchos::ParameterList())
virtual ordinal_type dimension() const =0
Return dimension of basis.
A multidimensional index.
virtual const MultiIndex< ordinal_type > & term(ordinal_type i) const =0
Get orders of each coordinate polynomial given an index i.
KOKKOS_INLINE_FUNCTION size_type variable_count() const
How many variables are being expanded.
StochasticProductTensor(const StochasticProductTensor &rhs)
StochasticProductTensor< ValueType, TensorType, typename TensorType::execution_space > create_stochastic_product_tensor(const Stokhos::ProductBasis< OrdinalType, ValueType > &basis, const CijkType &Cijk, const Teuchos::ParameterList &params=Teuchos::ParameterList())
KOKKOS_INLINE_FUNCTION size_type bases_degree(const iType &iBasis, const jType &iVariable) const
Basis function &#39;iBasis&#39; is the product of &#39;variable_count()&#39; polynomials. Return the polynomial degre...
void deep_copy(const Stokhos::CrsMatrix< ValueType, DstDevice, Layout > &dst, const Stokhos::CrsMatrix< ValueType, SrcDevice, Layout > &src)
KOKKOS_INLINE_FUNCTION size_type dimension() const
Dimension: number of bases and length of the vector block (and tensor).
KOKKOS_INLINE_FUNCTION size_type variable_degree(const iType &iVariable) const
Polynomial degree of a given variable.
Kokkos::View< size_type **, execution_space > m_degree_map
virtual ordinal_type size() const =0
Return total size of basis.
virtual MultiIndex< ordinal_type > getMaxOrders() const =0
Return maximum order allowable for each coordinate basis.
Stokhos::CrsMatrix< ValueType, Device, Layout >::HostMirror create_mirror_view(const Stokhos::CrsMatrix< ValueType, Device, Layout > &A)