Nektar++
Blas.hpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File Blas.hpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: wrapper of functions around standard BLAS routines
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #ifndef NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_BLAS_HPP
36 #define NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_BLAS_HPP
37 
38 #include <boost/core/ignore_unused.hpp>
39 
42 
43 // Translations for using Fortran version of blas
44 namespace Blas
45 {
46  extern "C"
47  {
48  // -- BLAS Level 1:
49  void F77NAME(dcopy) (const int& n, const double *x, const int& incx,
50  double *y, const int& incy);
51  void F77NAME(daxpy) (const int& n, const double& alpha, const double *x,
52  const int& incx, const double *y, const int& incy);
53  void F77NAME(dswap) (const int& n, double *x, const int& incx,
54  double *y, const int& incy);
55  void F77NAME(dscal) (const int& n, const double& alpha, double *x,
56  const int& incx);
57  void F77NAME(drot) (const int& n, double *x, const int& incx,
58  double *y, const int& incy, const double& c,
59  const double& s);
60  double F77NAME(ddot) (const int& n, const double *x, const int& incx,
61  const double *y, const int& incy);
62  double F77NAME(dnrm2) (const int& n, const double *x, const int& incx);
63  double F77NAME(dasum) (const int& n, const double *x, const int& incx);
64  int F77NAME(idamax)(const int& n, const double *x, const int& incx);
65 
66  // -- BLAS level 2
67  void F77NAME(dgemv) (const char& trans, const int& m,
68  const int& n, const double& alpha,
69  const double* a, const int& lda,
70  const double* x, const int& incx,
71  const double& beta, double* y, const int& incy);
72 
73  void F77NAME(dgbmv) (const char& trans, const int& m,
74  const int& n, const int& kl, const int& ku,
75  const double& alpha,
76  const double* a, const int& lda,
77  const double* x, const int& incx,
78  const double& beta, double* y, const int& incy);
79 
80  void F77NAME(dtpmv) (const char& uplo, const char& trans, const char& diag,
81  const int& n, const double* ap, double* x, const int& incx);
82 
83  void F77NAME(dspmv) (const char& uplo, const int& n, const double& alpha,
84  const double* a, const double* x, const int& incx,
85  const double& beta, double* y, const int& incy);
86 
87  void F77NAME(dsbmv) (const char& uplo, const int& m,
88  const int& k, const double& alpha,
89  const double* a, const int& lda,
90  const double* x, const int& incx,
91  const double& beta, double* y, const int& incy);
92 
93  // -- BLAS level 3:
94  void F77NAME(dgemm) (const char& trans, const char& transb,
95  const int& m1, const int& n,
96  const int& k, const double& alpha,
97  const double* a, const int& lda,
98  const double* b, const int& ldb,
99  const double& beta, double* c, const int& ldc);
100  }
101 
102  /// \brief BLAS level 1: Copy \a x to \a y
103  static inline void Dcopy (const int& n, const double *x, const int& incx,
104  double *y, const int& incy)
105  {
106  F77NAME(dcopy)(n,x,incx,y,incy);
107  }
108 
109  /// \brief BLAS level 1: y = alpha \a x plus \a y
110  static inline void Daxpy (const int& n, const double& alpha, const double *x,
111  const int& incx, const double *y, const int& incy)
112  {
113  F77NAME(daxpy)(n,alpha,x,incx,y,incy);
114  }
115 
116 
117  /// \brief BLAS level 1: Swap \a x with \a y
118  static inline void Dswap (const int& n,double *x, const int& incx,
119  double *y, const int& incy)
120  {
121  F77NAME(dswap)(n,x,incx,y,incy);
122  }
123 
124  /// \brief BLAS level 1: x = alpha \a x
125  static inline void Dscal (const int& n, const double& alpha, double *x,
126  const int& incx)
127  {
128  F77NAME(dscal)(n,alpha,x,incx);
129  }
130 
131  /// \brief BLAS level 1: Plane rotation by c = cos(theta), s = sin(theta)
132  static inline void Drot (const int& n, double *x, const int& incx,
133  double *y, const int& incy, const double& c,
134  const double& s)
135  {
136  F77NAME(drot)(n,x,incx,y,incy,c,s);
137  }
138 
139  /// \brief BLAS level 1: output = \f$ x^T y \f$
140  static inline double Ddot (const int& n, const double *x, const int& incx,
141  const double *y, const int& incy)
142  {
143  return F77NAME(ddot)(n,x,incx,y,incy);
144  }
145 
146  // \brief BLAS level 1: output = \f$ ||x||_2 \f$
147 
148  static inline double Dnrm2 (const int& n, const double *x, const int& incx)
149  {
150  return F77NAME(dnrm2)(n,x,incx);
151  }
152 
153  /// \brief BLAS level 1: output = \f$ ||x||_1 \f$
154  static inline double Dasum (const int& n, const double *x, const int& incx)
155  {
156  return F77NAME(dasum)(n,x,incx);
157  }
158 
159  /// \brief BLAS level 1: output = 1st value where \f$ |x[i]| = max |x|_1 \f$
160  /// Note it is modified to return a value between (0,n-1) as per
161  /// the standard C convention
162  static inline int Idamax (const int& n, const double *x, const int& incx)
163  {
164  return F77NAME(idamax)(n,x,incx) -1;
165  }
166 
167  /// \brief BLAS level 2: Matrix vector multiply y = A \e x where A[m x n]
168  static inline void Dgemv (const char& trans, const int& m, const int& n,
169  const double& alpha, const double* a, const int& lda,
170  const double* x, const int& incx, const double& beta,
171  double* y, const int& incy)
172  {
173  F77NAME(dgemv) (trans,m,n,alpha,a,lda,x,incx,beta,y,incy);
174  }
175 
176  static inline void Dgbmv (const char& trans, const int& m,
177  const int& n, const int& kl, const int& ku,
178  const double& alpha,
179  const double* a, const int& lda,
180  const double* x, const int& incx,
181  const double& beta, double* y, const int& incy)
182  {
183  F77NAME(dgbmv) (trans, m, n, kl, ku, alpha, a, lda, x, incx, beta, y, incy);
184  }
185 
186 
187  static inline void Dtpmv(const char& uplo, const char& trans, const char& diag,
188  const int& n, const double* ap, double* x, const int& incx)
189  {
190  F77NAME(dtpmv) (uplo, trans, diag, n, ap, x, incx);
191  }
192 
193  /// \brief BLAS level 2: Matrix vector multiply y = A \e x where A
194  /// is symmetric packed
195  static inline void Dspmv (const char& uplo, const int& n, const double& alpha,
196  const double* a, const double* x, const int& incx,
197  const double& beta, double* y, const int& incy)
198  {
199  F77NAME(dspmv) (uplo,n,alpha,a,x,incx,beta,y,incy);
200  }
201 
202  static inline void Dsbmv (const char& uplo, const int& m, const int& k,
203  const double& alpha, const double* a, const int& lda,
204  const double* x, const int& incx, const double& beta,
205  double* y, const int& incy)
206  {
207  F77NAME(dsbmv) (uplo,m,k,alpha,a,lda,x,incx,beta,y,incy);
208  }
209 
210 
211  /// \brief BLAS level 3: Matrix-matrix multiply C = A x B where A[m x n],
212  /// B[n x k], C[m x k]
213  static inline void Dgemm (const char& transa, const char& transb, const int& m,
214  const int& n, const int& k, const double& alpha,
215  const double* a, const int& lda, const double* b,
216  const int& ldb, const double& beta, double* c,
217  const int& ldc)
218  {
219  F77NAME(dgemm) (transa,transb,m,n,k,alpha,a,lda,b,ldb,beta,c,ldc);
220  }
221 
222  // \brief Wrapper to mutliply two (row major) matrices together C =
223  // a*A*B + b*C
224  static inline void Cdgemm(const int M, const int N, const int K, const double a,
225  const double *A, const int ldA, const double * B, const int ldB,
226  const double b, double *C, const int ldC)
227  {
228  boost::ignore_unused(ldA, ldB, ldC);
229  Dgemm('N','N',N,M,K,a,B,N,A,K,b,C,N) ;
230  }
231 }
232 #endif //NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_BLAS_HPP
void F77NAME() dscal(const int &n, const double &alpha, double *x, const int &incx)
void F77NAME() dgbmv(const char &trans, const int &m, const int &n, const int &kl, const int &ku, const double &alpha, const double *a, const int &lda, const double *x, const int &incx, const double &beta, double *y, const int &incy)
static void Dsbmv(const char &uplo, const int &m, const int &k, const double &alpha, const double *a, const int &lda, const double *x, const int &incx, const double &beta, double *y, const int &incy)
Definition: Blas.hpp:202
void F77NAME() dgemv(const char &trans, const int &m, const int &n, const double &alpha, const double *a, const int &lda, const double *x, const int &incx, const double &beta, double *y, const int &incy)
int F77NAME() idamax(const int &n, const double *x, const int &incx)
static double Dasum(const int &n, const double *x, const int &incx)
BLAS level 1: output = .
Definition: Blas.hpp:154
double F77NAME() dnrm2(const int &n, const double *x, const int &incx)
void F77NAME() dcopy(const int &n, const double *x, const int &incx, double *y, const int &incy)
void F77NAME() drot(const int &n, double *x, const int &incx, double *y, const int &incy, const double &c, const double &s)
#define F77NAME(x)
Fortran routines need an underscore.
Definition: TransF77.hpp:46
void F77NAME() dsbmv(const char &uplo, const int &m, const int &k, const double &alpha, const double *a, const int &lda, const double *x, const int &incx, const double &beta, double *y, const int &incy)
static void Dgemm(const char &transa, const char &transb, const int &m, const int &n, const int &k, const double &alpha, const double *a, const int &lda, const double *b, const int &ldb, const double &beta, double *c, const int &ldc)
BLAS level 3: Matrix-matrix multiply C = A x B where A[m x n], B[n x k], C[m x k].
Definition: Blas.hpp:213
static double Dnrm2(const int &n, const double *x, const int &incx)
Definition: Blas.hpp:148
static void Dspmv(const char &uplo, const int &n, const double &alpha, const double *a, const double *x, const int &incx, const double &beta, double *y, const int &incy)
BLAS level 2: Matrix vector multiply y = A x where A is symmetric packed.
Definition: Blas.hpp:195
static void Dgbmv(const char &trans, const int &m, const int &n, const int &kl, const int &ku, const double &alpha, const double *a, const int &lda, const double *x, const int &incx, const double &beta, double *y, const int &incy)
Definition: Blas.hpp:176
static int Idamax(const int &n, const double *x, const int &incx)
BLAS level 1: output = 1st value where Note it is modified to return a value between (0...
Definition: Blas.hpp:162
void F77NAME() dspmv(const char &uplo, const int &n, const double &alpha, const double *a, const double *x, const int &incx, const double &beta, double *y, const int &incy)
void F77NAME() dswap(const int &n, double *x, const int &incx, double *y, const int &incy)
double F77NAME() ddot(const int &n, const double *x, const int &incx, const double *y, const int &incy)
void F77NAME() daxpy(const int &n, const double &alpha, const double *x, const int &incx, const double *y, const int &incy)
static void Dgemv(const char &trans, const int &m, const int &n, const double &alpha, const double *a, const int &lda, const double *x, const int &incx, const double &beta, double *y, const int &incy)
BLAS level 2: Matrix vector multiply y = A x where A[m x n].
Definition: Blas.hpp:168
double F77NAME() dasum(const int &n, const double *x, const int &incx)
static void Dtpmv(const char &uplo, const char &trans, const char &diag, const int &n, const double *ap, double *x, const int &incx)
Definition: Blas.hpp:187
static void Dscal(const int &n, const double &alpha, double *x, const int &incx)
BLAS level 1: x = alpha x.
Definition: Blas.hpp:125
static void Cdgemm(const int M, const int N, const int K, const double a, const double *A, const int ldA, const double *B, const int ldB, const double b, double *C, const int ldC)
Definition: Blas.hpp:224
static void Dswap(const int &n, double *x, const int &incx, double *y, const int &incy)
BLAS level 1: Swap x with y.
Definition: Blas.hpp:118
static double Ddot(const int &n, const double *x, const int &incx, const double *y, const int &incy)
BLAS level 1: output = .
Definition: Blas.hpp:140
void F77NAME() dtpmv(const char &uplo, const char &trans, const char &diag, const int &n, const double *ap, double *x, const int &incx)
void F77NAME() dgemm(const char &trans, const char &transb, const int &m1, const int &n, const int &k, const double &alpha, const double *a, const int &lda, const double *b, const int &ldb, const double &beta, double *c, const int &ldc)
Definition: Blas.hpp:44
static void Drot(const int &n, double *x, const int &incx, double *y, const int &incy, const double &c, const double &s)
BLAS level 1: Plane rotation by c = cos(theta), s = sin(theta)
Definition: Blas.hpp:132
static void Daxpy(const int &n, const double &alpha, const double *x, const int &incx, const double *y, const int &incy)
BLAS level 1: y = alpha x plus y.
Definition: Blas.hpp:110
static void Dcopy(const int &n, const double *x, const int &incx, double *y, const int &incy)
BLAS level 1: Copy x to y.
Definition: Blas.hpp:103