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