SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Apply.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2015 Soumyajit De
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
31 #ifndef APPLY_IMPL_H_
32 #define APPLY_IMPL_H_
33 
34 #include <shogun/lib/config.h>
35 #include <shogun/lib/SGMatrix.h>
36 #include <shogun/lib/SGVector.h>
37 
39 
40 #ifdef HAVE_VIENNACL
41 #include <shogun/lib/GPUMatrix.h>
42 #include <shogun/lib/GPUVector.h>
43 #include <viennacl/linalg/matrix_operations.hpp>
44 #include <viennacl/linalg/vector_operations.hpp>
45 #endif // HAVE_VIENNACL
46 
47 #include <type_traits>
48 
49 namespace shogun
50 {
51 
52 namespace linalg
53 {
54 
55 namespace implementation
56 {
57 
61 template <enum Backend, class Matrix, class Vector>
62 struct apply
63 {
64 };
65 
69 template <class Matrix, class Vector>
70 struct apply<Backend::EIGEN3, Matrix, Vector>
71 {
72  static_assert(std::is_same<typename Matrix::Scalar,typename Vector::Scalar>::value,
73  "Different numeric scalar types for matrix and vector not allowed!\n");
74 
76  typedef typename Matrix::Scalar T;
77 
80 
82  typedef Eigen::Matrix<T,Eigen::Dynamic,1> VectorXt;
83 
91  static SGVector<T> compute(SGMatrix<T> A, SGVector<T> b, bool transpose)
92  {
93  SGVector<T> x(A.num_rows);
94  compute(A, b, x, transpose);
95  return x;
96  }
97 
105  static void compute(SGMatrix<T> A, SGVector<T> b, SGVector<T> x, bool transpose)
106  {
107  Eigen::Map<MatrixXt> A_eig=A;
108  Eigen::Map<VectorXt> b_eig=b;
109  Eigen::Map<VectorXt> x_eig=x;
110 
111  if (transpose)
112  x_eig=A_eig.transpose()*b_eig;
113  else
114  x_eig=A_eig*b_eig;
115  }
116 };
117 
118 #ifdef HAVE_VIENNACL
119 
122 template <class Matrix, class Vector>
123 struct apply<Backend::VIENNACL, Matrix, Vector>
124 {
125  static_assert(std::is_same<typename Matrix::Scalar,typename Vector::Scalar>::value,
126  "Different numeric scalar types for matrix and vector not allowed!\n");
127 
129  typedef typename Matrix::Scalar T;
130 
138  static CGPUVector<T> compute(CGPUMatrix<T> A, CGPUVector<T> b, bool transpose)
139  {
140  CGPUVector<T> x(A.num_rows);
141  compute(A, b, x, transpose);
142  return x;
143  }
144 
152  static void compute(CGPUMatrix<T> A, CGPUVector<T> b, CGPUVector<T> x, bool transpose)
153  {
154  if (transpose)
155  x.vcl_vector()=viennacl::linalg::prod(viennacl::trans(A.vcl_matrix()), b.vcl_vector());
156  else
157  x.vcl_vector()=viennacl::linalg::prod(A.vcl_matrix(), b.vcl_vector());
158  }
159 };
160 #endif // HAVE_VIENNACL
161 
162 }
163 
164 }
165 
166 }
167 #endif // APPLY_IMPL_H_
Generic class which is specialized for different backends to perform apply.
Definition: Apply.h:62
index_t num_rows
Definition: SGMatrix.h:374
shogun matrix
shogun vector
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

SHOGUN Machine Learning Toolbox - Documentation