Skip to content

Commit

Permalink
mutex thread lock
Browse files Browse the repository at this point in the history
  • Loading branch information
liu946 committed Oct 23, 2017
1 parent 9c023e3 commit 48689eb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/srl/DepSRL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

using namespace std;

std::mutex DepSRL::mtx;

// Load necessary resources into memory
int DepSRL::LoadResource(const string &modelFile)
{
Expand Down Expand Up @@ -69,22 +71,25 @@ int DepSRL::GetSRLResult(
sentence.push_back(word);
}
// pi prediction
mtx.lock();
{
ComputationGraph hg;
vector<Expression> adists = pi_model->label(hg, sentence);
pi_model->ExtractResults(hg, adists, sentence);
}
mtx.unlock();
if ( !sentence.getPredicateList().size() ) {
// skip all processing if no predicate
return 0;
}
// srl prediction
mtx.lock();
{
ComputationGraph hg;
vector<Expression> adists = srl_model->label(hg, sentence);
srl_model->ExtractResults(hg, adists, sentence);
}

mtx.unlock();
if (!FormResult(words, POSs, sentence.getPredicateList(), sentence, vecSRLResult))
return -1;
return 0;
Expand Down
2 changes: 2 additions & 0 deletions src/srl/DepSRL.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "Pi/model/SrlPiModel.h"
#include "Srl/model/SrlSrlModel.h"
#include "structure/WordEmbBuilder.h"
#include "mutex"

class DepSRL {

Expand Down Expand Up @@ -93,6 +94,7 @@ class DepSRL {
SrlSrlModel * srl_model;
PiModel * pi_model;
unordered_map<string, vector<float>> embedding;
static std::mutex mtx; // to fix dynet single CG constrain.
private:
void manageConfigPath(ModelConf &config, const string &dirPath);

Expand Down

0 comments on commit 48689eb

Please sign in to comment.