Skip to content

Commit

Permalink
Merge pull request #62 from HazyResearch/fix_factor_race_cond_paralle…
Browse files Browse the repository at this point in the history
…l_load

Fixes a race condition in load_factors with a bunch of mutexes
  • Loading branch information
alldefector authored Jan 3, 2017
2 parents 2607bdb + a460f2e commit 1505afb
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/binary_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ void FactorGraph::load_variables(const std::vector<std::string> &filenames) {

void FactorGraph::load_factors(const std::vector<std::string> &filenames) {
std::mutex mtx;
// an array of mutexes for serializing accesses to the variables
std::unique_ptr<std::mutex[]> mtx_variables(
new std::mutex[size.num_variables]);
std::atomic<size_t> factor_cntr(0), edge_cntr(0);
parallel_load(filenames, [this, &mtx, &factor_cntr,
parallel_load(filenames, [this, &mtx, &mtx_variables, &factor_cntr,
&edge_cntr](const std::string &filename) {
std::ifstream file(filename, std::ios::binary);
size_t num_factors = 0;
Expand Down Expand Up @@ -164,7 +167,10 @@ void FactorGraph::load_factors(const std::vector<std::string> &filenames) {
// normalize boolean var vals to 0 for indexing purpusoes
dense_val = Variable::BOOLEAN_DENSE_VALUE;
}
variables[variable_id].add_value_factor(dense_val, idx);
{
std::lock_guard<std::mutex> lck(mtx_variables[variable_id]);
variables[variable_id].add_value_factor(dense_val, idx);
}
++edge_idx;
}

Expand Down

0 comments on commit 1505afb

Please sign in to comment.