Logo ROOT   6.10/09
Reference Guide
Blas.h
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Simon Pfreundschuh 20/07/16
3 
4 /*************************************************************************
5  * Copyright (C) 2016, Simon Pfreundschuh *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 ///////////////////////////////////////////////////////////////////
13 // Declarations of the BLAS functions used for the forward and //
14 // backward propagation of activation through neural networks on //
15 // CPUs. //
16 ///////////////////////////////////////////////////////////////////
17 
18 #ifndef TMVA_DNN_ARCHITECTURES_CPU_BLAS
19 #define TMVA_DNN_ARCHITECTURES_CPU_BLAS
20 
21 #include <iostream>
22 
23 // External Library Routines
24 //____________________________________________________________________________
25 extern "C" void saxpy_(const int * n, const float * alpha, const float * x,
26  const int * incx, float * y, const int * incy);
27 extern "C" void daxpy_(const int * n, const double * alpha, const double * x,
28  const int * incx, double * y, const int * incy);
29 extern "C" void sger_(const int * m, const int * n, const float * alpha,
30  const float * x, const int * incx,
31  const float * y, const int * incy,
32  float * A, const int * lda);
33 extern "C" void dger_(const int * m, const int * n, const double * alpha,
34  const double * x, const int * incx,
35  const double * y, const int * incy,
36  double * A, const int * lda);
37 extern "C" void sgemv_(const char * trans, const int * m, const int * n,
38  const float * alpha, const float * A, const int * lda,
39  const float * x, const int * incx,
40  const float * beta, float * y, const int * incy);
41 extern "C" void dgemv_(const char * trans, const int * m, const int * n,
42  const double * alpha, const double * A, const int * lda,
43  const double * x, const int * incx,
44  const double * beta, double * y, const int * incy);
45 extern "C" void dgemm_(const char * transa, const char * transb,
46  const int * m, const int * n, const int * k,
47  const double * alpha, const double * A, const int * lda,
48  const double * B, const int * ldb, const double * beta,
49  double * C, const int * ldc);
50 extern "C" void sgemm_(const char * transa, const char * transb,
51  const int * m, const int * n, const int * k,
52  const float * alpha, const float * A, const int * lda,
53  const float * B, const int * ldb, const float * beta,
54  float * C, const int * ldc);
55 
56 namespace TMVA
57 {
58 namespace DNN
59 {
60 namespace Blas
61 {
62 
63 // Type-Generic Wrappers
64 //____________________________________________________________________________
65 /** Add the vector \p x scaled by \p alpha to \p y scaled by \beta */
66 template <typename Real_t>
67 inline void Axpy(const int * n, const Real_t * alpha,
68  const Real_t * x, const int * incx,
69  Real_t * y, const int * incy);
70 
71 /** Multiply the vector \p x with the matrix \p A and store the result in \p y. */
72 template <typename Real_t>
73 inline void Gemv(const char *trans, const int * m, const int * n,
74  const Real_t * alpha, const Real_t * A, const int * lda,
75  const Real_t * x, const int * incx,
76  const Real_t * beta, Real_t * y, const int * incy);
77 
78 /** Multiply the matrix \p A with the matrix \p B and store the result in \p C. */
79 template <typename Real_t>
80 inline void Gemm(const char *transa, const char *transb,
81  const int * m, const int * n, const int* k,
82  const Real_t * alpha, const Real_t * A, const int * lda,
83  const Real_t * B, const int * ldb, const Real_t * beta,
84  Real_t * C, const int * ldc);
85 
86 /** Add the outer product of \p x and \p y to the matrix \p A. */
87 template <typename Real_t>
88 inline void Ger(const int * m, const int * n, const Real_t * alpha,
89  const Real_t * x, const int * incx,
90  const Real_t * y, const int * incy,
91  Real_t * A, const int * lda);
92 
93 // Specializations
94 //____________________________________________________________________________
95 template<>
96 inline void Axpy<double>(const int * n, const double * alpha,
97  const double * x, const int * incx,
98  double * y, const int * incy)
99 {
100  daxpy_(n, alpha, x, incx, y, incy);
101 }
102 
103 template<>
104 inline void Axpy<float>(const int * n, const float * alpha,
105  const float * x, const int * incx,
106  float * y, const int * incy)
107 {
108  saxpy_(n, alpha, x, incx, y, incy);
109 }
110 
111 template<>
112 inline void Gemv<double>(const char *trans, const int * m, const int * n,
113  const double * alpha, const double * A, const int * lda,
114  const double * x, const int * incx,
115  const double * beta, double * y, const int * incy)
116 {
117  dgemv_(trans, m, n, alpha, A, lda, x, incx, beta, y, incy);
118 }
119 
120 template<>
121 inline void Gemv<float>(const char *trans, const int * m, const int * n,
122  const float * alpha, const float * A, const int * lda,
123  const float * x, const int * incx,
124  const float * beta, float * y, const int * incy)
125 {
126  sgemv_(trans, m, n, alpha, A, lda, x, incx, beta, y, incy);
127 }
128 
129 template<>
130 inline void Gemm<double>(const char *transa, const char *transb,
131  const int * m, const int * n, const int* k,
132  const double * alpha, const double * A, const int * lda,
133  const double * B, const int * ldb, const double * beta,
134  double * C, const int * ldc)
135 {
136  dgemm_(transa, transb, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
137 }
138 
139 template<>
140 inline void Gemm<float>(const char *transa, const char *transb,
141  const int * m, const int * n, const int* k,
142  const float * alpha, const float * A, const int * lda,
143  const float * B, const int * ldb, const float * beta,
144  float * C, const int * ldc)
145 {
146  sgemm_(transa, transb, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
147 }
148 
149 template <>
150 inline void Ger<double>(const int * m, const int * n, const double * alpha,
151  const double * x, const int * incx,
152  const double * y, const int * incy,
153  double * A, const int * lda)
154 {
155  dger_(m, n, alpha, x, incx, y, incy, A, lda);
156 }
157 
158 template <>
159 inline void Ger<float>(const int * m, const int * n, const float * alpha,
160  const float * x, const int * incx,
161  const float * y, const int * incy,
162  float * A, const int * lda)
163 {
164  sger_(m, n, alpha, x, incx, y, incy, A, lda);
165 }
166 
167 } // namespace Blas
168 } // namespace DNN
169 } // namespace TMVA
170 
171 #endif
static double B[]
void sgemv_(const char *trans, const int *m, const int *n, const float *alpha, const float *A, const int *lda, const float *x, const int *incx, const float *beta, float *y, const int *incy)
void Axpy< double >(const int *n, const double *alpha, const double *x, const int *incx, double *y, const int *incy)
Definition: Blas.h:96
void Ger(const int *m, const int *n, const Real_t *alpha, const Real_t *x, const int *incx, const Real_t *y, const int *incy, Real_t *A, const int *lda)
Add the outer product of x and y to the matrix A.
void Ger< float >(const int *m, const int *n, const float *alpha, const float *x, const int *incx, const float *y, const int *incy, float *A, const int *lda)
Definition: Blas.h:159
void Axpy< float >(const int *n, const float *alpha, const float *x, const int *incx, float *y, const int *incy)
Definition: Blas.h:104
static double A[]
void saxpy_(const int *n, const float *alpha, const float *x, const int *incx, float *y, const int *incy)
double beta(double x, double y)
Calculates the beta function.
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)
void Gemm(const char *transa, const char *transb, const int *m, const int *n, const int *k, const Real_t *alpha, const Real_t *A, const int *lda, const Real_t *B, const int *ldb, const Real_t *beta, Real_t *C, const int *ldc)
Multiply the matrix A with the matrix B and store the result in C.
void Ger< double >(const int *m, const int *n, const double *alpha, const double *x, const int *incx, const double *y, const int *incy, double *A, const int *lda)
Definition: Blas.h:150
Double_t x[n]
Definition: legend1.C:17
void dger_(const int *m, const int *n, const double *alpha, const double *x, const int *incx, const double *y, const int *incy, double *A, const int *lda)
void Gemm< float >(const char *transa, const char *transb, const int *m, const int *n, const int *k, const float *alpha, const float *A, const int *lda, const float *B, const int *ldb, const float *beta, float *C, const int *ldc)
Definition: Blas.h:140
void Gemv< float >(const char *trans, const int *m, const int *n, const float *alpha, const float *A, const int *lda, const float *x, const int *incx, const float *beta, float *y, const int *incy)
Definition: Blas.h:121
void Axpy(const int *n, const Real_t *alpha, const Real_t *x, const int *incx, Real_t *y, const int *incy)
Add the vector x scaled by alpha to y scaled by .
static double C[]
TMarker * m
Definition: textangle.C:8
void Gemv(const char *trans, const int *m, const int *n, const Real_t *alpha, const Real_t *A, const int *lda, const Real_t *x, const int *incx, const Real_t *beta, Real_t *y, const int *incy)
Multiply the vector x with the matrix A and store the result in y.
Double_t y[n]
Definition: legend1.C:17
void Gemm< double >(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)
Definition: Blas.h:130
void Gemv< double >(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)
Definition: Blas.h:112
float Real_t
Definition: RtypesCore.h:64
Abstract ClassifierFactory template that handles arbitrary types.
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)
void sgemm_(const char *transa, const char *transb, const int *m, const int *n, const int *k, const float *alpha, const float *A, const int *lda, const float *B, const int *ldb, const float *beta, float *C, const int *ldc)
void daxpy_(const int *n, const double *alpha, const double *x, const int *incx, double *y, const int *incy)
void sger_(const int *m, const int *n, const float *alpha, const float *x, const int *incx, const float *y, const int *incy, float *A, const int *lda)
const Int_t n
Definition: legend1.C:16