Logo ROOT   6.12/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 "CpuBuffer.h"
25 #include <TMVA/Config.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 std::vector<AFloat> fOnes; ///< Vector filled with ones used for BLAS calls.
51 
52  TCpuBuffer<AFloat> fBuffer; ///< The buffer holding the matrix elements
53  ///< in column-major format.
54  size_t fNCols;
55  size_t fNRows;
56 
57 public:
58 
59  /** Returns pointer to a vector holding only ones with a guaranteed length
60  * of the number of columns of every instantiated CpuMatrix object. */
61  static const AFloat * GetOnePointer() {return fOnes.data();}
62 
63  /** Construct matrix and allocate space for its elements. */
64  TCpuMatrix(size_t nRows, size_t nCols);
65  /** Construct a TCpuMatrix object by (deeply) copying from a
66  * TMatrixT<Double_t> matrix. */
68  /** Construct a m-times-n matrix from the given buffer. The size must of
69  * course match. */
70  TCpuMatrix(const TCpuBuffer<AFloat> &buffer, size_t m, size_t n);
71 
72  TCpuMatrix(const TCpuMatrix &) = default;
73  TCpuMatrix( TCpuMatrix &&) = default;
74  TCpuMatrix & operator=(const TCpuMatrix &) = default;
75  TCpuMatrix & operator=(TCpuMatrix &&) = default;
76  ~TCpuMatrix() = default;
77 
78  /** Convert to a TMatrixT<Double_t> object. Performs a deep copy of the matrix
79  * elements. */
80  operator TMatrixT<Double_t>() const;
81 
82  /** Map the given function over the matrix elements. Executed in parallel
83  * using TThreadExecutor. */
84  template <typename Function_t>
85  void Map(Function_t &f);
86 
87  /** Same as maps but takes the input values from the matrix \p A and writes
88  * the results in this matrix. */
89  template <typename Function_t>
90  void MapFrom(Function_t &f, const TCpuMatrix & A);
91 
92  size_t GetNrows() const {return fNRows;}
93  size_t GetNcols() const {return fNCols;}
94  size_t GetNElements() const {return fNRows * fNCols;}
95 
96  /** Return matrix element in row \p i and column \p j. */
97  AFloat operator()(size_t i, size_t j) const {return fBuffer[j * fNRows + i];}
98  AFloat & operator()(size_t i, size_t j) {return fBuffer[j * fNRows + i];}
99 
100  /** Return raw pointer to the elements stored contiguously in column-major
101  * order. */
102  AFloat * GetRawDataPointer() {return fBuffer;}
103  const AFloat * GetRawDataPointer() const {return fBuffer;}
104 
106 
107 private:
108 
109  void Initialize();
110 
111 };
112 
113 template<typename AFloat>
114 std::vector<AFloat> TCpuMatrix<AFloat>::fOnes {};
115 
116 
117 // Inline Functions.
118 //______________________________________________________________________________
119 template<typename AFloat>
120 template<typename Function_t>
121 inline void TCpuMatrix<AFloat>::Map(Function_t &f)
122 {
123  AFloat *data = GetRawDataPointer();
124 
125  auto ff = [data, &f](UInt_t workerID)
126  {
127  data[workerID] = f(data[workerID]);
128  return 0;
129  };
130 
132 }
133 
134 template<typename AFloat>
135 template<typename Function_t>
136 inline void TCpuMatrix<AFloat>::MapFrom(Function_t &f, const TCpuMatrix &A)
137 {
138  AFloat *dataB = GetRawDataPointer();
139  const AFloat *dataA = A.GetRawDataPointer();
140 
141  auto ff = [&dataB, &dataA, &f](UInt_t workerID)
142  {
143  dataB[workerID] = f(dataA[workerID]);
144  return 0;
145  };
146 
148 }
149 
150 } // namespace DNN
151 } // namespace TMVA
152 
153 #endif
AFloat operator()(size_t i, size_t j) const
Return matrix element in row i and column j.
Definition: CpuMatrix.h:97
TCpuBuffer< AFloat > fBuffer
The buffer holding the matrix elements in column-major format.
Definition: CpuMatrix.h:52
The TCpuMatrix class.
Definition: CpuMatrix.h:46
auto * m
Definition: textangle.C:8
size_t GetNcols() const
Definition: CpuMatrix.h:93
static Config & Instance()
static function: returns TMVA instance
Definition: Config.cxx:101
static double A[]
AFloat & operator()(size_t i, size_t j)
Definition: CpuMatrix.h:98
size_t GetNElements() const
Definition: CpuMatrix.h:94
static std::vector< AFloat > fOnes
Vector filled with ones used for BLAS calls.
Definition: CpuMatrix.h:50
ROOT::TThreadExecutor & GetThreadExecutor()
Definition: Config.h:77
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:136
TCpuBuffer.
Definition: CpuBuffer.h:43
unsigned int UInt_t
Definition: RtypesCore.h:42
AFloat * GetRawDataPointer()
Return raw pointer to the elements stored contiguously in column-major order.
Definition: CpuMatrix.h:102
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:61
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:121
ROOT::TThreadExecutor & GetThreadExecutor() const
Definition: CpuMatrix.h:105
const AFloat * GetRawDataPointer() const
Definition: CpuMatrix.h:103
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:92
const Int_t n
Definition: legend1.C:16