SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 <class Matrix, class Vector>
76 struct set_rows_const<Backend::EIGEN3, Matrix, Vector>
77 {
79  typedef typename Matrix::Scalar T;
80 
83 
86 
90  static void compute(SGMatrix<T> A, SGVector<T> v)
91  {
92  Eigen::Map<MatrixXt> A_eig = A;
93  Eigen::Map<VectorXt> v_eig = v;
94 
95  A_eig.colwise() = v_eig;
96  }
97 };
98 #endif // HAVE_EIGEN3
99 
100 #ifdef HAVE_VIENNACL
101 
103 template <class Matrix, class Vector>
104 struct set_rows_const<Backend::VIENNACL, Matrix, Vector>
105 {
107  typedef typename Matrix::Scalar T;
108 
110  template <class T>
111  static viennacl::ocl::kernel& generate_kernel()
112  {
113  std::string kernel_name = "set_rows_const_" + ocl::get_type_string<T>();
114 
115  if (ocl::kernel_exists(kernel_name))
116  return ocl::get_kernel(kernel_name);
117 
118  std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
119 
120  source.append(
121  R"(
122  __kernel void KERNEL_NAME(
123  __global DATATYPE* mat, int nrows, int ncols, int offset,
124  __global DATATYPE* vec, int vec_offset)
125  {
126  int i = get_global_id(0);
127  int j = get_global_id(1);
128 
129  if (i>=nrows || j>=ncols)
130  return;
131 
132  mat[offset + i+j*nrows] = vec[i+offset];
133  }
134  )"
135  );
136 
137  viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
138 
139  kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_2D);
140  kernel.local_work_size(1, OCL_WORK_GROUP_SIZE_2D);
141 
142  return kernel;
143  }
144 
148  static void compute(CGPUMatrix<T> A, CGPUVector<T> v)
149  {
150  viennacl::ocl::kernel& kernel = generate_kernel<T>();
151  kernel.global_work_size(0, ocl::align_to_multiple_2d(A.num_rows));
152  kernel.global_work_size(1, ocl::align_to_multiple_2d(A.num_cols));
153 
154  viennacl::ocl::enqueue(kernel(A.vcl_matrix(),
155  cl_int(A.num_rows), cl_int(A.num_cols), cl_int(A.offset),
156  v.vcl_vector(), cl_int(v.offset)));
157  }
158 };
159 
160 #endif // HAVE_VIENNACL
161 
162 }
163 
164 }
165 
166 }
167 #endif // SET_ROWS_CONST_IMPL_H_
shogun matrix
shogun vector
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