Logo ROOT   6.10/09
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 
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 
107 
108 private:
109 
110  void Initialize();
111 
112 };
113 
114 template<typename AFloat>
116 
117 template<typename AFloat>
118 std::vector<AFloat> TCpuMatrix<AFloat>::fOnes {};
119 
120 
121 // Inline Functions.
122 //______________________________________________________________________________
123 template<typename AFloat>
124 template<typename Function_t>
125 inline void TCpuMatrix<AFloat>::Map(Function_t &f)
126 {
127  AFloat *data = GetRawDataPointer();
128 
129  auto ff = [data, &f](UInt_t workerID)
130  {
131  data[workerID] = f(data[workerID]);
132  return 0;
133  };
134 
136 }
137 
138 template<typename AFloat>
139 template<typename Function_t>
140 inline void TCpuMatrix<AFloat>::MapFrom(Function_t &f, const TCpuMatrix &A)
141 {
142  AFloat *dataB = GetRawDataPointer();
143  const AFloat *dataA = A.GetRawDataPointer();
144 
145  auto ff = [&dataB, &dataA, &f](UInt_t workerID)
146  {
147  dataB[workerID] = f(dataA[workerID]);
148  return 0;
149  };
150 
152 }
153 
154 } // namespace DNN
155 } // namespace TMVA
156 
157 #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 double A[]
AFloat & operator()(size_t i, size_t j)
Definition: CpuMatrix.h:99
size_t GetNElements() const
Definition: CpuMatrix.h:95
static std::vector< AFloat > fOnes
Vector filled with ones used for BLAS calls.
Definition: CpuMatrix.h:51
static ROOT::TThreadExecutor fPool
Definition: CpuMatrix.h:50
This class provides a simple interface to execute the same task multiple times in parallel...
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:140
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:23
void Map(Function_t &f)
Map the given function over the matrix elements.
Definition: CpuMatrix.h:125
ROOT::TThreadExecutor & GetThreadExecutor() const
Definition: CpuMatrix.h:106
const AFloat * GetRawDataPointer() const
Definition: CpuMatrix.h:104
Abstract ClassifierFactory template that handles arbitrary types.
auto Map(F func, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type >
Execute func (with no arguments) nTimes in parallel.
size_t GetNrows() const
Definition: CpuMatrix.h:93
const Int_t n
Definition: legend1.C:16