SHOGUN  4.2.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 
39 
40 #ifdef HAVE_VIENNACL
41 #include <shogun/lib/GPUMatrix.h>
42 #include <shogun/lib/GPUVector.h>
44 #endif // HAVE_VIENNACL
45 
46 namespace shogun
47 {
48 
49 namespace linalg
50 {
51 
52 namespace implementation
53 {
54 
58 template <enum Backend, class Matrix, class Vector>
60 {
62  typedef typename Matrix::Scalar T;
63 
67  static void compute(Matrix A, Vector v);
68 };
69 
70 
72 template <class Matrix, class Vector>
73 struct set_rows_const<Backend::EIGEN3, Matrix, Vector>
74 {
76  typedef typename Matrix::Scalar T;
77 
80 
83 
87  static void compute(SGMatrix<T> A, SGVector<T> v)
88  {
89  Eigen::Map<MatrixXt> A_eig = A;
90  Eigen::Map<VectorXt> v_eig = v;
91 
92  A_eig.colwise() = v_eig;
93  }
94 };
95 
96 #ifdef HAVE_VIENNACL
97 
99 template <class Matrix, class Vector>
100 struct set_rows_const<Backend::VIENNACL, Matrix, Vector>
101 {
103  typedef typename Matrix::Scalar T;
104 
106  template <class T>
107  static viennacl::ocl::kernel& generate_kernel()
108  {
109  std::string kernel_name = "set_rows_const_" + ocl::get_type_string<T>();
110 
111  if (ocl::kernel_exists(kernel_name))
112  return ocl::get_kernel(kernel_name);
113 
114  std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
115 
116  source.append(
117  R"(
118  __kernel void KERNEL_NAME(
119  __global DATATYPE* mat, int nrows, int ncols, int offset,
120  __global DATATYPE* vec, int vec_offset)
121  {
122  int i = get_global_id(0);
123  int j = get_global_id(1);
124 
125  if (i>=nrows || j>=ncols)
126  return;
127 
128  mat[offset + i+j*nrows] = vec[i+offset];
129  }
130  )"
131  );
132 
133  viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
134 
135  kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_2D);
136  kernel.local_work_size(1, OCL_WORK_GROUP_SIZE_2D);
137 
138  return kernel;
139  }
140 
144  static void compute(CGPUMatrix<T> A, CGPUVector<T> v)
145  {
146  viennacl::ocl::kernel& kernel = generate_kernel<T>();
147  kernel.global_work_size(0, ocl::align_to_multiple_2d(A.num_rows));
148  kernel.global_work_size(1, ocl::align_to_multiple_2d(A.num_cols));
149 
150  viennacl::ocl::enqueue(kernel(A.vcl_matrix(),
151  cl_int(A.num_rows), cl_int(A.num_cols), cl_int(A.offset),
152  v.vcl_vector(), cl_int(v.offset)));
153  }
154 };
155 
156 #endif // HAVE_VIENNACL
157 
158 }
159 
160 }
161 
162 }
163 #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