I’m getting an unexpected behavior with my G4 (v10.7) app in multi-threaded mode, and I need some help in understanding the G4 threading model and where’s the issue with my implementation. The app source is available here: GitHub - gipert/remage: Simulation framework for germanium experiments
I wanted to have to possibility to support different generators, with the following design: the concrete class for G4VUserPrimaryGeneratorAction
[here] acts as a manager for generators, which in turn inherit from another virtual class. A pointer to the chosen generator is kept by the primary generator, which asks the sub-generator to generate a primary during the run [here]. The specific generator can be selected at run time with a dedicated macro command, defined [here]. The command invokes a method of the primary generator that instantiates a sub-generator, depending on the user selection [here].
Other parts of the source code relevant for the MT compatibility are [here] and [here].
In single-threaded mode, and with the example macro:
/run/initialize
/RMG/Generator/Select GPS
/gps/particle ion
/gps/ion 27 60
/gps/energy 0 eV
/run/beamOn 100000
the sub-generator is correctly configured and simulation runs smoothly:
$ G4RUN_MANAGER_TYPE=Serial build/02-hpge run.mac
[...]
[Debug ---> Primary generator set to kGPS
[...]
[Summary -> Starting run nr. 0. Current local time is 18-11-2021 17:40:31
But in multi-threaded mode the same sequence of commands gives me:
[...]
=======================================================================
--> G4MTRunManager::CreateAndStartWorkers() --> Initializing workers...
=======================================================================
***** COMMAND NOT FOUND </gps/particle ion> *****
***** Batch is interrupted!! *****
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : UIMAN0123
issued by : G4UImanager::ApplyCommand
Command aborted (100)
Error code : 100
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------
As far as I understand, the actual call to the function associated to the macro command is made later, at /run/beamOn
. This is why I cannot access the GPS commands, because a GPS instance has not been created yet.
Can anyone give me a hint of what’s wrong with my implementation? If needed I can easily provide a MWE.