kaldi-thread-test.cc
Go to the documentation of this file.
1 // util/kaldi-thread-test.cc
2 
3 // Copyright 2012 Johns Hopkins University (Author: Daniel Povey)
4 // Frantisek Skala
5 // 2017 University of Southern California (Author: Dogan Can)
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 #include <algorithm>
23 #include "base/kaldi-common.h"
24 #include "util/kaldi-thread.h"
25 
26 namespace kaldi {
27 
28 // Sums up integers from 0 to max_to_count-1.
30  public:
31  MyThreadClass(int32 max_to_count, int32 *i):
32  max_to_count_(max_to_count), iptr_(i), private_counter_(0) { }
33 
34  // We are defining a copy constructor to ensure that whenever an instance of
35  // this class is copied, the default *copy* constructor for MultiThreadable
36  // is called instead the default constructor for MultiThreadable.
38  MultiThreadable(other),
39  max_to_count_(other.max_to_count_), iptr_(other.iptr_),
40  private_counter_(0) { }
41 
42  void operator() () {
43  int32 block_size = (max_to_count_+ (num_threads_-1) ) / num_threads_;
44  int32 start = block_size * thread_id_,
45  end = std::min(max_to_count_, start + block_size);
46  for (int32 j = start; j < end; j++)
48  }
49 
52  }
53 
54  private:
55  MyThreadClass() { } // Disallow empty constructor.
59 };
60 
61 
62 void TestThreads() {
63  g_num_threads = 8;
64  // run method with temporary threads on 8 threads
65  // Note: uncomment following line for the possibility of simple benchmarking
66  // for(int i=0; i<100000; i++)
67  {
68  int32 max_to_count = 10000, tot = 0;
69  MyThreadClass c(max_to_count, &tot);
71  KALDI_ASSERT(tot == (10000*(10000-1))/2);
72  }
73  g_num_threads = 1;
74  // let's try the same, but with only one thread
75  {
76  int32 max_to_count = 10000, tot = 0;
77  MyThreadClass c(max_to_count, &tot);
79  KALDI_ASSERT(tot == (10000*(10000-1))/2);
80  }
81 }
82 
83 class MyTaskClass { // spins for a while, then outputs a pre-given integer.
84  public:
85  MyTaskClass(int32 i, std::vector<int32> *vec):
86  done_(false), i_(i), vec_(vec) { }
87 
88  void operator() () {
89  int32 spin = 1000000 * Rand() % 100;
90  for (int32 i = 0; i < spin; i++);
91  done_ = true;
92  }
94  KALDI_ASSERT(done_);
95  vec_->push_back(i_);
96  }
97 
98  private:
99  bool done_;
101  std::vector<int32> *vec_;
102 };
103 
104 
106  TaskSequencerConfig config;
107  config.num_threads = 1 + Rand() % 20;
108  if (Rand() % 2 == 1 )
109  config.num_threads_total = config.num_threads + Rand() % config.num_threads;
110 
111  int32 num_tasks = Rand() % 100;
112 
113  std::vector<int32> task_output;
114  {
115  TaskSequencer<MyTaskClass> sequencer(config);
116  for (int32 i = 0; i < num_tasks; i++) {
117  sequencer.Run(new MyTaskClass(i, &task_output));
118  }
119  } // and let "sequencer" be destroyed, which waits for the last threads.
120  KALDI_ASSERT(task_output.size() == static_cast<size_t>(num_tasks));
121  for (int32 i = 0; i < num_tasks; i++)
122  KALDI_ASSERT(task_output[i] == i);
123 }
124 
125 
126 } // end namespace kaldi.
127 
128 int main() {
129  using namespace kaldi;
130  TestThreads();
131  for (int32 i = 0; i < 10; i++)
133 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void Run(C *c)
This function takes ownership of the pointer "c", and will delete it in the same sequence as Run was ...
Definition: kaldi-thread.h:190
MyThreadClass(int32 max_to_count, int32 *i)
int32 g_num_threads
Definition: kaldi-thread.cc:25
kaldi::int32 int32
MyThreadClass(const MyThreadClass &other)
MyTaskClass(int32 i, std::vector< int32 > *vec)
void TestTaskSequencer()
int main()
void RunMultiThreaded(const C &c_in)
Here, class C should inherit from MultiThreadable.
Definition: kaldi-thread.h:151
std::vector< int32 > * vec_
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void TestThreads()