From 6423cb9742d2f7372f7470b82fb9289e0fcca9a4 Mon Sep 17 00:00:00 2001 From: Gavin Ridley Date: Thu, 27 Apr 2023 15:00:31 -0400 Subject: [PATCH] reproducibility: sort surf source bank before write --- src/simulation.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/simulation.cpp b/src/simulation.cpp index d46787c2822..149e9540f79 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -428,6 +428,18 @@ void finalize_batch() auto filename = settings::path_output + "surface_source"; auto surf_work_index = mpi::calculate_parallel_index_vector(simulation::surf_source_bank.size()); + + // Sort, because the file ordering will differ based on the order that + // threads finish work in. We want to produce the same output every time + // given the same random seed. We probably don't need to sort globally over + // MPI ranks to ensure reproducible output. + std::sort(simulation::surf_source_bank.begin(), + simulation::surf_source_bank.end(), + [](const SourceSite& l, const SourceSite& r) { + return std::tie(l.parent_id, l.progeny_id) < + std::tie(r.parent_id, r.progeny_id); + }); + gsl::span surfbankspan(simulation::surf_source_bank.begin(), simulation::surf_source_bank.size()); if (settings::surf_mcpl_write) {