forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeantUtils.cc
95 lines (87 loc) · 2.74 KB
/
GeantUtils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/ext/GeantUtils.cc
//---------------------------------------------------------------------------//
#include "GeantUtils.hh"
#include <G4RunManager.hh>
#include <G4Threading.hh>
#include <G4Version.hh>
#if G4VERSION_NUMBER < 1070
# include <G4MTRunManager.hh>
#endif
#if G4VERSION_NUMBER >= 1070
# include <G4Backtrace.hh>
#endif
#include "corecel/Assert.hh"
#include "corecel/io/Logger.hh"
namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Clear Geant4's signal handlers that get installed on startup/activation.
*
* This should be called before instantiating a run manager.
*/
void disable_geant_signal_handler()
{
#if G4VERSION_NUMBER >= 1070
CELER_LOG(debug) << "Disabling Geant4 signal handlers";
// Disable geant4 signal interception
G4Backtrace::DefaultSignals() = {};
#endif
}
//---------------------------------------------------------------------------//
/*!
* Get the number of threads in a version-portable way.
*
* G4RunManager::GetNumberOfThreads isn't virtual before Geant4 v10.7.0 so we
* need to explicitly dynamic cast to G4MTRunManager to get the number of
* threads.
*
* In tasking mode, the result may be zero!
*/
int get_geant_num_threads(G4RunManager const& runman)
{
// Default is 1 if not multithreaded
int result{1};
#if G4VERSION_NUMBER >= 1070
result = runman.GetNumberOfThreads();
#else
if (auto const* runman_mt = dynamic_cast<G4MTRunManager const*>(&runman))
{
result = runman_mt->GetNumberOfThreads();
}
#endif
CELER_ENSURE(result >= 0);
return result;
}
//---------------------------------------------------------------------------//
/*!
* Get the number of threads from the global run manager.
*/
int get_geant_num_threads()
{
auto* run_man = G4RunManager::GetRunManager();
CELER_VALIDATE(run_man,
<< "cannot query global thread count before G4RunManager "
"is created");
return get_geant_num_threads(*run_man);
}
//---------------------------------------------------------------------------//
/*!
* Get the Geant4 thread ID.
*/
int get_geant_thread_id()
{
// Thread ID is -1 when running serially
if (G4Threading::IsMultithreadedApplication())
{
return G4Threading::G4GetThreadId();
}
return 0;
}
//---------------------------------------------------------------------------//
} // namespace celeritas