MCKL
Monte Carlo Kernel Library
cblas.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/internal/cblas.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef MCKL_INTERNAL_CBLAS_HPP
33 #define MCKL_INTERNAL_CBLAS_HPP
34 
35 #include <mckl/internal/config.h>
36 
37 #if MCKL_USE_CBLAS
38 
39 #if MCKL_USE_MKL_CBLAS
40 #include <mkl_cblas.h>
41 #ifndef MCKL_BLAS_INT
42 #define MCKL_BLAS_INT MKL_INT
43 #endif
44 #else
45 #include <cblas.h>
46 #endif
47 
48 #ifndef MCKL_BLAS_INT
49 #define MCKL_BLAS_INT int
50 #endif
51 
52 namespace mckl {
53 
54 namespace internal {
55 
56 using ::CblasColMajor;
57 using ::CblasRowMajor;
58 
59 using ::CblasConjTrans;
60 using ::CblasNoTrans;
61 using ::CblasTrans;
62 
63 using ::CblasLower;
64 using ::CblasUpper;
65 
66 using ::CblasNonUnit;
67 using ::CblasUnit;
68 
69 using ::CblasLeft;
70 using ::CblasRight;
71 
72 using ::cblas_ddot;
73 using ::cblas_sdot;
74 
75 using ::cblas_dgemv;
76 using ::cblas_sgemv;
77 
78 using ::cblas_dtpmv;
79 using ::cblas_stpmv;
80 
81 using ::cblas_dsyr;
82 using ::cblas_ssyr;
83 
84 using ::cblas_dtrmm;
85 using ::cblas_strmm;
86 
87 using ::cblas_dsyrk;
88 using ::cblas_ssyrk;
89 
90 } // namespace internal
91 
92 } // namespace mckl
93 
94 #else // MCKL_USE_CBLAS
95 
96 #ifndef MCKL_BLAS_NAME
97 #ifdef MCKL_BLAS_NAME_NO_UNDERSCORE
98 #define MCKL_BLAS_NAME(x) x
99 #else
100 #define MCKL_BLAS_NAME(x) x##_
101 #endif
102 #endif
103 
104 #ifndef MCKL_BLAS_INT
105 #define MCKL_BLAS_INT int
106 #endif
107 
108 extern "C" {
109 
110 void MCKL_BLAS_NAME(sgemv)(const char *trans, const MCKL_BLAS_INT *m,
111  const MCKL_BLAS_INT *n, const float *alpha, const float *a,
112  const MCKL_BLAS_INT *lda, const float *x, const MCKL_BLAS_INT *incx,
113  const float *beta, float *y, const MCKL_BLAS_INT *incy);
114 
115 void MCKL_BLAS_NAME(dgemv)(const char *trans, const MCKL_BLAS_INT *m,
116  const MCKL_BLAS_INT *n, const double *alpha, const double *a,
117  const MCKL_BLAS_INT *lda, const double *x, const MCKL_BLAS_INT *incx,
118  const double *beta, double *y, const MCKL_BLAS_INT *incy);
119 
120 void MCKL_BLAS_NAME(stpmv)(const char *uplo, const char *trans,
121  const char *diag, const MCKL_BLAS_INT *n, const float *ap, float *x,
122  const MCKL_BLAS_INT *incx);
123 
124 void MCKL_BLAS_NAME(dtpmv)(const char *uplo, const char *trans,
125  const char *diag, const MCKL_BLAS_INT *n, const double *ap, double *x,
126  const MCKL_BLAS_INT *incx);
127 
128 void MCKL_BLAS_NAME(ssyr)(const char *uplo, const MCKL_BLAS_INT *n,
129  const float *alpha, const float *x, const MCKL_BLAS_INT *incx, float *a,
130  const MCKL_BLAS_INT *lda);
131 
132 void MCKL_BLAS_NAME(dsyr)(const char *uplo, const MCKL_BLAS_INT *n,
133  const double *alpha, const double *x, const MCKL_BLAS_INT *incx, double *a,
134  const MCKL_BLAS_INT *lda);
135 
136 void MCKL_BLAS_NAME(strmm)(const char *side, const char *uplo,
137  const char *transa, const char *diag, const MCKL_BLAS_INT *m,
138  const MCKL_BLAS_INT *n, const float *alpha, const float *a,
139  const MCKL_BLAS_INT *lda, float *b, const MCKL_BLAS_INT *ldb);
140 
141 void MCKL_BLAS_NAME(dtrmm)(const char *side, const char *uplo,
142  const char *transa, const char *diag, const MCKL_BLAS_INT *m,
143  const MCKL_BLAS_INT *n, const double *alpha, const double *a,
144  const MCKL_BLAS_INT *lda, double *b, const MCKL_BLAS_INT *ldb);
145 
146 void MCKL_BLAS_NAME(ssyrk)(const char *uplo, const char *trans,
147  const MCKL_BLAS_INT *n, const MCKL_BLAS_INT *k, const float *alpha,
148  const float *a, const MCKL_BLAS_INT *lda, const float *beta, float *c,
149  const MCKL_BLAS_INT *ldc);
150 
151 void MCKL_BLAS_NAME(dsyrk)(const char *uplo, const char *trans,
152  const MCKL_BLAS_INT *n, const MCKL_BLAS_INT *k, const double *alpha,
153  const double *a, const MCKL_BLAS_INT *lda, const double *beta, double *c,
154  const MCKL_BLAS_INT *ldc);
155 
156 } // extern "C"
157 
158 namespace mckl {
159 
160 namespace internal {
161 
162 enum CBLAS_LAYOUT { CblasRowMajor = 101, CblasColMajor = 102 };
163 
164 using CBLAS_ORDER = CBLAS_LAYOUT;
165 
166 enum CBLAS_TRANSPOSE {
167  CblasNoTrans = 111,
168  CblasTrans = 112,
169  CblasConjTrans = 113
170 }; // enum CBLAS_TRANSPOSE
171 
172 enum CBLAS_UPLO { CblasUpper = 121, CblasLower = 122 };
173 
174 enum CBLAS_DIAG { CblasNonUnit = 131, CblasUnit = 132 };
175 
176 enum CBLAS_SIDE { CblasLeft = 141, CblasRight = 142 };
177 
178 inline char cblas_trans(const CBLAS_LAYOUT layout, const CBLAS_TRANSPOSE trans)
179 {
180  if (layout == CblasColMajor) {
181  if (trans == CblasNoTrans)
182  return 'N';
183  else if (trans == CblasTrans)
184  return 'T';
185  else if (trans == CblasConjTrans)
186  return 'C';
187  }
188  if (layout == CblasRowMajor) {
189  if (trans == CblasNoTrans)
190  return 'T';
191  else if (trans == CblasTrans)
192  return 'N';
193  else if (trans == CblasConjTrans)
194  return 'N';
195  }
196  return 'N';
197 }
198 
199 inline char cblas_uplo(const CBLAS_LAYOUT layout, const CBLAS_UPLO uplo)
200 {
201  if (layout == CblasColMajor) {
202  if (uplo == CblasUpper)
203  return 'U';
204  if (uplo == CblasLower)
205  return 'L';
206  }
207  if (layout == CblasRowMajor) {
208  if (uplo == CblasUpper)
209  return 'L';
210  if (uplo == CblasLower)
211  return 'U';
212  }
213  return 'U';
214 }
215 
216 inline char cblas_diag(const CBLAS_DIAG diag)
217 {
218  if (diag == CblasUnit)
219  return 'U';
220  if (diag == CblasNonUnit)
221  return 'N';
222  return 'N';
223 }
224 
225 inline char cblas_side(const CBLAS_LAYOUT layout, const CBLAS_SIDE side)
226 {
227  if (layout == CblasColMajor) {
228  if (side == CblasLeft)
229  return 'L';
230  if (side == CblasRight)
231  return 'R';
232  }
233  if (layout == CblasRowMajor) {
234  if (side == CblasLeft)
235  return 'R';
236  if (side == CblasRight)
237  return 'L';
238  }
239  return 'L';
240 }
241 
242 inline float cblas_sdot(const MCKL_BLAS_INT n, const float *x,
243  const MCKL_BLAS_INT incx, const float *y, const MCKL_BLAS_INT incy)
244 {
245  float s = 0;
246  for (MCKL_BLAS_INT i = 0; i != n; ++i, x += incx, y += incy)
247  s += (*x) * (*y);
248 
249  return s;
250 }
251 
252 inline double cblas_ddot(const MCKL_BLAS_INT n, const double *x,
253  const MCKL_BLAS_INT incx, const double *y, const MCKL_BLAS_INT incy)
254 {
255  double s = 0;
256  for (MCKL_BLAS_INT i = 0; i != n; ++i, x += incx, y += incy)
257  s += (*x) * (*y);
258 
259  return s;
260 }
261 
262 inline void cblas_sgemv(const CBLAS_LAYOUT layout, const CBLAS_TRANSPOSE trans,
263  const MCKL_BLAS_INT m, const MCKL_BLAS_INT n, const float alpha,
264  const float *a, const MCKL_BLAS_INT lda, const float *x,
265  const MCKL_BLAS_INT incx, const float beta, float *y,
266  const MCKL_BLAS_INT incy)
267 {
268  const char transf = cblas_trans(layout, trans);
269  MCKL_BLAS_NAME(sgemv)
270  (&transf, &m, &n, &alpha, a, &lda, x, &incx, &beta, y, &incy);
271 }
272 
273 inline void cblas_dgemv(const CBLAS_LAYOUT layout, const CBLAS_TRANSPOSE trans,
274  const MCKL_BLAS_INT m, const MCKL_BLAS_INT n, const double alpha,
275  const double *a, const MCKL_BLAS_INT lda, const double *x,
276  const MCKL_BLAS_INT incx, const double beta, double *y,
277  const MCKL_BLAS_INT incy)
278 {
279  const char transf = cblas_trans(layout, trans);
280  MCKL_BLAS_NAME(dgemv)
281  (&transf, &m, &n, &alpha, a, &lda, x, &incx, &beta, y, &incy);
282 }
283 
284 inline void cblas_stpmv(const CBLAS_LAYOUT layout, const CBLAS_UPLO uplo,
285  const CBLAS_TRANSPOSE trans, const CBLAS_DIAG diag, const MCKL_BLAS_INT n,
286  const float *ap, float *x, const MCKL_BLAS_INT incx)
287 {
288  const char uplof = cblas_uplo(layout, uplo);
289  const char transf = cblas_trans(layout, trans);
290  const char diagf = cblas_diag(diag);
291  MCKL_BLAS_NAME(stpmv)(&uplof, &transf, &diagf, &n, ap, x, &incx);
292 }
293 
294 inline void cblas_dtpmv(CBLAS_LAYOUT layout, const CBLAS_UPLO uplo,
295  const CBLAS_TRANSPOSE trans, const CBLAS_DIAG diag, const MCKL_BLAS_INT n,
296  const double *ap, double *x, const MCKL_BLAS_INT incx)
297 {
298  const char uplof = cblas_uplo(layout, uplo);
299  const char transf = cblas_trans(layout, trans);
300  const char diagf = cblas_diag(diag);
301  MCKL_BLAS_NAME(dtpmv)(&uplof, &transf, &diagf, &n, ap, x, &incx);
302 }
303 
304 inline void cblas_ssyr(const CBLAS_LAYOUT layout, const CBLAS_UPLO uplo,
305  const MCKL_BLAS_INT n, const float alpha, const float *x,
306  const MCKL_BLAS_INT incx, float *a, const MCKL_BLAS_INT lda)
307 {
308  const char uplof = cblas_uplo(layout, uplo);
309  MCKL_BLAS_NAME(ssyr)(&uplof, &n, &alpha, x, &incx, a, &lda);
310 }
311 
312 inline void cblas_dsyr(const CBLAS_LAYOUT layout, const CBLAS_UPLO uplo,
313  const MCKL_BLAS_INT n, const double alpha, const double *x,
314  const MCKL_BLAS_INT incx, double *a, const MCKL_BLAS_INT lda)
315 {
316  const char uplof = cblas_uplo(layout, uplo);
317  MCKL_BLAS_NAME(dsyr)(&uplof, &n, &alpha, x, &incx, a, &lda);
318 }
319 
320 inline void cblas_strmm(const CBLAS_LAYOUT layout, const CBLAS_SIDE side,
321  const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE trans, const CBLAS_DIAG diag,
322  const MCKL_BLAS_INT m, const MCKL_BLAS_INT n, const float alpha,
323  const float *a, const MCKL_BLAS_INT lda, float *b, const MCKL_BLAS_INT ldb)
324 {
325  const char sidef = cblas_side(layout, side);
326  const char uplof = cblas_uplo(layout, uplo);
327  const char transf = cblas_trans(CblasColMajor, trans);
328  const char diagf = cblas_diag(diag);
329  MCKL_BLAS_NAME(strmm)
330  (&sidef, &uplof, &transf, &diagf, &m, &n, &alpha, a, &lda, b, &ldb);
331 }
332 
333 inline void cblas_dtrmm(const CBLAS_LAYOUT layout, const CBLAS_SIDE side,
334  const CBLAS_UPLO uplo, const CBLAS_TRANSPOSE trans, const CBLAS_DIAG diag,
335  const MCKL_BLAS_INT m, const MCKL_BLAS_INT n, const double alpha,
336  const double *a, const MCKL_BLAS_INT lda, double *b,
337  const MCKL_BLAS_INT ldb)
338 {
339  const char sidef = cblas_side(layout, side);
340  const char uplof = cblas_uplo(layout, uplo);
341  const char transf = cblas_trans(CblasColMajor, trans);
342  const char diagf = cblas_diag(diag);
343  MCKL_BLAS_NAME(dtrmm)
344  (&sidef, &uplof, &transf, &diagf, &m, &n, &alpha, a, &lda, b, &ldb);
345 }
346 
347 inline void cblas_ssyrk(const CBLAS_LAYOUT layout, const CBLAS_UPLO uplo,
348  const CBLAS_TRANSPOSE trans, const MCKL_BLAS_INT n, const MCKL_BLAS_INT k,
349  const float alpha, const float *a, const MCKL_BLAS_INT lda,
350  const float beta, float *c, const MCKL_BLAS_INT ldc)
351 {
352  const char uplof = cblas_uplo(layout, uplo);
353  const char transf = cblas_trans(layout, trans);
354  MCKL_BLAS_NAME(ssyrk)
355  (&uplof, &transf, &n, &k, &alpha, a, &lda, &beta, c, &ldc);
356 }
357 
358 inline void cblas_dsyrk(const CBLAS_LAYOUT layout, const CBLAS_UPLO uplo,
359  const CBLAS_TRANSPOSE trans, const MCKL_BLAS_INT n, const MCKL_BLAS_INT k,
360  const double alpha, const double *a, const MCKL_BLAS_INT lda,
361  const double beta, double *c, const MCKL_BLAS_INT ldc)
362 {
363  const char uplof = cblas_uplo(layout, uplo);
364  const char transf = cblas_trans(layout, trans);
365  MCKL_BLAS_NAME(dsyrk)
366  (&uplof, &transf, &n, &k, &alpha, a, &lda, &beta, c, &ldc);
367 }
368 
369 } // namespace internal
370 
371 } // namespace mckl
372 
373 #endif // MCKL_USE_CBLAS
374 
375 #endif // MCKL_INTERNAL_CBLAS_HPP
#define MCKL_BLAS_INT
Definition: cblas.hpp:42
Definition: mcmc.hpp:40