cu-array-test.cc
Go to the documentation of this file.
1 // cudamatrix/cu-array-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-array.h"
28 
29 using namespace kaldi;
30 
31 
32 namespace kaldi {
33 
34 
35 template<class T>
36 void AssertEqual(const std::vector<T> &vec1,
37  const std::vector<T> &vec2) {
38  // use this instead of "vec1 == vec2" because
39  // for doubles and floats, this can fail if we
40  // have NaNs, even if identical.
41  KALDI_ASSERT(vec1.size() == vec2.size());
42  if (vec1.size() > 0) {
43  const char *p1 = reinterpret_cast<const char*>(&(vec1[0]));
44  const char *p2 = reinterpret_cast<const char*>(&(vec2[0]));
45  size_t size = sizeof(T) * vec1.size();
46  for (size_t i = 0; i < size; i++)
47  KALDI_ASSERT(p1[i] == p2[i]);
48  }
49 }
50 
51 
52 template<class T>
53 static void UnitTestCuArray() {
54  for (int32 i = 0; i < 30; i++) {
55  int32 size = Rand() % 5;
56  size = size * size * size; // Have a good distribution of sizes, including >256.
57  int32 size2 = Rand() % 4;
58  std::vector<T> vec(size);
59  std::vector<T> garbage_vec(size2); // We just use garbage_vec to make sure
60  // we sometimes resize from empty,
61  // sometimes not.
62 
63  int32 byte_size = size * sizeof(T);
64  std::vector<char> rand_c(byte_size);
65  for (size_t i = 0; i < byte_size; i++)
66  rand_c[i] = Rand() % 256;
67  if (!vec.empty()) {
68  std::memcpy((void*)&(vec[0]), (void*)&(rand_c[0]),
69  byte_size);
70  }
71 
72  { // test constructor from vector and CopyToVec.
73  CuArray<T> cu_vec(vec);
74  std::vector<T> vec2;
75  cu_vec.CopyToVec(&vec2);
76  T *vec22 = new T[vec.size()];
77  cu_vec.CopyToHost(vec22);
78  delete[] vec22;
79  }
80 
81  { // test assignment operator from CuArray.
82  CuArray<T> cu_vec(vec);
83  CuArray<T> cu_vec2(garbage_vec);
84  cu_vec2 = cu_vec;
85  std::vector<T> vec2;
86  cu_vec2.CopyToVec(&vec2);
87  AssertEqual(vec, vec2);
88  KALDI_ASSERT(cu_vec2.Dim() == int32(vec2.size())); // test Dim()
89  }
90 
91  { // test resize with resize_type = kSetZero.
92  CuArray<T> cu_vec(vec);
93  cu_vec.Resize(size, kSetZero);
94  std::vector<T> vec2(vec);
95 
96  if (!vec2.empty())
97  std::memset(&(vec2[0]), 0, vec2.size() * sizeof(T));
98  std::vector<T> vec3;
99  cu_vec.CopyToVec(&vec3);
100  AssertEqual(vec2, vec3); // testing equality of zero arrays.
101  }
102 
103  if (sizeof(T) == sizeof(int32) && size > 0) { // test Set for type int32, or same size.
104  CuArray<T> cu_vec(vec);
105  cu_vec.Set(vec[0]);
106  for (size_t i = 1; i < vec.size(); i++) vec[i] = vec[0];
107  std::vector<T> vec2;
108  cu_vec.CopyToVec(&vec2);
109  AssertEqual(vec, vec2);
110  }
111  }
112 }
113 
114 
115 } // namespace kaldi
116 
117 
118 int main() {
119  SetVerboseLevel(1);
120  int32 loop = 0;
121 #if HAVE_CUDA == 1
122  for (; loop < 2; loop++) {
123  CuDevice::Instantiate().SetDebugStrideMode(true);
124  if (loop == 0)
125  CuDevice::Instantiate().SelectGpuId("no");
126  else
127  CuDevice::Instantiate().SelectGpuId("yes");
128 #endif
129 
130  //kaldi::UnitTestCuArray<float>();
131  kaldi::UnitTestCuArray<double>();
132  kaldi::UnitTestCuArray<int32>();
133  kaldi::UnitTestCuArray<std::pair<int32, int32> >();
134 
135  if (loop == 0)
136  KALDI_LOG << "Tests without GPU use succeeded.";
137  else
138  KALDI_LOG << "Tests with GPU use (if available) succeeded.";
139 #if HAVE_CUDA == 1
140  }
141  CuDevice::Instantiate().PrintProfile();
142 #endif
143  return 0;
144 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void CopyToVec(std::vector< T > *dst) const
This function resizes *dst if needed.
Definition: cu-array-inl.h:177
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
static void UnitTestCuArray()
void Set(const T &value)
Set to a constant value.
Definition: cu-array-inl.h:234
int main()
void CopyToHost(T *dst) const
Version of the above function that copies contents to a host array (i.e.
Definition: cu-array-inl.h:198
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
Class CuArray represents a vector of an integer or struct of type T.
Definition: cu-array.h:32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Resize(MatrixIndexT dim, MatrixResizeType resize_type=kSetZero)
Allocate the memory.
Definition: cu-array-inl.h:43
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
MatrixIndexT Dim() const
Return the vector dimension.
Definition: cu-array.h:49
#define KALDI_LOG
Definition: kaldi-error.h:153