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 //
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 //#include "Kokkos_View.hpp"
49 
50 #include "Stokhos_ProductBasis.hpp"
52 
53 namespace Stokhos {
54 
55 //----------------------------------------------------------------------------
56 
78 template< typename ValueType , typename TensorType, class Device >
80 public:
81 
82  typedef Device execution_space ;
83  typedef ValueType value_type ;
84  typedef TensorType tensor_type ;
85  typedef typename tensor_type::size_type size_type ;
86 
87 private:
88 
90  Kokkos::View< size_type** , execution_space > m_degree_map ;
92 
93 public:
94 
95  inline
97 
98  inline
100  : m_tensor()
101  , m_degree_map()
102  , m_variable(0)
103  {}
104 
105  inline
107  : m_tensor( rhs.m_tensor )
108  , m_degree_map( rhs.m_degree_map )
109  , m_variable( rhs.m_variable )
110  {}
111 
112  inline
114  {
115  m_tensor = rhs.m_tensor ;
116  m_degree_map = rhs.m_degree_map ;
117  m_variable = rhs.m_variable ;
118  return *this ;
119  }
120 
121  KOKKOS_INLINE_FUNCTION
122  const tensor_type & tensor() const { return m_tensor ; }
123 
127  KOKKOS_INLINE_FUNCTION
128  size_type dimension() const { return m_tensor.dimension(); }
129 
132  KOKKOS_INLINE_FUNCTION
134  const bool is_cuda =
135 #if defined( KOKKOS_ENABLE_CUDA )
136  Kokkos::Impl::is_same<execution_space,Kokkos::Cuda>::value;
137 #else
138  false ;
139 #endif
140  const size_type AlignBytes = is_cuda ? 128 : 64;
141  const size_type NumAlign = AlignBytes/sizeof(value_type);
142  return (dimension() + NumAlign-1) & ~(NumAlign-1);
143  }
144 
146  KOKKOS_INLINE_FUNCTION
147  size_type variable_count() const { return m_variable ; }
148 
150  template< typename iType >
151  KOKKOS_INLINE_FUNCTION
152  size_type variable_degree( const iType & iVariable ) const
153  { return m_degree_map( 0 , iVariable ); }
154 
159  template< typename iType , typename jType >
160  KOKKOS_INLINE_FUNCTION
161  size_type bases_degree( const iType & iBasis , const jType & iVariable ) const
162  { return m_degree_map( iBasis + 1 , iVariable ); }
163 
164  void print( std::ostream & s ) const
165  {
166  for ( unsigned i = 1 ; i < m_degree_map.extent(0) ; ++i ) {
167  s << " bases[" << i - 1 << "] (" ;
168  for ( unsigned j = 0 ; j < m_degree_map.extent(1) ; ++j ) {
169  s << " " << m_degree_map(i,j);
170  }
171  s << " )" << std::endl ;
172  }
173  }
174 
175  template <typename OrdinalType, typename CijkType>
178  const CijkType& Cijk,
180  {
182 
183  // Allocate and transfer data to the device-resident object.
184 
185  typedef Kokkos::View< size_type** , execution_space > int_array_type ;
186  typedef typename int_array_type::HostMirror host_int_array_type ;
187 
188  OrdinalType basis_sz = basis.size();
189  OrdinalType basis_dim = basis.dimension();
190  Stokhos::MultiIndex<OrdinalType> max_orders = basis.getMaxOrders();
191 
192  spt.m_degree_map =
193  int_array_type( "stochastic_tensor_degree_map" ,
194  basis_sz + 1 ,
195  basis_dim );
196 
197  spt.m_variable = basis_dim ;
198 
199  // Build degree_map
200  host_int_array_type degree_map =
202  for ( OrdinalType j = 0 ; j < basis_dim ; ++j )
203  degree_map(0,j) = max_orders[j];
204  for ( OrdinalType i = 0 ; i < basis_sz ; ++i ) {
205  const Stokhos::MultiIndex<OrdinalType>& term = basis.term(i);
206  for ( OrdinalType j = 0 ; j < basis_dim ; ++j ) {
207  degree_map(i+1,j) = term[j];
208  }
209  }
210  Kokkos::deep_copy( spt.m_degree_map , degree_map );
211 
212  // Build 3 tensor
213  spt.m_tensor = tensor_type::create( basis, Cijk, params );
214 
215  return spt ;
216  }
217 };
218 
219 template< typename TensorType, typename OrdinalType , typename ValueType, typename CijkType >
220 StochasticProductTensor<ValueType, TensorType, typename TensorType::execution_space>
223  const CijkType& Cijk,
225 {
226  typedef typename TensorType::execution_space Device;
228  basis, Cijk, params);
229 }
230 
231 template < typename ValueType , typename Device, class TensorType >
232 class BlockMultiply< StochasticProductTensor< ValueType, TensorType, Device > >
233 {
234 public:
235  typedef Device execution_space ;
236  typedef typename execution_space::size_type size_type ;
238 
239  template< typename MatrixValue , typename VectorValue >
240  KOKKOS_INLINE_FUNCTION
241  static void apply( const block_type & block ,
242  const MatrixValue * a ,
243  const VectorValue * const x ,
244  VectorValue * const y )
245  {
246  typedef BlockMultiply< typename block_type::tensor_type > tensor_multiply ;
247 
248  tensor_multiply::apply( block.tensor() , a , x , y );
249  }
250 };
251 
252 //----------------------------------------------------------------------------
253 //----------------------------------------------------------------------------
254 
255 
256 
257 } // namespace Stokhos
258 
259 #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)