SHOGUN  4.0.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>
37 
38 #ifdef HAVE_EIGEN3
40 #endif // HAVE_EIGEN3
41 
42 #ifdef HAVE_VIENNACL
44 #include <shogun/lib/GPUMatrix.h>
45 #include <shogun/lib/GPUVector.h>
46 #endif
47 
48 #include <string>
49 
50 namespace shogun
51 {
52 
53 namespace linalg
54 {
55 
56 namespace implementation
57 {
58 
62 template <enum Backend,class Matrix>
63 struct max
64 {
66  typedef typename Matrix::Scalar T;
67 
69  static T compute(Matrix m);
70 };
71 
72 #ifdef HAVE_EIGEN3
73 
74 template <> template <class Matrix>
75 struct max<Backend::EIGEN3,Matrix>
76 {
77  typedef typename Matrix::Scalar T;
80 
82  static T compute(SGMatrix<T> mat)
83  {
84  Eigen::Map<MatrixXt> m = mat;
85 
86  return m.maxCoeff();
87  }
88 
90  static T compute(SGVector<T> vec)
91  {
92  Eigen::Map<VectorXt> v = vec;
93 
94  return v.maxCoeff();
95  }
96 };
97 #endif // HAVE_EIGEN3
98 
99 #ifdef HAVE_VIENNACL
100 
101 template <> template <class Matrix>
102 struct max<Backend::VIENNACL,Matrix>
103 {
104  typedef typename Matrix::Scalar T;
105 
107  template <class T>
108  static viennacl::ocl::kernel& generate_kernel()
109  {
110  std::string kernel_name = "max_" + ocl::get_type_string<T>();
111 
112  if (ocl::kernel_exists(kernel_name))
113  return ocl::get_kernel(kernel_name);
114 
115  std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
116 
117  source.append(
118  R"(
119  __kernel void KERNEL_NAME(
120  __global DATATYPE* vec, int size, int offset,
121  __global DATATYPE* result)
122  {
123  __local DATATYPE buffer[WORK_GROUP_SIZE_1D];
124 
125  int local_id = get_local_id(0);
126 
127  DATATYPE thread_max = -INFINITY;
128  for (int i=local_id; i<size; i+=WORK_GROUP_SIZE_1D)
129  {
130  DATATYPE v = vec[i+offset];
131  thread_max = max(v, thread_max);
132  }
133 
134  buffer[local_id] = thread_max;
135 
136  for (int j = WORK_GROUP_SIZE_1D/2; j > 0; j = j>>1)
137  {
138  barrier(CLK_LOCAL_MEM_FENCE);
139  if (local_id < j)
140  buffer[local_id] = max(buffer[local_id], buffer[local_id + j]);
141  }
142 
143  barrier(CLK_LOCAL_MEM_FENCE);
144 
145  if (get_global_id(0)==0)
146  *result = buffer[0];
147  }
148  )"
149  );
150 
151  viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
152 
153  kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_1D);
154  kernel.global_work_size(0, OCL_WORK_GROUP_SIZE_1D);
155 
156  return kernel;
157  }
158 
160  static T compute(CGPUMatrix<T> mat)
161  {
162  viennacl::ocl::kernel& kernel = generate_kernel<T>();
163 
164  CGPUVector<T> result(1);
165 
166  viennacl::ocl::enqueue(kernel(mat.vcl_matrix(),
167  cl_int(mat.num_rows*mat.num_cols), cl_int(mat.offset),
168  result.vcl_vector()));
169 
170  return result[0];
171  }
172 
174  static T compute(CGPUVector<T> vec)
175  {
176  viennacl::ocl::kernel& kernel = generate_kernel<T>();
177 
178  CGPUVector<T> result(1);
179 
180  viennacl::ocl::enqueue(kernel(vec.vcl_vector(),
181  cl_int(vec.vlen), cl_int(vec.offset),
182  result.vcl_vector()));
183 
184  return result[0];
185  }
186 };
187 #endif // HAVE_VIENNACL
188 
189 }
190 
191 }
192 
193 }
194 #endif // MAX_IMPL_H_
shogun vector
Definition: Parameter.h:28
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorXt
Definition: Max.h:79
shogun matrix
Definition: Parameter.h:26
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Matrix::Scalar max(Matrix m)
Definition: Redux.h:177
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: Max.h:78

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