Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SymmetricDiagonalSpec.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_SYMMETRIC_DIAGONAL_SPEC_HPP
43 #define STOKHOS_SYMMETRIC_DIAGONAL_SPEC_HPP
44 
45 #include "Kokkos_StaticCrsGraph.hpp"
46 
47 namespace Stokhos {
48 
62 template< class ExecutionSpace >
64 public:
65 
66  typedef unsigned size_type;
67 
69  KOKKOS_INLINE_FUNCTION
70  unsigned dimension() const { return m_dimension ; }
71 
73  KOKKOS_INLINE_FUNCTION
74  unsigned matrix_offset( const unsigned row , const unsigned column ) const
75  {
76  const int diag_count = 1 + ( m_dimension >> 1 );
77  const int diag = (int) column - (int) row ;
78 
79  unsigned offset = 0 ;
80 
81  if ( ( 0 <= diag && diag < diag_count ) || ( diag <= - diag_count ) ) {
82  offset = row + m_dimension * ( ( m_dimension + diag ) % m_dimension );
83  }
84  else {
85  offset = column + m_dimension * ( ( m_dimension - diag ) % m_dimension );
86  }
87 
88  return offset ;
89  }
90 
92  KOKKOS_INLINE_FUNCTION
93  unsigned matrix_size() const
94  { return ( m_dimension * ( m_dimension + 1 ) ) >> 1 ; }
95 
97  : m_dimension( 0 ) {}
98 
100  : m_dimension( rhs.m_dimension ) {}
101 
102  SymmetricDiagonalSpec & operator =
103  ( const SymmetricDiagonalSpec & rhs )
104  { m_dimension = rhs.m_dimension ; return *this ; }
105 
106  explicit
107  SymmetricDiagonalSpec( const unsigned dim )
108  : m_dimension( dim ) {}
109 
110 private:
111  unsigned m_dimension ;
112 };
113 
114 template < typename Device >
116 public:
117  typedef Device execution_space ;
118  typedef typename execution_space::size_type size_type ;
120 
121  template< typename MatrixValue , typename VectorValue >
122  KOKKOS_INLINE_FUNCTION
123  static void apply( const block_type & block ,
124  const MatrixValue * a ,
125  const VectorValue * const x ,
126  VectorValue * const y )
127  {
128  const size_type dimension = block.dimension();
129  const size_type dim_half = ( dimension + 1 ) >> 1 ;
130 
131  // Multiply the main diagonal (first diagonal)
132  for ( size_type j = 0 ; j < dimension ; ++j ) {
133  y[j] += a[j] * x[j] ; // Contiguous access
134  }
135 
136  // Multiply remaining full diagionals, each diagonal is accessed twice
137  for ( size_type d = 1 ; d < dim_half ; ++d ) {
138  size_type kx = d ;
139  size_type kxr = dimension - d ;
140 
141  a += dimension ; // next diagonal
142 
143  for ( size_type j = 0 ; j < dimension ; ++j ) {
144  y[j] += a[j] * x[kx] + a[kxr] * x[kxr]; // Contiguous access
145  if ( dimension == ++kx ) kx = 0 ;
146  if ( dimension == ++kxr ) kxr = 0 ;
147  }
148  }
149 
150  // If even number of diagonals then the last diagonal is half-length
151  if ( ! ( dimension & 01 ) ) {
152  size_type kx = dim_half ;
153 
154  a += dimension ; // next diagonal
155 
156  for ( size_type j = 0 ; j < dim_half ; ++j , ++kx ) {
157  y[j] += a[j] * x[kx] ; // Contiguous access
158  y[kx] += a[j] * x[j] ; // Contiguous access
159  }
160  }
161  }
162 
163  KOKKOS_INLINE_FUNCTION
164  static size_type matrix_size( const block_type & block )
165  { return block.matrix_size(); }
166 };
167 
168 } // namespace Stokhos
169 
170 #endif /* #ifndef STOKHOS_SYMMETRIC_DIAGONAL_SPEC_HPP */
static KOKKOS_INLINE_FUNCTION size_type matrix_size(const block_type &block)
static KOKKOS_INLINE_FUNCTION void apply(const block_type &block, const MatrixValue *a, const VectorValue *const x, VectorValue *const y)
KOKKOS_INLINE_FUNCTION unsigned matrix_size() const
Storage size for block coefficients.
KOKKOS_INLINE_FUNCTION unsigned matrix_offset(const unsigned row, const unsigned column) const
Storage location for the (row,column) entry.
SymmetricDiagonalSpec(const SymmetricDiagonalSpec &rhs)
Symmetric diagonal storage for a dense matrix.
KOKKOS_INLINE_FUNCTION unsigned dimension() const
Dimension of vector block.