-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Core.h
296 lines (274 loc) · 12.5 KB
/
Core.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2014 Soumyajit De
* Written (w) 2014 Khaled Nasr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*/
#ifndef CORE_H_
#define CORE_H_
#include <shogun/mathematics/linalg/internal/implementation/ElementwiseSquare.h>
#include <shogun/mathematics/linalg/internal/implementation/MatrixProduct.h>
#include <shogun/mathematics/linalg/internal/implementation/Add.h>
#include <shogun/mathematics/linalg/internal/implementation/Apply.h>
#include <shogun/mathematics/linalg/internal/implementation/ElementwiseProduct.h>
#include <shogun/mathematics/linalg/internal/implementation/Scale.h>
#include <shogun/mathematics/linalg/internal/implementation/Convolve.h>
#include <shogun/mathematics/linalg/internal/implementation/RangeFill.h>
namespace shogun
{
namespace linalg
{
/** Performs the operation \f$C = \alpha A + \beta B\f$.
* Works for both matrices and vectors.
*
* This version should be used for backend specific code requirements. For example,
* use this with CGPUMatrix and explicitly set ViennaCL backend, or SGMatrix and
* explicitly set Eigen3 backend. If matrix-type/backend-type independent code is
* desired, use the version that does not support preallocated result matrix but
* returns the result in a newly created matrix instead.
*
* @param A First matrix/vector
* @param B Second matrix/vector
* @param C Result of the operation
* @param alpha scaling parameter for first matrix/vector
* @param beta scaling parameter for second matrix/vector
*/
template <Backend backend=linalg_traits<Core>::backend,class Matrix>
void add(Matrix A, Matrix B, Matrix C, typename Matrix::Scalar alpha=1.0,
typename Matrix::Scalar beta=1.0)
{
implementation::add<backend, Matrix>::compute(A, B, C, alpha, beta);
}
/** Performs the operation \f$C = \alpha A + \beta B\f$.
* Works for both matrices and vectors.
*
* This version returns the result in a newly created matrix/vector. If add
* is desired that will work irrespective of the backend and the matrix/vector
* type used, then this method should be used.
*
* @param A First matrix/vector
* @param B Second matrix/vector
* @param alpha scaling parameter for first matrix/vector
* @param beta scaling parameter for second matrix/vector
* @return The result of the operation
*/
template <Backend backend=linalg_traits<Core>::backend,class Matrix>
Matrix add(Matrix A, Matrix B, typename Matrix::Scalar alpha=1.0,
typename Matrix::Scalar beta=1.0)
{
return implementation::add<backend, Matrix>::compute(A, B, alpha, beta);
}
/** Performs the operation B = alpha*A. Works for both matrices and vectors */
template <Backend backend=linalg_traits<Core>::backend,class Matrix>
void scale(Matrix A, Matrix B, typename Matrix::Scalar alpha)
{
implementation::scale<backend, Matrix>::compute(A, B, alpha);
}
/** Performs the operation A = alpha*A. Works for both matrices and vectors */
template <Backend backend=linalg_traits<Core>::backend,class Matrix>
void scale(Matrix A, typename Matrix::Scalar alpha)
{
implementation::scale<backend, Matrix>::compute(A, A, alpha);
}
/** Range fill a vector with start...start+len-1
* @param A - the matrix to be filled
* @param start - value to be assigned to first element of vector or matrix
*/
template <Backend backend=linalg_traits<Core>::backend, class Matrix>
void range_fill(Matrix A, typename Matrix::Scalar start=0.0)
{
implementation::range_fill<backend, Matrix>::compute(A, start);
}
/** Range fill a vector array with start...start+len-1
* @param A - the array to be filled
* @param len - length of the array to be filled
* @param start - value to be assigned to first element of array
*/
template <Backend backend=linalg_traits<Core>::backend, class Matrix>
void range_fill(Matrix A, index_t len, typename Matrix::Scalar start=0.0)
{
implementation::range_fill<backend, Matrix>::compute(A, len, start);
}
#ifdef HAVE_LINALG_LIB
/** Performs the operation of matrix applied to a vector \f$x = Ab\f$.
*
* This version should be used for backend specific code requirements. For example,
* use this with CGPUMatrix, CGPUVector and explicitly set ViennaCL backend, or
* SGMatrix, SGVector and explicitly set Eigen3 backend. If matrix-type/backend-type
* independent code is desired, use the version that does not support preallocated
* result vector but returns the result in a newly created vector instead.
*
* @param A The matrix
* @param b The vector
* @param x Result vector
*/
template <Backend backend=linalg_traits<Core>::backend,class Matrix,class Vector>
void apply(Matrix A, Vector b, Vector x, bool transpose=false)
{
implementation::apply<backend,Matrix,Vector>::compute(A, b, x, transpose);
}
/** Performs the operation of matrix applied to a vector \f$x = Ab\f$.
*
* This version returns the result in a newly created vector. If apply is desired
* that will work irrespective of the backend and the matrix/vector type used,
* then this method should be used.
*
* @param A The matrix
* @param b The vector
* @param x Result vector
*/
template <Backend backend=linalg_traits<Core>::backend,class Matrix,class Vector>
Vector apply(Matrix A, Vector b, bool transpose=false)
{
return implementation::apply<backend,Matrix,Vector>::compute(A, b, transpose);
}
/** Performs matrix multiplication. This version should be used for backend specific
* code requirements. For example, use this with CGPUMatrix and explicitly set
* ViennaCL backend, or SGMatrix and explicitly set Eigen3 backend. If matrix-type/
* backend-type independent code is desired, use the version that does not support
* preallocated result matrix but returns the result in a newly created matrix instead.
*
* @param A First matrix
* @param B Second matrix
* @param C Result of the operation
* @param transpose_A Whether to the transpose of A should be used instead of A
* @param transpose_B Whether to the transpose of B should be used instead of B
* @param overwrite If true, the values in C are overwritten with the result,
* otherwise, the result is added to the existing values
*/
template <Backend backend=linalg_traits<Core>::backend,class Matrix>
void matrix_product(Matrix A, Matrix B, Matrix C,
bool transpose_A=false, bool transpose_B=false, bool overwrite=true)
{
implementation::matrix_product<backend, Matrix>::compute(A, B, C, transpose_A, transpose_B, overwrite);
}
/** Performs matrix multiplication. This version returns the result in a newly
* created matrix. If matrix-product is desired that will work irrespective of the
* backend and the matrix type used, then this method should be used.
*
* @param A First matrix
* @param B Second matrix
* @param transpose_A Whether to the transpose of A should be used instead of A
* @param transpose_B Whether to the transpose of B should be used instead of B
* @return Result of the operation
*/
template <Backend backend=linalg_traits<Core>::backend,class Matrix>
typename implementation::matrix_product<backend,Matrix>::ReturnType matrix_product(Matrix A, Matrix B,
bool transpose_A=false, bool transpose_B=false)
{
return implementation::matrix_product<backend, Matrix>::compute(A, B, transpose_A, transpose_B);
}
/** Performs the operation C = alpha*A - beta*B. Works for both matrices and vectors */
template <Backend backend=linalg_traits<Core>::backend,class Matrix>
void subtract(Matrix A, Matrix B, Matrix C,
typename Matrix::Scalar alpha=1.0, typename Matrix::Scalar beta=1.0)
{
implementation::add<backend, Matrix>::compute(A, B, C, alpha, -1*beta);
}
/** Performs the operation C = A .* B where ".*" denotes elementwise multiplication.
*
* This version should be used for backend specific code requirements. For example,
* use this with CGPUMatrix and explicitly set ViennaCL backend, or SGMatrix and
* explicitly set Eigen3 backend. If matrix-type/backend-type independent code is
* desired, use the version that does not support preallocated result matrix but
* returns the result in a newly created matrix instead.
*
* @param A First matrix
* @param B Second matrix
* @param C Result of the operation
*/
template <Backend backend=linalg_traits<Core>::backend,class Matrix>
void elementwise_product(Matrix A, Matrix B, Matrix C)
{
implementation::elementwise_product<backend, Matrix>::compute(A, B, C);
}
/** Performs the operation C = A .* B where ".*" denotes elementwise multiplication.
*
* This version returns the result in a newly created matrix. If elementwise-product
* is desired that will work irrespective of the backend and the matrix type used,
* then this method should be used.
*
* @param A First matrix
* @param B Second matrix
* @return The result of the operation
*/
template <Backend backend=linalg_traits<Core>::backend,class Matrix>
typename implementation::elementwise_product<backend,Matrix>::ReturnType elementwise_product(Matrix A, Matrix B)
{
return implementation::elementwise_product<backend,Matrix>::compute(A, B);
}
/**
* Wrapper method for internal implementation of square of co-efficients that works
* with generic dense matrices.
*
* @param m the matrix whose squared co-efficients matrix has to be computed
* @return another matrix whose co-efficients are \f$m'_{i,j}=m_(i,j}^2\f$
* for all \f$i,j\f$
*/
template <Backend backend=linalg_traits<Core>::backend,class Matrix>
typename implementation::elementwise_square<backend,Matrix>::ReturnType elementwise_square(Matrix m)
{
return implementation::elementwise_square<backend,Matrix>::compute(m);
}
/**
* Wrapper method for internal implementation of square of co-efficients that works
* with generic dense matrices.
*
* @param m the matrix whose squared co-efficients matrix has to be computed
* @param result Pre-allocated matrix for the result of the computation
*/
template <Backend backend=linalg_traits<Core>::backend,class Matrix, class ResultMatrix>
void elementwise_square(Matrix m, ResultMatrix result)
{
implementation::elementwise_square<backend,Matrix>::compute(m,result);
}
/** Computes the 2D convolution of X with W
*
* NOTE: For the ViennaCL backend, the size of W (number of bytes) must not exceed
* [CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE](http://www.khronos.org/registry/cl/sdk/1.2/docs/man/xhtml/clGetDeviceInfo.html).
*
* @param X Input image
* @param W Filter coefficients. The dimensions of the matrix must be odd-numbered.
* @param Y Output image of the same size as the input image, as the borders
* of the input image are implicitly padded with zeros during the computation
* @param flip If true the filter coefficients are flipped, performing cross-correlation
* instead of convolution
* @param overwrite If true, the values in Y are overwritten with result of the
* computation. Otherwise, the result is added to the existing values in Y.
* @param stride_x Stride in the x (column) direction
* @param stride_y Stride in the y (row) direction
*/
template <Backend backend=linalg_traits<Core>::backend,class Matrix>
void convolve(Matrix X, Matrix W, Matrix Y, bool flip = false,
bool overwrite=true, int32_t stride_x=1, int32_t stride_y=1)
{
implementation::convolve<backend, Matrix>::compute(X, W, Y, flip, overwrite, stride_x, stride_y);
}
#endif // HAVE_LINALG_LIB
}
}
#endif // CORE_H_