SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
Max.h
浏览该文件的文档.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Khaled Nasr
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 MAX_IMPL_H_
32 #define MAX_IMPL_H_
33 
34 #include <shogun/lib/config.h>
35 #include <shogun/lib/SGMatrix.h>
36 #include <shogun/lib/SGVector.h>
38 
39 #ifdef HAVE_EIGEN3
41 #endif // HAVE_EIGEN3
42 
43 #ifdef HAVE_VIENNACL
45 #include <shogun/lib/GPUMatrix.h>
46 #include <shogun/lib/GPUVector.h>
47 #endif
48 
49 #include <string>
50 
51 namespace shogun
52 {
53 
54 namespace linalg
55 {
56 
57 namespace implementation
58 {
59 
64 template <enum Backend,class Matrix>
65 struct max
66 {
68  typedef typename Matrix::Scalar T;
69 
75  static T compute(Matrix m);
76 };
77 
81 template <class Matrix>
82 struct max<Backend::NATIVE, Matrix>
83 {
85  typedef typename Matrix::Scalar T;
86 
92  static T compute(SGMatrix<T> mat)
93  {
94  REQUIRE(mat.num_cols*mat.num_rows > 0, "Matrix can not be empty!\n");
95  return compute(mat.matrix, mat.num_cols*mat.num_rows);
96  }
97 
103  static T compute(SGVector<T> vec)
104  {
105  REQUIRE(vec.vlen > 0, "Vector can not be empty!\n");
106  return compute(vec.vector, vec.vlen);
107  }
108 
115  static T compute(T* vec, index_t len)
116  {
117  return *std::max_element(vec, vec+len);
118  }
119 };
120 
121 #ifdef HAVE_EIGEN3
122 
126 template <class Matrix>
127 struct max<Backend::EIGEN3,Matrix>
128 {
130  typedef typename Matrix::Scalar T;
131 
134 
137 
143  static T compute(SGMatrix<T> mat)
144  {
145  Eigen::Map<MatrixXt> m = mat;
146 
147  return m.maxCoeff();
148  }
149 
155  static T compute(SGVector<T> vec)
156  {
157  Eigen::Map<VectorXt> v = vec;
158 
159  return v.maxCoeff();
160  }
161 };
162 #endif // HAVE_EIGEN3
163 
164 #ifdef HAVE_VIENNACL
165 
169 template <class Matrix>
170 struct max<Backend::VIENNACL,Matrix>
171 {
173  typedef typename Matrix::Scalar T;
174 
176  template <class T>
177  static viennacl::ocl::kernel& generate_kernel()
178  {
179  std::string kernel_name = "max_" + ocl::get_type_string<T>();
180 
181  if (ocl::kernel_exists(kernel_name))
182  return ocl::get_kernel(kernel_name);
183 
184  std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
185 
186  source.append(
187  R"(
188  __kernel void KERNEL_NAME(
189  __global DATATYPE* vec, int size, int offset,
190  __global DATATYPE* result)
191  {
192  __local DATATYPE buffer[WORK_GROUP_SIZE_1D];
193 
194  int local_id = get_local_id(0);
195 
196  DATATYPE thread_max = -INFINITY;
197  for (int i=local_id; i<size; i+=WORK_GROUP_SIZE_1D)
198  {
199  DATATYPE v = vec[i+offset];
200  thread_max = max(v, thread_max);
201  }
202 
203  buffer[local_id] = thread_max;
204 
205  for (int j = WORK_GROUP_SIZE_1D/2; j > 0; j = j>>1)
206  {
207  barrier(CLK_LOCAL_MEM_FENCE);
208  if (local_id < j)
209  buffer[local_id] = max(buffer[local_id], buffer[local_id + j]);
210  }
211 
212  barrier(CLK_LOCAL_MEM_FENCE);
213 
214  if (get_global_id(0)==0)
215  *result = buffer[0];
216  }
217  )"
218  );
219 
220  viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
221 
222  kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_1D);
223  kernel.global_work_size(0, OCL_WORK_GROUP_SIZE_1D);
224 
225  return kernel;
226  }
227 
233  static T compute(CGPUMatrix<T> mat)
234  {
235  viennacl::ocl::kernel& kernel = generate_kernel<T>();
236 
237  CGPUVector<T> result(1);
238 
239  viennacl::ocl::enqueue(kernel(mat.vcl_matrix(),
240  cl_int(mat.num_rows*mat.num_cols), cl_int(mat.offset),
241  result.vcl_vector()));
242 
243  return result[0];
244  }
245 
251  static T compute(CGPUVector<T> vec)
252  {
253  viennacl::ocl::kernel& kernel = generate_kernel<T>();
254 
255  CGPUVector<T> result(1);
256 
257  viennacl::ocl::enqueue(kernel(vec.vcl_vector(),
258  cl_int(vec.vlen), cl_int(vec.offset),
259  result.vcl_vector()));
260 
261  return result[0];
262  }
263 };
264 #endif // HAVE_VIENNACL
265 
266 }
267 
268 }
269 
270 }
271 #endif // MAX_IMPL_H_
int32_t index_t
Definition: common.h:62
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:378
index_t num_rows
Definition: SGMatrix.h:376
shogun matrix
index_t vlen
Definition: SGVector.h:494
shogun vector
Generic class which is specialized for different backends to perform the max operation.
Definition: Max.h:65
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorXt
Definition: Max.h:136
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Matrix::Scalar max(Matrix m)
Definition: Redux.h:66
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: Max.h:133

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