Geant4
11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
Threading.cc
Go to the documentation of this file.
1
//
2
// MIT License
3
// Copyright (c) 2020 Jonathan R. Madsen
4
// Permission is hereby granted, free of charge, to any person obtaining a copy
5
// of this software and associated documentation files (the "Software"), to deal
6
// in the Software without restriction, including without limitation the rights
7
// to use, copy, modify, merge, publish, distribute, sublicense, and
8
// copies of the Software, and to permit persons to whom the Software is
9
// furnished to do so, subject to the following conditions:
10
// The above copyright notice and this permission notice shall be included in
11
// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
12
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
13
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
17
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18
//
19
// ---------------------------------------------------------------
20
// Tasking class implementation
21
//
22
// Threading.cc
23
//
24
25
#include "
PTL/Threading.hh
"
26
#include "
PTL/Types.hh
"
27
#include "
PTL/Utility.hh
"
28
29
#if defined(PTL_WINDOWS)
30
# include <windows.h>
31
#endif
32
33
#if defined(PTL_MACOS)
34
# include <sys/sysctl.h>
35
#endif
36
37
#if defined(PTL_LINUX)
38
# include <fstream>
39
#endif
40
41
using namespace
PTL
;
42
43
//======================================================================================//
44
45
namespace
46
{
47
thread_local
int
ThreadID =
Threading::MASTER_ID
;
48
49
}
// namespace
50
51
//======================================================================================//
52
53
Pid_t
54
Threading::GetPidId
()
55
{
56
// In multithreaded mode return Thread ID
57
return
std::this_thread::get_id();
58
}
59
60
//======================================================================================//
61
62
unsigned
63
Threading::GetNumberOfCores
()
64
{
65
return
std::thread::hardware_concurrency();
66
}
67
68
//======================================================================================//
69
70
unsigned
71
Threading::GetNumberOfPhysicalCpus
()
72
{
73
#if defined(PTL_MACOS)
74
int
count;
75
size_t
count_len =
sizeof
(count);
76
sysctlbyname(
"hw.physicalcpu"
, &count, &count_len,
nullptr
, 0);
77
return
static_cast<
unsigned
>
(count);
78
#elif defined(PTL_LINUX)
79
unsigned
core_id_count = 0;
80
std::ifstream ifs(
"/proc/cpuinfo"
);
81
if
(ifs)
82
{
83
std::set<std::string> core_ids;
84
while
(
true
)
85
{
86
std::string line = {};
87
getline(ifs, line);
88
if
(!ifs.good())
89
break
;
90
if
(line.find(
"core id"
) != std::string::npos)
91
{
92
for
(std::string itr : {
"core id"
,
":"
,
" "
,
"\t"
})
93
{
94
static
auto
_npos = std::string::npos;
95
auto
_pos = _npos;
96
while
((_pos = line.find(itr)) != _npos)
97
line = line.replace(_pos, itr.length(),
""
);
98
}
99
core_ids.insert(line);
100
}
101
}
102
core_id_count =
static_cast<
unsigned
>
(core_ids.size());
103
if
(core_id_count > 0)
104
return
core_id_count;
105
}
106
return
GetNumberOfCores
();
107
#else
108
return
GetNumberOfCores
();
109
#endif
110
}
111
112
//======================================================================================//
113
114
void
115
Threading::SetThreadId
(
int
value)
116
{
117
ThreadID = value;
118
}
119
120
int
121
Threading::GetThreadId
()
122
{
123
return
ThreadID;
124
}
125
126
//======================================================================================//
127
128
bool
129
Threading::SetPinAffinity
(
int
_cpu)
130
{
131
#if defined(__linux__) || defined(_AIX)
132
cpu_set_t _cpu_set{};
133
CPU_ZERO(&_cpu_set);
134
if
(_cpu >= 0)
135
CPU_SET(_cpu, &_cpu_set);
136
return
(pthread_setaffinity_np(pthread_self(),
sizeof
(cpu_set_t), &_cpu_set) == 0);
137
#else
// Not available for Mac, WIN,...
138
ConsumeParameters
(_cpu);
139
return
true
;
140
#endif
141
}
142
143
//======================================================================================//
144
145
bool
146
Threading::SetThreadPriority
(
int
_prio)
147
{
148
#if defined(__linux__) || defined(_AIX)
149
return
(pthread_setschedprio(pthread_self(), _prio) == 0);
150
#else
// Not available for Mac, WIN,...
151
ConsumeParameters
(_prio);
152
return
true
;
153
#endif
154
}
155
156
//======================================================================================//
157
158
bool
159
Threading::SetPinAffinity
(
int
_cpu,
NativeThread
& _t)
160
{
161
#if defined(__linux__) || defined(_AIX)
162
cpu_set_t _cpu_set{};
163
CPU_ZERO(&_cpu_set);
164
CPU_SET(_cpu, &_cpu_set);
165
return
(pthread_setaffinity_np(
static_cast<
pthread_t
>
(_t),
sizeof
(cpu_set_t),
166
&_cpu_set) == 0);
167
#else
// Not available for Mac, WIN,...
168
ConsumeParameters
(_cpu, _t);
169
return
true
;
170
#endif
171
}
172
173
//======================================================================================//
174
175
bool
176
Threading::SetThreadPriority
(
int
_prio,
NativeThread
& _t)
177
{
178
#if defined(__linux__) || defined(_AIX)
179
return
(pthread_setschedprio(
static_cast<
pthread_t
>
(_t), _prio) == 0);
180
#else
181
ConsumeParameters
(_prio, _t);
182
return
true
;
183
#endif
184
}
185
186
//======================================================================================//
Threading.hh
Types.hh
Utility.hh
PTL::Threading::GetPidId
Pid_t GetPidId()
Definition
Threading.cc:54
PTL::Threading::MASTER_ID
@ MASTER_ID
Definition
Threading.hh:87
PTL::Threading::GetNumberOfCores
unsigned GetNumberOfCores()
Definition
Threading.cc:63
PTL::Threading::GetNumberOfPhysicalCpus
unsigned GetNumberOfPhysicalCpus()
Definition
Threading.cc:71
PTL::Threading::SetPinAffinity
bool SetPinAffinity(int idx)
Definition
Threading.cc:129
PTL::Threading::SetThreadPriority
bool SetThreadPriority(int _v)
Definition
Threading.cc:146
PTL::Threading::SetThreadId
void SetThreadId(int aNewValue)
Definition
Threading.cc:115
PTL::Threading::GetThreadId
int GetThreadId()
Definition
Threading.cc:121
PTL
Definition
AutoLock.hh:255
PTL::ConsumeParameters
void ConsumeParameters(Args &&...)
Definition
Utility.hh:44
PTL::NativeThread
std::thread::native_handle_type NativeThread
Definition
Threading.hh:38
PTL::Pid_t
std::thread::id Pid_t
Definition
Threading.hh:40
geant4-v11.2.2
source
externals
ptl
src
Threading.cc
Generated by
1.12.0