From cc6822f171e9963f91a6adeb7d3fabcbbe363f46 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 7 Oct 2019 00:11:51 -0500 Subject: [PATCH 1/8] Pulling in C++ side changes for counter caching. --- include/openmc/geometry_aux.h | 70 +++++++++++++++++++ src/finalize.cpp | 4 ++ src/geometry_aux.cpp | 126 +++++++++++++++++++++++++++++++--- 3 files changed, 189 insertions(+), 11 deletions(-) diff --git a/include/openmc/geometry_aux.h b/include/openmc/geometry_aux.h index ffcdcf885..557d82193 100644 --- a/include/openmc/geometry_aux.h +++ b/include/openmc/geometry_aux.h @@ -7,8 +7,78 @@ #include #include #include +#include namespace openmc { + struct CellCountStorage { + private: + CellCountStorage() {} + CellCountStorage(CellCountStorage& c) {} + CellCountStorage(const CellCountStorage& c) {} + + static CellCountStorage* instance_; + + std::map> counts; + + public: + + static CellCountStorage* instance() { + if (instance_ == nullptr) + instance_ = new CellCountStorage; + + return instance_; + } + + + + void clear(); + + void set_cell_count_for_univ(int32_t univ, int32_t cell, int count); + + void increment_count_for_univ(int32_t univ, int32_t cell); + + bool has_count(int32_t univ, int32_t cell); + + bool has_count(int32_t univ); + + void absorb_b_into_a(int32_t a, int32_t b); + + auto get_count(int32_t univ); + }; + + struct LevelCountStorage { + private: + LevelCountStorage() {} + LevelCountStorage(LevelCountStorage& c) {} + LevelCountStorage(const LevelCountStorage& c) {} + + static LevelCountStorage* instance_; + + std::map counts; + + public: + + static LevelCountStorage* instance() { + if (instance_ == nullptr) + instance_ = new LevelCountStorage; + + return instance_; + } + + void clear(); + + void set_cell_count_for_univ(int32_t univ, int count); + + void increment_count_for_univ(int32_t univ); + + bool has_count(int32_t univ); + + void absorb_b_into_a(int32_t a, int32_t b); + + void set_count(int32_t univ, int count); + + int get_count(int32_t univ); + }; void read_geometry_xml(); diff --git a/src/finalize.cpp b/src/finalize.cpp index 3de20d6d0..f2cbc973c 100644 --- a/src/finalize.cpp +++ b/src/finalize.cpp @@ -135,6 +135,10 @@ int openmc_finalize() int openmc_reset() { + + CellCountStorage::instance()->clear(); + LevelCountStorage::instance()->clear(); + for (auto& t : model::tallies) { t->reset(); } diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index d195a9de3..662b6790d 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -60,6 +60,91 @@ void read_geometry_xml() model::root_universe = find_root_universe(); } +void CellCountStorage::clear() { + if (instance_ != nullptr) { + delete instance_; + instance_ = nullptr; + } +} + +void CellCountStorage::set_cell_count_for_univ(int32_t univ, int32_t cell, int count) { + counts[univ][cell] = count; +} + + void CellCountStorage::increment_count_for_univ(int32_t univ, int32_t cell) { + if (has_count(univ,cell)) { + counts[univ][cell] += 1; + } else { + counts[univ][cell] = 1; + } + } + +bool CellCountStorage::has_count(int32_t univ, int32_t cell) { + return counts.count(univ) && counts[univ].count(cell); +} + +bool CellCountStorage::has_count(int32_t univ) { + return counts.count(univ); +} + +void CellCountStorage::absorb_b_into_a(int32_t a, int32_t b) { + std::map b_map = counts[b]; + + for (auto it : b_map) { + if (has_count(a, it.first)) { + counts[a][it.first] += it.second; + } else { + counts[a][it.first] = it.second; + } + } +} + +auto CellCountStorage::get_count(int32_t univ) { + return counts[univ]; +} + +void LevelCountStorage::clear() { + if (instance_ != nullptr) { + delete instance_; + instance_ = nullptr; + } +} + +void LevelCountStorage::set_cell_count_for_univ(int32_t univ, int count) { + counts[univ] = count; +} + +void LevelCountStorage::increment_count_for_univ(int32_t univ) { + if (has_count(univ)) { + counts[univ] += 1; + } else { + counts[univ] = 1; + } +} + +bool LevelCountStorage::has_count(int32_t univ) { + return counts.count(univ); +} + +void LevelCountStorage::absorb_b_into_a(int32_t a, int32_t b) { + if (has_count(a)) { + counts[a] += counts[b]; + } else { + counts[a] = counts[b]; + } +} + +void LevelCountStorage::set_count(int32_t univ, int count) { + counts[univ] = count; +} + +int LevelCountStorage::get_count(int32_t univ) { + return counts[univ]; +} + +CellCountStorage* CellCountStorage::instance_ = nullptr; +LevelCountStorage* LevelCountStorage::instance_ = nullptr; + //============================================================================== void @@ -395,19 +480,31 @@ prepare_distribcell() void count_cell_instances(int32_t univ_indx) { - for (int32_t cell_indx : model::universes[univ_indx]->cells_) { - Cell& c = *model::cells[cell_indx]; - ++c.n_instances_; + CellCountStorage* counter = CellCountStorage::instance(); - if (c.type_ == FILL_UNIVERSE) { - // This cell contains another universe. Recurse into that universe. - count_cell_instances(c.fill_); + if (counter->has_count(univ_indx)) { + std::map univ_counts = counter->get_count(univ_indx); + for(auto it : univ_counts) { + Cell& c = *model::cells[it.first]; + c.n_instances_ += it.second; + } + } else { + for (int32_t cell_indx : model::universes[univ_indx]->cells_) { + Cell& c = *model::cells[cell_indx]; + ++c.n_instances_; + counter->increment_count_for_univ(univ_indx, cell_indx); - } else if (c.type_ == FILL_LATTICE) { - // This cell contains a lattice. Recurse into the lattice universes. - Lattice& lat = *model::lattices[c.fill_]; - for (auto it = lat.begin(); it != lat.end(); ++it) { - count_cell_instances(*it); + if (c.type_ == FILL_UNIVERSE) { + // This cell contains another universe. Recurse into that universe. + count_cell_instances(c.fill_); + counter->absorb_b_into_a(univ_indx, c.fill_); + } else if (c.type_ == FILL_LATTICE) { + // This cell contains a lattice. Recurse into the lattice universes. + Lattice& lat = *model::lattices[c.fill_]; + for (auto it = lat.begin(); it != lat.end(); ++it) { + count_cell_instances(*it); + counter->absorb_b_into_a(univ_indx, *it); + } } } } @@ -529,6 +626,12 @@ distribcell_path(int32_t target_cell, int32_t map, int32_t target_offset) int maximum_levels(int32_t univ) { + LevelCountStorage* counter = LevelCountStorage::instance(); + + if (counter->has_count(univ)) { + return counter->get_count(univ); + } + int levels_below {0}; for (int32_t cell_indx : model::universes[univ]->cells_) { @@ -546,6 +649,7 @@ maximum_levels(int32_t univ) } ++levels_below; + counter->set_count(univ, levels_below); return levels_below; } From 4860bd5ed207bbe1584b0ca29d97022c902c8fff Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 6 Nov 2019 09:31:56 -0600 Subject: [PATCH 2/8] Updating name of Universer counter class. --- include/openmc/geometry_aux.h | 14 +++++++------- src/finalize.cpp | 2 +- src/geometry_aux.cpp | 18 +++++++++--------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/include/openmc/geometry_aux.h b/include/openmc/geometry_aux.h index 557d82193..14e119282 100644 --- a/include/openmc/geometry_aux.h +++ b/include/openmc/geometry_aux.h @@ -10,21 +10,21 @@ #include namespace openmc { - struct CellCountStorage { + struct UniverseCellCounter { private: - CellCountStorage() {} - CellCountStorage(CellCountStorage& c) {} - CellCountStorage(const CellCountStorage& c) {} + UniverseCellCounter() {} + UniverseCellCounter(UniverseCellCounter& c) {} + UniverseCellCounter(const UniverseCellCounter& c) {} - static CellCountStorage* instance_; + static UniverseCellCounter* instance_; std::map> counts; public: - static CellCountStorage* instance() { + static UniverseCellCounter* instance() { if (instance_ == nullptr) - instance_ = new CellCountStorage; + instance_ = new UniverseCellCounter; return instance_; } diff --git a/src/finalize.cpp b/src/finalize.cpp index f2cbc973c..3b8530b21 100644 --- a/src/finalize.cpp +++ b/src/finalize.cpp @@ -136,7 +136,7 @@ int openmc_finalize() int openmc_reset() { - CellCountStorage::instance()->clear(); + UniverseCellCounter::instance()->clear(); LevelCountStorage::instance()->clear(); for (auto& t : model::tallies) { diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index 662b6790d..42fb33284 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -60,18 +60,18 @@ void read_geometry_xml() model::root_universe = find_root_universe(); } -void CellCountStorage::clear() { +void UniverseCellCounter::clear() { if (instance_ != nullptr) { delete instance_; instance_ = nullptr; } } -void CellCountStorage::set_cell_count_for_univ(int32_t univ, int32_t cell, int count) { +void UniverseCellCounter::set_cell_count_for_univ(int32_t univ, int32_t cell, int count) { counts[univ][cell] = count; } - void CellCountStorage::increment_count_for_univ(int32_t univ, int32_t cell) { + void UniverseCellCounter::increment_count_for_univ(int32_t univ, int32_t cell) { if (has_count(univ,cell)) { counts[univ][cell] += 1; } else { @@ -79,15 +79,15 @@ void CellCountStorage::set_cell_count_for_univ(int32_t univ, int32_t cell, int c } } -bool CellCountStorage::has_count(int32_t univ, int32_t cell) { +bool UniverseCellCounter::has_count(int32_t univ, int32_t cell) { return counts.count(univ) && counts[univ].count(cell); } -bool CellCountStorage::has_count(int32_t univ) { +bool UniverseCellCounter::has_count(int32_t univ) { return counts.count(univ); } -void CellCountStorage::absorb_b_into_a(int32_t a, int32_t b) { +void UniverseCellCounter::absorb_b_into_a(int32_t a, int32_t b) { std::map b_map = counts[b]; for (auto it : b_map) { @@ -99,7 +99,7 @@ void CellCountStorage::absorb_b_into_a(int32_t a, int32_t b) { } } -auto CellCountStorage::get_count(int32_t univ) { +auto UniverseCellCounter::get_count(int32_t univ) { return counts[univ]; } @@ -142,7 +142,7 @@ int LevelCountStorage::get_count(int32_t univ) { return counts[univ]; } -CellCountStorage* CellCountStorage::instance_ = nullptr; +UniverseCellCounter* UniverseCellCounter::instance_ = nullptr; LevelCountStorage* LevelCountStorage::instance_ = nullptr; //============================================================================== @@ -480,7 +480,7 @@ prepare_distribcell() void count_cell_instances(int32_t univ_indx) { - CellCountStorage* counter = CellCountStorage::instance(); + UniverseCellCounter* counter = UniverseCellCounter::instance(); if (counter->has_count(univ_indx)) { std::map univ_counts = counter->get_count(univ_indx); From c20e942f1ccb7deb6edd6c63d31d3575f735b484 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 6 Nov 2019 09:36:41 -0600 Subject: [PATCH 3/8] Update name of universe level counter. --- include/openmc/geometry_aux.h | 14 +++++++------- src/finalize.cpp | 2 +- src/geometry_aux.cpp | 18 +++++++++--------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/include/openmc/geometry_aux.h b/include/openmc/geometry_aux.h index 14e119282..0e694e982 100644 --- a/include/openmc/geometry_aux.h +++ b/include/openmc/geometry_aux.h @@ -46,21 +46,21 @@ namespace openmc { auto get_count(int32_t univ); }; - struct LevelCountStorage { + struct UniverseLevelCounter { private: - LevelCountStorage() {} - LevelCountStorage(LevelCountStorage& c) {} - LevelCountStorage(const LevelCountStorage& c) {} + UniverseLevelCounter() {} + UniverseLevelCounter(UniverseLevelCounter& c) {} + UniverseLevelCounter(const UniverseLevelCounter& c) {} - static LevelCountStorage* instance_; + static UniverseLevelCounter* instance_; std::map counts; public: - static LevelCountStorage* instance() { + static UniverseLevelCounter* instance() { if (instance_ == nullptr) - instance_ = new LevelCountStorage; + instance_ = new UniverseLevelCounter; return instance_; } diff --git a/src/finalize.cpp b/src/finalize.cpp index 3b8530b21..534382382 100644 --- a/src/finalize.cpp +++ b/src/finalize.cpp @@ -137,7 +137,7 @@ int openmc_reset() { UniverseCellCounter::instance()->clear(); - LevelCountStorage::instance()->clear(); + UniverseLevelCounter::instance()->clear(); for (auto& t : model::tallies) { t->reset(); diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index 42fb33284..3a98d02b6 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -103,18 +103,18 @@ auto UniverseCellCounter::get_count(int32_t univ) { return counts[univ]; } -void LevelCountStorage::clear() { +void UniverseLevelCounter::clear() { if (instance_ != nullptr) { delete instance_; instance_ = nullptr; } } -void LevelCountStorage::set_cell_count_for_univ(int32_t univ, int count) { +void UniverseLevelCounter::set_cell_count_for_univ(int32_t univ, int count) { counts[univ] = count; } -void LevelCountStorage::increment_count_for_univ(int32_t univ) { +void UniverseLevelCounter::increment_count_for_univ(int32_t univ) { if (has_count(univ)) { counts[univ] += 1; } else { @@ -122,11 +122,11 @@ void LevelCountStorage::increment_count_for_univ(int32_t univ) { } } -bool LevelCountStorage::has_count(int32_t univ) { +bool UniverseLevelCounter::has_count(int32_t univ) { return counts.count(univ); } -void LevelCountStorage::absorb_b_into_a(int32_t a, int32_t b) { +void UniverseLevelCounter::absorb_b_into_a(int32_t a, int32_t b) { if (has_count(a)) { counts[a] += counts[b]; } else { @@ -134,16 +134,16 @@ void LevelCountStorage::absorb_b_into_a(int32_t a, int32_t b) { } } -void LevelCountStorage::set_count(int32_t univ, int count) { +void UniverseLevelCounter::set_count(int32_t univ, int count) { counts[univ] = count; } -int LevelCountStorage::get_count(int32_t univ) { +int UniverseLevelCounter::get_count(int32_t univ) { return counts[univ]; } UniverseCellCounter* UniverseCellCounter::instance_ = nullptr; -LevelCountStorage* LevelCountStorage::instance_ = nullptr; +UniverseLevelCounter* UniverseLevelCounter::instance_ = nullptr; //============================================================================== @@ -626,7 +626,7 @@ distribcell_path(int32_t target_cell, int32_t map, int32_t target_offset) int maximum_levels(int32_t univ) { - LevelCountStorage* counter = LevelCountStorage::instance(); + UniverseLevelCounter* counter = UniverseLevelCounter::instance(); if (counter->has_count(univ)) { return counter->get_count(univ); From d37c416308bb6ec13e5857ef8d3eb28ce47a1e9f Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 6 Nov 2019 15:28:06 -0600 Subject: [PATCH 4/8] Updating attribute name and placing below methods. --- include/openmc/geometry_aux.h | 15 +-- src/geometry_aux.cpp | 170 +++++++++++++++++----------------- 2 files changed, 94 insertions(+), 91 deletions(-) diff --git a/include/openmc/geometry_aux.h b/include/openmc/geometry_aux.h index 0e694e982..bf09aacec 100644 --- a/include/openmc/geometry_aux.h +++ b/include/openmc/geometry_aux.h @@ -18,10 +18,9 @@ namespace openmc { static UniverseCellCounter* instance_; - std::map> counts; - public: + // Methods static UniverseCellCounter* instance() { if (instance_ == nullptr) instance_ = new UniverseCellCounter; @@ -29,8 +28,6 @@ namespace openmc { return instance_; } - - void clear(); void set_cell_count_for_univ(int32_t univ, int32_t cell, int count); @@ -44,6 +41,9 @@ namespace openmc { void absorb_b_into_a(int32_t a, int32_t b); auto get_count(int32_t univ); + + // Members + std::map> counts_; }; struct UniverseLevelCounter { @@ -54,10 +54,10 @@ namespace openmc { static UniverseLevelCounter* instance_; - std::map counts; - public: + //Methods + static UniverseLevelCounter* instance() { if (instance_ == nullptr) instance_ = new UniverseLevelCounter; @@ -78,6 +78,9 @@ namespace openmc { void set_count(int32_t univ, int count); int get_count(int32_t univ); + + // Members + std::map counts_; }; void read_geometry_xml(); diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index 3a98d02b6..436528927 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -23,6 +23,91 @@ namespace openmc { +void UniverseCellCounter::clear() { + if (instance_ != nullptr) { + delete instance_; + instance_ = nullptr; + } +} + +void UniverseCellCounter::set_cell_count_for_univ(int32_t univ, int32_t cell, int count) { + counts_[univ][cell] = count; +} + + void UniverseCellCounter::increment_count_for_univ(int32_t univ, int32_t cell) { + if (has_count(univ,cell)) { + counts_[univ][cell] += 1; + } else { + counts_[univ][cell] = 1; + } + } + +bool UniverseCellCounter::has_count(int32_t univ, int32_t cell) { + return counts_.count(univ) && counts_[univ].count(cell); +} + +bool UniverseCellCounter::has_count(int32_t univ) { + return counts_.count(univ); +} + +void UniverseCellCounter::absorb_b_into_a(int32_t a, int32_t b) { + std::map b_map = counts_[b]; + + for (auto it : b_map) { + if (has_count(a, it.first)) { + counts_[a][it.first] += it.second; + } else { + counts_[a][it.first] = it.second; + } + } +} + +auto UniverseCellCounter::get_count(int32_t univ) { + return counts_[univ]; +} + +void UniverseLevelCounter::clear() { + if (instance_ != nullptr) { + delete instance_; + instance_ = nullptr; + } +} + +void UniverseLevelCounter::set_cell_count_for_univ(int32_t univ, int count) { + counts_[univ] = count; +} + +void UniverseLevelCounter::increment_count_for_univ(int32_t univ) { + if (has_count(univ)) { + counts_[univ] += 1; + } else { + counts_[univ] = 1; + } +} + +bool UniverseLevelCounter::has_count(int32_t univ) { + return counts_.count(univ); +} + +void UniverseLevelCounter::absorb_b_into_a(int32_t a, int32_t b) { + if (has_count(a)) { + counts_[a] += counts_[b]; + } else { + counts_[a] = counts_[b]; + } +} + +void UniverseLevelCounter::set_count(int32_t univ, int count) { + counts_[univ] = count; +} + +int UniverseLevelCounter::get_count(int32_t univ) { + return counts_[univ]; +} + +UniverseCellCounter* UniverseCellCounter::instance_ = nullptr; +UniverseLevelCounter* UniverseLevelCounter::instance_ = nullptr; + void read_geometry_xml() { #ifdef DAGMC @@ -60,91 +145,6 @@ void read_geometry_xml() model::root_universe = find_root_universe(); } -void UniverseCellCounter::clear() { - if (instance_ != nullptr) { - delete instance_; - instance_ = nullptr; - } -} - -void UniverseCellCounter::set_cell_count_for_univ(int32_t univ, int32_t cell, int count) { - counts[univ][cell] = count; -} - - void UniverseCellCounter::increment_count_for_univ(int32_t univ, int32_t cell) { - if (has_count(univ,cell)) { - counts[univ][cell] += 1; - } else { - counts[univ][cell] = 1; - } - } - -bool UniverseCellCounter::has_count(int32_t univ, int32_t cell) { - return counts.count(univ) && counts[univ].count(cell); -} - -bool UniverseCellCounter::has_count(int32_t univ) { - return counts.count(univ); -} - -void UniverseCellCounter::absorb_b_into_a(int32_t a, int32_t b) { - std::map b_map = counts[b]; - - for (auto it : b_map) { - if (has_count(a, it.first)) { - counts[a][it.first] += it.second; - } else { - counts[a][it.first] = it.second; - } - } -} - -auto UniverseCellCounter::get_count(int32_t univ) { - return counts[univ]; -} - -void UniverseLevelCounter::clear() { - if (instance_ != nullptr) { - delete instance_; - instance_ = nullptr; - } -} - -void UniverseLevelCounter::set_cell_count_for_univ(int32_t univ, int count) { - counts[univ] = count; -} - -void UniverseLevelCounter::increment_count_for_univ(int32_t univ) { - if (has_count(univ)) { - counts[univ] += 1; - } else { - counts[univ] = 1; - } -} - -bool UniverseLevelCounter::has_count(int32_t univ) { - return counts.count(univ); -} - -void UniverseLevelCounter::absorb_b_into_a(int32_t a, int32_t b) { - if (has_count(a)) { - counts[a] += counts[b]; - } else { - counts[a] = counts[b]; - } -} - -void UniverseLevelCounter::set_count(int32_t univ, int count) { - counts[univ] = count; -} - -int UniverseLevelCounter::get_count(int32_t univ) { - return counts[univ]; -} - -UniverseCellCounter* UniverseCellCounter::instance_ = nullptr; -UniverseLevelCounter* UniverseLevelCounter::instance_ = nullptr; - //============================================================================== void From 053434b99daf246f954155d9568a00eaf5764e8d Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 6 Nov 2019 15:32:00 -0600 Subject: [PATCH 5/8] Simplifying clears and absorption methods. --- src/geometry_aux.cpp | 37 ++++++------------------------------- 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index 436528927..3ee0648f9 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -24,23 +24,16 @@ namespace openmc { void UniverseCellCounter::clear() { - if (instance_ != nullptr) { - delete instance_; - instance_ = nullptr; - } + instance_->counts_.clear(); } void UniverseCellCounter::set_cell_count_for_univ(int32_t univ, int32_t cell, int count) { counts_[univ][cell] = count; } - void UniverseCellCounter::increment_count_for_univ(int32_t univ, int32_t cell) { - if (has_count(univ,cell)) { - counts_[univ][cell] += 1; - } else { - counts_[univ][cell] = 1; - } - } +void UniverseCellCounter::increment_count_for_univ(int32_t univ, int32_t cell) { + counts_[univ][cell] += 1; +} bool UniverseCellCounter::has_count(int32_t univ, int32_t cell) { return counts_.count(univ) && counts_[univ].count(cell); @@ -52,14 +45,7 @@ bool UniverseCellCounter::has_count(int32_t univ) { void UniverseCellCounter::absorb_b_into_a(int32_t a, int32_t b) { std::map b_map = counts_[b]; - - for (auto it : b_map) { - if (has_count(a, it.first)) { - counts_[a][it.first] += it.second; - } else { - counts_[a][it.first] = it.second; - } - } + for (auto it : b_map) { counts_[a][it.first] += it.second; } } auto UniverseCellCounter::get_count(int32_t univ) { @@ -67,10 +53,7 @@ auto UniverseCellCounter::get_count(int32_t univ) { } void UniverseLevelCounter::clear() { - if (instance_ != nullptr) { - delete instance_; - instance_ = nullptr; - } + instance_->counts_.clear(); } void UniverseLevelCounter::set_cell_count_for_univ(int32_t univ, int count) { @@ -78,11 +61,7 @@ void UniverseLevelCounter::set_cell_count_for_univ(int32_t univ, int count) { } void UniverseLevelCounter::increment_count_for_univ(int32_t univ) { - if (has_count(univ)) { counts_[univ] += 1; - } else { - counts_[univ] = 1; - } } bool UniverseLevelCounter::has_count(int32_t univ) { @@ -90,11 +69,7 @@ bool UniverseLevelCounter::has_count(int32_t univ) { } void UniverseLevelCounter::absorb_b_into_a(int32_t a, int32_t b) { - if (has_count(a)) { counts_[a] += counts_[b]; - } else { - counts_[a] = counts_[b]; - } } void UniverseLevelCounter::set_count(int32_t univ, int count) { From a685e4768c3e36f89d6c0967729962b2c9e2ea8f Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 6 Nov 2019 16:03:51 -0600 Subject: [PATCH 6/8] Removing class defs in favor of STL types. --- include/openmc/geometry_aux.h | 72 +--------------------------- src/finalize.cpp | 4 +- src/geometry_aux.cpp | 88 +++++++++-------------------------- 3 files changed, 26 insertions(+), 138 deletions(-) diff --git a/include/openmc/geometry_aux.h b/include/openmc/geometry_aux.h index bf09aacec..04c377400 100644 --- a/include/openmc/geometry_aux.h +++ b/include/openmc/geometry_aux.h @@ -10,78 +10,10 @@ #include namespace openmc { - struct UniverseCellCounter { - private: - UniverseCellCounter() {} - UniverseCellCounter(UniverseCellCounter& c) {} - UniverseCellCounter(const UniverseCellCounter& c) {} - static UniverseCellCounter* instance_; + extern std::map> universe_cell_counts; + extern std::map universe_level_counts; - public: - - // Methods - static UniverseCellCounter* instance() { - if (instance_ == nullptr) - instance_ = new UniverseCellCounter; - - return instance_; - } - - void clear(); - - void set_cell_count_for_univ(int32_t univ, int32_t cell, int count); - - void increment_count_for_univ(int32_t univ, int32_t cell); - - bool has_count(int32_t univ, int32_t cell); - - bool has_count(int32_t univ); - - void absorb_b_into_a(int32_t a, int32_t b); - - auto get_count(int32_t univ); - - // Members - std::map> counts_; - }; - - struct UniverseLevelCounter { - private: - UniverseLevelCounter() {} - UniverseLevelCounter(UniverseLevelCounter& c) {} - UniverseLevelCounter(const UniverseLevelCounter& c) {} - - static UniverseLevelCounter* instance_; - - public: - - //Methods - - static UniverseLevelCounter* instance() { - if (instance_ == nullptr) - instance_ = new UniverseLevelCounter; - - return instance_; - } - - void clear(); - - void set_cell_count_for_univ(int32_t univ, int count); - - void increment_count_for_univ(int32_t univ); - - bool has_count(int32_t univ); - - void absorb_b_into_a(int32_t a, int32_t b); - - void set_count(int32_t univ, int count); - - int get_count(int32_t univ); - - // Members - std::map counts_; - }; void read_geometry_xml(); diff --git a/src/finalize.cpp b/src/finalize.cpp index 534382382..6012dfd0b 100644 --- a/src/finalize.cpp +++ b/src/finalize.cpp @@ -136,8 +136,8 @@ int openmc_finalize() int openmc_reset() { - UniverseCellCounter::instance()->clear(); - UniverseLevelCounter::instance()->clear(); + universe_cell_counts.clear(); + universe_level_counts.clear(); for (auto& t : model::tallies) { t->reset(); diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index 3ee0648f9..0d95ccbcc 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -23,66 +23,24 @@ namespace openmc { -void UniverseCellCounter::clear() { - instance_->counts_.clear(); +std::map> universe_cell_counts; +std::map universe_level_counts; + + +void update_universe_cell_count(int32_t a, int32_t b) { + auto& universe_a_counts = universe_cell_counts[a]; + const auto& universe_b_counts = universe_cell_counts[b]; + for (auto it : universe_b_counts) { + universe_a_counts[it.first] += it.second; + } } -void UniverseCellCounter::set_cell_count_for_univ(int32_t univ, int32_t cell, int count) { - counts_[univ][cell] = count; +void update_universe_level_count(int32_t a, int32_t b) { + auto& universe_a_count = universe_level_counts[a]; + const auto& universe_b_count = universe_level_counts[b]; + universe_a_count += universe_b_count; } -void UniverseCellCounter::increment_count_for_univ(int32_t univ, int32_t cell) { - counts_[univ][cell] += 1; -} - -bool UniverseCellCounter::has_count(int32_t univ, int32_t cell) { - return counts_.count(univ) && counts_[univ].count(cell); -} - -bool UniverseCellCounter::has_count(int32_t univ) { - return counts_.count(univ); -} - -void UniverseCellCounter::absorb_b_into_a(int32_t a, int32_t b) { - std::map b_map = counts_[b]; - for (auto it : b_map) { counts_[a][it.first] += it.second; } -} - -auto UniverseCellCounter::get_count(int32_t univ) { - return counts_[univ]; -} - -void UniverseLevelCounter::clear() { - instance_->counts_.clear(); -} - -void UniverseLevelCounter::set_cell_count_for_univ(int32_t univ, int count) { - counts_[univ] = count; -} - -void UniverseLevelCounter::increment_count_for_univ(int32_t univ) { - counts_[univ] += 1; -} - -bool UniverseLevelCounter::has_count(int32_t univ) { - return counts_.count(univ); -} - -void UniverseLevelCounter::absorb_b_into_a(int32_t a, int32_t b) { - counts_[a] += counts_[b]; -} - -void UniverseLevelCounter::set_count(int32_t univ, int count) { - counts_[univ] = count; -} - -int UniverseLevelCounter::get_count(int32_t univ) { - return counts_[univ]; -} - -UniverseCellCounter* UniverseCellCounter::instance_ = nullptr; -UniverseLevelCounter* UniverseLevelCounter::instance_ = nullptr; - void read_geometry_xml() { #ifdef DAGMC @@ -455,10 +413,9 @@ prepare_distribcell() void count_cell_instances(int32_t univ_indx) { - UniverseCellCounter* counter = UniverseCellCounter::instance(); - if (counter->has_count(univ_indx)) { - std::map univ_counts = counter->get_count(univ_indx); + if (universe_cell_counts.count(univ_indx)) { + std::map univ_counts = universe_cell_counts[univ_indx]; for(auto it : univ_counts) { Cell& c = *model::cells[it.first]; c.n_instances_ += it.second; @@ -467,18 +424,18 @@ count_cell_instances(int32_t univ_indx) for (int32_t cell_indx : model::universes[univ_indx]->cells_) { Cell& c = *model::cells[cell_indx]; ++c.n_instances_; - counter->increment_count_for_univ(univ_indx, cell_indx); + universe_cell_counts[univ_indx][cell_indx] += 1; if (c.type_ == FILL_UNIVERSE) { // This cell contains another universe. Recurse into that universe. count_cell_instances(c.fill_); - counter->absorb_b_into_a(univ_indx, c.fill_); + update_universe_cell_count(univ_indx, c.fill_); } else if (c.type_ == FILL_LATTICE) { // This cell contains a lattice. Recurse into the lattice universes. Lattice& lat = *model::lattices[c.fill_]; for (auto it = lat.begin(); it != lat.end(); ++it) { count_cell_instances(*it); - counter->absorb_b_into_a(univ_indx, *it); + update_universe_cell_count(univ_indx, *it); } } } @@ -601,10 +558,9 @@ distribcell_path(int32_t target_cell, int32_t map, int32_t target_offset) int maximum_levels(int32_t univ) { - UniverseLevelCounter* counter = UniverseLevelCounter::instance(); - if (counter->has_count(univ)) { - return counter->get_count(univ); + if (universe_level_counts.count(univ)) { + return universe_level_counts[univ]; } int levels_below {0}; @@ -624,7 +580,7 @@ maximum_levels(int32_t univ) } ++levels_below; - counter->set_count(univ, levels_below); + universe_level_counts[univ] = levels_below; return levels_below; } From 031047ee80a8952d7e5bad8e7ee0db3c122c700e Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 6 Nov 2019 16:07:28 -0600 Subject: [PATCH 7/8] Moving counters into the model namespace. --- include/openmc/geometry_aux.h | 3 ++- src/finalize.cpp | 4 ++-- src/geometry_aux.cpp | 27 ++++++++++++++------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/include/openmc/geometry_aux.h b/include/openmc/geometry_aux.h index 04c377400..677dcf180 100644 --- a/include/openmc/geometry_aux.h +++ b/include/openmc/geometry_aux.h @@ -11,9 +11,10 @@ namespace openmc { +namespace model { extern std::map> universe_cell_counts; extern std::map universe_level_counts; - +} // namespace model void read_geometry_xml(); diff --git a/src/finalize.cpp b/src/finalize.cpp index 6012dfd0b..16ff0fbcc 100644 --- a/src/finalize.cpp +++ b/src/finalize.cpp @@ -136,8 +136,8 @@ int openmc_finalize() int openmc_reset() { - universe_cell_counts.clear(); - universe_level_counts.clear(); + model::universe_cell_counts.clear(); + model::universe_level_counts.clear(); for (auto& t : model::tallies) { t->reset(); diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index 0d95ccbcc..229b46b8c 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -23,21 +23,22 @@ namespace openmc { -std::map> universe_cell_counts; -std::map universe_level_counts; - +namespace model { + std::map> universe_cell_counts; + std::map universe_level_counts; +} // namespace model void update_universe_cell_count(int32_t a, int32_t b) { - auto& universe_a_counts = universe_cell_counts[a]; - const auto& universe_b_counts = universe_cell_counts[b]; + auto& universe_a_counts = model::universe_cell_counts[a]; + const auto& universe_b_counts = model::universe_cell_counts[b]; for (auto it : universe_b_counts) { universe_a_counts[it.first] += it.second; } } void update_universe_level_count(int32_t a, int32_t b) { - auto& universe_a_count = universe_level_counts[a]; - const auto& universe_b_count = universe_level_counts[b]; + auto& universe_a_count = model::universe_level_counts[a]; + const auto& universe_b_count = model::universe_level_counts[b]; universe_a_count += universe_b_count; } @@ -414,8 +415,8 @@ void count_cell_instances(int32_t univ_indx) { - if (universe_cell_counts.count(univ_indx)) { - std::map univ_counts = universe_cell_counts[univ_indx]; + if (model::universe_cell_counts.count(univ_indx)) { + std::map univ_counts = model::universe_cell_counts[univ_indx]; for(auto it : univ_counts) { Cell& c = *model::cells[it.first]; c.n_instances_ += it.second; @@ -424,7 +425,7 @@ count_cell_instances(int32_t univ_indx) for (int32_t cell_indx : model::universes[univ_indx]->cells_) { Cell& c = *model::cells[cell_indx]; ++c.n_instances_; - universe_cell_counts[univ_indx][cell_indx] += 1; + model::universe_cell_counts[univ_indx][cell_indx] += 1; if (c.type_ == FILL_UNIVERSE) { // This cell contains another universe. Recurse into that universe. @@ -559,8 +560,8 @@ int maximum_levels(int32_t univ) { - if (universe_level_counts.count(univ)) { - return universe_level_counts[univ]; + if (model::universe_level_counts.count(univ)) { + return model::universe_level_counts[univ]; } int levels_below {0}; @@ -580,7 +581,7 @@ maximum_levels(int32_t univ) } ++levels_below; - universe_level_counts[univ] = levels_below; + model::universe_level_counts[univ] = levels_below; return levels_below; } From 4394582657f5d95dd9112b44bb053c60528c4351 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Fri, 8 Nov 2019 14:43:35 -0600 Subject: [PATCH 8/8] Addressing PR comments from @paulromano. --- include/openmc/geometry_aux.h | 6 +++--- src/geometry_aux.cpp | 28 ++++++++++++---------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/include/openmc/geometry_aux.h b/include/openmc/geometry_aux.h index 677dcf180..800baef26 100644 --- a/include/openmc/geometry_aux.h +++ b/include/openmc/geometry_aux.h @@ -7,13 +7,13 @@ #include #include #include -#include +#include namespace openmc { namespace model { - extern std::map> universe_cell_counts; - extern std::map universe_level_counts; + extern std::unordered_map> universe_cell_counts; + extern std::unordered_map universe_level_counts; } // namespace model void read_geometry_xml(); diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index 229b46b8c..5ebee4aca 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -24,24 +24,20 @@ namespace openmc { namespace model { - std::map> universe_cell_counts; - std::map universe_level_counts; + std::unordered_map> universe_cell_counts; + std::unordered_map universe_level_counts; } // namespace model + +// adds the cell counts of universe b to universe a void update_universe_cell_count(int32_t a, int32_t b) { auto& universe_a_counts = model::universe_cell_counts[a]; const auto& universe_b_counts = model::universe_cell_counts[b]; - for (auto it : universe_b_counts) { + for (const auto& it : universe_b_counts) { universe_a_counts[it.first] += it.second; } } -void update_universe_level_count(int32_t a, int32_t b) { - auto& universe_a_count = model::universe_level_counts[a]; - const auto& universe_b_count = model::universe_level_counts[b]; - universe_a_count += universe_b_count; -} - void read_geometry_xml() { #ifdef DAGMC @@ -415,11 +411,10 @@ void count_cell_instances(int32_t univ_indx) { - if (model::universe_cell_counts.count(univ_indx)) { - std::map univ_counts = model::universe_cell_counts[univ_indx]; - for(auto it : univ_counts) { - Cell& c = *model::cells[it.first]; - c.n_instances_ += it.second; + const auto univ_counts = model::universe_cell_counts.find(univ_indx); + if (univ_counts != model::universe_cell_counts.end()) { + for (const auto& it : univ_counts->second) { + model::cells[it.first]->n_instances_ += it.second; } } else { for (int32_t cell_indx : model::universes[univ_indx]->cells_) { @@ -560,8 +555,9 @@ int maximum_levels(int32_t univ) { - if (model::universe_level_counts.count(univ)) { - return model::universe_level_counts[univ]; + const auto level_count = model::universe_level_counts.find(univ); + if (level_count != model::universe_level_counts.end()) { + return level_count->second; } int levels_below {0};