Example B1 - Multithreading

Hi

I am using example B1. To use it in multithread mode, I have included #include “G4Types.hh” and added following lines in the main()

#ifdef G4MULTITHREADED
G4MTRunManager* runManager = new G4MTRunManager;
runManager->SetNumberOfThreads(2*(G4Threading::G4GetNumberOfCores()));

#else
G4RunManager* runManager = new G4RunManager;
#endif

Then I build the code using -
cmake -DGEANT4_BUILD_MULTITHREADED=ON

then make -j10

but it is still taking same time to complete the same number of primaries. I m running the code on a computer having 20 threads.

Please let me know if I have to further modifications in the example B1 for MT.

The multitheading it set by the core geant4 installation, so you need to use -DGEANT4_BUILD_MULTITHREADED=ON when configuring and compiling geant4 itself.

You can’t turn it on/off like that for individual simulations.

Thanks for the clarification. I am using a 10 core (20 thread) CPU for simulation and I am configuring it using " -DGEANT4_BUILD_MULTITHREADED=ON". But I am not observing any difference in simulation time using 10 core CPU compared to the earlier 4 core CPU.

Have you seen outputs like the following?

G4WT2 > balabala
G4WT2 > balabala
G4WT4 > balabala
G4WT4 > balabala
G4WT4 > balabala
G4WT3 > balabala
G4WT3 > balabala

If not, you are probably still using one single core.
Did you use the flag -DGEANT4_BUILD_MULTITHREADED=ON when building Geant4 itself?
You do not need to use the flag -DGEANT4_BUILD_MULTITHREADED=ON when building example B1.

In B1, you could also modify the code snippet you posted to

#ifdef G4MULTITHREADED
G4MTRunManager* runManager = new G4MTRunManager;
runManager->SetNumberOfThreads(2*(G4Threading::G4GetNumberOfCores()));
G4cout << "Multithreaded" << G4endl;
#else
G4RunManager* runManager = new G4RunManager;
G4cout << "Single threaded" << G4endl;
#endif

to confirm how your code is being run.

Yes I am seeing outputs like G4WT0, G4WT1…upto G4WT29 when running the code on 10-core computer.

When I am running the code on quad core computer I am sseeing outputs like G4WT0, … upto G4WT10.

But I was expecting 10 core cpu should be quicker compared to quad core to complete the simulations but both are taking same time. I am using 10^8 primaries and both cpu’s are taking 1 hr 20 mins.

I am using the flag -DGEANT4_BUILD_MULTITHREADED=ON to configure.

Yes I havve modified the code like this and now I have included the two lines

G4cout << "Multithreaded" << G4endl;

G4cout << "Single threaded" << G4endl;

as suggested by you but after configuring it using

-DGEANT4_BUILD_MULTITHREADED=ON

I am getting following lines

– Configuring done
– Generating done
CMake Warning:
Manually-specified variables were not used by the project:

GEANT4_BUILD_MULTITHREADED

and after running the simulation for certain number of primaries I am not getting print of “Multithrreaded or “Single threaded””

Did you rerun make? Adding these lines into B1 on my machine print the appropriate line.

Wasn’t at a geant4 machine yesterday, and have just realised that the initial text printed tells you what mode you compiled the core components of geant4 in

$ ./exampleB1 run1.mac 

**************************************************************
 Geant4 version Name: geant4-10-05-patch-01 [MT]   (17-April-2019)
  << in Multi-threaded mode >> 
                       Copyright : Geant4 Collaboration
                      References : NIM A 506 (2003), 250-303
                                 : IEEE-TNS 53 (2006), 270-278
                                 : NIM A 835 (2016), 186-225
                             WWW : http://geant4.org/
**************************************************************

Multithreaded
<<< Reference Physics List QBBC 
Visualization Manager instantiating with verbosity "warnings (3)"...
.
.
.