22.2. Running with Multiple Threads
Table of Contents
Setting the thread count
Geant4 supports multithreading through the class G4MTRunManager. When you use this run manager in your main() program, each worker thread runs its own copy of the user actions and transports its own set of events. You only need to choose how many threads to use for a given run. This is controlled either in C++ in your main program or from macro commands.
Geant4 uses one thread by default if you do not specify anything, so you must explicitly increase the thread count if you want parallel execution.
When using G4MTRunManager, always set the thread count before initializing the run manager or starting a run, and never change it while a run is in progress.
Setting the thread count in C++
The most direct way to control the number of threads is in main(), after you create the run manager and before you initialize the geometry and physics. A typical pattern looks like this:
#include "G4MTRunManager.hh"
#include "G4UImanager.hh"
#include "YourDetectorConstruction.hh"
#include "YourPhysicsList.hh"
#include "YourActionInitialization.hh"
int main(int argc, char** argv)
{
auto* runManager = new G4MTRunManager;
// Set number of threads here
runManager->SetNumberOfThreads(4);
runManager->SetUserInitialization(new YourDetectorConstruction);
runManager->SetUserInitialization(new YourPhysicsList);
runManager->SetUserInitialization(new YourActionInitialization);
runManager->Initialize();
// ... UI or macro handling and /run/beamOn ...
delete runManager;
return 0;
}
Call SetNumberOfThreads() only once, before Initialize() or before the first /run/initialize if you perform initialization from a macro.
If you want to choose the thread count from the command line, you can parse argc and argv yourself, or you can rely on the built in UI commands as described next.
Setting the thread count from macros
For interactive or batch runs it is often convenient to set the thread count from a macro file, without recompiling your code. Geant4 provides the command:
/run/numberOfThreads N
where N is an integer greater than or equal to 1. You must issue this command before /run/initialize and before /run/beamOn. A minimal macro could look like this:
#/control/verbose 2
#/run/verbose 2
/run/numberOfThreads 8
/run/initialize
/run/beamOn 10000
If you already called Initialize() in C++, the command will have no effect, because the worker threads have already been created.
Rule: Use /run/numberOfThreads only before /run/initialize. After initialization, changing the thread count with this command is not allowed and can lead to inconsistent behavior.
In an interactive session you can type the command directly in the Geant4 prompt:
/run/numberOfThreads 4
/run/initialize
Then you can start runs with /run/beamOn.
Choosing an appropriate thread count
A common first choice is to use as many threads as CPU cores. On a desktop with 8 logical cores, using 8 threads is usually reasonable. On shared systems such as clusters or workstations used by multiple users, you should follow site policies and may need to use fewer threads.
You can query the number of available cores outside Geant4 using your operating system tools or your own C++ code, then pass that number to SetNumberOfThreads(). For beginners, it is usually enough to try a small number such as 2 or 4 and check that the application runs correctly before increasing it further.
Remember that more threads can increase memory usage. If your geometry or physics configuration is very heavy, try a smaller thread count and see whether the program still fits within your system memory.
Views: 9
KAHIBARO