tpmv.hpp
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  * \brief Implements the triangular packed matrix-vector multiplication
5  *
6  * \author O. Krause
7  * \date 2010
8  *
9  *
10  * \par Copyright 1995-2015 Shark Development Team
11  *
12  * <BR><HR>
13  * This file is part of Shark.
14  * <http://image.diku.dk/shark/>
15  *
16  * Shark is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Lesser General Public License as published
18  * by the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * Shark is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public License
27  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 //===========================================================================
31 #ifndef REMORA_KERNELS_DEFAULT_tpmv_HPP
32 #define REMORA_KERNELS_DEFAULT_tpmv_HPP
33 
34 #include "../../expression_types.hpp" //matrix/vector_expression
35 #include "../../detail/traits.hpp" //orientations and triangular types
36 #include <type_traits> //std::false_type marker for unoptimized
37 
38 
39 namespace remora{ namespace bindings{
40 
41 //Lower triangular(row-major) - vector product
42 // computes the row-wise inner product of the matrix
43 // starting with row 0 and stores the result in b(i)
44 //this does not interfere with the next products as
45 // the element b(i) is not needed for iterations j > i
46 template<class MatA, class V>
47 void tpmv_impl(
48  matrix_expression<MatA, cpu_tag> const& A,
49  vector_expression<V, cpu_tag>& b,
50  row_major,
51  upper
52 ){
53  typedef typename V::value_type value_type;
54  typedef typename V::size_type size_type;
55  typedef typename MatA::const_row_iterator row_iterator;
56  size_type size = A().size1();
57 
58  for(size_type i = 0; i != size; ++i){
59  value_type sum(0);
60  row_iterator end = A().row_end(i);
61  for(row_iterator pos = A().row_begin(i); pos != end; ++pos){
62  sum += *pos * b()(pos.index());
63  }
64  b()(i) = sum;
65  }
66 }
67 
68 //Upper triangular(row-major) - vector product
69 // computes the row-wise inner product of the matrix
70 // starting with the last row and stores the result in b(i)
71 // this does not interfere with the next products as
72 // the element b(i) is not needed for row products j < i
73 template<class MatA, class V>
74 void tpmv_impl(
75  matrix_expression<MatA, cpu_tag> const& A,
76  vector_expression<V, cpu_tag>& b,
77  row_major,
78  lower
79 ){
80  typedef typename V::value_type value_type;
81  typedef typename V::size_type size_type;
82  typedef typename MatA::const_row_iterator row_iterator;
83  size_type size = A().size1();
84 
85  for(size_type irev = size; irev != 0; --irev){
86  size_type i= irev-1;
87  value_type sum(0);
88  row_iterator end = A().row_end(i);
89  for(row_iterator pos = A().row_begin(i); pos != end; ++pos){
90  sum += *pos * b()(pos.index());
91  }
92  b()(i) = sum;
93  }
94 }
95 
96 //Upper triangular(column-major) - vector product
97 //computes the product as a series of vector-additions
98 //on b starting with the last column of A.
99 template<class MatA, class V>
100 void tpmv_impl(
101  matrix_expression<MatA, cpu_tag> const& A,
102  vector_expression<V, cpu_tag>& b,
103  column_major,
104  upper
105 ){
106  typedef typename MatA::const_column_iterator column_iterator;
107  typedef typename V::size_type size_type;
108  typedef typename V::value_type value_type;
109  size_type size = A().size1();
110  for(size_type i = 0; i != size; ++i){
111  value_type bi = b()(i);
112  b()(i)= value_type/*zero*/();
113  column_iterator end = A().column_end(i);
114  for(column_iterator pos = A().column_begin(i); pos != end; ++pos){
115  b()(pos.index()) += *pos*bi;
116  }
117  }
118 
119 }
120 
121 //Lower triangular(column-major) - vector product
122 // computes the product as a series of vector-additions
123 // on b starting with the first column of A.
124 template<class MatA, class V>
125 void tpmv_impl(
126  matrix_expression<MatA, cpu_tag> const& A,
127  vector_expression<V, cpu_tag>& b,
128  column_major,
129  lower
130 ){
131  typedef typename MatA::const_column_iterator column_iterator;
132  typedef typename V::size_type size_type;
133  typedef typename V::value_type value_type;
134  size_type size = A().size1();
135 
136  for(size_type irev = size; irev != 0; --irev){
137  size_type i= irev-1;
138  value_type bi = b()(i);
139  b()(i)= value_type/*zero*/();
140  column_iterator end = A().column_end(i);
141  for(column_iterator pos = A().column_begin(i); pos != end; ++pos){
142  b()(pos.index()) += *pos*bi;
143  }
144  }
145 }
146 
147 //dispatcher
148 template <typename MatA, typename V>
149 void tpmv(
150  matrix_expression<MatA, cpu_tag> const& A,
151  vector_expression<V, cpu_tag>& b,
152  std::false_type//unoptimized
153 ){
154  REMORA_SIZE_CHECK(A().size1() == A().size2());
155  REMORA_SIZE_CHECK(A().size2() == b().size());
156  tpmv_impl(
157  A, b,
158  typename MatA::orientation::orientation(),
159  typename MatA::orientation::triangular_type()
160  );
161 }
162 
163 }}
164 #endif