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