Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
PTL::Threading Namespace Reference

Enumerations

enum  { SEQUENTIAL_ID = -2 , MASTER_ID = -1 , WORKER_ID = 0 , GENERICTHREAD_ID = -1000 }
 

Functions

Pid_t GetPidId ()
 
unsigned GetNumberOfPhysicalCpus ()
 
unsigned GetNumberOfCores ()
 
int GetThreadId ()
 
void SetThreadId (int aNewValue)
 
bool SetPinAffinity (int idx)
 
bool SetThreadPriority (int _v)
 
bool SetPinAffinity (int idx, NativeThread &_t)
 
bool SetThreadPriority (int _v, NativeThread &_t)
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
SEQUENTIAL_ID 
MASTER_ID 
WORKER_ID 
GENERICTHREAD_ID 

Definition at line 84 of file Threading.hh.

85{
86 SEQUENTIAL_ID = -2,
87 MASTER_ID = -1,
88 WORKER_ID = 0,
89 GENERICTHREAD_ID = -1000
90};

Function Documentation

◆ GetNumberOfCores()

unsigned PTL::Threading::GetNumberOfCores ( )

Definition at line 63 of file Threading.cc.

64{
65 return std::thread::hardware_concurrency();
66}

Referenced by PTL::ThreadPool::execute_on_all_threads(), PTL::ThreadPool::execute_on_specific_threads(), and GetNumberOfPhysicalCpus().

◆ GetNumberOfPhysicalCpus()

unsigned PTL::Threading::GetNumberOfPhysicalCpus ( )

Definition at line 71 of file Threading.cc.

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}
unsigned GetNumberOfCores()
Definition: Threading.cc:63

◆ GetPidId()

Pid_t PTL::Threading::GetPidId ( )

Definition at line 54 of file Threading.cc.

55{
56 // In multithreaded mode return Thread ID
57 return std::this_thread::get_id();
58}

◆ GetThreadId()

int PTL::Threading::GetThreadId ( )

Definition at line 121 of file Threading.cc.

122{
123 return ThreadID;
124}

◆ SetPinAffinity() [1/2]

bool PTL::Threading::SetPinAffinity ( int  idx)

Definition at line 129 of file Threading.cc.

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}
void ConsumeParameters(Args &&...)
Definition: Utility.hh:44

Referenced by PTL::ThreadPool::set_affinity().

◆ SetPinAffinity() [2/2]

bool PTL::Threading::SetPinAffinity ( int  idx,
NativeThread _t 
)

Definition at line 159 of file Threading.cc.

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}

◆ SetThreadId()

void PTL::Threading::SetThreadId ( int  aNewValue)

Definition at line 115 of file Threading.cc.

116{
117 ThreadID = value;
118}

Referenced by PTL::ThreadPool::add_thread_id(), and PTL::ThreadPool::start_thread().

◆ SetThreadPriority() [1/2]

bool PTL::Threading::SetThreadPriority ( int  _v)

Definition at line 146 of file Threading.cc.

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}

Referenced by PTL::ThreadPool::set_priority().

◆ SetThreadPriority() [2/2]

bool PTL::Threading::SetThreadPriority ( int  _v,
NativeThread _t 
)

Definition at line 176 of file Threading.cc.

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}