From 2ddda76366689031cb4509645c52f43167f8ca82 Mon Sep 17 00:00:00 2001 From: John Helly Date: Wed, 14 Feb 2024 14:39:37 +0000 Subject: [PATCH 1/4] Seed RNG for each halo using first particle ID --- src/subhalo_unbind.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/subhalo_unbind.cpp b/src/subhalo_unbind.cpp index 58c598f9..40805294 100644 --- a/src/subhalo_unbind.cpp +++ b/src/subhalo_unbind.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "datatypes.h" #include "gravity_tree.h" @@ -323,8 +324,13 @@ void Subhalo_t::Unbind(const Snapshot_t &epoch) GravityTree_t tree; tree.Reserve(Particles.size()); Nbound = Particles.size(); // start from full set - if (MaxSampleSize > 0 && Nbound > MaxSampleSize) - random_shuffle(Particles.begin(), Particles.end()); // shuffle for easy resampling later. + if (MaxSampleSize > 0 && Nbound > MaxSampleSize) { + // Initialize random number generator with first particle ID for reproducibility + mt19937 rng; + rng.seed(Particles[0].Id); + // shuffle for easy resampling later. + shuffle(Particles.begin(), Particles.end(), rng); + } HBTInt Nlast; vector Elist(Nbound); From 6e7fd48b4810ea4655d115f553ce9488738745e5 Mon Sep 17 00:00:00 2001 From: John Helly Date: Wed, 14 Feb 2024 16:52:58 +0000 Subject: [PATCH 2/4] Use multiple particle IDs to seed the random number generator --- src/subhalo_unbind.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/subhalo_unbind.cpp b/src/subhalo_unbind.cpp index 40805294..3e836138 100644 --- a/src/subhalo_unbind.cpp +++ b/src/subhalo_unbind.cpp @@ -325,9 +325,13 @@ void Subhalo_t::Unbind(const Snapshot_t &epoch) tree.Reserve(Particles.size()); Nbound = Particles.size(); // start from full set if (MaxSampleSize > 0 && Nbound > MaxSampleSize) { - // Initialize random number generator with first particle ID for reproducibility - mt19937 rng; - rng.seed(Particles[0].Id); + // Initialize random number generator with first few particle IDs for reproducibility + int nr_seed_vals = min(Nbound, (HBTInt) 20); + vector ids(nr_seed_vals); + for(int i=0; i Date: Wed, 14 Feb 2024 17:02:39 +0000 Subject: [PATCH 3/4] Add parameter to modify random number seed (via xor with particle IDs) --- src/config_parser.cpp | 6 ++++-- src/config_parser.h | 6 ++++-- src/subhalo_unbind.cpp | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/config_parser.cpp b/src/config_parser.cpp index 0985d705..71429d0a 100644 --- a/src/config_parser.cpp +++ b/src/config_parser.cpp @@ -69,6 +69,7 @@ bool Parameter_t::TrySingleValueParameter(string ParameterName, stringstream &Pa TrySetPar(PeriodicBoundaryOn); TrySetPar(SnapshotHasIdBlock); TrySetPar(MaxPhysicalSofteningHalo); + TrySetPar(RandomSeedFactor); TrySetPar(TracerParticleBitMask); #undef TrySetPar @@ -263,9 +264,9 @@ void Parameter_t::BroadCast(MpiWorker_t &world, int root) _SyncAtom(MaxSnapshotIndex, MPI_INT); _SyncReal(BoxSize); _SyncReal(SofteningHalo); - _SyncReal(MaxPhysicalSofteningHalo); + _SyncReal(MaxPhysicalSofteningHalo); _SyncVecBool(IsSet); - + _SyncAtom(RandomSeedFactor, MPI_HBT_INT); _SyncVec(SnapshotDirBase, MPI_CHAR); _SyncVec(SnapshotFormat, MPI_CHAR); _SyncVec(GroupFileFormat, MPI_CHAR); @@ -412,6 +413,7 @@ void Parameter_t::DumpParameters() DumpPar(SourceSubRelaxFactor); DumpPar(RefineMostboundParticle); DumpPar(MaxSampleSizeOfPotentialEstimate); + DumpPar(RandomSeedFactor); DumpHeader("Subhalo Tracking"); DumpPar(MinNumPartOfSub); diff --git a/src/config_parser.h b/src/config_parser.h index 0d6f59ac..ff8f812a 100644 --- a/src/config_parser.h +++ b/src/config_parser.h @@ -75,7 +75,8 @@ class Parameter_t // leads to more accuracy mostbound particle int TracerParticleBitMask; /* Bitmask used to identify which particle type can be used as tracer */ - + HBTInt RandomSeedFactor; // Modifies the random number seed used for sampling + /*derived parameters; do not require user input*/ HBTReal TreeNodeOpenAngleSquare; HBTReal TreeNodeResolution; @@ -119,7 +120,8 @@ class Parameter_t RefineMostboundParticle = true; GroupLoadedFullParticle = false; MaxPhysicalSofteningHalo = -1; // Indicates no max. physical softening is used. - + RandomSeedFactor = 0; // If zero, just use particle IDs to seed random number generator + /* Tracer-related parameters. If unset, only use collisionless particles (DM * + Stars) as tracer. Here we assume they correspond to particle types 1 * and 4, respectively. */ diff --git a/src/subhalo_unbind.cpp b/src/subhalo_unbind.cpp index 3e836138..434ff691 100644 --- a/src/subhalo_unbind.cpp +++ b/src/subhalo_unbind.cpp @@ -329,7 +329,7 @@ void Subhalo_t::Unbind(const Snapshot_t &epoch) int nr_seed_vals = min(Nbound, (HBTInt) 20); vector ids(nr_seed_vals); for(int i=0; i Date: Mon, 29 Apr 2024 13:56:10 +0100 Subject: [PATCH 4/4] Force deterministic ordering of new halo creation --- src/subhalo_tracking.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/subhalo_tracking.cpp b/src/subhalo_tracking.cpp index 1fdcc291..e5e57bd4 100644 --- a/src/subhalo_tracking.cpp +++ b/src/subhalo_tracking.cpp @@ -735,7 +735,7 @@ void SubhaloSnapshot_t::FeedCentrals(HaloSnapshot_t &halo_snap) // Subhalos.reserve(Snapshot->size()*0.1);//reserve enough branches....... Subhalos.resize(Npro + MemberTable.NBirth); } -#pragma omp for +#pragma omp for ordered for (HBTInt hostid = 0; hostid < halo_snap.Halos.size(); hostid++) { MemberShipTable_t::MemberList_t &Members = MemberTable.SubGroups[hostid]; @@ -743,7 +743,7 @@ void SubhaloSnapshot_t::FeedCentrals(HaloSnapshot_t &halo_snap) if (0 == Members.size()) // create a new sub { HBTInt subid; -#pragma omp critical(AddNewSub) // maybe consider ordered for here.. +#pragma omp ordered { subid = Npro++; }