SHOGUN  4.0.0
SetRowsConst.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 SET_ROWS_CONST_IMPL_H_
32 #define SET_ROWS_CONST_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
43 #include <shogun/lib/GPUMatrix.h>
44 #include <shogun/lib/GPUVector.h>
46 #endif // HAVE_VIENNACL
47 
48 namespace shogun
49 {
50 
51 namespace linalg
52 {
53 
54 namespace implementation
55 {
56 
60 template <enum Backend, class Matrix, class Vector>
62 {
64  typedef typename Matrix::Scalar T;
65 
69  static void compute(Matrix A, Vector v);
70 };
71 
72 #ifdef HAVE_EIGEN3
73 
75 template <> template <class Matrix, class Vector>
76 struct set_rows_const<Backend::EIGEN3, Matrix, Vector>
77 {
78  typedef typename Matrix::Scalar T;
81 
85  static void compute(SGMatrix<T> A, SGVector<T> v)
86  {
87  Eigen::Map<MatrixXt> A_eig = A;
88  Eigen::Map<VectorXt> v_eig = v;
89 
90  A_eig.colwise() = v_eig;
91  }
92 };
93 #endif // HAVE_EIGEN3
94 
95 #ifdef HAVE_VIENNACL
96 
98 template <> template <class Matrix, class Vector>
99 struct set_rows_const<Backend::VIENNACL, Matrix, Vector>
100 {
101  typedef typename Matrix::Scalar T;
102 
104  template <class T>
105  static viennacl::ocl::kernel& generate_kernel()
106  {
107  std::string kernel_name = "set_rows_const_" + ocl::get_type_string<T>();
108 
109  if (ocl::kernel_exists(kernel_name))
110  return ocl::get_kernel(kernel_name);
111 
112  std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
113 
114  source.append(
115  R"(
116  __kernel void KERNEL_NAME(
117  __global DATATYPE* mat, int nrows, int ncols, int offset,
118  __global DATATYPE* vec, int vec_offset)
119  {
120  int i = get_global_id(0);
121  int j = get_global_id(1);
122 
123  if (i>=nrows || j>=ncols)
124  return;
125 
126  mat[offset + i+j*nrows] = vec[i+offset];
127  }
128  )"
129  );
130 
131  viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
132 
133  kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_2D);
134  kernel.local_work_size(1, OCL_WORK_GROUP_SIZE_2D);
135 
136  return kernel;
137  }
138 
142  static void compute(CGPUMatrix<T> A, CGPUVector<T> v)
143  {
144  viennacl::ocl::kernel& kernel = generate_kernel<T>();
145  kernel.global_work_size(0, ocl::align_to_multiple_2d(A.num_rows));
146  kernel.global_work_size(1, ocl::align_to_multiple_2d(A.num_cols));
147 
148  viennacl::ocl::enqueue(kernel(A.vcl_matrix(),
149  cl_int(A.num_rows), cl_int(A.num_cols), cl_int(A.offset),
150  v.vcl_vector(), cl_int(v.offset)));
151  }
152 };
153 
154 #endif // HAVE_VIENNACL
155 
156 }
157 
158 }
159 
160 }
161 #endif // SET_ROWS_CONST_IMPL_H_
void set_rows_const(Matrix A, Vector v)
Definition: Util.h:49
shogun vector
Definition: Parameter.h:28
shogun matrix
Definition: Parameter.h:26
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static void compute(Matrix A, Vector v)

SHOGUN Machine Learning Toolbox - Documentation