Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4UserWorkerThreadInitialization.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4UserWorkerThreadInitialization implementation
27//
28// Authors: M.Asai, A.Dotti (SLAC), 16 September 2013
29// --------------------------------------------------------------------
30
31#include <sstream>
32
34#include "G4AutoLock.hh"
36#include "G4UImanager.hh"
38#include "G4VUserPhysicsList.hh"
39#include "G4WorkerRunManager.hh"
40#include "G4WorkerThread.hh"
41
42// Will need this for TPMalloc
43//#ifdef G4MULTITHREADED
44//#define TPMALLOCDEFINESTUB
45//#include "tpmalloc/tpmallocstub.h"
46//#endif
47
48// --------------------------------------------------------------------
49#ifdef G4MULTITHREADED
52{
53 // Note: this method is called by G4MTRunManager,
54 // here we are still sequential.
55 // Create a new thread/worker structure
56 G4Thread* worker = new G4Thread;
58 return worker;
59}
60#else
63{
64 return new G4Thread;
65}
66#endif
67
68// --------------------------------------------------------------------
69#ifdef G4MULTITHREADED
71{
72 G4THREADJOIN(*aThread);
73}
74#else // Avoid compilation warning in sequential
76{
77}
78#endif
79
80// --------------------------------------------------------------------
82{
83}
84
85// --------------------------------------------------------------------
87{
88}
89
90// --------------------------------------------------------------------
91namespace
92{
93 G4Mutex rngCreateMutex = G4MUTEX_INITIALIZER;
94}
95
96// --------------------------------------------------------------------
98SetupRNGEngine(const CLHEP::HepRandomEngine* aNewRNG) const
99{
100 G4AutoLock l(&rngCreateMutex);
101 // No default available, let's create the instance of random stuff
102 // A Call to this just forces the creation to defaults
103 G4Random::getTheEngine();
104 // Poor man's solution to check which RNG Engine is used in master thread
105 CLHEP::HepRandomEngine* retRNG = nullptr;
106
107 // Need to make these calls thread safe
108 if(dynamic_cast<const CLHEP::HepJamesRandom*>(aNewRNG))
109 {
110 retRNG = new CLHEP::HepJamesRandom;
111 }
112 if(dynamic_cast<const CLHEP::MixMaxRng*>(aNewRNG))
113 {
114 retRNG = new CLHEP::MixMaxRng;
115 }
116 if(dynamic_cast<const CLHEP::RanecuEngine*>(aNewRNG))
117 {
118 retRNG = new CLHEP::RanecuEngine;
119 }
120 if(dynamic_cast<const CLHEP::RanluxppEngine*>(aNewRNG))
121 {
122 retRNG = new CLHEP::RanluxppEngine;
123 }
124 if(dynamic_cast<const CLHEP::Ranlux64Engine*>(aNewRNG))
125 {
126 const CLHEP::Ranlux64Engine* theRNG =
127 dynamic_cast<const CLHEP::Ranlux64Engine*>(aNewRNG);
128 retRNG = new CLHEP::Ranlux64Engine(123, theRNG->getLuxury());
129 }
130 if(dynamic_cast<const CLHEP::MTwistEngine*>(aNewRNG))
131 {
132 retRNG = new CLHEP::MTwistEngine;
133 }
134 if(dynamic_cast<const CLHEP::DualRand*>(aNewRNG))
135 {
136 retRNG = new CLHEP::DualRand;
137 }
138 if(dynamic_cast<const CLHEP::RanluxEngine*>(aNewRNG))
139 {
140 const CLHEP::RanluxEngine* theRNG =
141 dynamic_cast<const CLHEP::RanluxEngine*>(aNewRNG);
142 retRNG = new CLHEP::RanluxEngine(123, theRNG->getLuxury());
143 }
144 if(dynamic_cast<const CLHEP::RanshiEngine*>(aNewRNG))
145 {
146 retRNG = new CLHEP::RanshiEngine;
147 }
148
149 if(retRNG != nullptr)
150 {
151 G4Random::setTheEngine(retRNG);
152 }
153 else
154 {
155 // Does a new method, such as aNewRng->newEngine() exist to clone it ?
157 msg << " Unknown type of RNG Engine - " << G4endl
158 << " Can cope only with HepJamesRandom, MixMaxRng, Ranecu, Ranlux64,"
159 << " Ranlux++, MTwistEngine, DualRand, Ranlux or Ranshi." << G4endl
160 << " Cannot clone this type of RNG engine, as required for this thread"
161 << G4endl << " Aborting " << G4endl;
162 G4Exception("G4UserWorkerThreadInitialization::SetupRNGEngine()",
163 "Run0122", FatalException, msg);
164 }
165}
166
167// --------------------------------------------------------------------
170{
171 return new G4WorkerRunManager();
172}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:59
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:85
#define G4THREADJOIN(worker)
Definition: G4Threading.hh:259
void G4THREADCREATE(_Worker *&worker, _Func func, _Args... args)
Definition: G4Threading.hh:268
G4DummyThread G4Thread
Definition: G4Threading.hh:247
std::mutex G4Mutex
Definition: G4Threading.hh:81
#define G4endl
Definition: G4ios.hh:57
int getLuxury() const
Definition: RanluxEngine.h:90
static void StartThread(G4WorkerThread *context)
virtual void SetupRNGEngine(const CLHEP::HepRandomEngine *aRNGEngine) const
virtual G4Thread * CreateAndStartWorker(G4WorkerThread *workerThreadContext)
virtual G4WorkerRunManager * CreateWorkerRunManager() const