Debian Codegraph @

3rdparty/dissection/src/Compiler/blas.hpp

raw
1 /*! \file blas.hpp
2 \brief BLAS function interface
3 \author Xavier Juvigny, ONERA
4 \date Jan. 12th 2005
5 \modification function from NETLIB source with BLAS and CBLAS wrapper
6 \author Atsushi Suzuki, Laboratoire Jacques-Louis Lions
7 \date Mar. 16th 2015
8 \date Jul. 17th 2015
9 \date Nov. 30th 2016
10 */
11
12 // This file is part of Dissection
13 //
14 // Dissection is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 // Linking Dissection statically or dynamically with other modules is making
20 // a combined work based on Disssection. Thus, the terms and conditions of
21 // the GNU General Public License cover the whole combination.
22 //
23 // As a special exception, the copyright holders of Dissection give you
24 // permission to combine Dissection program with free software programs or
25 // libraries that are released under the GNU LGPL and with independent modules
26 // that communicate with Dissection solely through the Dissection-fortran
27 // interface. You may copy and distribute such a system following the terms of
28 // the GNU GPL for Dissection and the licenses of the other code concerned,
29 // provided that you include the source code of that other code when and as
30 // the GNU GPL requires distribution of source code and provided that you do
31 // not modify the Dissection-fortran interface.
32 //
33 // Note that people who make modified versions of Dissection are not obligated
34 // to grant this special exception for their modified versions; it is their
35 // choice whether to do so. The GNU General Public License gives permission to
36 // release a modified version without this exception; this exception also makes
37 // it possible to release a modified version which carries forward this
38 // exception. If you modify the Dissection-fortran interface, this exception
39 // does not apply to your modified version of Dissection, and you must remove
40 // this exception when you distribute your modified version.
41 //
42 // This exception is an additional permission under section 7 of the GNU
43 // General Public License, version 3 ("GPLv3")
44 //
45 // Dissection is distributed in the hope that it will be useful,
46 // but WITHOUT ANY WARRANTY; without even the implied warranty of
47 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 // GNU General Public License for more details.
49 //
50 // You should have received a copy of the GNU General Public License
51 // along with Dissection. If not, see <http://www.gnu.org/licenses/>.
52 //
53
54 #ifndef _COMPILER_BLAS_H
55 # define _COMPILER_BLAS_H
56 # include "Compiler/OptionCompiler.hpp"
57 # include "Compiler/arithmetic.hpp"
58
59 #ifdef BLAS_MKL
60 #define MKL_Complex16 std::complex<double>
61 #include <mkl_cblas.h>
62 #include <mkl_trans.h>
63 typedef MKL_INT BLAS_INT;
64 typedef void BLAS_VOID;
65 #endif
66 #ifdef VECLIB
67 #include <cblas.h>
68 typedef int BLAS_INT;
69 typedef void BLAS_VOID;
70 #endif
71 #ifdef SX_ACE_BLAS
72 #include <cblas.h>
73 typedef int BLAS_INT;
74 typedef void BLAS_VOID;
75 #endif
76 #ifdef OPENBLAS
77 #include "cblas.h"
78 typedef int BLAS_INT;
79 typedef double BLAS_VOID;
80 #endif
81 #ifdef SUNPERF
82 #define floatcomplex std::complex<float>
83 #define doublecomplex std::complex<double>
84 #define _SUNPERF_COMPLEX
85 #include <sunperf.h>
86 typedef int BLAS_INT;
87 typedef void BLAS_VOID;
88 #endif
89 #if (defined(BLAS_GENERIC) || defined(BLAS_FORTRAN))
90 typedef enum {CblasNoTrans=111, CblasTrans=112, CblasConjTrans=113 } CBLAS_TRANSPOSE;
91 typedef enum {CblasUpper=121, CblasLower=122} CBLAS_UPLO;
92 typedef enum {CblasNonUnit=131, CblasUnit=132} CBLAS_DIAG;
93 typedef enum {CblasLeft=141, CblasRight=142} CBLAS_SIDE;
94 #endif
95
96 using std::complex;
97
98 template<typename T, typename U>
99 inline U blas_abs(const T& x) {}
100
101 template<>
102 inline double blas_abs<double, double>(const double &x) {
103 return fabs(x);
104 }
105
106 template<>
107 inline double
108 blas_abs<complex<double>, double>(const complex<double> &x) {
109 return abs(x);
110 }
111
112 template<>
113 inline float blas_abs<float, float>(const float &x) {
114 return fabsf(x);
115 }
116
117 template<>
118 inline double blas_abs<float, double>(const float &x) {
119 return double(fabsf(x));
120 }
121
122 template<>
123 inline float
124 blas_abs<complex<float>, float>(const complex<float> &x) {
125 return abs(x); // using <complex> class
126 }
127
128 template<>
129 inline double
130 blas_abs<complex<float>, double>(const complex<float> &x) {
131 return double(abs(x)); // using <complex> class
132 }
133
134
135 template<>
136 inline quadruple blas_abs<quadruple, quadruple>(const quadruple &x) {
137 quadruple zero(0.0);
138 return (x > zero ? x : (-x));
139 }
140
141 template<>
142 inline quadruple
143 blas_abs<complex<quadruple>, quadruple>(const complex<quadruple> &x) {
144 quadruple xx = x.real();
145 quadruple yy = x.imag();
146 return sqrt<quadruple>(xx * xx + yy * yy);
147 }
148
149 #ifndef NO_OCTRUPLE
150 template<>
151 inline octruple blas_abs<octruple, octruple>(const octruple &x) {
152 octruple zero(0.0);
153 return (x > zero ? x : (-x));
154 }
155
156 template<>
157 inline octruple
158 blas_abs<complex<octruple>, octruple>(const complex<octruple> &x) {
159 octruple xx = x.real();
160 octruple yy = x.imag();
161 return sqrt<octruple>(xx * xx + yy * yy);
162 }
163 #endif
164
165 template<>
166 inline double blas_abs<quadruple, double>(const quadruple &x) {
167 quadruple zero(0.0);
168 return quad2double(x > zero ? x : (-x));
169 }
170
171 template<>
172 inline double
173 blas_abs<complex<quadruple>, double>(const complex<quadruple> &x) {
174 quadruple xx = x.real();
175 quadruple yy = x.imag();
176 return quad2double(sqrt<quadruple>(xx * xx + yy * yy));
177 }
178
179 #ifndef NO_OCTRUPLE
180 template<>
181 inline double blas_abs<octruple, double>(const octruple &x) {
182 octruple zero(0.0);
183 return oct2double(x > zero ? x : (-x));
184 }
185
186 template<>
187 inline double
188 blas_abs<complex<octruple>, double>(const complex<octruple> &x) {
189 octruple xx = x.real();
190 octruple yy = x.imag();
191 return oct2double(sqrt<octruple>(xx * xx + yy * yy));
192 }
193 #endif
194
195 inline double
196 blas_conj(double x)
197 {
198 return x;
199 }
200
201 inline float
202 blas_conj(float x)
203 {
204 return x;
205 }
206
207 inline quadruple
208 blas_conj(const quadruple &x)
209 {
210 return x;
211 }
212
213 #ifndef NO_OCTRUPLE
214 inline octruple
215 blas_conj(const octruple &x)
216 {
217 return x;
218 }
219 #endif
220 inline complex<double>
221 blas_conj(const complex<double> &x)
222 {
223 return std::conj(x);
224 }
225
226 inline complex<float>
227 blas_conj(const complex<float> &x)
228 {
229 return std::conj(x);
230 }
231
232 inline complex<quadruple>
233 blas_conj(const complex<quadruple> &x)
234 {
235 return std::conj(x);
236 }
237
238 #ifndef NO_OCTRUPLE
239 inline complex<octruple>
240 blas_conj(const complex<octruple> &x)
241 {
242 return std::conj(x);
243 }
244 #endif
245 // ====================== Blas subroutine level 1 =========================
246
247 template<typename T>
248 void
249 blas_axpy(const int n, const T &alpha,
250 const T* x, const int incx,
251 T* y, int incy);
252
253 #ifndef BLAS_GENERIC
254 template<>
255 void
256 blas_axpy<double>(const int n, const double &alpha,
257 const double* x, const int incx,
258 double* y, const int incy);
259 template<>
260 void
261 blas_axpy<complex<double> >(const int n, const complex<double> &alpha,
262 const complex<double>* x, const int incx,
263 complex<double>* y, int incy);
264 template<>
265 void
266 blas_axpy<float>(const int n, const float &alpha,
267 const float* x, const int incx,
268 float* y, const int incy);
269 template<>
270 void
271 blas_axpy<complex<float> >(const int n, const complex<float> &alpha,
272 const complex<float>* x, const int incx,
273 complex<float>* y, int incy);
274 #endif
275
276 template<typename T>
277 void
278 blas_copy(const int n, const T* x, const int incx, T* y, const int incy);
279
280 #ifndef BLAS_GENERIC
281 template<>
282 void
283 blas_copy<double>(const int n, const double* x, const int incx, double* y,
284 const int incy);
285
286 template<>
287 void
288 blas_copy<complex<double> >(const int n,
289 const complex<double>* x, const int incx,
290 complex<double>* y, const int incy);
291
292 template<>
293 void
294 blas_copy<float>(const int n, const float* x, const int incx, float* y,
295 const int incy);
296
297 template<>
298 void
299 blas_copy<complex<float> >(const int n,
300 const complex<float>* x, const int incx,
301 complex<float>* y, const int incy);
302 #endif
303 template<typename T>
304 T
305 blas_dot(const int n, const T* x, const int incx,
306 const T* y, const int incy);
307
308 #ifndef BLAS_GENERIC
309 template<>
310 double
311 blas_dot<double>(const int n, const double* x, const int incx,
312 const double* y, const int incy);
313
314 template<>
315 complex<double>
316 blas_dot<complex<double> >(const int n,
317 const complex<double>* x, const int incx,
318 const complex<double>* y, const int incy);
319 template<>
320 float
321 blas_dot<float>(const int n, const float* x, const int incx,
322 const float* y, const int incy);
323
324 template<>
325 complex<float>
326 blas_dot<complex<float> >(const int n,
327 const complex<float>* x, const int incx,
328 const complex<float>* y, const int incy);
329
330 #endif
331 template<typename T>
332 void
333 blas_scal(const int N, const T &alpha,
334 T *X, const int incX);
335
336 #ifndef BLAS_GENERIC
337 template<>
338 void
339 blas_scal<double>(const int N, const double &alpha, double *X, const int incX);
340
341 template<>
342 void
343 blas_scal<complex<double> >(const int N, const complex<double> &alpha,
344 complex<double> *X, const int incX);
345 template<>
346 void
347 blas_scal<float>(const int N, const float &alpha, float *X, const int incX);
348
349 template<>
350 void
351 blas_scal<complex<float> >(const int N, const complex<float> &alpha,
352 complex<float> *X, const int incX);
353
354 #endif
355 // T may be std::complex of U
356 template<typename T, typename U>
357 void
358 blas_scal2(const int N, const U &alpha,
359 T *X, const int incX);
360 #ifndef BLAS_GENERIC
361 template<>
362 void
363 blas_scal2<double, double>(const int N, const double &alpha,
364 double *X, const int incX);
365
366 template<>
367 void
368 blas_scal2<complex<double>, double>(const int N, const double &alpha_,
369 complex<double> *X, const int incX);
370
371 template<>
372 void
373 blas_scal2<float, float>(const int N, const float &alpha,
374 float *X, const int incX);
375
376 template<>
377 void
378 blas_scal2<complex<float>, float>(const int N, const float &alpha_,
379 complex<float> *X, const int incX);
380
381
382 template<>
383 void
384 blas_scal2<quadruple, quadruple>(const int N, const quadruple &alpha,
385 quadruple *X, const int incX);
386
387 template<>
388 void
389 blas_scal2<complex<quadruple>, quadruple>(const int N, const quadruple &alpha_,
390 complex<quadruple> *X, const int incX);
391 #endif
392
393 template<typename T, typename U>
394 int blas_iamax(const int n, const T *x, const int incx);
395 #ifndef BLAS_GENERIC
396 template<>
397 int blas_iamax<double, double>(const int n, const double *x, const int incx);
398 template<>
399 int blas_iamax<complex<double>, double>(const int n,
400 const complex<double> *x,
401 const int incx);
402 template<>
403 int blas_iamax<float, float>(const int n, const float *x, const int incx);
404 template<>
405 int blas_iamax<complex<float>, float>(const int n,
406 const complex<float> *x,
407 const int incx);
408
409 #endif
410 // ====================== Blas subroutine level 2 =========================
411
412 template<typename T>
413 void
414 blas_gemv(const CBLAS_TRANSPOSE trA,
415 const int m, const int n, const T &alpha,
416 const T* A, const int lda, const T* x, const int incx,
417 const T &beta, T* y, const int incy);
418 #ifndef BLAS_GENERIC
419 template<>
420 void
421 blas_gemv<double>(const CBLAS_TRANSPOSE trA,
422 const int m, const int n, const double &alpha,
423 const double* A, const int lda, const double* x,
424 const int incx,
425 const double &beta, double* y, const int incy);
426
427 template<>
428 void
429 blas_gemv<complex<double> >(const CBLAS_TRANSPOSE trA,
430 const int m, const int n,
431 const complex<double> &alpha,
432 const complex<double>* A, const int lda,
433 const complex<double>* x, const int incx,
434 const complex<double> &beta,
435 complex<double>* y, const int incy);
436 template<>
437 void
438 blas_gemv<float>(const CBLAS_TRANSPOSE trA,
439 const int m, const int n, const float &alpha,
440 const float* A, const int lda, const float* x,
441 const int incx,
442 const float &beta, float* y, const int incy);
443
444 template<>
445 void
446 blas_gemv<complex<float> >(const CBLAS_TRANSPOSE trA,
447 const int m, const int n,
448 const complex<float> &alpha,
449 const complex<float>* A, const int lda,
450 const complex<float>* x, const int incx,
451 const complex<float> &beta,
452 complex<float>* y, const int incy);
453 #endif
454 template<typename T>
455 void
456 blas_trsv(const CBLAS_UPLO Uplo,
457 const CBLAS_TRANSPOSE TransA, const CBLAS_DIAG Diag,
458 const int N, const T *A, const int lda, T *X,
459 const int incX);
460 #ifndef BLAS_GENERIC
461 template<>
462 void
463 blas_trsv<double>(const CBLAS_UPLO Uplo,
464 const CBLAS_TRANSPOSE TransA, const CBLAS_DIAG Diag,
465 const int N, const double *A, const int lda, double *X,
466 const int incX);
467
468 template<>
469 void
470 blas_trsv<complex<double> >(const CBLAS_UPLO Uplo,
471 const CBLAS_TRANSPOSE TransA, const CBLAS_DIAG Diag,
472 const int N, const complex<double>* A,
473 const int lda,
474 complex<double> *X, const int incX);
475 template<>
476 void
477 blas_trsv<float>(const CBLAS_UPLO Uplo,
478 const CBLAS_TRANSPOSE TransA, const CBLAS_DIAG Diag,
479 const int N, const float *A, const int lda, float *X,
480 const int incX);
481
482 template<>
483 void
484 blas_trsv<complex<float> >(const CBLAS_UPLO Uplo,
485 const CBLAS_TRANSPOSE TransA, const CBLAS_DIAG Diag,
486 const int N, const complex<float>* A,
487 const int lda,
488 complex<float> *X, const int incX);
489
490 #endif
491
492 template<typename T>
493 void
494 blas_syr(const CBLAS_UPLO Uplo,
495 const int N, const T &alpha,
496 const T *X, const int incX,
497 T *A, const int lda);
498 #ifndef BLAS_GENERIC
499 template<>
500 void
501 blas_syr<double>(const CBLAS_UPLO Uplo,
502 const int N, const double &alpha,
503 const double *X, const int incX,
504 double *A, const int lda);
505
506 template<>
507 void
508 blas_syr<complex<double> >(const CBLAS_UPLO Uplo,
509 const int N, const complex<double> &alpha,
510 const complex<double> *X, const int incX,
511 complex<double> *A, const int lda);
512 template<>
513 void
514 blas_syr<float>(const CBLAS_UPLO Uplo,
515 const int N, const float &alpha,
516 const float *X, const int incX,
517 float *A, const int lda);
518
519 template<>
520 void
521 blas_syr<complex<float> >(const CBLAS_UPLO Uplo,
522 const int N, const complex<float> &alpha,
523 const complex<float> *X, const int incX,
524 complex<float> *A, const int lda);
525
526 #endif
527 template<typename T>
528 void
529 blas_syrc(const CBLAS_UPLO Uplo,
530 const int N, const T &alpha,
531 const T *X, const int incX,
532 T *A, const int lda);
533
534 template<typename T>
535 void
536 blas_syr2(const CBLAS_UPLO Uplo,
537 const int N, const T &alpha,
538 const T *X, const int incX,
539 const T *Y, const int incY,
540 T *A, const int lda);
541
542 #ifndef BLAS_GENERIC
543 template<>
544 void
545 blas_syr2<double>(const CBLAS_UPLO Uplo, const int N,
546 const double &alpha,
547 const double *X, const int incX,
548 const double *Y, const int incY,
549 double *A, const int lda);
550 template<>
551 void
552 blas_syr2<float>(const CBLAS_UPLO Uplo, const int N,
553 const float &alpha,
554 const float *X, const int incX,
555 const float *Y, const int incY,
556 float *A, const int lda);
557 #endif
558
559 template<typename T>
560 void
561 blas_ger(const int M, const int N, const T &alpha,
562 const T *X, const int incX,
563 const T *Y, const int incY,
564 T *A, const int lda);
565
566 #ifndef BLAS_GENERIC
567 template<>
568 void
569 blas_ger<double>(const int M, const int N, const double &alpha,
570 const double *X, const int incX,
571 const double *Y, const int incY,
572 double *A, const int lda);
573
574 template<>
575 void
576 blas_ger<complex<double> >(const int M, const int N,
577 const complex<double> &alpha,
578 const complex<double> *X, const int incX,
579 const complex<double> *Y, const int incY,
580 complex<double> *A, const int lda);
581 template<>
582 void
583 blas_ger<float>(const int M, const int N, const float &alpha,
584 const float *X, const int incX,
585 const float *Y, const int incY,
586 float *A, const int lda);
587
588 template<>
589 void
590 blas_ger<complex<float> >(const int M, const int N,
591 const complex<float> &alpha,
592 const complex<float> *X, const int incX,
593 const complex<float> *Y, const int incY,
594 complex<float> *A, const int lda);
595 #endif
596
597 template<typename T>
598 void
599 blas_gerc(const int M, const int N, const T &alpha,
600 const T *X, const int incX,
601 const T *Y, const int incY,
602 T *A, const int lda);
603
604 #ifndef BLAS_GENERIC
605 template<>
606 void
607 blas_gerc<double>(const int M, const int N, const double &alpha,
608 const double *X, const int incX,
609 const double *Y, const int incY,
610 double *A, const int lda);
611 template<>
612 void
613 blas_gerc<complex<double> >(const int M, const int N,
614 const complex<double> &alpha,
615 const complex<double> *X, const int incX,
616 const complex<double> *Y, const int incY,
617 complex<double> *A, const int lda);
618 template<>
619 void
620 blas_gerc<float>(const int M, const int N, const float &alpha,
621 const float *X, const int incX,
622 const float *Y, const int incY,
623 float *A, const int lda);
624 template<>
625 void
626 blas_gerc<complex<float> >(const int M, const int N,
627 const complex<float> &alpha,
628 const complex<float> *X, const int incX,
629 const complex<float> *Y, const int incY,
630 complex<float> *A, const int lda);
631 #endif
632 // ====================== Blas subroutine level 3 =========================
633
634 template<typename T>
635 void
636 blas_gemm( CBLAS_TRANSPOSE trA, CBLAS_TRANSPOSE trB,
637 int m, int n, int k,
638 const T &alpha,
639 const T* A, int lda,
640 const T* B, int ldb,
641 const T &beta,
642 T* C, int ldc );
643
644 #ifndef BLAS_GENERIC
645 template<>
646 void
647 blas_gemm<double>(CBLAS_TRANSPOSE trA, CBLAS_TRANSPOSE trB,
648 int m, int n, int k,
649 const double &alpha, const double* A, int lda,
650 const double* B, int ldb, const double &beta,
651 double* C, int ldc );
652
653 template<>
654 void
655 blas_gemm<complex<double> >(CBLAS_TRANSPOSE trA, CBLAS_TRANSPOSE trB,
656 int m, int n, int k, const complex<double> &alpha,
657 const complex<double>* A, int lda,
658 const complex<double>* B, int ldb,
659 const complex<double> &beta,
660 complex<double>* C, int ldc );
661 template<>
662 void
663 blas_gemm<float>(CBLAS_TRANSPOSE trA, CBLAS_TRANSPOSE trB,
664 int m, int n, int k,
665 const float &alpha, const float* A, int lda,
666 const float* B, int ldb, const float &beta,
667 float* C, int ldc );
668
669 template<>
670 void
671 blas_gemm<complex<float> >(CBLAS_TRANSPOSE trA, CBLAS_TRANSPOSE trB,
672 int m, int n, int k, const complex<float> &alpha,
673 const complex<float>* A, int lda,
674 const complex<float>* B, int ldb,
675 const complex<float> &beta,
676 complex<float>* C, int ldc );
677 #endif
678
679 template<typename T>
680 void
681 blas_trsm(const CBLAS_SIDE Side,
682 const CBLAS_UPLO Uplo, const CBLAS_TRANSPOSE TransA,
683 const CBLAS_DIAG Diag, const int M, const int N,
684 const T &alpha,
685 const T *A, const int lda,
686 T *B, const int ldb);
687 #ifndef BLAS_GENERIC
688 template<>
689 void
690 blas_trsm<double>(const CBLAS_SIDE Side,
691 const CBLAS_UPLO Uplo, const CBLAS_TRANSPOSE TransA,
692 const CBLAS_DIAG Diag, const int M, const int N,
693 const double &alpha, const double *A, const int lda,
694 double *B, const int ldb);
695
696 template<>
697 void
698 blas_trsm<complex<double> >(const CBLAS_SIDE Side,
699 const CBLAS_UPLO Uplo, const CBLAS_TRANSPOSE TransA,
700 const CBLAS_DIAG Diag, const int M, const int N,
701 const complex<double> &alpha,
702 const complex<double> *A, const int lda,
703 complex<double> *B, const int ldb);
704 template<>
705 void
706 blas_trsm<float>(const CBLAS_SIDE Side,
707 const CBLAS_UPLO Uplo, const CBLAS_TRANSPOSE TransA,
708 const CBLAS_DIAG Diag, const int M, const int N,
709 const float &alpha, const float *A, const int lda,
710 float *B, const int ldb);
711
712 template<>
713 void
714 blas_trsm<complex<float> >(const CBLAS_SIDE Side,
715 const CBLAS_UPLO Uplo, const CBLAS_TRANSPOSE TransA,
716 const CBLAS_DIAG Diag, const int M, const int N,
717 const complex<float> &alpha,
718 const complex<float> *A, const int lda,
719 complex<float> *B, const int ldb);
720 #endif
721 //#ifdef BLAS_MKL
722
723 // ======== for computation of norm and norm^2
724
725 template<typename T, typename U>
726 U blas_l2norm(const int n, T *x, const int incX);
727
728 template<>
729 double blas_l2norm<complex<double>, double>(const int n, complex<double> *x,
730 const int incX);
731
732 template<>
733 float blas_l2norm<complex<float>, float>(const int n, complex<float> *x,
734 const int incX);
735
736 template<>
737 quadruple blas_l2norm<complex<quadruple>, quadruple>(const int n,
738 complex<quadruple> *x,
739 const int incX);
740
741 template<typename T, typename U>
742 U blas_l2norm2(const int n, T *x, const int incX);
743
744 template<>
745 double blas_l2norm2<complex<double>, double>(const int n, complex<double> *x,
746 const int incX);
747
748 template<>
749 float blas_l2norm2<complex<float>, float>(const int n, complex<float> *x,
750 const int incX);
751
752 template<>
753 quadruple blas_l2norm2<complex<quadruple>, quadruple>(const int n,
754 complex<quadruple> *x,
755 const int incX);
756 #endif
757