Logo ROOT   6.07/09
Reference Guide
TestCuda.cxx
Go to the documentation of this file.
1 #include "Utility.h"
4 #include <stdlib.h>
5 
6 using namespace TMVA::DNN;
7 
8 //_________________________________________________________________________________
10 {
11  const size_t ntests = 100;
12 
13  Double_t maximumError = 0;
14 
15  for (size_t i = 0; i < ntests; i++) {
16  size_t m, n, k;
17  m = rand() % 50 + 1;
18  n = rand() % 50 + 1;
19  k = rand() % 50 + 1;
20 
21  TMatrixT<Double_t> A(m,k), AT(k,m) , B(k,n), BT(n,k), C(m,n);
22  randomMatrix(A);
23  randomMatrix(AT);
24  randomMatrix(B);
25  randomMatrix(BT);
26  TCudaMatrix ACuda(A), ATCuda(AT), BCuda(B), BTCuda(BT), CCuda(C);
27 
29  TCuda<false>::MultiplyTranspose(CCuda, ACuda, BTCuda);
30  TMatrixT<Double_t> CRef(CCuda);
31  Double_t error = maximumRelativeError(C, CRef);
32  maximumError = std::max(error, maximumError);
33 
34  C.Mult(A,B);
35  TCuda<false>::Multiply(CCuda, ACuda, BCuda);
36  CRef = CCuda;
37  error = maximumRelativeError(C, CRef);
38  maximumError = std::max(error, maximumError);
39 
40  C.TMult(AT,B);
41  TCuda<false>::TransposeMultiply(CCuda, ATCuda, BCuda);
42  CRef = CCuda;
43  error = maximumRelativeError(C, CRef);
44  maximumError = std::max(error, maximumError);
45  }
46  return maximumError;
47 }
48 
49 //_________________________________________________________________________________
51 {
52  const size_t ntests = 10;
53 
54  Double_t maximumError = 0;
55 
56  for (size_t i = 0; i < ntests; i++) {
57  size_t m, n;
58  m = rand() % 50 + 1;
59  n = rand() % 50 + 1;
60 
61  TMatrixT<Double_t> A(m,n), B(m,n), theta(n,1);
62  //randomMatrix(A);
63  randomMatrix(theta);
64  TCudaMatrix ACuda(A), BCuda(B), thetaCuda(theta);
65 
67  TCuda<false>::AddRowWise(ACuda,thetaCuda);
68  TMatrixT<Double_t> ARef(ACuda);
69 
70  Double_t error = maximumRelativeError(A, ARef);
71  maximumError = std::max(error, maximumError);
72  }
73  return maximumError;
74 }
75 
76 //_________________________________________________________________________________
78 {
79  const size_t ntests = 10;
80  Double_t maximumError = 0;
81 
82  for (size_t i = 0; i < ntests; i++) {
83  size_t m, n;
84  m = rand() % 10 + 1;
85  n = rand() % 10 + 1;
86 
87  TMatrixT<Double_t> A(m,n), B(m,n);
88  randomMatrix(A);
89  randomMatrix(B);
90  TCudaMatrix ACuda(A), BCuda(B);
91 
92  for (size_t j = 0; j < (size_t) A.GetNrows(); j++) {
93  for (size_t k = 0; k < (size_t) A.GetNcols(); k++) {
94  A(j,k) *= B(j,k);
95  }
96  }
97 
98  TCuda<false>::Hadamard(ACuda, BCuda);
99  TMatrixT<Double_t> ARef(ACuda);
100  Double_t error = maximumRelativeError(A, ARef);
101  maximumError = std::max(error, maximumError);
102  }
103  return maximumError;
104 }
105 
106 //_________________________________________________________________________________
108 {
109  const size_t ntests = 10;
110  Double_t maximumError = 0;
111 
112  for (size_t i = 0; i < ntests; i++) {
113  size_t m, n;
114  m = rand() % 1000 + 1;
115  n = rand() % 1000 + 1;
116 
117  TMatrixT<Double_t> A(m,n);
118 
119  for (size_t j = 0; j < m; j++) {
120  for (size_t k = 0; k < n; k++) {
121  A(j,k) = 1.0;
122  }
123  }
124  TCudaMatrix ACuda(A);
125 
126  TCudaMatrix BCuda(1,n);
129  TCuda<false>::SumColumns(BCuda, ACuda);
130  TMatrixT<Double_t> B(BCuda);
131 
132  Double_t error = s - ((Double_t) m * n);
133  maximumError = std::max(error, maximumError);
134 
135  for (size_t j = 0; j < n; j++) {
136  //std::cout << B(0,j) << " / " << j * m << std::endl;
137  error = std::abs(B(0,j) - m);
138  maximumError = std::max(error, maximumError);
139  }
140  }
141  return maximumError;
142 }
143 
144 //_________________________________________________________________________________
146 {
147  const size_t ntests = 10;
148  Double_t maximumError = 0;
149 
150  for (size_t i = 0; i < ntests; i++) {
151  size_t m, n;
152  m = rand() % 1000 + 1;
153  n = rand() % 1000 + 1;
154 
155  TMatrixT<Double_t> A(m,n), B(m,n);
156 
157  randomMatrix(A);
158  randomMatrix(B);
159 
160  TCudaMatrix ACuda(A);
161  TCudaMatrix BCuda(B);
162 
163  Double_t beta = ((Double_t) rand()) / ((Double_t) RAND_MAX);
165  TCuda<false>::ScaleAdd(ACuda, BCuda, beta);
166 
168  maximumError = std::max(error, maximumError);
169  }
170  return maximumError;
171 }
172 
173 //_________________________________________________________________________________
174 int main()
175 {
176  Double_t error;
177  error = testReduction();
178  std::cout << "Testing reduction: max. rel. error = ";
179  std::cout << error << std::endl;
180 
181  error = testScaleAdd();
182  std::cout << "Testing scale_add: max. rel. error = ";
183  std::cout << error << std::endl;
184 
185  error = testHadamard();
186  std::cout << "Testing hadamard: max. rel. error = ";
187  std::cout << error << std::endl;
188 
189  error = testMultiply();
190  std::cout << "Testing multiplication: max. rel. error = ";
191  std::cout << error << std::endl;
192 
193  error = testAddRowWise();
194  std::cout << "Testing add_row_wise: max. rel. error = ";
195  std::cout << error << std::endl;
196 }
static double B[]
void randomMatrix(AMatrix &X)
Fill matrix with random, Gaussian-distributed values.
Definition: Utility.h:59
auto maximumRelativeError(const AMatrix &X, const AMatrix &Y) -> decltype(X(0, 0))
Compute the maximum, element-wise relative error of the matrices X and Y normalized by the element of...
Definition: Utility.h:213
static void MultiplyTranspose(TMatrixT< Scalar_t > &output, const TMatrixT< Scalar_t > &input, const TMatrixT< Scalar_t > &weights)
Matrix-multiply input with the transpose of and write the results into output.
Definition: Propagation.cxx:25
Double_t testScaleAdd()
Definition: TestCuda.cxx:145
static void MultiplyTranspose(TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &input, const TCudaMatrix< AFloat > &weights)
Matrix-multiply input with the transpose of and write the results into output.
Definition: Blas.h:58
static void ScaleAdd(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B, Scalar_t beta=1.0)
Adds a the elements in matrix B scaled by c to the elements in the matrix A.
static void AddRowWise(TMatrixT< Scalar_t > &output, const TMatrixT< Scalar_t > &biases)
Add the vectors biases row-wise to the matrix output.
Definition: Propagation.cxx:33
static double A[]
static void Hadamard(TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B)
In-place Hadamard (element-wise) product of matrices A and B with the result being written into A...
double beta(double x, double y)
Calculates the beta function.
Double_t testMultiply()
Definition: TestCuda.cxx:9
static void AddRowWise(TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &biases)
Add the vectors biases row-wise to the matrix output.
void TMult(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
Create a matrix C such that C = A&#39; * B.
Definition: TMatrixT.cxx:852
Double_t testAddRowWise()
Definition: TestCuda.cxx:50
int main()
Definition: TestCuda.cxx:174
static double C[]
static void Multiply(TCudaMatrix< AFloat > &C, const TCudaMatrix< AFloat > &A, const TCudaMatrix< AFloat > &B)
Standard multiplication of two matrices A and B with the result being written into C...
TMarker * m
Definition: textangle.C:8
static void ScaleAdd(TMatrixT< Scalar_t > &A, const TMatrixT< 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: Propagation.cxx:83
Double_t testHadamard()
Definition: TestCuda.cxx:77
double Double_t
Definition: RtypesCore.h:55
static AFloat Sum(const TCudaMatrix< AFloat > &A)
Compute the sum of all elements in A.
static void InitializeZero(TCudaMatrix< AFloat > &A)
static void TransposeMultiply(TCudaMatrix< AFloat > &output, const TCudaMatrix< AFloat > &input, const TCudaMatrix< AFloat > &Weights)
Matrix multiplication of two matrices A and B^T (transposed) with the result being written into C...
static void SumColumns(TCudaMatrix< AFloat > &B, const TCudaMatrix< AFloat > &A)
Sum columns of (m x n) matrixx A and write the results into the first m elements in A...
void Mult(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
General matrix multiplication. Create a matrix C such that C = A * B.
Definition: TMatrixT.cxx:648
Double_t testReduction()
Definition: TestCuda.cxx:107
const Int_t n
Definition: legend1.C:16
TCudaMatrix Class.
Definition: CudaMatrix.h:98