ATLAS Offline Software
G4InitTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "G4InitTool.h"
6 
7 #include "G4WorkerRunManager.hh"
8 
9 #include "G4UImanager.hh"
10 
11 #include "G4MTRunManager.hh"
12 #include "G4WorkerThread.hh"
13 #include "G4UserWorkerThreadInitialization.hh"
14 #include "G4RunManager.hh"
15 #include "G4VUserActionInitialization.hh"
16 #include "G4UserWorkerInitialization.hh"
17 #include "G4AutoDelete.hh"
18 
20 
21 #include <unistd.h>
22 #include <sys/syscall.h>
23 
24 
25 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
26 
27 G4InitTool::G4InitTool( const std::string& type, const std::string& name,
28  const IInterface* parent )
29  : base_class(type, name, parent),
30  m_nInitThreads(0)
31 
32 {
33 }
34 
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
37 
38 void
40 
41  ATH_MSG_INFO ("==> tbb thread started with id: 0x" << std::hex
42  << pthread_self() << std::dec);
43 
44  // The following code is modeled after G4MTRunManagerKernel::StartThread
45 
46  // Define the G4 worker thread context
47  auto wThreadContext = new G4WorkerThread;
48  G4AutoDelete::Register(wThreadContext);
49 
50  // Assign the thread id
51  // TODO: implement a better solution.
52  // WARNING: not portable!
53 #ifdef __USE_GNU
54  pid_t tid = gettid();
55  wThreadContext->SetThreadId( tid );
56 #else
57  #error "gettid() unavailable on this system"
58 #endif
59  //wThreadContext->SetThreadId( pthread_self() );
60  G4int thisID = wThreadContext->GetThreadId();
61  G4Threading::G4SetThreadId(thisID);
62 
63  // Retrieve the G4AtlasMTRunManager
64  G4MTRunManager* masterRM = G4MTRunManager::GetMasterRunManager();
65  // Worker thread initialization
66  const G4UserWorkerThreadInitialization* workerInitializer = masterRM->GetUserWorkerThreadInitialization();
67 
68  const CLHEP::HepRandomEngine* masterEngine = masterRM->getMasterRandomEngine();
69  workerInitializer->SetupRNGEngine(masterEngine);
70 
71  // Setup up thread-local workspace
72  wThreadContext->BuildGeometryAndPhysicsVector();
73 
74  // Create the thread-local worker run manager (G4AtlasWorkerRunManager)
75  ATH_MSG_INFO("Creating worker RM");
76  G4WorkerRunManager* wrm = workerInitializer->CreateWorkerRunManager();
77  wrm->SetWorkerThread(wThreadContext);
78 
79  // Share detector from master with worker.
80  ATH_MSG_INFO("Assigning detector construction");
81  auto detector ATLAS_THREAD_SAFE = const_cast<G4VUserDetectorConstruction*>
82  (masterRM->GetUserDetectorConstruction());
83  wrm->G4RunManager::SetUserInitialization(detector);
84 
85  // Share physics list from master with worker.
86  auto physicslist ATLAS_THREAD_SAFE = const_cast<G4VUserPhysicsList*>(masterRM->GetUserPhysicsList());
87  wrm->SetUserInitialization(physicslist);
88 
89  // Build thread-local user actions - NOT CURRENTLY USED.
90  if(masterRM->GetUserActionInitialization()) {
91  masterRM->GetNonConstUserActionInitialization()->Build();
92  }
93 
94  // Start user worker initialization
95  if(masterRM->GetUserWorkerInitialization()) {
96  masterRM->GetUserWorkerInitialization()->WorkerStart();
97  }
98 
99  // Initialize the worker run manager
100  ATH_MSG_INFO("Initializing worker RM");
101  wrm->Initialize();
102 
103  // Atomic increment number of initialized threads
104  m_nInitThreads++;
105 
106  ATH_MSG_INFO ("==> tbb thread end of initThread with id: 0x" << std::hex <<
107  pthread_self() << std::dec );
108 
109 }
110 
111 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
112 
113 void
115 
116  ATH_MSG_INFO ("terminateThread ==> tbb thread 0x"
117  << std::hex << pthread_self() << std::dec );
118 
119  // call the run termination method on the G4 side
120  G4RunManager::GetRunManager()->RunTermination();
121 
122  m_nInitThreads--;
123 
124 }
G4InitTool::m_nInitThreads
std::atomic_uint m_nInitThreads
Definition: G4InitTool.h:26
pid_t
int32_t pid_t
Definition: FPGATrackSimTypes.h:19
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
G4InitTool::G4InitTool
G4InitTool(const std::string &, const std::string &, const IInterface *)
Definition: G4InitTool.cxx:27
TRT::Hit::detector
@ detector
Definition: HitInfo.h:78
test_pyathena.parent
parent
Definition: test_pyathena.py:15
G4InitTool::terminateThread
virtual void terminateThread()
Definition: G4InitTool.cxx:114
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
G4InitTool.h
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
G4InitTool::initThread
virtual void initThread()
Definition: G4InitTool.cxx:39
checker_macros.h
Define macros for attributes used to control the static checker.