Skip to content

Commit

Permalink
fix(torch): avoid race condition when building alphabet
Browse files Browse the repository at this point in the history
  • Loading branch information
royale authored and mergify[bot] committed Jan 25, 2023
1 parent 9541de1 commit b1accb7
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/backends/torch/torchdataset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,21 @@ namespace dd
target_tensor[i] = it->second;
}
else if (!_test)
#pragma omp ordered
{
this->_logger->info("added {} to alphabet", c);
int id = alphabet.size();
alphabet[c] = id;
target_tensor[i] = id;
// in a parallel loop, recheck in order to avoid race condition
auto it = alphabet.find(c);
if (it != alphabet.end())
{
target_tensor[i] = it->second;
}
else
{
this->_logger->info("added {} to alphabet", c);
int id = alphabet.size();
alphabet[c] = id;
target_tensor[i] = id;
}
}
else
{
Expand Down

0 comments on commit b1accb7

Please sign in to comment.