Logo ROOT   6.08/07
Reference Guide
CpuMatrix.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 // Definition of the CpuMatrix class used to represent //
14 // weight and bias matrices in neural nets. //
15 //////////////////////////////////////////////////////////
16 
17 #ifndef TMVA_DNN_ARCHITECTURES_CPU_CPUMATRIX
18 #define TMVA_DNN_ARCHITECTURES_CPU_CPUMATRIX
19 
20 #include <cstddef>
21 #include <vector>
22 
23 #include "TMatrix.h"
24 #include "ROOT/TThreadExecutor.hxx"
25 #include "CpuBuffer.h"
26 
27 namespace TMVA
28 {
29 namespace DNN
30 {
31 
32 /** The TCpuMatrix class.
33  *
34  * Matrix class for multi-threaded CPU architectures. Uses the TCpuBuffer
35  * class to store the matrices in column-major format for compatibility with
36  * BLAS. Provides Map and MapFrom member functions to simplify the application of
37  * activation functions and derivatives to matrices.
38  *
39  * Copying and assignment of TCpuMatrix objects only performs shallow copies, i.e.
40  * copying is fast and the resulting objects share the element data.
41  *
42  * \tparam AFloat The floating point type used to represent the matrix elements.
43  */
44 //______________________________________________________________________________
45 template<typename AFloat>
47 {
48 private:
49 
50  static ROOT::TThreadExecutor fPool;
51  static std::vector<AFloat> fOnes; ///< Vector filled with ones used for BLAS calls.
52 
53  TCpuBuffer<AFloat> fBuffer; ///< The buffer holding the matrix elements
54  ///< in column-major format.
55  size_t fNCols;
56  size_t fNRows;
57 
58 public:
59 
60  /** Returns pointer to a vector holding only ones with a guaranteed length
61  * of the number of columns of every instantiated CpuMatrix object. */
62  static const AFloat * GetOnePointer() {return fOnes.data();}
63 
64  /** Construct matrix and allocate space for its elements. */
65  TCpuMatrix(size_t nRows, size_t nCols);
66  /** Construct a TCpuMatrix object by (deeply) copying from a
67  * TMatrixT<Double_t> matrix. */
69  /** Construct a m-times-n matrix from the given buffer. The size must of
70  * course match. */
71  TCpuMatrix(const TCpuBuffer<AFloat> &buffer, size_t m, size_t n);
72 
73  TCpuMatrix(const TCpuMatrix &) = default;
74  TCpuMatrix( TCpuMatrix &&) = default;
75  TCpuMatrix & operator=(const TCpuMatrix &) = default;
76  TCpuMatrix & operator=(TCpuMatrix &&) = default;
77  ~TCpuMatrix() = default;
78 
79  /** Convert to a TMatrixT<Double_t> object. Performs a deep copy of the matrix
80  * elements. */
81  operator TMatrixT<Double_t>() const;
82 
83  /** Map the given function over the matrix elements. Executed in parallel
84  * using TThreadExecutor. */
85  template <typename Function_t>
86  void Map(Function_t &f);
87 
88  /** Same as maps but takes the input values from the matrix \p A and writes
89  * the results in this matrix. */
90  template <typename Function_t>
91  void MapFrom(Function_t &f, const TCpuMatrix & A);
92 
93  size_t GetNrows() const {return fNRows;}
94  size_t GetNcols() const {return fNCols;}
95  size_t GetNElements() const {return fNRows * fNCols;}
96 
97  /** Return matrix element in row \p i and column \p j. */
98  AFloat operator()(size_t i, size_t j) const {return fBuffer[j * fNRows + i];}
99  AFloat & operator()(size_t i, size_t j) {return fBuffer[j * fNRows + i];}
100 
101  /** Return raw pointer to the elements stored contiguously in column-major
102  * order. */
103  AFloat * GetRawDataPointer() {return fBuffer;}
104  const AFloat * GetRawDataPointer() const {return fBuffer;}
105 
106  ROOT::TThreadExecutor & GetThreadExecutor() const {return fPool;}
107 
108 private:
109 
110  void Initialize();
111 
112 };
113 
114 // Inline Functions.
115 //______________________________________________________________________________
116 template<typename AFloat>
117 template<typename Function_t>
118 inline void TCpuMatrix<AFloat>::Map(Function_t &f)
119 {
120  AFloat *data = GetRawDataPointer();
121 
122  auto ff = [data, &f](UInt_t workerID)
123  {
124  data[workerID] = f(data[workerID]);
125  return 0;
126  };
127 
128  fPool.Map(ff, ROOT::TSeqI(fNCols * fNRows));
129 }
130 
131 template<typename AFloat>
132 template<typename Function_t>
133 inline void TCpuMatrix<AFloat>::MapFrom(Function_t &f, const TCpuMatrix &A)
134 {
135  AFloat *dataB = GetRawDataPointer();
136  const AFloat *dataA = A.GetRawDataPointer();
137 
138  auto ff = [&dataB, &dataA, &f](UInt_t workerID)
139  {
140  dataB[workerID] = f(dataA[workerID]);
141  return 0;
142  };
143 
144  fPool.Map(ff, ROOT::TSeqI(fNCols * fNRows));
145 }
146 
147 } // namespace DNN
148 } // namespace TMVA
149 
150 #endif
AFloat operator()(size_t i, size_t j) const
Return matrix element in row i and column j.
Definition: CpuMatrix.h:98
TCpuBuffer< AFloat > fBuffer
The buffer holding the matrix elements in column-major format.
Definition: CpuMatrix.h:53
The TCpuMatrix class.
Definition: CpuMatrix.h:46
size_t GetNcols() const
Definition: CpuMatrix.h:94
static ROOT::TThreadExecutor fPool
Definition: CpuMatrix.h:50
static double A[]
AFloat & operator()(size_t i, size_t j)
Definition: CpuMatrix.h:99
size_t GetNElements() const
Definition: CpuMatrix.h:95
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
TCpuBuffer.
Definition: CpuBuffer.h:43
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
double f(double x)
TCpuMatrix & operator=(const TCpuMatrix &)=default
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
static const AFloat * GetOnePointer()
Returns pointer to a vector holding only ones with a guaranteed length of the number of columns of ev...
Definition: CpuMatrix.h:62
TCpuMatrix(size_t nRows, size_t nCols)
Construct matrix and allocate space for its elements.
Definition: CpuMatrix.cxx:28
void Map(Function_t &f)
Map the given function over the matrix elements.
Definition: CpuMatrix.h:118
ROOT::TThreadExecutor & GetThreadExecutor() const
Definition: CpuMatrix.h:106
const AFloat * GetRawDataPointer() const
Definition: CpuMatrix.h:104
Abstract ClassifierFactory template that handles arbitrary types.
size_t GetNrows() const
Definition: CpuMatrix.h:93
const Int_t n
Definition: legend1.C:16
static std::vector< AFloat > fOnes
Vector filled with ones used for BLAS calls.
Definition: CpuMatrix.h:51