41 static void fail(
const std::string& _prefix,
const std::string& _name,
42 const std::set<std::string>& _opts,
G4int _num)
45 msg << _prefix <<
": \"" << _name <<
"\". "
46 <<
"Must be one of: ";
48 for(
const auto& itr : _opts)
49 ss <<
", \"" << itr <<
"\"";
50 msg << ss.str().substr(2);
51 auto mnum = std::string(
"RunManagerFactory000") + std::to_string(_num);
52 G4Exception(
"G4RunManagerFactory::CreateRunManager", mnum.c_str(),
69 std::string rm_type =
GetName(_type);
70 if(_type == G4RunManagerType::SerialOnly ||
71 _type == G4RunManagerType::MTOnly ||
72 _type == G4RunManagerType::TaskingOnly ||
73 _type == G4RunManagerType::TBBOnly)
76 fail_if_unavail =
true;
85 rm_type = G4GetEnv<std::string>(
"G4RUN_MANAGER_TYPE",
GetName(_type),
86 "Overriding G4RunManager type...");
87 auto force_rm = G4GetEnv<std::string>(
"G4FORCE_RUN_MANAGER_TYPE",
"",
88 "Forcing G4RunManager type...");
90 if(force_rm.length() > 0)
93 fail_if_unavail =
true;
95 else if(rm_type.empty())
105 if(opts.find(rm_type) == opts.end())
109 fail(
"Run manager type is not available", rm_type, opts, 1);
123 case G4RunManagerType::Serial:
126 case G4RunManagerType::MT:
127#if defined(G4MULTITHREADED)
131 case G4RunManagerType::Tasking:
132#if defined(G4MULTITHREADED)
136 case G4RunManagerType::TBB:
137#if defined(G4MULTITHREADED) && defined(GEANT4_USE_TBB)
142 case G4RunManagerType::SerialOnly:
144 case G4RunManagerType::MTOnly:
146 case G4RunManagerType::TaskingOnly:
148 case G4RunManagerType::TBBOnly:
150 case G4RunManagerType::Default:
158 if(nthreads > 0 && mtrm)
161 master_run_manager = rm;
162 mt_master_run_manager = mtrm;
163 master_run_manager_kernel = rm->
kernel;
173#if defined(G4MULTITHREADED)
186 static auto _instance = []() {
187 std::set<std::string> options = {
"Serial" };
188#if defined(G4MULTITHREADED)
189 options.insert({
"MT",
"Tasking" });
190# if defined(GEANT4_USE_TBB)
191 options.insert(
"TBB");
204 static const auto opts = std::regex::icase;
206 if(std::regex_match(key, std::regex(
"^(Serial).*", opts)))
207 return G4RunManagerType::Serial;
208 else if(std::regex_match(key, std::regex(
"^(MT).*", opts)))
209 return G4RunManagerType::MT;
210 else if(std::regex_match(key, std::regex(
"^(Task).*", opts)))
211 return G4RunManagerType::Tasking;
212 else if(std::regex_match(key, std::regex(
"^(TBB).*", opts)))
213 return G4RunManagerType::TBB;
215 return G4RunManagerType::Default;
224 case G4RunManagerType::Serial:
226 case G4RunManagerType::SerialOnly:
228 case G4RunManagerType::MT:
230 case G4RunManagerType::MTOnly:
232 case G4RunManagerType::Tasking:
234 case G4RunManagerType::TaskingOnly:
236 case G4RunManagerType::TBB:
238 case G4RunManagerType::TBBOnly:
250#if !defined(G4MULTITHREADED)
255 if(master_run_manager)
256 return master_run_manager;
275#if defined(G4MULTITHREADED)
277 if(mt_master_run_manager)
278 return mt_master_run_manager;
297#if !defined(G4MULTITHREADED)
302 if(master_run_manager_kernel)
303 return master_run_manager_kernel;
310 return mt_rm->kernel;
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
virtual void SetNumberOfThreads(G4int n)
static G4MTRunManager * GetMasterRunManager()
static G4RunManager * CreateRunManager(G4RunManagerType _type=G4RunManagerType::Default, G4VUserTaskQueue *_queue=nullptr, G4bool fail_if_unavail=true, G4int nthreads=0)
static G4RunManager * GetMasterRunManager()
static G4MTRunManager * GetMTMasterRunManager()
static std::set< std::string > GetOptions()
static std::string GetName(G4RunManagerType)
static std::string GetDefault()
static G4RunManagerType GetType(const std::string &)
static G4RunManagerKernel * GetMasterRunManagerKernel()
G4RunManagerKernel * kernel
static G4RunManager * GetRunManager()
static G4TaskRunManager * GetMasterRunManager()
G4bool IsMultithreadedApplication()
void G4ConsumeParameters(_Args &&...)