Visual Servoing Platform  version 3.4.0
vpMatrix_mul.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * BLAS subroutines.
33  *
34  *****************************************************************************/
35 
36 #include <visp3/core/vpConfig.h>
37 #include <visp3/core/vpMatrix.h>
38 
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 
41 // Since GSL doesn't provide Fortran interface for Lapack we should use
42 // gsl_blas_dgemm() and gsl_blas_dgemv() instead of dgemm() and dgemv().
43 // As a side effect, it means that we have to allocate and copy the matrix
44 // or vector content in a gsl_matrix or gsl_vector. This is not acceptable here.
45 // that's why we prefer use naive code when VISP_HAVE_GSL is defined.
46 #if defined(VISP_HAVE_LAPACK)
47 # ifdef VISP_HAVE_MKL
48 #include <mkl.h>
49 typedef MKL_INT integer;
50 
51 void vpMatrix::blas_dgemm(char trans_a, char trans_b, unsigned int M_, unsigned int N_, unsigned int K_, double alpha,
52  double *a_data, unsigned int lda_, double *b_data, unsigned int ldb_, double beta, double *c_data,
53  unsigned int ldc_)
54 {
55  MKL_INT M = static_cast<MKL_INT>(M_);
56  MKL_INT N = static_cast<MKL_INT>(N_);
57  MKL_INT K = static_cast<MKL_INT>(K_);
58  MKL_INT lda = static_cast<MKL_INT>(lda_);
59  MKL_INT ldb = static_cast<MKL_INT>(ldb_);
60  MKL_INT ldc = static_cast<MKL_INT>(ldc_);
61 
62  dgemm(&trans_a, &trans_b, &M, &N, &K, &alpha, a_data, &lda, b_data, &ldb, &beta, c_data, &ldc);
63 }
64 
65 void vpMatrix::blas_dgemv(char trans, unsigned int M_, unsigned int N_, double alpha, double *a_data, unsigned int lda_,
66  double *x_data, int incx_, double beta, double *y_data, int incy_)
67 {
68  MKL_INT M = static_cast<MKL_INT>(M_);
69  MKL_INT N = static_cast<MKL_INT>(N_);
70  MKL_INT lda = static_cast<MKL_INT>(lda_);
71  MKL_INT incx = static_cast<MKL_INT>(incx_);
72  MKL_INT incy = static_cast<MKL_INT>(incy_);
73 
74  dgemv(&trans, &M, &N, &alpha, a_data, &lda, x_data, &incx, &beta, y_data, &incy);
75 }
76 # elif !defined(VISP_HAVE_GSL)
77 # ifdef VISP_HAVE_LAPACK_BUILT_IN
78 typedef long int integer;
79 # else
80 typedef int integer;
81 # endif
82 
83 extern "C" void dgemm_(char *transa, char *transb, integer *M, integer *N, integer *K, double *alpha, double *a,
84  integer *lda, double *b, integer *ldb, double *beta, double *c, integer *ldc);
85 
86 extern "C" void dgemv_(char *trans, integer *M, integer *N, double *alpha, double *a, integer *lda, double *x,
87  integer *incx, double *beta, double *y, integer *incy);
88 
89 void vpMatrix::blas_dgemm(char trans_a, char trans_b, unsigned int M_, unsigned int N_, unsigned int K_, double alpha,
90  double *a_data, unsigned int lda_, double *b_data, unsigned int ldb_, double beta, double *c_data,
91  unsigned int ldc_)
92 {
93  integer M = static_cast<integer>(M_);
94  integer K = static_cast<integer>(K_);
95  integer N = static_cast<integer>(N_);
96  integer lda = static_cast<integer>(lda_);
97  integer ldb = static_cast<integer>(ldb_);
98  integer ldc = static_cast<integer>(ldc_);
99 
100  dgemm_(&trans_a, &trans_b, &M, &N, &K, &alpha, a_data, &lda, b_data, &ldb, &beta, c_data, &ldc);
101 }
102 
103 void vpMatrix::blas_dgemv(char trans, unsigned int M_, unsigned int N_, double alpha, double *a_data, unsigned int lda_,
104  double *x_data, int incx_, double beta, double *y_data, int incy_)
105 {
106  integer M = static_cast<integer>(M_);
107  integer N = static_cast<integer>(N_);
108  integer lda = static_cast<integer>(lda_);
109  integer incx = static_cast<integer>(incx_);
110  integer incy = static_cast<integer>(incy_);
111 
112  dgemv_(&trans, &M, &N, &alpha, a_data, &lda, x_data, &incx, &beta, y_data, &incy);
113 }
114 # endif
115 #else
116 // Work arround to avoid warning LNK4221: This object file does not define any
117 // previously undefined public symbols
118 void dummy_vpMatrix_blas() {};
119 #endif
120 
121 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS