Skip to content

Commit

Permalink
[src] Use STL thread support library instead of pthread. (#1350)
Browse files Browse the repository at this point in the history
  • Loading branch information
dogancan authored and danpovey committed May 26, 2017
1 parent 6739d60 commit 6cc8e3a
Show file tree
Hide file tree
Showing 105 changed files with 942 additions and 1,572 deletions.
51 changes: 25 additions & 26 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
SHELL := /bin/bash


SUBDIRS = base matrix util feat tree thread gmm transform \
SUBDIRS = base matrix util feat tree gmm transform \
fstext hmm lm decoder lat kws cudamatrix nnet \
bin fstbin gmmbin fgmmbin featbin \
nnetbin latbin sgmm2 sgmm2bin nnet2 nnet3 chain nnet3bin nnet2bin kwsbin \
ivector ivectorbin online2 online2bin lmbin chainbin

MEMTESTDIRS = base matrix util feat tree thread gmm transform \
MEMTESTDIRS = base matrix util feat tree gmm transform \
fstext hmm lm decoder lat nnet kws chain \
bin fstbin gmmbin fgmmbin featbin \
nnetbin latbin sgmm2 nnet2 nnet3 nnet2bin nnet3bin sgmm2bin kwsbin \
Expand Down Expand Up @@ -151,33 +151,32 @@ $(EXT_SUBDIRS) : mklibdir ext_depend
#1)The tools depend on all the libraries

bin fstbin gmmbin fgmmbin sgmm2bin featbin nnetbin nnet2bin nnet3bin chainbin latbin ivectorbin lmbin kwsbin online2bin: \
base matrix util feat tree thread gmm transform sgmm2 fstext hmm \
base matrix util feat tree gmm transform sgmm2 fstext hmm \
lm decoder lat cudamatrix nnet nnet2 nnet3 ivector chain kws online2

#2)The libraries have inter-dependencies
base: base/.depend.mk
matrix: base
thread: base
util: base matrix thread
feat: base matrix util gmm transform tree thread
tree: base util thread matrix
gmm: base util matrix tree thread
transform: base util matrix gmm tree thread
sgmm2: base util matrix gmm tree transform thread hmm
fstext: base util thread matrix tree
hmm: base tree matrix util thread
lm: base util thread matrix fstext
decoder: base util thread matrix gmm hmm tree transform lat
lat: base util thread hmm tree matrix
cudamatrix: base util thread matrix
nnet: base util hmm tree thread matrix cudamatrix
nnet2: base util matrix thread lat gmm hmm tree transform cudamatrix
nnet3: base util matrix thread lat gmm hmm tree transform cudamatrix chain fstext
chain: lat hmm tree fstext matrix cudamatrix util thread base
ivector: base util matrix thread transform tree gmm
util: base matrix
feat: base matrix util gmm transform tree
tree: base util matrix
gmm: base util matrix tree
transform: base util matrix gmm tree
sgmm2: base util matrix gmm tree transform hmm
fstext: base util matrix tree
hmm: base tree matrix util
lm: base util matrix fstext
decoder: base util matrix gmm hmm tree transform lat
lat: base util hmm tree matrix
cudamatrix: base util matrix
nnet: base util hmm tree matrix cudamatrix
nnet2: base util matrix lat gmm hmm tree transform cudamatrix
nnet3: base util matrix lat gmm hmm tree transform cudamatrix chain fstext
chain: lat hmm tree fstext matrix cudamatrix util base
ivector: base util matrix transform tree gmm
#3)Dependencies for optional parts of Kaldi
onlinebin: base matrix util feat tree gmm transform sgmm2 fstext hmm lm decoder lat cudamatrix nnet nnet2 online thread
# python-kaldi-decoding: base matrix util feat tree thread gmm transform sgmm2 fstext hmm decoder lat online
online: decoder gmm transform feat matrix util base lat hmm thread tree
online2: decoder gmm transform feat matrix util base lat hmm thread tree ivector cudamatrix nnet2 nnet3 chain
kws: base util thread hmm tree matrix lat
onlinebin: base matrix util feat tree gmm transform sgmm2 fstext hmm lm decoder lat cudamatrix nnet nnet2 online
# python-kaldi-decoding: base matrix util feat tree gmm transform sgmm2 fstext hmm decoder lat online
online: decoder gmm transform feat matrix util base lat hmm tree
online2: decoder gmm transform feat matrix util base lat hmm tree ivector cudamatrix nnet2 nnet3 chain
kws: base util hmm tree matrix lat
16 changes: 4 additions & 12 deletions src/base/kaldi-math.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

#include "base/kaldi-math.h"
#ifndef _MSC_VER
#include <pthread.h>
#include <stdlib.h>
#endif
#include <string>
#include <mutex>

namespace kaldi {
// These routines are tested in matrix/matrix-test.cc
Expand All @@ -39,9 +39,7 @@ int32 RoundUpToNearestPowerOfTwo(int32 n) {
return n+1;
}

#ifndef _MSC_VER
static pthread_mutex_t _RandMutex = PTHREAD_MUTEX_INITIALIZER;
#endif
static std::mutex _RandMutex;

int Rand(struct RandomState* state) {
#ifdef _MSC_VER
Expand All @@ -51,12 +49,8 @@ int Rand(struct RandomState* state) {
if (state) {
return rand_r(&(state->seed));
} else {
int rs = pthread_mutex_lock(&_RandMutex);
KALDI_ASSERT(rs == 0);
int val = rand();
rs = pthread_mutex_unlock(&_RandMutex);
KALDI_ASSERT(rs == 0);
return val;
std::lock_guard<std::mutex> lock(_RandMutex);
return rand();
}
#endif
}
Expand Down Expand Up @@ -167,5 +161,3 @@ void RandGauss2(double *a, double *b, RandomState *state) {


} // end namespace kaldi


2 changes: 1 addition & 1 deletion src/bin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ OBJFILES =
ADDLIBS = ../decoder/kaldi-decoder.a ../lat/kaldi-lat.a ../lm/kaldi-lm.a \
../fstext/kaldi-fstext.a ../hmm/kaldi-hmm.a \
../transform/kaldi-transform.a ../gmm/kaldi-gmm.a \
../tree/kaldi-tree.a ../util/kaldi-util.a ../thread/kaldi-thread.a \
../tree/kaldi-tree.a ../util/kaldi-util.a \
../matrix/kaldi-matrix.a ../base/kaldi-base.a


Expand Down
26 changes: 13 additions & 13 deletions src/bin/latgen-faster-mapped-parallel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// 2013 Johns Hopkins University (author: Daniel Povey)
// 2013 GoVIvace Inc. (author: Nagendra Goel)
// 2014 Guoguo Chen

// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -29,7 +29,7 @@
#include "decoder/decoder-wrappers.h"
#include "decoder/decodable-matrix.h"
#include "base/timer.h"
#include "thread/kaldi-task-sequence.h"
#include "util/kaldi-thread.h"

int main(int argc, char *argv[]) {
try {
Expand Down Expand Up @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) {

po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
po.Register("allow-partial", &allow_partial, "If true, produce output even if end state was not reached.");

po.Read(argc, argv);

if (po.NumArgs() < 4 || po.NumArgs() > 6) {
Expand All @@ -73,7 +73,7 @@ int main(int argc, char *argv[]) {
lattice_wspecifier = po.GetArg(4),
words_wspecifier = po.GetOptArg(5),
alignment_wspecifier = po.GetOptArg(6);

TransitionModel trans_model;
ReadKaldiObject(model_in_filename, &trans_model);

Expand All @@ -90,7 +90,7 @@ int main(int argc, char *argv[]) {
Int32VectorWriter alignment_writer(alignment_wspecifier);

fst::SymbolTable *word_syms = NULL;
if (word_syms_filename != "")
if (word_syms_filename != "")
if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
KALDI_ERR << "Could not read symbol table from file "
<< word_syms_filename;
Expand All @@ -100,7 +100,7 @@ int main(int argc, char *argv[]) {
int num_success = 0, num_fail = 0;
VectorFst<StdArc> *decode_fst = NULL; // only used if there is a single
// decoding graph.

TaskSequencer<DecodeUtteranceLatticeFasterClass> sequencer(sequencer_config);
if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
SequentialBaseFloatMatrixReader loglike_reader(feature_rspecifier);
Expand All @@ -110,7 +110,7 @@ int main(int argc, char *argv[]) {
{
for (; !loglike_reader.Done(); loglike_reader.Next()) {
std::string utt = loglike_reader.Key();
Matrix<BaseFloat> *loglikes =
Matrix<BaseFloat> *loglikes =
new Matrix<BaseFloat>(loglike_reader.Value());
loglike_reader.FreeCurrent();
if (loglikes->NumRows() == 0) {
Expand All @@ -119,10 +119,10 @@ int main(int argc, char *argv[]) {
delete loglikes;
continue;
}

LatticeFasterDecoder *decoder = new LatticeFasterDecoder(*decode_fst,
config);
DecodableMatrixScaledMapped *decodable =
DecodableMatrixScaledMapped *decodable =
new DecodableMatrixScaledMapped(trans_model, acoustic_scale, loglikes);
DecodeUtteranceLatticeFasterClass *task =
new DecodeUtteranceLatticeFasterClass(
Expand All @@ -137,7 +137,7 @@ int main(int argc, char *argv[]) {
}
} else { // We have different FSTs for different utterances.
SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
RandomAccessBaseFloatMatrixReader loglike_reader(feature_rspecifier);
RandomAccessBaseFloatMatrixReader loglike_reader(feature_rspecifier);
for (; !fst_reader.Done(); fst_reader.Next()) {
std::string utt = fst_reader.Key();
if (!loglike_reader.HasKey(utt)) {
Expand All @@ -146,15 +146,15 @@ int main(int argc, char *argv[]) {
num_fail++;
continue;
}
const Matrix<BaseFloat> *loglikes =
const Matrix<BaseFloat> *loglikes =
new Matrix<BaseFloat>(loglike_reader.Value(utt));
if (loglikes->NumRows() == 0) {
KALDI_WARN << "Zero-length utterance: " << utt;
num_fail++;
delete loglikes;
continue;
}
LatticeFasterDecoder *decoder =
LatticeFasterDecoder *decoder =
new LatticeFasterDecoder(fst_reader.Value(), config);
DecodableMatrixScaledMapped *decodable = new
DecodableMatrixScaledMapped(trans_model, acoustic_scale, loglikes);
Expand All @@ -171,7 +171,7 @@ int main(int argc, char *argv[]) {
sequencer.Wait();

delete decode_fst;

double elapsed = timer.Elapsed();
KALDI_LOG << "Decoded with " << sequencer_config.num_threads << " threads.";
KALDI_LOG << "Time taken "<< elapsed
Expand Down
2 changes: 1 addition & 1 deletion src/chain/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ LIBNAME = kaldi-chain

ADDLIBS = ../cudamatrix/kaldi-cudamatrix.a ../lat/kaldi-lat.a \
../fstext/kaldi-fstext.a ../hmm/kaldi-hmm.a ../tree/kaldi-tree.a \
../util/kaldi-util.a ../thread/kaldi-thread.a \
../util/kaldi-util.a \
../matrix/kaldi-matrix.a ../base/kaldi-base.a

# Make sure we have CUDA_ARCH from kaldi.mk,
Expand Down
2 changes: 1 addition & 1 deletion src/chainbin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ADDLIBS = ../nnet3/kaldi-nnet3.a ../chain/kaldi-chain.a \
../cudamatrix/kaldi-cudamatrix.a ../decoder/kaldi-decoder.a \
../lat/kaldi-lat.a ../fstext/kaldi-fstext.a ../hmm/kaldi-hmm.a \
../transform/kaldi-transform.a ../gmm/kaldi-gmm.a \
../tree/kaldi-tree.a ../util/kaldi-util.a ../thread/kaldi-thread.a \
../tree/kaldi-tree.a ../util/kaldi-util.a \
../matrix/kaldi-matrix.a ../base/kaldi-base.a

include ../makefiles/default_rules.mk
2 changes: 1 addition & 1 deletion src/cudamatrix/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endif

LIBNAME = kaldi-cudamatrix

ADDLIBS = ../util/kaldi-util.a ../thread/kaldi-thread.a \
ADDLIBS = ../util/kaldi-util.a \
../matrix/kaldi-matrix.a ../base/kaldi-base.a

# Make sure we have CUDA_ARCH from kaldi.mk,
Expand Down
2 changes: 1 addition & 1 deletion src/decoder/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LIBNAME = kaldi-decoder

ADDLIBS = ../lat/kaldi-lat.a ../hmm/kaldi-hmm.a \
../transform/kaldi-transform.a ../gmm/kaldi-gmm.a \
../tree/kaldi-tree.a ../util/kaldi-util.a ../thread/kaldi-thread.a \
../tree/kaldi-tree.a ../util/kaldi-util.a \
../matrix/kaldi-matrix.a ../base/kaldi-base.a

include ../makefiles/default_rules.mk
7 changes: 3 additions & 4 deletions src/decoder/decoder-wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,9 @@ bool DecodeUtteranceLatticeFaster(

/// This class basically does the same job as the function
/// DecodeUtteranceLatticeFaster, but in a way that allows us
/// to build a multi-threaded command line program more easily,
/// using code in ../thread/kaldi-task-sequence.h. The main
/// computation takes place in operator (), and the output happens
/// in the destructor.
/// to build a multi-threaded command line program more easily.
/// The main computation takes place in operator (), and the output
/// happens in the destructor.
class DecodeUtteranceLatticeFasterClass {
public:
// Initializer sets various variables.
Expand Down
2 changes: 1 addition & 1 deletion src/feat/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ OBJFILES = feature-functions.o feature-mfcc.o feature-plp.o feature-fbank.o \
LIBNAME = kaldi-feat

ADDLIBS = ../transform/kaldi-transform.a ../gmm/kaldi-gmm.a \
../tree/kaldi-tree.a ../util/kaldi-util.a ../thread/kaldi-thread.a \
../tree/kaldi-tree.a ../util/kaldi-util.a \
../matrix/kaldi-matrix.a ../base/kaldi-base.a

include ../makefiles/default_rules.mk
Expand Down
2 changes: 1 addition & 1 deletion src/featbin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TESTFILES =

ADDLIBS = ../hmm/kaldi-hmm.a ../feat/kaldi-feat.a \
../transform/kaldi-transform.a ../gmm/kaldi-gmm.a \
../tree/kaldi-tree.a ../util/kaldi-util.a ../thread/kaldi-thread.a \
../tree/kaldi-tree.a ../util/kaldi-util.a \
../matrix/kaldi-matrix.a ../base/kaldi-base.a

include ../makefiles/default_rules.mk
Expand Down
2 changes: 1 addition & 1 deletion src/fgmmbin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TESTFILES =
ADDLIBS = ../decoder/kaldi-decoder.a ../lat/kaldi-lat.a ../hmm/kaldi-hmm.a \
../feat/kaldi-feat.a ../transform/kaldi-transform.a \
../gmm/kaldi-gmm.a ../tree/kaldi-tree.a ../util/kaldi-util.a \
../thread/kaldi-thread.a ../matrix/kaldi-matrix.a \
../matrix/kaldi-matrix.a \
../base/kaldi-base.a

include ../makefiles/default_rules.mk
3 changes: 1 addition & 2 deletions src/fstbin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ TESTFILES =
LIBFILE =

ADDLIBS = ../fstext/kaldi-fstext.a ../util/kaldi-util.a \
../thread/kaldi-thread.a ../matrix/kaldi-matrix.a \
../base/kaldi-base.a
../matrix/kaldi-matrix.a ../base/kaldi-base.a

include ../makefiles/default_rules.mk
2 changes: 1 addition & 1 deletion src/fstext/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LIBNAME = kaldi-fstext

# tree and matrix archives needed for test-context-fst
# matrix archive needed for push-special.
ADDLIBS = ../tree/kaldi-tree.a ../util/kaldi-util.a ../thread/kaldi-thread.a \
ADDLIBS = ../tree/kaldi-tree.a ../util/kaldi-util.a \
../matrix/kaldi-matrix.a ../base/kaldi-base.a

include ../makefiles/default_rules.mk
2 changes: 1 addition & 1 deletion src/gmm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ OBJFILES = diag-gmm.o diag-gmm-normal.o mle-diag-gmm.o am-diag-gmm.o \

LIBNAME = kaldi-gmm

ADDLIBS = ../tree/kaldi-tree.a ../util/kaldi-util.a ../thread/kaldi-thread.a \
ADDLIBS = ../tree/kaldi-tree.a ../util/kaldi-util.a \
../matrix/kaldi-matrix.a ../base/kaldi-base.a


Expand Down
13 changes: 6 additions & 7 deletions src/gmm/mle-diag-gmm-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ test_io(const DiagGmm &gmm, const AccumDiagGmm &est_gmm, bool binary,
}

AssertEqual(loglike1, loglike2, 1.0e-6);

unlink("tmp_stats");
}

Expand Down Expand Up @@ -278,7 +278,7 @@ UnitTestEstimateDiagGmm() {
DiagGmm rgmm;
rgmm.Resize(1, dim);
ngmm.CopyToDiagGmm(&rgmm);

// check contents
KALDI_ASSERT(ApproxEqual(weights(0), 1.0F, 1e-6));
KALDI_ASSERT(ApproxEqual(gmm->weights()(0), rgmm.weights()(0), 1e-6));
Expand Down Expand Up @@ -329,7 +329,7 @@ UnitTestEstimateDiagGmm() {
lastloglike = loglike;
lastloglike_nM = gmm->NumGauss();
}

// binary write
est_gmm.Write(Output("tmp_stats", true).Stream(), true);

Expand Down Expand Up @@ -384,7 +384,7 @@ UnitTestEstimateDiagGmm() {
for (size_t i = 0; i < counter; i++)
weights(i) = 0.5 + 0.1 * (Rand() % 10);


float loglike = 0.0;
for (size_t i = 0; i < counter; i++) {
loglike += weights(i) *
Expand All @@ -398,9 +398,9 @@ UnitTestEstimateDiagGmm() {
est_gmm.AssertEqual(est_gmm2);
}


delete gmm;

unlink("tmp_stats");
}

Expand All @@ -410,4 +410,3 @@ int main() {
UnitTestEstimateDiagGmm();
std::cout << "Test OK.\n";
}

Loading

0 comments on commit 6cc8e3a

Please sign in to comment.