Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
Stokhos_StochasticProductTensor.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_STOCHASTICPRODUCTTENSOR_HPP
43 #define STOKHOS_STOCHASTICPRODUCTTENSOR_HPP
44 
45 #include <ostream>
46 
47 #include "Kokkos_Core.hpp"
48 
49 #include "Stokhos_ProductBasis.hpp"
50 #include "Teuchos_ParameterList.hpp"
51 
52 namespace Stokhos {
53 
54 //----------------------------------------------------------------------------
55 
77 template< typename ValueType , typename TensorType, class Device >
79 public:
80 
81  typedef Device execution_space ;
82  typedef ValueType value_type ;
83  typedef TensorType tensor_type ;
84  typedef typename tensor_type::size_type size_type ;
85 
86 private:
87 
89  Kokkos::View< size_type** , execution_space > m_degree_map ;
91 
92 public:
93 
94  inline
96 
97  inline
99  : m_tensor()
100  , m_degree_map()
101  , m_variable(0)
102  {}
103 
104  inline
106  : m_tensor( rhs.m_tensor )
107  , m_degree_map( rhs.m_degree_map )
108  , m_variable( rhs.m_variable )
109  {}
110 
111  inline
113  {
114  m_tensor = rhs.m_tensor ;
115  m_degree_map = rhs.m_degree_map ;
116  m_variable = rhs.m_variable ;
117  return *this ;
118  }
119 
120  KOKKOS_INLINE_FUNCTION
121  const tensor_type & tensor() const { return m_tensor ; }
122 
126  KOKKOS_INLINE_FUNCTION
127  size_type dimension() const { return m_tensor.dimension(); }
128 
131  KOKKOS_INLINE_FUNCTION
133  const bool is_cuda =
134 #if defined( KOKKOS_HAVE_CUDA )
135  Kokkos::Impl::is_same<execution_space,Kokkos::Cuda>::value;
136 #else
137  false ;
138 #endif
139  const size_type AlignBytes = is_cuda ? 128 : 64;
140  const size_type NumAlign = AlignBytes/sizeof(value_type);
141  return (dimension() + NumAlign-1) & ~(NumAlign-1);
142  }
143 
145  KOKKOS_INLINE_FUNCTION
146  size_type variable_count() const { return m_variable ; }
147 
149  template< typename iType >
150  KOKKOS_INLINE_FUNCTION
151  size_type variable_degree( const iType & iVariable ) const
152  { return m_degree_map( 0 , iVariable ); }
153 
158  template< typename iType , typename jType >
159  KOKKOS_INLINE_FUNCTION
160  size_type bases_degree( const iType & iBasis , const jType & iVariable ) const
161  { return m_degree_map( iBasis + 1 , iVariable ); }
162 
163  void print( std::ostream & s ) const
164  {
165  for ( unsigned i = 1 ; i < m_degree_map.dimension_0() ; ++i ) {
166  s << " bases[" << i - 1 << "] (" ;
167  for ( unsigned j = 0 ; j < m_degree_map.dimension_1() ; ++j ) {
168  s << " " << m_degree_map(i,j);
169  }
170  s << " )" << std::endl ;
171  }
172  }
173 
174  template <typename OrdinalType, typename CijkType>
177  const CijkType& Cijk,
178  const Teuchos::ParameterList& params = Teuchos::ParameterList())
179  {
181 
182  // Allocate and transfer data to the device-resident object.
183 
184  typedef Kokkos::View< size_type** , execution_space > int_array_type ;
185  typedef typename int_array_type::HostMirror host_int_array_type ;
186 
187  OrdinalType basis_sz = basis.size();
188  OrdinalType basis_dim = basis.dimension();
189  Stokhos::MultiIndex<OrdinalType> max_orders = basis.getMaxOrders();
190 
191  spt.m_degree_map =
192  int_array_type( "stochastic_tensor_degree_map" ,
193  basis_sz + 1 ,
194  basis_dim );
195 
196  spt.m_variable = basis_dim ;
197 
198  // Build degree_map
199  host_int_array_type degree_map =
201  for ( OrdinalType j = 0 ; j < basis_dim ; ++j )
202  degree_map(0,j) = max_orders[j];
203  for ( OrdinalType i = 0 ; i < basis_sz ; ++i ) {
204  const Stokhos::MultiIndex<OrdinalType>& term = basis.term(i);
205  for ( OrdinalType j = 0 ; j < basis_dim ; ++j ) {
206  degree_map(i+1,j) = term[j];
207  }
208  }
209  Kokkos::deep_copy( spt.m_degree_map , degree_map );
210 
211  // Build 3 tensor
212  spt.m_tensor = tensor_type::create( basis, Cijk, params );
213 
214  return spt ;
215  }
216 };
217 
218 template< typename TensorType, typename OrdinalType , typename ValueType, typename CijkType >
219 StochasticProductTensor<ValueType, TensorType, typename TensorType::execution_space>
222  const CijkType& Cijk,
223  const Teuchos::ParameterList& params = Teuchos::ParameterList())
224 {
225  typedef typename TensorType::execution_space Device;
227  basis, Cijk, params);
228 }
229 
230 template < typename ValueType , typename Device, class TensorType >
231 class BlockMultiply< StochasticProductTensor< ValueType, TensorType, Device > >
232 {
233 public:
234  typedef Device execution_space ;
235  typedef typename execution_space::size_type size_type ;
237 
238  template< typename MatrixValue , typename VectorValue >
239  KOKKOS_INLINE_FUNCTION
240  static void apply( const block_type & block ,
241  const MatrixValue * a ,
242  const VectorValue * const x ,
243  VectorValue * const y )
244  {
245  typedef BlockMultiply< typename block_type::tensor_type > tensor_multiply ;
246 
247  tensor_multiply::apply( block.tensor() , a , x , y );
248  }
249 };
250 
251 //----------------------------------------------------------------------------
252 //----------------------------------------------------------------------------
253 
254 
255 
256 } // namespace Stokhos
257 
258 #endif /* #ifndef STOKHOS_STOCHASTICPRODUCTTENSOR_HPP */
StochasticProductTensor & operator=(const StochasticProductTensor &rhs)
Bases defined by combinatorial product of polynomial bases.
KOKKOS_INLINE_FUNCTION size_type aligned_dimension() const
Aligned dimension: length of the vector block properly aligned.
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.
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...
A multidimensional index.
virtual const MultiIndex< ordinal_type > & term(ordinal_type i) const =0
Get orders of each coordinate polynomial given an index i.
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Definition: csr_vector.h:260
StochasticProductTensor(const StochasticProductTensor &rhs)
Top-level namespace for Stokhos classes and functions.
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())
void deep_copy(const Stokhos::CrsMatrix< ValueType, DstDevice, Layout > &dst, const Stokhos::CrsMatrix< ValueType, SrcDevice, Layout > &src)
Abstract base class for multivariate orthogonal polynomials generated from tensor products of univari...
expr expr expr expr j
KOKKOS_INLINE_FUNCTION const tensor_type & tensor() const
KOKKOS_INLINE_FUNCTION size_type variable_degree(const iType &iVariable) const
Polynomial degree of a given variable.
KOKKOS_INLINE_FUNCTION size_type variable_count() const
How many variables are being expanded.
Kokkos::View< size_type **, execution_space > m_degree_map
KOKKOS_INLINE_FUNCTION size_type dimension() const
Dimension: number of bases and length of the vector block (and tensor).
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType ValueType * y
Definition: csr_vector.h:267
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)