Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to make random sampling for unbinding reproducible #16

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/config_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ bool Parameter_t::TrySingleValueParameter(string ParameterName, stringstream &Pa
TrySetPar(PeriodicBoundaryOn);
TrySetPar(SnapshotHasIdBlock);
TrySetPar(MaxPhysicalSofteningHalo);
TrySetPar(RandomSeedFactor);
TrySetPar(TracerParticleBitMask);

#undef TrySetPar
Expand Down Expand Up @@ -265,9 +266,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);
Expand Down Expand Up @@ -419,6 +420,7 @@ void Parameter_t::DumpParameters()
DumpPar(SourceSubRelaxFactor);
DumpPar(RefineMostboundParticle);
DumpPar(MaxSampleSizeOfPotentialEstimate);
DumpPar(RandomSeedFactor);

DumpHeader("Subhalo Tracking");
DumpPar(MinNumPartOfSub);
Expand Down
6 changes: 4 additions & 2 deletions src/config_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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;
Expand Down Expand Up @@ -124,7 +125,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. */
Expand Down
4 changes: 2 additions & 2 deletions src/subhalo_tracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,15 +735,15 @@ 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];
auto &Host = halo_snap.Halos[hostid];
if (0 == Members.size()) // create a new sub
{
HBTInt subid;
#pragma omp critical(AddNewSub) // maybe consider ordered for here..
#pragma omp ordered
{
subid = Npro++;
}
Expand Down
14 changes: 12 additions & 2 deletions src/subhalo_unbind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>
#include <new>
#include <omp.h>
#include <random>

#include "datatypes.h"
#include "gravity_tree.h"
Expand Down Expand Up @@ -314,8 +315,17 @@ 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 few particle IDs for reproducibility
int nr_seed_vals = min(Nbound, (HBTInt) 20);
vector<HBTInt> ids(nr_seed_vals);
for(int i=0; i<nr_seed_vals; i+=1)
ids[i] = Particles[i].Id ^ HBTConfig.RandomSeedFactor;
seed_seq seed(ids.begin(), ids.end());
mt19937 rng(seed);
// shuffle for easy resampling later.
shuffle(Particles.begin(), Particles.end(), rng);
}
HBTInt Nlast;

vector<ParticleEnergy_t> Elist(Nbound);
Expand Down