Warning, /geant4/examples/extended/parallel/ThreadsafeScorers/README is written in an unsupported language. File is not indexed.
0001
0002 -------------------------------------------------------------------
0003
0004 =========================================================
0005 Geant4 - an Object-Oriented Toolkit for Simulation in HEP
0006 =========================================================
0007
0008 Example ThreadsafeScorers
0009 ----------------------------
0010
0011 This example demonstrates a very simple application where an energy
0012 deposit and # of steps is accounted in thread-local (i.e. one instance per
0013 thread) hits maps with underlying types of plain-old data (POD) and global
0014 (i.e. one instance) hits maps with underlying types of atomics.
0015 The example uses a coarse mesh, extensive physics, and step limiters
0016 to ensure that there is a higher degree of conflict between threads
0017 when updating the scorers to test the robustness of the atomics
0018 classes and maximize the compounding of thread-local round-off error.
0019 At the end of the simulation, the scorers are printed to
0020 "mfd_<DATA_TYPE>_<SCORER_TYPE>.out", where DATA_TYPE is either
0021 "tl" (thread-local) or "tg" (thread-global) and SCORER_TYPE is "EnergyDeposit"
0022 or "NumberOfSteps". These values are then compared to a thread-global
0023 sum of these scorers that were updated via mutex locking. If round-off
0024 errors in thread-local EnergyDeposit are present, they can be viewed
0025 in "mfd_diff.out" at the end of the simulation
0026
0027 1- ATOMICS and the ATOMIC SCORERS
0028
0029 atomics can ONLY handle plain-old data (POD) types, e.g. int, double, etc.
0030 The implementation of atomics in compiler-dependent. At the very worst,
0031 the performance of an atomic is the same mutex locking.
0032 Atomics, in general, are not copy-constructable. This has to do with
0033 thread safety (e.g. making a copy while another thread tries to update)
0034 This is why atomics cannot be used in STL containers. The implementation
0035 in atomic.hh has limited copy-construction and still cannot be used in
0036 STL containers. Use these copy-constructors with extreme caution. See
0037 opening comments of G4atomic.hh for more details.
0038
0039 The newly provided classes in this example (G4atomic, G4TAtomicHitsMap, and
0040 G4TAtomicHitsCollection) are intended for applications where memory is a
0041 greater concern than performance. While atomics generally perform better than
0042 mutex locking, the synchronization is not without a cost. However, since
0043 the memory consumed by thread-local hits maps scales roughly linearly
0044 with the number of threads, simulations with a large number of scoring
0045 volumes can decrease simulation time by increasing the number of threads
0046 beyond what was previously allowed due to the increase in memory consumption.
0047
0048 The G4TAtomicHitsMap and G4TAtomicHitsCollection work exactly the same way
0049 as the standard G4THitsMap and G4THitsCollection, respectively, with the
0050 exception(s) that you should only implement one instance and provide a
0051 pointer/reference of that instance to the threads instead of having the
0052 threads create them. Additionally, there is no need to include them
0053 in the G4Run::Merge().
0054
0055 2- GEOMETRY DEFINITION
0056
0057 The geometry is constructed in the TSDetectorConstruction class.
0058 The setup consists of a box filling the world. The volume is divided into
0059 subregions, where the outermost boxes are a different material. The materials
0060 by default are water and boron as these have large scattering cross-sections
0061 for neutrons (the default particle).
0062
0063 3- PHYSICS LIST
0064
0065 The particle's type and the physic processes which will be available
0066 in this example are set are built from a variety of physics constructors.
0067 The chosen physics lists are extensive, primarily
0068 The constructors are:
0069
0070 G4EmStandardPhysics_option4
0071 G4DecayPhysics
0072 G4RadioactiveDecayPhysics
0073 G4HadronPhysicsQGSP_BERT_HP
0074 G4HadronElasticPhysicsHP
0075 G4StepLimiterPhysics
0076 G4IonElasticPhysics
0077 G4IonBinaryCascadePhysics
0078
0079 4- ACTION INITALIZATION
0080
0081 TSActionInitialization, instantiates and registers to Geant4 kernel
0082 all user action classes.
0083
0084 While in sequential mode the action classes are instatiated just once,
0085 via invoking the method:
0086 TSActionInitialization::Build()
0087 in multi-threading mode the same method is invoked for each thread worker
0088 and so all user action classes are defined thread-local.
0089
0090 A run action class is instantiated both thread-local
0091 and global that's why its instance is created also in the method
0092 TSActionInitialization::BuildForMaster()
0093 which is invoked only in multi-threading mode.
0094
0095 5- PRIMARY GENERATOR
0096
0097 The primary generator is defined in the TSPrimaryGeneratorAction class.
0098 The default kinematics is a 1 MeV neutron, randomly distributed in front
0099 of the target across 100% of the transverse (X,Y) target size.
0100 This default setting can be changed via the Geant4 built-in commands
0101 of the G4ParticleGun class.
0102
0103 6- DETECTOR RESPONSE
0104
0105 This example demonstrates a scoring implemented
0106 in the user action classes and TSRun object.
0107
0108 The energy deposited is collected per event in the PrimitiveScorer
0109 G4PSEnergyDeposit (as part of a MultiFunctionalDetector)
0110 and the thread-local version are merged at the end of the run.
0111
0112 The number of steps is collected per event in the PrimativeScorer
0113 G4PSNoOfSteps and the thread-local version are merged at the end of the run.
0114
0115 When the MFD is recording an event i.e. TSRun::RecordEvent(const G4Event*),
0116 the global atomic hits map adds the same hits collections
0117
0118 In multi-threading mode the energy accumulated in TSRun MFD object per
0119 workers is merged to the master in TSRun::Merge().
0120
0121 TSRun contains five hits collections types:
0122 1) a thread-local hits map,
0123 2) a global atomic hits map
0124 3) a global "mutex" hits map
0125 4) a global G4StatAnalysis hits deque
0126 5) a global G4ConvergenceTester hits deque
0127
0128 The thread-local hits map is the same as you will find in many other
0129 examples.
0130
0131 The atomics hits map is the purpose of this example. Code-wise, the
0132 implementation looks extremely similar to the thread-local version with
0133 3 primary exceptions:
0134 (1) construction - there should only be one instance so it should be a
0135 static member variable or a pointer/reference to a single instance
0136 (2) It does not need to, nor should be, summed in G4Run::Merge()
0137 (3) destruction -- it should only be cleared by the master thread since
0138 there is only one instance.
0139
0140 The "mutex" hits map is also included as reference for checking the results
0141 accumulated by the thread-local hits maps and atomic hits maps. The
0142 differences w.r.t. this hits maps are computed in
0143 TSRunAction::EndOfRunAction
0144
0145 The "G4StatAnalysis" and "G4ConvergenceTester" hits deques are
0146 memory-efficient version of the standard G4THitsMap. While maps are
0147 ideal for scoring at the G4Event-level, where sparsity w.r.t. indices
0148 is common; at the G4Run-level, these data structures require much
0149 less memory overhead. Due to a lack of
0150 G4ConvergenceTester::operator+=(G4ConvergenceTester), the static version
0151 of G4ConvergenceTester is the only valid way to use G4ConvergenceTester
0152 in a scoring container. This is not the case for G4StatAnalysis, which
0153 can be used in lieu of G4double.
0154
0155 7- HOW TO RUN
0156
0157 - Execute ts_scorers in the 'interactive mode' with visualization:
0158 % ./ts_scorers
0159 and type in the commands from run.mac line by line:
0160 Idle> /control/verbose 2
0161 Idle> /tracking/verbose 1
0162 Idle> /run/beamOn 10
0163 Idle> ...
0164 Idle> exit
0165 or
0166 Idle> /control/execute run.mac
0167 ....
0168 Idle> exit
0169
0170 - Execute ts_scorers in the 'batch' mode from macro files
0171 (without visualization)
0172 % ./ts_scorers run.mac
0173 % ./ts_scorers run.mac > run.out