SHOGUN  6.1.3
LinalgBackendBase.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * Authors: 2016 Pan Deng, Soumyajit De, Heiko Strathmann, Viktor Gal
31  */
32 
33 #ifndef LINALG_BACKEND_BASE_H__
34 #define LINALG_BACKEND_BASE_H__
35 
36 #include <memory>
37 #include <shogun/io/SGIO.h>
38 #include <shogun/lib/SGMatrix.h>
39 #include <shogun/lib/SGVector.h>
40 #include <shogun/lib/common.h>
41 #include <shogun/lib/config.h>
46 
47 namespace shogun
48 {
49 
54  {
55  public:
56 #define DEFINE_FOR_ALL_PTYPE(METHODNAME, Container) \
57  METHODNAME(bool, Container); \
58  METHODNAME(char, Container); \
59  METHODNAME(int8_t, Container); \
60  METHODNAME(uint8_t, Container); \
61  METHODNAME(int16_t, Container); \
62  METHODNAME(uint16_t, Container); \
63  METHODNAME(int32_t, Container); \
64  METHODNAME(uint32_t, Container); \
65  METHODNAME(int64_t, Container); \
66  METHODNAME(uint64_t, Container); \
67  METHODNAME(float32_t, Container); \
68  METHODNAME(float64_t, Container); \
69  METHODNAME(floatmax_t, Container); \
70  METHODNAME(complex128_t, Container);
71 
72 #define DEFINE_FOR_REAL_PTYPE(METHODNAME, Container) \
73  METHODNAME(bool, Container); \
74  METHODNAME(char, Container); \
75  METHODNAME(int8_t, Container); \
76  METHODNAME(uint8_t, Container); \
77  METHODNAME(int16_t, Container); \
78  METHODNAME(uint16_t, Container); \
79  METHODNAME(int32_t, Container); \
80  METHODNAME(uint32_t, Container); \
81  METHODNAME(int64_t, Container); \
82  METHODNAME(uint64_t, Container); \
83  METHODNAME(float32_t, Container); \
84  METHODNAME(float64_t, Container); \
85  METHODNAME(floatmax_t, Container);
86 
87 #define DEFINE_FOR_NON_INTEGER_PTYPE(METHODNAME, Container) \
88  METHODNAME(float32_t, Container); \
89  METHODNAME(float64_t, Container); \
90  METHODNAME(floatmax_t, Container); \
91  METHODNAME(complex128_t, Container);
92 
98 #define BACKEND_GENERIC_IN_PLACE_ADD(Type, Container) \
99  virtual void add( \
100  Container<Type>& a, Container<Type>& b, Type alpha, Type beta, \
101  Container<Type>& result) const \
102  { \
103  SG_SNOTIMPLEMENTED; \
104  }
107 #undef BACKEND_GENERIC_IN_PLACE_ADD
108 
114 #define BACKEND_GENERIC_ADD_COL_VEC(Type, Container) \
115  virtual void add_col_vec( \
116  const SGMatrix<Type>& A, index_t i, const SGVector<Type>& b, \
117  Container<Type>& result, Type alpha, Type beta) const \
118  { \
119  SG_SNOTIMPLEMENTED; \
120  return; \
121  }
124 #undef BACKEND_GENERIC_ADD_COL_VEC
125 
131 #define BACKEND_GENERIC_ADD_VECTOR(Type, Container) \
132  virtual void add_vector( \
133  const SGMatrix<Type>& A, const SGVector<Type>& b, \
134  SGMatrix<Type>& result, Type alpha, Type beta) const \
135  { \
136  SG_SNOTIMPLEMENTED; \
137  return; \
138  }
140 #undef BACKEND_GENERIC_ADD_VECTOR
141 
147 #define BACKEND_GENERIC_ADD_SCALAR(Type, Container) \
148  virtual void add_scalar(Container<Type>& a, Type b) const \
149  { \
150  SG_SNOTIMPLEMENTED; \
151  }
154 #undef BACKEND_GENERIC_ADD_SCALAR
155 
161 #define BACKEND_GENERIC_CENTER_MATRIX(Type, Container) \
162  virtual void center_matrix(Container<Type>& A) const \
163  { \
164  SG_SNOTIMPLEMENTED; \
165  }
167 #undef BACKEND_GENERIC_CENTER_MATRIX
168 
174 #define BACKEND_GENERIC_CHOLESKY_FACTOR(Type, Container) \
175  virtual Container<Type> cholesky_factor( \
176  const Container<Type>& A, const bool lower) const \
177  { \
178  SG_SNOTIMPLEMENTED; \
179  return 0; \
180  }
182 #undef BACKEND_GENERIC_CHOLESKY_FACTOR
183 
189 #define BACKEND_GENERIC_CHOLESKY_SOLVER(Type, Container) \
190  virtual SGVector<Type> cholesky_solver( \
191  const Container<Type>& L, const SGVector<Type>& b, const bool lower) \
192  const \
193  { \
194  SG_SNOTIMPLEMENTED; \
195  return 0; \
196  }
198 #undef BACKEND_GENERIC_CHOLESKY_SOLVER
199 
205 #define BACKEND_GENERIC_CROSS_ENTROPY(Type, Container) \
206  virtual Type cross_entropy( \
207  const Container<Type>& P, const Container<Type>& Q) const \
208  { \
209  SG_SNOTIMPLEMENTED; \
210  return 0; \
211  }
213 #undef BACKEND_GENERIC_CROSS_ENTROPY
214 
220 #define BACKEND_GENERIC_DOT(Type, Container) \
221  virtual Type dot(const Container<Type>& a, const Container<Type>& b) const \
222  { \
223  SG_SNOTIMPLEMENTED; \
224  return 0; \
225  }
227 #undef BACKEND_GENERIC_DOT
228 
234 #define BACKEND_GENERIC_EIGEN_SOLVER(Type, Container) \
235  virtual void eigen_solver( \
236  const Container<Type>& A, SGVector<Type>& eigenvalues, \
237  SGMatrix<Type>& eigenvectors) const \
238  { \
239  SG_SNOTIMPLEMENTED; \
240  }
242 #undef BACKEND_GENERIC_EIGEN_SOLVER
243 
250 #define BACKEND_GENERIC_EIGEN_SOLVER_SYMMETRIC(Type, Container) \
251  virtual void eigen_solver_symmetric( \
252  const Container<Type>& A, SGVector<Type>& eigenvalues, \
253  SGMatrix<Type>& eigenvectors, index_t k) const \
254  { \
255  SG_SNOTIMPLEMENTED; \
256  }
259 #undef BACKEND_GENERIC_EIGEN_SOLVER_SYMMETRIC
260 
266 #define BACKEND_GENERIC_IN_PLACE_ELEMENT_PROD(Type, Container) \
267  virtual void element_prod( \
268  Container<Type>& a, Container<Type>& b, Container<Type>& result) const \
269  { \
270  SG_SNOTIMPLEMENTED; \
271  }
273 #undef BACKEND_GENERIC_IN_PLACE_ELEMENT_PROD
274 
280 #define BACKEND_GENERIC_IN_PLACE_BLOCK_ELEMENT_PROD(Type, Container) \
281  virtual void element_prod( \
282  linalg::Block<Container<Type>>& a, linalg::Block<Container<Type>>& b, \
283  Container<Type>& result) const \
284  { \
285  SG_SNOTIMPLEMENTED; \
286  }
289 #undef BACKEND_GENERIC_IN_PLACE_BLOCK_ELEMENT_PROD
290 
296 #define BACKEND_GENERIC_EXPONENT(Type, Container) \
297  virtual void exponent(const Container<Type>& a, Container<Type>& result) \
298  const \
299  { \
300  SG_SNOTIMPLEMENTED; \
301  }
304 #undef BACKEND_GENERIC_EXPONENT
305 
311 #define BACKEND_GENERIC_IDENTITY(Type, Container) \
312  virtual void identity(Container<Type>& identity_matrix) const \
313  { \
314  SG_SNOTIMPLEMENTED; \
315  return; \
316  }
318 #undef BACKEND_GENERIC_IDENTITY
319 
325 #define BACKEND_GENERIC_LOGISTIC(Type, Container) \
326  virtual void logistic(Container<Type>& a, Container<Type>& result) const \
327  { \
328  SG_SNOTIMPLEMENTED; \
329  }
331 #undef BACKEND_GENERIC_LOGISTIC
332 
338 #define BACKEND_GENERIC_IN_PLACE_MATRIX_PROD(Type, Container) \
339  virtual void matrix_prod( \
340  SGMatrix<Type>& a, Container<Type>& b, Container<Type>& result, \
341  bool transpose_A, bool transpose_B) const \
342  { \
343  SG_SNOTIMPLEMENTED; \
344  }
347 #undef BACKEND_GENERIC_IN_PLACE_MATRIX_PROD
348 
355 #define BACKEND_GENERIC_MAX(Type, Container) \
356  virtual Type max(const Container<Type>& a) const \
357  { \
358  SG_SNOTIMPLEMENTED; \
359  return 0; \
360  }
363 #undef BACKEND_GENERIC_MAX
364 
371 #define BACKEND_GENERIC_REAL_MEAN(Type, Container) \
372  virtual float64_t mean(const Container<Type>& a) const \
373  { \
374  SG_SNOTIMPLEMENTED; \
375  return 0; \
376  }
379 #undef BACKEND_GENERIC_REAL_MEAN
380 
387 #define BACKEND_GENERIC_COMPLEX_MEAN(Container) \
388  virtual complex128_t mean(const Container<complex128_t>& a) const \
389  { \
390  SG_SNOTIMPLEMENTED; \
391  return 0; \
392  }
395 #undef BACKEND_GENERIC_COMPLEX_MEAN
396 
403 #define BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV(Type, Container) \
404  virtual void multiply_by_logistic_derivative( \
405  Container<Type>& a, Container<Type>& result) const \
406  { \
407  SG_SNOTIMPLEMENTED; \
408  }
411 #undef BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV
412 
418 #define BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV(Type, Container) \
419  virtual void multiply_by_rectified_linear_derivative( \
420  Container<Type>& a, Container<Type>& result) const \
421  { \
422  SG_SNOTIMPLEMENTED; \
423  }
426 #undef BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV
427 
433 #define BACKEND_GENERIC_RANGE_FILL(Type, Container) \
434  virtual void range_fill(Container<Type>& a, const Type start) const \
435  { \
436  SG_SNOTIMPLEMENTED; \
437  }
440 #undef BACKEND_GENERIC_RANGE_FILL
441 
447 #define BACKEND_GENERIC_RECTIFIED_LINEAR(Type, Container) \
448  virtual void rectified_linear(Container<Type>& a, Container<Type>& result) \
449  const \
450  { \
451  SG_SNOTIMPLEMENTED; \
452  }
454 #undef BACKEND_GENERIC_RECTIFIED_LINEAR
455 
462 #define BACKEND_GENERIC_QR_SOLVER(Type, Container) \
463  virtual Container<Type> qr_solver( \
464  const SGMatrix<Type>& A, const Container<Type>& b) const \
465  { \
466  SG_SNOTIMPLEMENTED; \
467  return 0; \
468  }
471 #undef BACKEND_GENERIC_QR_SOLVER
472 
478 #define BACKEND_GENERIC_IN_PLACE_SCALE(Type, Container) \
479  virtual void scale( \
480  Container<Type>& a, Type alpha, Container<Type>& result) const \
481  { \
482  SG_SNOTIMPLEMENTED; \
483  }
486 #undef BACKEND_GENERIC_IN_PLACE_SCALE
487 
493 #define BACKEND_GENERIC_SET_CONST(Type, Container) \
494  virtual void set_const(Container<Type>& a, const Type value) const \
495  { \
496  SG_SNOTIMPLEMENTED; \
497  }
500 #undef BACKEND_GENERIC_SET_CONST
501 
507 #define BACKEND_GENERIC_SUM(Type, Container) \
508  virtual Type sum(const Container<Type>& a, bool no_diag) const \
509  { \
510  SG_SNOTIMPLEMENTED; \
511  return 0; \
512  }
515 #undef BACKEND_GENERIC_SUM
516 
522 #define BACKEND_GENERIC_SOFTMAX(Type, Container) \
523  virtual void softmax(Container<Type>& a) const \
524  { \
525  SG_SNOTIMPLEMENTED; \
526  }
528 #undef BACKEND_GENERIC_SOFTMAX
529 
535 #define BACKEND_GENERIC_SQUARED_ERROR(Type, Container) \
536  virtual Type squared_error( \
537  const Container<Type>& P, const Container<Type>& Q) const \
538  { \
539  SG_SNOTIMPLEMENTED; \
540  return 0; \
541  }
543 #undef BACKEND_GENERIC_SQUARED_ERROR
544 
550 #define BACKEND_GENERIC_BLOCK_SUM(Type, Container) \
551  virtual Type sum(const linalg::Block<Container<Type>>& a, bool no_diag) \
552  const \
553  { \
554  SG_SNOTIMPLEMENTED; \
555  return 0; \
556  }
558 #undef BACKEND_GENERIC_BLOCK_SUM
559 
565 #define BACKEND_GENERIC_SYMMETRIC_SUM(Type, Container) \
566  virtual Type sum_symmetric(const Container<Type>& a, bool no_diag) const \
567  { \
568  SG_SNOTIMPLEMENTED; \
569  return 0; \
570  }
572 #undef BACKEND_GENERIC_SYMMETRIC_SUM
573 
579 #define BACKEND_GENERIC_SYMMETRIC_BLOCK_SUM(Type, Container) \
580  virtual Type sum_symmetric( \
581  const linalg::Block<Container<Type>>& a, bool no_diag) const \
582  { \
583  SG_SNOTIMPLEMENTED; \
584  return 0; \
585  }
587 #undef BACKEND_GENERIC_SYMMETRIC_BLOCK_SUM
588 
594 #define BACKEND_GENERIC_COLWISE_SUM(Type, Container) \
595  virtual SGVector<Type> colwise_sum(const Container<Type>& a, bool no_diag) \
596  const \
597  { \
598  SG_SNOTIMPLEMENTED; \
599  return 0; \
600  }
602 #undef BACKEND_GENERIC_COLWISE_SUM
603 
609 #define BACKEND_GENERIC_BLOCK_COLWISE_SUM(Type, Container) \
610  virtual SGVector<Type> colwise_sum( \
611  const linalg::Block<Container<Type>>& a, bool no_diag) const \
612  { \
613  SG_SNOTIMPLEMENTED; \
614  return 0; \
615  }
617 #undef BACKEND_GENERIC_BLOCK_COLWISE_SUM
618 
624 #define BACKEND_GENERIC_ROWWISE_SUM(Type, Container) \
625  virtual SGVector<Type> rowwise_sum(const Container<Type>& a, bool no_diag) \
626  const \
627  { \
628  SG_SNOTIMPLEMENTED; \
629  return 0; \
630  }
632 #undef BACKEND_GENERIC_ROWWISE_SUM
633 
639 #define BACKEND_GENERIC_BLOCK_ROWWISE_SUM(Type, Container) \
640  virtual SGVector<Type> rowwise_sum( \
641  const linalg::Block<Container<Type>>& a, bool no_diag) const \
642  { \
643  SG_SNOTIMPLEMENTED; \
644  return 0; \
645  }
647 #undef BACKEND_GENERIC_BLOCK_ROWWISE_SUM
648 
654 #define BACKEND_GENERIC_SVD(Type, Container) \
655  virtual void svd( \
656  const Container<Type>& A, SGVector<Type> s, SGMatrix<Type> U, \
657  bool thin_U, linalg::SVDAlgorithm alg) const \
658  { \
659  SG_SNOTIMPLEMENTED; \
660  }
662 #undef BACKEND_GENERIC_SVD
663 
669 #define BACKEND_GENERIC_TRACE(Type, Container) \
670  virtual Type trace(const Container<Type>& A) const \
671  { \
672  SG_SNOTIMPLEMENTED; \
673  return 0; \
674  }
676 #undef BACKEND_GENERIC_TRACE
677 
683 #define BACKEND_GENERIC_TRANSPOSE_MATRIX(Type, Container) \
684  virtual Container<Type> transpose_matrix(const Container<Type>& A) const \
685  { \
686  SG_SNOTIMPLEMENTED; \
687  return 0; \
688  }
690 #undef BACKEND_GENERIC_TRANSPOSE_MATRIX
691 
697 #define BACKEND_GENERIC_TRIANGULAR_SOLVER(Type, Container) \
698  virtual Container<Type> triangular_solver( \
699  const SGMatrix<Type>& L, const Container<Type>& b, \
700  const bool lower = true) const \
701  { \
702  SG_SNOTIMPLEMENTED; \
703  return 0; \
704  }
709 #undef BACKEND_GENERIC_TRIANGULAR_SOLVER
710 
716 #define BACKEND_GENERIC_ZERO(Type, Container) \
717  virtual void zero(Container<Type>& a) const \
718  { \
719  SG_SNOTIMPLEMENTED; \
720  return; \
721  }
724 #undef BACKEND_GENERIC_ZERO
725 
732 #define BACKEND_GENERIC_TO_GPU(Type, Container) \
733  virtual GPUMemoryBase<Type>* to_gpu(const Container<Type>&) const \
734  { \
735  SG_SNOTIMPLEMENTED; \
736  return 0; \
737  }
740 #undef BACKEND_GENERIC_TO_GPU
741 
747 #define BACKEND_GENERIC_FROM_GPU(Type, Container) \
748  virtual void from_gpu(const Container<Type>&, Type* data) const \
749  { \
750  SG_SNOTIMPLEMENTED; \
751  }
754 #undef BACKEND_GENERIC_FROM_GPU
755 
756 #undef DEFINE_FOR_ALL_PTYPE
757 #undef DEFINE_FOR_REAL_PTYPE
758 #undef DEFINE_FOR_NON_INTEGER_PTYPE
759  };
760 }
761 
762 #endif // LINALG_BACKEND_BASE_H__
#define BACKEND_GENERIC_MAX(Type, Container)
#define BACKEND_GENERIC_IDENTITY(Type, Container)
#define BACKEND_GENERIC_ADD_COL_VEC(Type, Container)
#define BACKEND_GENERIC_IN_PLACE_ADD(Type, Container)
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_EIGEN_SOLVER_SYMMETRIC, SGMatrix) DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_IN_PLACE_BLOCK_ELEMENT_PROD
#define BACKEND_GENERIC_CROSS_ENTROPY(Type, Container)
#define BACKEND_GENERIC_QR_SOLVER(Type, Container)
#define BACKEND_GENERIC_ROWWISE_SUM(Type, Container)
#define BACKEND_GENERIC_SOFTMAX(Type, Container)
#define BACKEND_GENERIC_RANGE_FILL(Type, Container)
#define BACKEND_GENERIC_IN_PLACE_SCALE(Type, Container)
#define BACKEND_GENERIC_SVD(Type, Container)
#define BACKEND_GENERIC_SQUARED_ERROR(Type, Container)
#define BACKEND_GENERIC_ADD_VECTOR(Type, Container)
#define BACKEND_GENERIC_SET_CONST(Type, Container)
#define BACKEND_GENERIC_RECTIFIED_LINEAR(Type, Container)
#define BACKEND_GENERIC_REAL_MEAN(Type, Container)
#define BACKEND_GENERIC_CHOLESKY_SOLVER(Type, Container)
#define BACKEND_GENERIC_TRANSPOSE_MATRIX(Type, Container)
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV, SGMatrix) DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV
#define BACKEND_GENERIC_DOT(Type, Container)
#define BACKEND_GENERIC_LOGISTIC(Type, Container)
#define BACKEND_GENERIC_SYMMETRIC_SUM(Type, Container)
#define BACKEND_GENERIC_SUM(Type, Container)
#define BACKEND_GENERIC_TRIANGULAR_SOLVER(Type, Container)
#define BACKEND_GENERIC_EXPONENT(Type, Container)
#define DEFINE_FOR_REAL_PTYPE(METHODNAME, Container)
#define BACKEND_GENERIC_COMPLEX_MEAN(Container)
#define BACKEND_GENERIC_ZERO(Type, Container)
#define BACKEND_GENERIC_SYMMETRIC_BLOCK_SUM(Type, Container)
#define BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV(Type, Container)
#define BACKEND_GENERIC_FROM_GPU(Type, Container)
#define BACKEND_GENERIC_IN_PLACE_MATRIX_PROD(Type, Container)
#define BACKEND_GENERIC_BLOCK_COLWISE_SUM(Type, Container)
#define BACKEND_GENERIC_CHOLESKY_FACTOR(Type, Container)
#define BACKEND_GENERIC_TRACE(Type, Container)
#define BACKEND_GENERIC_ADD_SCALAR(Type, Container)
#define BACKEND_GENERIC_EIGEN_SOLVER_SYMMETRIC(Type, Container)
#define BACKEND_GENERIC_IN_PLACE_ELEMENT_PROD(Type, Container)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Base interface of generic linalg methods and generic memory transfer methods.
#define BACKEND_GENERIC_TO_GPU(Type, Container)
#define BACKEND_GENERIC_BLOCK_SUM(Type, Container)
#define BACKEND_GENERIC_EIGEN_SOLVER(Type, Container)
#define BACKEND_GENERIC_COLWISE_SUM(Type, Container)
#define BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV(Type, Container)
#define BACKEND_GENERIC_BLOCK_ROWWISE_SUM(Type, Container)
#define BACKEND_GENERIC_IN_PLACE_BLOCK_ELEMENT_PROD(Type, Container)
#define BACKEND_GENERIC_CENTER_MATRIX(Type, Container)

SHOGUN Machine Learning Toolbox - Documentation