Code for download: session7_start.tar.gz
Exercise:
- Migrate the exampleED application to MT. After each step rebuild the program. You can also try to run and observe the behavior (breaks) after each step and understand how the next step fixes the observed break. Steps:
- Update main: add
G4MTRunManager.
- Update
ActionInitialization
class: addBuildForMaster()
function. - Update Hit classes: declare
G4Allocator
thread local.
After these steps the migration is complete. Rebuild and rerun the program.
- Update main: add
- Change the default number of threads: first in the code and then via the environment variable. Observe which setting has the preference.
- Limit the output to one thread only (via a command in a macro).
- Increase the number of events (~300) and observe the Root output. Inspect the ntuple files generated per threads with TChain, for example:
TChain chain("Chamber1");
chain.Add("ED_t0.root");
chain.Add("ED_t1.root");
chain.Add("ED_t2.root");
chain.Add("ED_t3.root");
// Inspect tree branched (ntuple colums) from all files
chain.Draw("Zpos");
// Merge the files in one
chain.Merge("ED_Chamber1.root"); - Get an experience with data race. Add a global variable defined in a file scope in
EDChamberSD.cc
just after the headers in commented lines:
G4double* myGlobalValue = new G4double(1.);
and inEDChamberSD::ProcessHits()
:
// simulate data race
if ( (*myGlobalValue) > 0.) {
delete myGlobalValue;
myGlobalValue = new G4double(-1);
}
else {
delete myGlobalValue;
myGlobalValue = new G4double(1);
}
Does the program break now?
Congratulation, the race condition was added successfully.
Use G4AutoLock to fix this thread-unsafe code.
Solution: session7_solution.tar.gz
Exercise ++:
The exercises marked as ++ are optional; they are recommended for participants who have already some experience with Geant4 and get some time left for practicing more than the basic exercise proposed above.
- There is no complementary exercise for this session.