cu-device-test.cc
Go to the documentation of this file.
1 // cudamatrix/cu-device-test.cc
2 
3 // Copyright 2015 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 
30 using namespace kaldi;
31 
32 
33 namespace kaldi {
34 
35 
36 template<typename Real>
37 std::string NameOf() {
38  return (sizeof(Real) == 8 ? "<double>" : "<float>");
39 }
40 
41 template<typename Real> void TestCuMatrixResize(int32 size_multiple) {
42  int32 num_matrices = 256;
43  BaseFloat time_in_secs = 0.2;
44 
45  std::vector<std::pair<int32, int32> > sizes(num_matrices);
46 
47  for (int32 i = 0; i < num_matrices; i++) {
48  int32 num_rows = RandInt(1, 10);
49  num_rows *= num_rows;
50  num_rows *= size_multiple;
51  int32 num_cols = RandInt(1, 10);
52  num_cols *= num_cols;
53  num_cols *= size_multiple;
54  sizes[i].first = num_rows;
55  sizes[i].second = num_rows;
56  }
57 
58  std::vector<CuMatrix<BaseFloat> > matrices(num_matrices);
59 
60  Timer tim;
61  size_t num_floats_processed = 0;
62  for (;tim.Elapsed() < time_in_secs; ) {
63  int32 matrix = RandInt(0, num_matrices - 1);
64  if (matrices[matrix].NumRows() == 0) {
65  int32 num_rows = sizes[matrix].first,
66  num_cols = sizes[matrix].second;
67  matrices[matrix].Resize(num_rows, num_cols, kUndefined);
68  num_floats_processed += num_rows * num_cols;
69  } else {
70  matrices[matrix].Resize(0, 0);
71  }
72  }
73 
74  BaseFloat gflops = num_floats_processed / (tim.Elapsed() * 1.0e+09);
75 
76  KALDI_LOG << "For CuMatrix::Resize" << NameOf<Real>() << ", for size_multiple = "
77  << size_multiple << ", speed was " << gflops << " gigaflops.";
78 }
79 
80 template <typename Real>
82  std::vector<int32> sizes;
83  sizes.push_back(1);
84  sizes.push_back(2);
85  sizes.push_back(4);
86  sizes.push_back(8);
87  sizes.push_back(16);
88  //sizes.push_back(24);
89  //sizes.push_back(32);
90  //sizes.push_back(40);
91 
92  int32 ns = sizes.size();
93  for (int32 s = 0; s < ns; s++)
94  TestCuMatrixResize<Real>(sizes[s]);
95 }
96 
97 
98 } // namespace kaldi
99 
100 
101 int main() {
102  SetVerboseLevel(1);
103 #if HAVE_CUDA == 1
104  for (int32 loop = 0; loop < 2; loop++) {
105  CuDevice::Instantiate().SetDebugStrideMode(true);
106  if (loop == 0)
107  CuDevice::Instantiate().SelectGpuId("no");
108  else
109  CuDevice::Instantiate().SelectGpuId("yes");
110 #endif
111 
112  kaldi::CudaMatrixResizeTest<float>();
113 #if HAVE_CUDA == 1
114  if (CuDevice::Instantiate().DoublePrecisionSupported()) {
115  kaldi::CudaMatrixResizeTest<double>();
116  } else {
117  KALDI_WARN << "Double precision not supported";
118  }
119 #else
120  kaldi::CudaMatrixResizeTest<double>();
121 #endif
122 
123 #if HAVE_CUDA == 1
124  }
125  CuDevice::Instantiate().PrintProfile();
126 #endif
127  KALDI_LOG << "Tests succeeded.";
128 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void CudaMatrixResizeTest()
void TestCuMatrixResize(int32 size_multiple)
std::string NameOf()
kaldi::int32 int32
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
int main()
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_LOG
Definition: kaldi-error.h:153
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95