cu-sp-matrix-speed-test.cc
Go to the documentation of this file.
1 // cudamatrix/cu-sp-matrix-speed-test.cc
2 
3 // Copyright 2013 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include <iostream>
22 #include <vector>
23 #include <cstdlib>
24 
25 #include "base/kaldi-common.h"
26 #include "util/common-utils.h"
27 #include "cudamatrix/cu-matrix.h"
28 #include "cudamatrix/cu-vector.h"
29 #include "cudamatrix/cu-math.h"
31 
32 using namespace kaldi;
33 
34 
35 namespace kaldi {
36 
37 template<typename Real>
38 std::string NameOf() {
39  return (sizeof(Real) == 8 ? "<double>" : "<float>");
40 }
41 
42 template<typename Real>
43 static void UnitTestCuSpMatrixInvert(int32 dim) {
44  BaseFloat time_in_secs = 0.01;
45  int32 iter = 0;
46  Timer tim;
47  CuSpMatrix<Real> A(dim);
48  A.SetRandn();
49  for (;tim.Elapsed() < time_in_secs; iter++) {
50  KALDI_ASSERT(A.Trace() != 0.0); // true with probability 1...
51  CuSpMatrix<Real> B(A);
52 
53  if (iter > 0) {
54  B.Invert();
55  } else { // do some more testing...
56 
57  CuMatrix<Real> D(A);
58  A.AddMat2(1.0, D, kTrans, 1.0);
59  A.AddToDiag(0.1 * dim);
60 
61  CuMatrix<Real> C(B);
62  B.AddMat2(1.0, C, kTrans, 1.0);
63  B.AddToDiag(0.1 * dim);
64 
65  A.Invert();
66  B.Invert();
67 
68  SpMatrix<Real> E(dim);
69  B.CopyToSp(&E);
70 
71  SpMatrix<Real> A2(A);
72  AssertEqual(A2, E);
73  }
74  }
75  BaseFloat fdim = dim;
76  BaseFloat gflops = (fdim * fdim * fdim * iter) / (tim.Elapsed() * 1.0e+09);
77  KALDI_LOG << "For CuSpMatrix::Invert" << NameOf<Real>() << ", for dim = "
78  << dim << ", speed was " << gflops << " gigaflops.";
79 }
80 
81 
82 
83 template<typename Real>
84 static void UnitTestCuSpMatrixCopyFromMat(int32 dim, SpCopyType copy_type) {
85  BaseFloat time_in_secs = 0.01;
86  int32 iter = 0;
87  Timer tim;
88  CuMatrix<Real> A(dim, dim);
89  CuSpMatrix<Real> S(dim);
90 
91  for (;tim.Elapsed() < time_in_secs; iter++) {
92  S.CopyFromMat(A, copy_type);
93  }
94  BaseFloat fdim = dim;
95  BaseFloat gflops = (fdim * fdim * iter) / (tim.Elapsed() * 1.0e+09);
96  KALDI_LOG << "For CuSpMatrix::CopyFromMat" << NameOf<Real>()
97  << ", with copy-type "
98  <<(copy_type == kTakeLower ? "kTakeLower" :
99  (copy_type == kTakeUpper ? "kTakeUpper" :
100  "kTakeMeanAndCheck")) << " and dim = "
101  << dim << ", speed was " << gflops << " gigaflops.";
102 }
103 
104 
105 
106 
107 template<typename Real> void CuSpMatrixSpeedTest() {
108  std::vector<int32> sizes;
109  sizes.push_back(16);
110  sizes.push_back(32);
111  sizes.push_back(64);
112  sizes.push_back(128);
113  sizes.push_back(256);
114  sizes.push_back(512);
115  sizes.push_back(1024);
116  int32 ns = sizes.size();
117 
118  for (int32 s = 0; s < ns; s++) {
119  UnitTestCuSpMatrixInvert<Real>(sizes[s]);
120  UnitTestCuSpMatrixCopyFromMat<Real>(sizes[s], kTakeLower);
121  UnitTestCuSpMatrixCopyFromMat<Real>(sizes[s], kTakeUpper);
122  UnitTestCuSpMatrixCopyFromMat<Real>(sizes[s], kTakeMean);
123  }
124 }
125 
126 
127 } // namespace kaldi
128 
129 
130 int main() {
131  SetVerboseLevel(1);
132  //Select the GPU
133 #if HAVE_CUDA == 1
134  CuDevice::Instantiate().SelectGpuId("yes"); //-2 .. automatic selection
135 #endif
136 
137  kaldi::CuSpMatrixSpeedTest<float>();
138 #if HAVE_CUDA == 1
139  if (CuDevice::Instantiate().DoublePrecisionSupported()) {
140  kaldi::CuSpMatrixSpeedTest<double>();
141  } else {
142  KALDI_WARN << "Double precision not supported";
143  }
144 #else
145  kaldi::CuSpMatrixSpeedTest<double>();
146 #endif
147 #if HAVE_CUDA == 1
148  CuDevice::Instantiate().PrintProfile();
149 #endif
150  KALDI_LOG << "Tests succeeded.";
151 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Packed symetric matrix class.
Definition: matrix-common.h:62
std::string NameOf()
void AddToDiag(Real r)
< Set the diagonal value to alpha
kaldi::int32 int32
This class represents a matrix that&#39;s stored on the GPU if we have one, and in memory if not...
Definition: matrix-common.h:71
void Invert()
Note: the CuMatrix version of the Invert() function will only work for positive definite matrices; it...
Definition: cu-sp-matrix.cc:93
void SetRandn()
< Set to unit matrix.
void SetVerboseLevel(int32 i)
This should be rarely used, except by programs using Kaldi as library; command-line programs set the ...
Definition: kaldi-error.h:64
#define KALDI_WARN
Definition: kaldi-error.h:150
void CopyFromMat(const CuMatrixBase< Real > &orig, SpCopyType copy_type=kTakeLower)
Definition: cu-sp-matrix.cc:39
void CopyToSp(SpMatrix< Real > *dst) const
Definition: cu-sp-matrix.h:95
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
static void UnitTestCuSpMatrixInvert(int32 dim)
static void UnitTestCuSpMatrixCopyFromMat(int32 dim, SpCopyType copy_type)
void CuSpMatrixSpeedTest()
#define KALDI_LOG
Definition: kaldi-error.h:153
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
void AddMat2(const Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)