Skip to content

Commit

Permalink
use local iterator to prevent race condition when multiple threads ge…
Browse files Browse the repository at this point in the history
…t importance values
  • Loading branch information
joostverburg committed Apr 25, 2017
1 parent 3a54076 commit 8f8b31b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
5 changes: 1 addition & 4 deletions source/geometry/biasing/include/G4IStore.hh
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public: // with description
// Add a "cell" together with a importance value to the store.

void ChangeImportance(G4double importance,
const G4GeometryCell &gCell);
const G4GeometryCell &gCell);
void ChangeImportance(G4double importance,
const G4VPhysicalVolume &,
G4int aRepNum = 0);
Expand All @@ -131,7 +131,6 @@ public: // with description
private:

G4bool IsInWorld(const G4VPhysicalVolume &) const;
void SetInternalIterator(const G4GeometryCell &gCell) const;
void Error(const G4String &m) const;

private:
Expand All @@ -141,8 +140,6 @@ private:
// G4bool fParaFlag;
G4GeometryCellImportance fGeometryCelli;

mutable G4GeometryCellImportance::const_iterator fCurrentIterator;

static G4ThreadLocal G4IStore* fInstance;
};

Expand Down
35 changes: 18 additions & 17 deletions source/geometry/biasing/src/G4IStore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ const G4VPhysicalVolume* G4IStore::GetParallelWorldVolumePointer() const
return fWorldVolume;
}

void G4IStore::SetInternalIterator(const G4GeometryCell &gCell) const
{
fCurrentIterator = fGeometryCelli.find(gCell);
}

void G4IStore::AddImportanceGeometryCell(G4double importance,
const G4GeometryCell &gCell)
{
Expand All @@ -109,8 +104,9 @@ void G4IStore::AddImportanceGeometryCell(G4double importance,
if (!IsInWorld(gCell.GetPhysicalVolume()) ) {
Error("AddImportanceGeometryCell() - Physical volume not found!");
}
SetInternalIterator(gCell);
if (fCurrentIterator!=fGeometryCelli.end()) {
G4GeometryCellImportance::const_iterator gCellIterator;
gCellIterator = fGeometryCelli.find(gCell);
if (gCellIterator!=fGeometryCelli.end()) {
Error("AddImportanceGeometryCell() - Region already existing!");
}
fGeometryCelli[gCell] = importance;
Expand All @@ -132,8 +128,9 @@ void G4IStore::ChangeImportance(G4double importance,
if (!IsInWorld(gCell.GetPhysicalVolume()) ) {
Error("ChangeImportance() - Physical volume not found!");
}
SetInternalIterator(gCell);
if (fCurrentIterator==fGeometryCelli.end()) {
G4GeometryCellImportance::const_iterator gCellIterator;
gCellIterator = fGeometryCelli.find(gCell);
if (gCellIterator==fGeometryCelli.end()) {
Error("ChangeImportance() - Region does not exist!");
}
fGeometryCelli[gCell] = importance;
Expand All @@ -149,20 +146,23 @@ void G4IStore::ChangeImportance(G4double importance,
G4double G4IStore::GetImportance(const G4VPhysicalVolume &aVolume,
G4int aRepNum) const
{
SetInternalIterator(G4GeometryCell(aVolume, aRepNum));
G4GeometryCellImportance::const_iterator gCellIterator = fCurrentIterator;
G4GeometryCell gCell = G4GeometryCell(aVolume, aRepNum);
G4GeometryCellImportance::const_iterator gCellIterator;
gCellIterator = fGeometryCelli.find(gCell);

if (gCellIterator==fGeometryCelli.end()) {
Error("GetImportance() - Region does not exist!");
return 0.;
}
return (*fCurrentIterator).second;
return (*gCellIterator).second;
}


G4double G4IStore::GetImportance(const G4GeometryCell &gCell) const
{
SetInternalIterator(gCell);
G4GeometryCellImportance::const_iterator gCellIterator = fCurrentIterator;
G4GeometryCellImportance::const_iterator gCellIterator;
gCellIterator = fGeometryCelli.find(gCell);

if (gCellIterator==fGeometryCelli.end()) {
std::ostringstream err_mess;
err_mess << "GetImportance() - Region does not exist!" << G4endl
Expand All @@ -171,15 +171,16 @@ G4double G4IStore::GetImportance(const G4GeometryCell &gCell) const
Error(err_mess.str());
return 0.;
}
return (*fCurrentIterator).second;
return (*gCellIterator).second;
}

G4bool G4IStore::IsKnown(const G4GeometryCell &gCell) const {
G4bool inWorldKnown(IsInWorld(gCell.GetPhysicalVolume()));

if ( inWorldKnown ) {
SetInternalIterator(gCell);
inWorldKnown = (fCurrentIterator!=fGeometryCelli.end());
G4GeometryCellImportance::const_iterator gCellIterator;
gCellIterator = fGeometryCelli.find(gCell);
inWorldKnown = (gCellIterator!=fGeometryCelli.end());
}
return inWorldKnown;
}
Expand Down

0 comments on commit 8f8b31b

Please sign in to comment.