SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
Dot.h
浏览该文件的文档.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Soumyajit De
4  * Written (w) 2014 Khaled Nasr
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation are those
28  * of the authors and should not be interpreted as representing official policies,
29  * either expressed or implied, of the Shogun Development Team.
30  */
31 
32 #ifndef DOT_IMPL_H_
33 #define DOT_IMPL_H_
34 
35 #include <shogun/lib/config.h>
36 #include <shogun/lib/SGVector.h>
37 #include <shogun/io/SGIO.h>
38 
39 #ifdef HAVE_EIGEN3
41 #endif // HAVE_EIGEN3
42 
43 #ifdef HAVE_VIENNACL
44 #include <shogun/lib/GPUVector.h>
45 #include <viennacl/linalg/inner_prod.hpp>
46 #endif // HAVE_VIENNACL
47 
48 namespace shogun
49 {
50 
51 namespace linalg
52 {
53 
57 namespace implementation
58 {
59 
65 template <enum Backend, class Vector>
66 struct dot
67 {
69  typedef typename Vector::Scalar T;
70 
79  static T compute(Vector a, Vector b);
80 };
81 
85 template <class Vector>
86 struct dot<Backend::NATIVE, Vector>
87 {
89  typedef typename Vector::Scalar T;
90 
100  {
101  REQUIRE(a.vlen == b.vlen, "Vectors should have same length!\n");
102  return compute(a.vector, b.vector, a.vlen);
103  }
104 
114  static T compute(T* a, T* b, index_t len)
115  {
116  T result=static_cast<T>(0);
117  for (index_t i=0; i<len; ++i)
118  result+=a[i]*b[i];
119  return result;
120  }
121 };
122 
123 #ifdef HAVE_EIGEN3
124 
127 template <class Vector>
128 struct dot<Backend::EIGEN3, Vector>
129 {
131  typedef typename Vector::Scalar T;
132 
142  {
143  typedef Eigen::Matrix<T, Eigen::Dynamic, 1> VectorXt;
144  Eigen::Map<VectorXt> vec_a = a;
145  Eigen::Map<VectorXt> vec_b = b;
146  return vec_a.dot(vec_b);
147  }
148 };
149 #endif // HAVE_EIGEN3
150 
151 #ifdef HAVE_VIENNACL
152 
155 template <class Vector>
156 struct dot<Backend::VIENNACL, Vector>
157 {
159  typedef typename Vector::Scalar T;
160 
169  static T compute(shogun::CGPUVector<T> a, shogun::CGPUVector<T> b)
170  {
171  return viennacl::linalg::inner_prod(a.vcl_vector(), b.vcl_vector());
172  }
173 };
174 #endif // HAVE_VIENNACL
175 
176 }
177 
178 }
179 
180 }
181 #endif // DOT_IMPL_H_
int32_t index_t
Definition: common.h:62
Vector::Scalar dot(Vector a, Vector b)
Definition: Redux.h:56
#define REQUIRE(x,...)
Definition: SGIO.h:206
Generic class dot which provides a static compute method. This class is specialized for different typ...
Definition: Dot.h:66
static T compute(shogun::SGVector< T > a, shogun::SGVector< T > b)
Definition: Dot.h:141
index_t vlen
Definition: SGVector.h:494
shogun vector
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static T compute(shogun::SGVector< T > a, shogun::SGVector< T > b)
Definition: Dot.h:99
static T compute(Vector a, Vector b)

SHOGUN 机器学习工具包 - 项目文档