Logo ROOT   6.08/07
Reference Guide
Arithmetic.cxx
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 // Implementation of Helper arithmetic functions for the //
14 // multi-threaded CPU implementation of DNNs. //
15 ////////////////////////////////////////////////////////////
16 
19 #include "tbb/tbb.h"
20 
21 namespace TMVA
22 {
23 namespace DNN
24 {
25 
26 //____________________________________________________________________________
27 template<typename Real_t>
29  const TCpuMatrix<Real_t> &A,
30  const TCpuMatrix<Real_t> &B)
31 {
32  int m = (int) A.GetNrows();
33  int k = (int) A.GetNcols();
34  int n = (int) B.GetNcols();
35 
36  char transa = 'N';
37  char transb = 'N';
38 
39  Real_t alpha = 1.0;
40  Real_t beta = 0.0;
41 
42  const Real_t * APointer = A.GetRawDataPointer();
43  const Real_t * BPointer = B.GetRawDataPointer();
44  Real_t * CPointer = C.GetRawDataPointer();
45 
46  ::TMVA::DNN::Blas::Gemm(&transa, &transb, &m, &n, &k, &alpha,
47  APointer, &m, BPointer, &k, &beta, CPointer, &m);
48 }
49 
50 //____________________________________________________________________________
51 template<typename Real_t>
53  const TCpuMatrix<Real_t> &A,
54  const TCpuMatrix<Real_t> &B)
55 {
56  int m = (int) A.GetNcols();
57  int k = (int) A.GetNrows();
58  int n = (int) B.GetNcols();
59 
60  char transa = 'T';
61  char transb = 'N';
62 
63  Real_t alpha = 1.0;
64  Real_t beta = 0.0;
65 
66  const Real_t *APointer = A.GetRawDataPointer();
67  const Real_t *BPointer = B.GetRawDataPointer();
68  Real_t *CPointer = C.GetRawDataPointer();
69 
70  ::TMVA::DNN::Blas::Gemm(&transa, &transb, &m, &n, &k, &alpha,
71  APointer, &k, BPointer, &k, &beta, CPointer, &m);
72 }
73 
74 //____________________________________________________________________________
75 template<typename Real_t>
77  const TCpuMatrix<Real_t> &A)
78 {
79  const Real_t *dataA = A.GetRawDataPointer();
80  Real_t *dataB = B.GetRawDataPointer();
81 
82  auto f = [&dataA, &dataB](UInt_t workerID)
83  {
84  dataB[workerID] *= dataA[workerID];
85  return 0;
86  };
87 
89 }
90 
91 //____________________________________________________________________________
92 template<typename Real_t>
94  const TCpuMatrix<Real_t> &A)
95 {
96  int m = (int) A.GetNrows();
97  int n = (int) A.GetNcols();
98  int inc = 1;
99 
100  Real_t alpha = 1.0;
101  Real_t beta = 0.0;
102  char trans = 'T';
103 
104  const Real_t * APointer = A.GetRawDataPointer();
105  Real_t * BPointer = B.GetRawDataPointer();
106 
107  ::TMVA::DNN::Blas::Gemv(&trans, &m, &n, &alpha, APointer, &m,
109  &beta, BPointer, &inc);
110 }
111 
112 //____________________________________________________________________________
113 template<typename Real_t>
115  const TCpuMatrix<Real_t> &A,
116  Real_t alpha)
117 {
118  int n = (int) (A.GetNcols() * A.GetNrows());
119  int inc = 1;
120 
121  const Real_t *x = A.GetRawDataPointer();
122  Real_t *y = B.GetRawDataPointer();
123 
124  ::TMVA::DNN::Blas::Axpy(&n, &alpha, x, &inc, y, &inc);
125 }
126 
127 //____________________________________________________________________________
128 template<typename Real_t>
130  const TCpuMatrix<Real_t> &A)
131 {
132  auto f = [](Real_t x) {return x;};
133  B.MapFrom(f, A);
134 }
135 
136 } // DNN
137 } // TMVA
static double B[]
The TCpuMatrix class.
Definition: CpuMatrix.h:46
size_t GetNcols() const
Definition: CpuMatrix.h:94
static double A[]
double beta(double x, double y)
Calculates the beta function.
size_t GetNElements() const
Definition: CpuMatrix.h:95
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.
Double_t x[n]
Definition: legend1.C:17
static void Multiply(TCpuMatrix< Scalar_t > &C, const TCpuMatrix< Scalar_t > &A, const TCpuMatrix< Scalar_t > &B)
Standard multiplication of two matrices A and B with the result being written into C...
Definition: Arithmetic.cxx:28
static void Hadamard(TCpuMatrix< Scalar_t > &A, const TCpuMatrix< Scalar_t > &B)
In-place Hadamard (element-wise) product of matrices A and B with the result being written into A...
Definition: Arithmetic.cxx:76
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[]
void MapFrom(Function_t &f, const TCpuMatrix &A)
Same as maps but takes the input values from the matrix A and writes the results in this matrix...
Definition: CpuMatrix.h:133
static void SumColumns(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
Sum columns of (m x n) matrixx A and write the results into the first m elements in A...
Definition: Arithmetic.cxx:93
unsigned int UInt_t
Definition: RtypesCore.h:42
TMarker * m
Definition: textangle.C:8
AFloat * GetRawDataPointer()
Return raw pointer to the elements stored contiguously in column-major order.
Definition: CpuMatrix.h:103
static void Copy(TCpuMatrix< Scalar_t > &B, const TCpuMatrix< Scalar_t > &A)
Definition: Arithmetic.cxx:129
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 f(double x)
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
Double_t y[n]
Definition: legend1.C:17
ROOT::TThreadExecutor & GetThreadExecutor() const
Definition: CpuMatrix.h:106
float Real_t
Definition: RtypesCore.h:64
Abstract ClassifierFactory template that handles arbitrary types.
size_t GetNrows() const
Definition: CpuMatrix.h:93
static void ScaleAdd(TCpuMatrix< Scalar_t > &A, const TCpuMatrix< Scalar_t > &B, Scalar_t beta=1.0)
Adds a the elements in matrix B scaled by c to the elements in the matrix A.
Definition: Arithmetic.cxx:114
const Int_t n
Definition: legend1.C:16
static void TransposeMultiply(TCpuMatrix< Scalar_t > &output, const TCpuMatrix< Scalar_t > &input, const TCpuMatrix< Scalar_t > &Weights)
Matrix multiplication of two matrices A and B^T (transposed) with the result being written into C...
Definition: Arithmetic.cxx:52