From 1c497e1759b1692c9327dc89e5be0b075c2ab39a Mon Sep 17 00:00:00 2001 From: John Tramm Date: Fri, 17 Jan 2020 18:08:14 +0000 Subject: [PATCH] Reduced memory usage of the fission bank sort in the average case by adding the ability to use the empty portions of the fission bank allocation for storing the sorted fission bank. The sort only does this if there is enough space, otherwise, it will allocate extra space as was done before this commit. --- src/bank.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/bank.cpp b/src/bank.cpp index 8d0ffe49df..6bf43b5d89 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -79,9 +79,20 @@ void sort_fission_bank() // TODO: C++17 introduces the exclusive_scan() function which could be // used to replace everything above this point in this function. + + // We need a scratch vector to make permutation of the fission bank into + // sorted order easy. Under normal usage conditions, the fission bank is + // over provisioned, so we can use that as scratch space. + Particle::Bank* sorted_bank; + std::vector sorted_bank_holder; - // Create temporary scratch vector for permuting the fission bank - std::vector sorted_bank(simulation::fission_bank_length); + // If there is not enough space, allocate a temporary vector and point to it + if (simulation::fission_bank_length > simulation::fission_bank_max / 2) { + sorted_bank_holder = std::vector(simulation::fission_bank_length); + sorted_bank = sorted_bank_holder.data(); + } else { // otherwise, point sorted_bank to unused portion of the fission bank + sorted_bank = &simulation::fission_bank[simulation::fission_bank_length]; + } // Use parent and progeny indices to sort fission bank for (int64_t i = 0; i < simulation::fission_bank_length; i++) { @@ -92,9 +103,8 @@ void sort_fission_bank() } // Copy sorted bank into the fission bank - for (int64_t i = 0; i < simulation::fission_bank_length; i++) { - simulation::fission_bank[i] = sorted_bank[i]; - } + std::copy(sorted_bank, sorted_bank + simulation::fission_bank_length, + simulation::fission_bank.get()); } //==============================================================================