gemv.hpp
Go to the documentation of this file.
1/*!
2 *
3 *
4 * \brief -
5 *
6 * \author O. Krause
7 * \date 2012
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 * MatAERCHANTABILITY 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#ifndef REMORA_KERNELS_DEFAULT_GEMatAV_HPP
31#define REMORA_KERNELS_DEFAULT_GEMatAV_HPP
32
33#include "../../expression_types.hpp" //matrix/vector_expression
34#include "../../proxy_expressions.hpp" //matrix row,, transpose
35#include "../../detail/traits.hpp" //matrix orientations
36#include "../dot.hpp" //inner product
37#include "../vector_assign.hpp" //assignment of vectors
38#include <type_traits> //std::false_type marker for unoptimized
39
40namespace remora{namespace bindings {
41
42//row major can be further reduced to inner_prod()
43template<class ResultV, class MatA, class V>
44void gemv_impl(
45 matrix_expression<MatA, cpu_tag> const& A,
46 vector_expression<V, cpu_tag> const& x,
47 vector_expression<ResultV, cpu_tag>& result,
48 typename ResultV::value_type alpha,
49 row_major
50) {
51 typedef typename ResultV::value_type value_type;
52 value_type value;
53 for(std::size_t i = 0; i != A().size1();++i){
54 kernels::dot(row(A,i),x,value);
55 if(value != value_type())//handling of sparse results.
56 result()(i) += alpha * value;
57 }
58}
59
60//column major is implemented by computing a linear combination of matrix-rows
61template<class ResultV, class MatA, class V>
62void gemv_impl(
63 matrix_expression<MatA, cpu_tag> const& A,
64 vector_expression<V, cpu_tag> const& x,
65 vector_expression<ResultV, cpu_tag>& result,
66 typename ResultV::value_type alpha,
67 column_major
68) {
69 typedef typename V::const_iterator iterator;
70 typedef typename ResultV::value_type value_type;
71 typedef device_traits<cpu_tag>::multiply_and_add<value_type> MultAdd;
72 iterator end = x().end();
73 for(iterator it = x().begin(); it != end; ++it) {
74 //FIXME: for sparse result vectors, this might hurt.
75 kernels::assign(result, column(A,it.index()), MultAdd(alpha * (*it)));
76 }
77}
78
79//unknown orientation is dispatched to row_major
80template<class ResultV, class MatA, class V>
81void gemv_impl(
82 matrix_expression<MatA, cpu_tag> const& A,
83 vector_expression<V, cpu_tag> const& x,
84 vector_expression<ResultV, cpu_tag>& result,
85 typename ResultV::value_type alpha,
86 unknown_orientation
87) {
88 gemv_impl(A,x,result,alpha,row_major());
89}
90
91// result += alpha * A * x
92template<class ResultV, class MatA, class V>
93void gemv(
94 matrix_expression<MatA, cpu_tag> const& A,
95 vector_expression<V, cpu_tag> const& x,
96 vector_expression<ResultV, cpu_tag>& result,
97 typename ResultV::value_type alpha,
98 std::false_type
99) {
100 typedef typename MatA::orientation orientation;
101
102 gemv_impl(A, x, result, alpha, orientation());
103}
104
105}}
106#endif