TaskSequencer< C > Class Template Reference

#include <kaldi-thread.h>

Collaboration diagram for TaskSequencer< C >:

Classes

struct  RunTaskArgsList
 

Public Member Functions

 TaskSequencer (const TaskSequencerConfig &config)
 
void Run (C *c)
 This function takes ownership of the pointer "c", and will delete it in the same sequence as Run was called on the jobs. More...
 
void Wait ()
 
 ~TaskSequencer ()
 The destructor waits for the last thread to exit. More...
 

Static Private Member Functions

static void RunTask (RunTaskArgsList *args)
 

Private Attributes

int32 num_threads_
 
Semaphore threads_avail_
 
Semaphore tot_threads_avail_
 
RunTaskArgsListthread_list_
 

Detailed Description

template<class C>
class kaldi::TaskSequencer< C >

Definition at line 175 of file kaldi-thread.h.

Constructor & Destructor Documentation

◆ TaskSequencer()

TaskSequencer ( const TaskSequencerConfig config)
inline

Definition at line 177 of file kaldi-thread.h.

References KALDI_ASSERT, TaskSequencerConfig::num_threads, and TaskSequencerConfig::num_threads_total.

177  :
178  num_threads_(config.num_threads),
179  threads_avail_(config.num_threads),
180  tot_threads_avail_(config.num_threads_total > 0 ? config.num_threads_total :
181  config.num_threads + 20),
182  thread_list_(NULL) {
183  KALDI_ASSERT((config.num_threads_total <= 0 ||
184  config.num_threads_total >= config.num_threads) &&
185  "num-threads-total, if specified, must be >= num-threads");
186  }
Semaphore tot_threads_avail_
Definition: kaldi-thread.h:277
RunTaskArgsList * thread_list_
Definition: kaldi-thread.h:279
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Semaphore threads_avail_
Definition: kaldi-thread.h:274

◆ ~TaskSequencer()

~TaskSequencer ( )
inline

The destructor waits for the last thread to exit.

Definition at line 221 of file kaldi-thread.h.

221  {
222  Wait();
223  }

Member Function Documentation

◆ Run()

void Run ( C *  c)
inline

This function takes ownership of the pointer "c", and will delete it in the same sequence as Run was called on the jobs.

Definition at line 190 of file kaldi-thread.h.

References MultiThreadable::num_threads_.

Referenced by IvectorExtractor::ComputeDerivedVars(), kaldi::nnet2::LimitRankParallel(), main(), kaldi::ProcessUtterance(), kaldi::TestTaskSequencer(), IvectorExtractorStats::UpdateProjections(), and IvectorExtractorStats::UpdateWeights().

190  {
191  // run in main thread
192  if (num_threads_ == 0) {
193  (*c)();
194  delete c;
195  return;
196  }
197 
198  threads_avail_.Wait(); // wait till we have a thread for computation free.
199  tot_threads_avail_.Wait(); // this ensures we don't have too many threads
200  // waiting on I/O, and consume too much memory.
201 
202  // put the new RunTaskArgsList object at head of the singly
203  // linked list thread_list_.
204  thread_list_ = new RunTaskArgsList(this, c, thread_list_);
206  thread_list_);
207  }
static void RunTask(RunTaskArgsList *args)
Definition: kaldi-thread.h:234
Semaphore tot_threads_avail_
Definition: kaldi-thread.h:277
RunTaskArgsList * thread_list_
Definition: kaldi-thread.h:279
Semaphore threads_avail_
Definition: kaldi-thread.h:274
void Wait()
decrease the counter

◆ RunTask()

static void RunTask ( RunTaskArgsList args)
inlinestaticprivate

Definition at line 234 of file kaldi-thread.h.

References TaskSequencer< C >::RunTaskArgsList::c, KALDI_ASSERT, TaskSequencer< C >::RunTaskArgsList::me, Semaphore::Signal(), TaskSequencer< C >::RunTaskArgsList::tail, TaskSequencer< C >::RunTaskArgsList::thread, TaskSequencer< C >::threads_avail_, and TaskSequencer< C >::tot_threads_avail_.

234  {
235  // (1) run the job.
236  (*(args->c))(); // call operator () on args->c, which does the computation.
237  args->me->threads_avail_.Signal(); // Signal that the compute-intensive
238  // part of the thread is done (we want to run no more than
239  // config_.num_threads of these.)
240 
241  // (2) we want to destroy the object "c" now, by deleting it. But for
242  // correct sequencing (this is the whole point of this class, it
243  // is intended to ensure the output of the program is in correct order),
244  // we first wait till the previous thread, whose details will be in "tail",
245  // is finished.
246  if (args->tail != NULL) {
247  args->tail->thread.join();
248  }
249 
250  delete args->c; // delete the object "c". This may cause some output,
251  // e.g. to a stream. We don't need to worry about concurrent access to
252  // the output stream, because each thread waits for the previous thread
253  // to be done, before doing this. So there is no risk of concurrent
254  // access.
255  args->c = NULL;
256 
257  if (args->tail != NULL) {
258  KALDI_ASSERT(args->tail->tail == NULL); // Because we already
259  // did join on args->tail->thread, which means that
260  // thread was done, and before it exited, it would have
261  // deleted and set to NULL its tail (which is the next line of code).
262  delete args->tail;
263  args->tail = NULL;
264  }
265  // At this point we are exiting from the thread. Signal the
266  // "tot_threads_avail_" semaphore which is used to limit the total number of threads that are alive, including
267  // not onlhy those that are in active computation in c->operator (), but those
268  // that are waiting on I/O or other threads.
269  args->me->tot_threads_avail_.Signal();
270  }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Wait()

void Wait ( )
inline

Definition at line 209 of file kaldi-thread.h.

References KALDI_ASSERT.

Referenced by main().

209  { // You call this at the end if it's more convenient
210  // than waiting for the destructor. It waits for all tasks to finish.
211  if (thread_list_ != NULL) {
212  thread_list_->thread.join();
213  KALDI_ASSERT(thread_list_->tail == NULL); // thread would not
214  // have exited without setting tail to NULL.
215  delete thread_list_;
216  thread_list_ = NULL;
217  }
218  }
RunTaskArgsList * thread_list_
Definition: kaldi-thread.h:279
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

Member Data Documentation

◆ num_threads_

int32 num_threads_
private

Definition at line 272 of file kaldi-thread.h.

◆ thread_list_

RunTaskArgsList* thread_list_
private

Definition at line 279 of file kaldi-thread.h.

◆ threads_avail_

Semaphore threads_avail_
private

Definition at line 274 of file kaldi-thread.h.

Referenced by TaskSequencer< C >::RunTask().

◆ tot_threads_avail_

Semaphore tot_threads_avail_
private

Definition at line 277 of file kaldi-thread.h.

Referenced by TaskSequencer< C >::RunTask().


The documentation for this class was generated from the following file: