SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Max.h
Go to the documentation of this file.
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 
40 
41 #ifdef HAVE_VIENNACL
43 #include <shogun/lib/GPUMatrix.h>
44 #include <shogun/lib/GPUVector.h>
45 #endif
46 
47 #include <string>
48 
49 namespace shogun
50 {
51 
52 namespace linalg
53 {
54 
55 namespace implementation
56 {
57 
62 template <enum Backend,class Matrix>
63 struct max
64 {
66  typedef typename Matrix::Scalar T;
67 
73  static T compute(Matrix m);
74 };
75 
79 template <class Matrix>
80 struct max<Backend::EIGEN3,Matrix>
81 {
83  typedef typename Matrix::Scalar T;
84 
87 
90 
96  static T compute(SGMatrix<T> mat)
97  {
98  Eigen::Map<MatrixXt> m = mat;
99 
100  return m.maxCoeff();
101  }
102 
108  static T compute(SGVector<T> vec)
109  {
110  Eigen::Map<VectorXt> v = vec;
111 
112  return v.maxCoeff();
113  }
114 };
115 
116 #ifdef HAVE_VIENNACL
117 
121 template <class Matrix>
122 struct max<Backend::VIENNACL,Matrix>
123 {
125  typedef typename Matrix::Scalar T;
126 
128  template <class T>
129  static viennacl::ocl::kernel& generate_kernel()
130  {
131  std::string kernel_name = "max_" + ocl::get_type_string<T>();
132 
133  if (ocl::kernel_exists(kernel_name))
134  return ocl::get_kernel(kernel_name);
135 
136  std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
137 
138  source.append(
139  R"(
140  __kernel void KERNEL_NAME(
141  __global DATATYPE* vec, int size, int offset,
142  __global DATATYPE* result)
143  {
144  __local DATATYPE buffer[WORK_GROUP_SIZE_1D];
145 
146  int local_id = get_local_id(0);
147 
148  DATATYPE thread_max = -INFINITY;
149  for (int i=local_id; i<size; i+=WORK_GROUP_SIZE_1D)
150  {
151  DATATYPE v = vec[i+offset];
152  thread_max = max(v, thread_max);
153  }
154 
155  buffer[local_id] = thread_max;
156 
157  for (int j = WORK_GROUP_SIZE_1D/2; j > 0; j = j>>1)
158  {
159  barrier(CLK_LOCAL_MEM_FENCE);
160  if (local_id < j)
161  buffer[local_id] = max(buffer[local_id], buffer[local_id + j]);
162  }
163 
164  barrier(CLK_LOCAL_MEM_FENCE);
165 
166  if (get_global_id(0)==0)
167  *result = buffer[0];
168  }
169  )"
170  );
171 
172  viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
173 
174  kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_1D);
175  kernel.global_work_size(0, OCL_WORK_GROUP_SIZE_1D);
176 
177  return kernel;
178  }
179 
185  static T compute(CGPUMatrix<T> mat)
186  {
187  viennacl::ocl::kernel& kernel = generate_kernel<T>();
188 
189  CGPUVector<T> result(1);
190 
191  viennacl::ocl::enqueue(kernel(mat.vcl_matrix(),
192  cl_int(mat.num_rows*mat.num_cols), cl_int(mat.offset),
193  result.vcl_vector()));
194 
195  return result[0];
196  }
197 
203  static T compute(CGPUVector<T> vec)
204  {
205  viennacl::ocl::kernel& kernel = generate_kernel<T>();
206 
207  CGPUVector<T> result(1);
208 
209  viennacl::ocl::enqueue(kernel(vec.vcl_vector(),
210  cl_int(vec.vlen), cl_int(vec.offset),
211  result.vcl_vector()));
212 
213  return result[0];
214  }
215 };
216 #endif // HAVE_VIENNACL
217 
218 }
219 
220 }
221 
222 }
223 #endif // MAX_IMPL_H_
shogun matrix
shogun vector
Generic class which is specialized for different backends to perform the max operation.
Definition: Max.h:63
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorXt
Definition: Max.h:89
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Matrix::Scalar max(Matrix m)
Definition: Redux.h:68
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: Max.h:86

SHOGUN Machine Learning Toolbox - Documentation