Import Geant4 11.3.0 source tree

This commit is contained in:
Gabriele Cosmo
2024-12-06 11:11:40 +01:00
parent e58e650b32
commit 32390e802b
1984 changed files with 98713 additions and 83996 deletions
+7 -2
View File
@@ -4,22 +4,25 @@
# Locate sources and headers for this project - headers are included so they will show up
# in IDEs
file(GLOB_RECURSE ptl_headers ${PROJECT_SOURCE_DIR}/include/PTL/*.hh
${PROJECT_SOURCE_DIR}/include/PTL/*.icc)
${PROJECT_SOURCE_DIR}/include/PTL/detail/*.hh)
file(GLOB_RECURSE ptl_sources ${CMAKE_CURRENT_LIST_DIR}/*.cc)
# -------------------------------------------------------------------------------------- #
# Config, Version
# -------------------------------------------------------------------------------------- #
set(ptl_generated_headers)
configure_file(${PROJECT_SOURCE_DIR}/cmake/Templates/Config.hh.in
${CMAKE_CURRENT_BINARY_DIR}/PTL/Config.hh @ONLY)
list(APPEND ptl_headers ${CMAKE_CURRENT_BINARY_DIR}/PTL/Config.hh)
list(APPEND ptl_generated_headers ${CMAKE_CURRENT_BINARY_DIR}/PTL/Config.hh)
configure_file(${PROJECT_SOURCE_DIR}/cmake/Templates/Version.hh.in
${CMAKE_CURRENT_BINARY_DIR}/PTL/Version.hh @ONLY)
list(APPEND ptl_headers ${CMAKE_CURRENT_BINARY_DIR}/PTL/Version.hh)
list(APPEND ptl_generated_headers ${CMAKE_CURRENT_BINARY_DIR}/PTL/Version.hh)
# -------------------------------------------------------------------------------------- #
# PTL Library
@@ -116,8 +119,10 @@ endif()
if(PTL_INSTALL_HEADERS)
# headers
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/PTL
DESTINATION ${PTL_INSTALL_INCLUDEDIR})
install(
FILES ${ptl_headers}
FILES ${ptl_generated_headers}
DESTINATION ${PTL_INSTALL_INCLUDEDIR}/PTL
COMPONENT Development)
endif()
-1
View File
@@ -32,7 +32,6 @@
#include "PTL/TaskRunManager.hh"
#include "PTL/ThreadData.hh"
#include "PTL/ThreadPool.hh"
//======================================================================================//
+10 -44
View File
@@ -20,18 +20,11 @@
// Tasking class implementation
#include "PTL/TaskRunManager.hh"
#ifndef G4GMAKE
#include "PTL/Config.hh"
#endif
#include "PTL/TaskManager.hh"
#include "PTL/ThreadPool.hh"
#include "PTL/Threading.hh"
#include "PTL/Utility.hh"
#include <iostream>
using namespace PTL;
namespace PTL
{
//======================================================================================//
TaskRunManager::pointer&
@@ -75,19 +68,10 @@ TaskRunManager::GetInstance(bool useTBB)
TaskRunManager::TaskRunManager(bool useTBB)
: m_workers(std::thread::hardware_concurrency())
, m_use_tbb(useTBB)
{
if(!GetPrivateMasterRunManager())
GetPrivateMasterRunManager() = this;
#if defined(PTL_USE_TBB)
auto _useTBB = GetEnv<bool>("PTL_FORCE_TBB", GetEnv<bool>("FORCE_TBB", useTBB));
if(_useTBB)
useTBB = true;
#endif
// handle TBB
ThreadPool::set_use_tbb(useTBB);
m_workers = GetEnv<uint64_t>("PTL_NUM_THREADS", m_workers);
}
//======================================================================================//
@@ -108,40 +92,20 @@ TaskRunManager::Initialize(uint64_t n)
// create threadpool if needed + task manager
if(!m_thread_pool)
{
if(m_verbose > 0)
std::cout << "TaskRunManager :: Creating thread pool..." << std::endl;
m_thread_pool = new ThreadPool(m_workers, m_task_queue);
if(m_verbose > 0)
std::cout << "TaskRunManager :: Creating task manager..." << std::endl;
ThreadPool::Config cfg;
cfg.pool_size = m_workers;
cfg.task_queue = m_task_queue;
cfg.use_tbb = m_use_tbb;
m_thread_pool = new ThreadPool(cfg);
m_task_manager = new TaskManager(m_thread_pool);
}
// or resize
else if(m_workers != m_thread_pool->size())
{
if(m_verbose > 0)
{
std::cout << "TaskRunManager :: Resizing thread pool from "
<< m_thread_pool->size() << " to " << m_workers << " threads ..."
<< std::endl;
}
m_thread_pool->resize(m_workers);
}
// create the joiners
if(ThreadPool::using_tbb())
{
if(m_verbose > 0)
std::cout << "TaskRunManager :: Using TBB..." << std::endl;
}
else
{
if(m_verbose > 0)
std::cout << "TaskRunManager :: Using ThreadPool..." << std::endl;
}
m_is_initialized = true;
if(m_verbose > 0)
std::cout << "TaskRunManager :: initialized..." << std::endl;
}
//======================================================================================//
@@ -159,3 +123,5 @@ TaskRunManager::Terminate()
}
//======================================================================================//
} // namespace PTL
+4 -3
View File
@@ -22,10 +22,9 @@
#include "PTL/ThreadData.hh"
#include "PTL/ThreadPool.hh"
#include "PTL/VUserTaskQueue.hh"
using namespace PTL;
namespace PTL
{
//======================================================================================//
ThreadData*&
@@ -56,3 +55,5 @@ ThreadData::update()
}
//======================================================================================//
} // namespace PTL
+12 -117
View File
@@ -29,10 +29,11 @@
// ---------------------------------------------------------------
#include "PTL/ThreadPool.hh"
#include "PTL/GetEnv.hh"
#include "PTL/ScopeDestructor.hh"
#include "PTL/ThreadData.hh"
#include "PTL/Threading.hh"
#include "PTL/UserTaskQueue.hh"
#include "PTL/Utility.hh"
#include "PTL/VUserTaskQueue.hh"
#include <cassert>
@@ -41,19 +42,19 @@
#include <stdexcept>
#include <thread>
using namespace PTL;
//======================================================================================//
namespace
{
ThreadData*&
PTL::ThreadData*&
thread_data()
{
return ThreadData::GetInstance();
return PTL::ThreadData::GetInstance();
}
} // namespace
namespace PTL
{
//======================================================================================//
ThreadPool::thread_id_map_t&
@@ -65,42 +66,6 @@ ThreadPool::f_thread_ids()
//======================================================================================//
bool&
ThreadPool::f_use_tbb()
{
static bool _v = GetEnv<bool>("PTL_USE_TBB", false);
return _v;
}
//======================================================================================//
bool&
ThreadPool::f_use_cpu_affinity()
{
static bool _v = GetEnv<bool>("PTL_CPU_AFFINITY", false);
return _v;
}
//======================================================================================//
int&
ThreadPool::f_thread_priority()
{
static int _v = GetEnv<int>("PTL_THREAD_PRIORITY", 0);
return _v;
}
//======================================================================================//
int&
ThreadPool::f_verbose()
{
static int _v = GetEnv<int>("PTL_VERBOSE", 0);
return _v;
}
//======================================================================================//
ThreadPool::size_type&
ThreadPool::f_default_pool_size()
{
@@ -129,7 +94,7 @@ ThreadPool::start_thread(ThreadPool* tp, thread_data_t* _data, intmax_t _idx)
if(_idx < 0)
_idx = f_thread_ids().size();
f_thread_ids()[std::this_thread::get_id()] = _idx;
Threading::SetThreadId((int)_idx);
SetThreadId((int)_idx);
_data->emplace_back(_thr_data);
}
thread_data() = _thr_data.get();
@@ -145,38 +110,6 @@ ThreadPool::start_thread(ThreadPool* tp, thread_data_t* _data, intmax_t _idx)
}
}
//======================================================================================//
// static member function that checks enabling of tbb library
bool
ThreadPool::using_tbb()
{
return f_use_tbb();
}
//======================================================================================//
// static member function that initialized tbb library
void
ThreadPool::set_use_tbb(bool enable)
{
#if defined(PTL_USE_TBB)
f_use_tbb() = enable;
#else
ConsumeParameters(enable);
#endif
}
//======================================================================================//
// static member function that initialized tbb library
void
ThreadPool::set_default_use_cpu_affinity(bool enable)
{
#if defined(PTL_USE_TBB)
f_use_cpu_affinity() = enable;
#else
ConsumeParameters(enable);
#endif
}
//======================================================================================//
const ThreadPool::thread_id_map_t&
@@ -229,31 +162,13 @@ ThreadPool::add_thread_id(ThreadId _tid)
{
auto _idx = f_thread_ids().size();
f_thread_ids()[_tid] = _idx;
Threading::SetThreadId((int)_idx);
SetThreadId((int)_idx);
}
return f_thread_ids().at(_tid);
}
//======================================================================================//
ThreadPool::Config::Config(bool _init, bool _use_tbb, bool _use_affinity, int _verbose,
int _prio, size_type _size, VUserTaskQueue* _task_queue,
affinity_func_t _affinity_func, initialize_func_t _init_func,
finalize_func_t _fini_func)
: init{ _init }
, use_tbb{ _use_tbb }
, use_affinity{ _use_affinity }
, verbose{ _verbose }
, priority{ _prio }
, pool_size{ _size }
, task_queue{ _task_queue }
, set_affinity{ std::move(_affinity_func) }
, initializer{ std::move(_init_func) }
, finalizer{ std::move(_fini_func) }
{}
//======================================================================================//
ThreadPool::ThreadPool(const Config& _cfg)
: m_use_affinity{ _cfg.use_affinity }
, m_tbb_tp{ _cfg.use_tbb }
@@ -279,25 +194,6 @@ ThreadPool::ThreadPool(const Config& _cfg)
this->initialize_threadpool(_cfg.pool_size);
}
ThreadPool::ThreadPool(const size_type& _pool_size, VUserTaskQueue* _task_queue,
bool _use_affinity, affinity_func_t _affinity_func,
initialize_func_t _init_func, finalize_func_t _fini_func)
: ThreadPool{ Config{ true, f_use_tbb(), _use_affinity, f_verbose(), f_thread_priority(),
_pool_size, _task_queue, std::move(_affinity_func),
std::move(_init_func), std::move(_fini_func) } }
{}
ThreadPool::ThreadPool(const size_type& pool_size, initialize_func_t _init_func,
finalize_func_t _fini_func, bool _use_affinity,
affinity_func_t _affinity_func, VUserTaskQueue* task_queue)
: ThreadPool{ pool_size,
task_queue,
_use_affinity,
std::move(_affinity_func),
std::move(_init_func),
std::move(_fini_func) }
{}
//======================================================================================//
ThreadPool::~ThreadPool()
@@ -364,7 +260,7 @@ ThreadPool::set_affinity(intmax_t i, Thread& _thread) const
std::cerr << "[PTL::ThreadPool] Setting pin affinity for thread "
<< get_thread_id(_thread.get_id()) << " to " << _pin << std::endl;
}
Threading::SetPinAffinity((int)_pin, native_thread);
SetPinAffinity((int)_pin, native_thread);
} catch(std::runtime_error& e)
{
std::cerr << "[PTL::ThreadPool] Error setting pin affinity: " << e.what()
@@ -387,7 +283,7 @@ ThreadPool::set_priority(int _prio, Thread& _thread) const
<< get_thread_id(_thread.get_id()) << " priority to " << _prio
<< std::endl;
}
Threading::SetThreadPriority(_prio, native_thread);
SetThreadPriority(_prio, native_thread);
} catch(std::runtime_error& e)
{
AutoLock lock(TypeMutex<decltype(std::cerr)>());
@@ -771,9 +667,6 @@ ThreadPool::get_valid_queue(task_queue_t*& _queue) const
void
ThreadPool::execute_thread(VUserTaskQueue* _task_queue)
{
// how long the thread waits on condition variable
// static int wait_time = GetEnv<int>("PTL_POOL_WAIT_TIME", 5);
++(*m_thread_awake);
// initialization function
@@ -937,3 +830,5 @@ ThreadPool::execute_thread(VUserTaskQueue* _task_queue)
}
//======================================================================================//
} // namespace PTL
+21 -15
View File
@@ -23,8 +23,9 @@
//
#include "PTL/Threading.hh"
#include "PTL/Types.hh"
#include "PTL/Utility.hh"
#include "PTL/ConsumeParameters.hh"
#include "PTL/Macros.hh"
#if defined(PTL_WINDOWS)
# include <windows.h>
@@ -36,22 +37,25 @@
#if defined(PTL_LINUX)
# include <fstream>
# include <set>
#endif
using namespace PTL;
#include <cstddef>
#include <thread>
//======================================================================================//
namespace
{
thread_local int ThreadID = Threading::MASTER_ID;
thread_local int ThreadID = PTL::Threading::MASTER_ID;
} // namespace
//======================================================================================//
namespace PTL
{
Pid_t
Threading::GetPidId()
GetPidId()
{
// In multithreaded mode return Thread ID
return std::this_thread::get_id();
@@ -60,7 +64,7 @@ Threading::GetPidId()
//======================================================================================//
unsigned
Threading::GetNumberOfCores()
GetNumberOfCores()
{
return std::thread::hardware_concurrency();
}
@@ -68,7 +72,7 @@ Threading::GetNumberOfCores()
//======================================================================================//
unsigned
Threading::GetNumberOfPhysicalCpus()
GetNumberOfPhysicalCpus()
{
#if defined(PTL_MACOS)
int count;
@@ -96,7 +100,7 @@ Threading::GetNumberOfPhysicalCpus()
while((_pos = line.find(itr)) != _npos)
line = line.replace(_pos, itr.length(), "");
}
core_ids.insert(line);
core_ids.insert(std::move(line));
}
}
core_id_count = static_cast<unsigned>(core_ids.size());
@@ -112,13 +116,13 @@ Threading::GetNumberOfPhysicalCpus()
//======================================================================================//
void
Threading::SetThreadId(int value)
SetThreadId(int value)
{
ThreadID = value;
}
int
Threading::GetThreadId()
GetThreadId()
{
return ThreadID;
}
@@ -126,7 +130,7 @@ Threading::GetThreadId()
//======================================================================================//
bool
Threading::SetPinAffinity(int _cpu)
SetPinAffinity(int _cpu)
{
#if defined(__linux__) || defined(_AIX)
cpu_set_t _cpu_set{};
@@ -143,7 +147,7 @@ Threading::SetPinAffinity(int _cpu)
//======================================================================================//
bool
Threading::SetThreadPriority(int _prio)
SetThreadPriority(int _prio)
{
#if defined(__linux__) || defined(_AIX)
return (pthread_setschedprio(pthread_self(), _prio) == 0);
@@ -156,7 +160,7 @@ Threading::SetThreadPriority(int _prio)
//======================================================================================//
bool
Threading::SetPinAffinity(int _cpu, NativeThread& _t)
SetPinAffinity(int _cpu, NativeThread& _t)
{
#if defined(__linux__) || defined(_AIX)
cpu_set_t _cpu_set{};
@@ -173,7 +177,7 @@ Threading::SetPinAffinity(int _cpu, NativeThread& _t)
//======================================================================================//
bool
Threading::SetThreadPriority(int _prio, NativeThread& _t)
SetThreadPriority(int _prio, NativeThread& _t)
{
#if defined(__linux__) || defined(_AIX)
return (pthread_setschedprio(static_cast<pthread_t>(_t), _prio) == 0);
@@ -184,3 +188,5 @@ Threading::SetThreadPriority(int _prio, NativeThread& _t)
}
//======================================================================================//
} // namespace PTL
-124
View File
@@ -1,124 +0,0 @@
//
// MIT License
// Copyright (c) 2019 Jonathan R. Madsen
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// ----------------------------------------------------------------------
// class Timer
//
// Implementation
// 29.04.97 G.Cosmo Added timings for Windows systems
#include "PTL/Timer.hh"
#include <stdexcept>
using namespace PTL;
#if !(defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64))
# include <unistd.h>
#else
# include <sys/types.h>
# include <windows.h>
//======================================================================================//
// extract milliseconds time unit
int
sysconf(int a)
{
if(a == _SC_CLK_TCK)
return 1000;
else
return 0;
}
//======================================================================================//
static clock_t
filetime2msec(FILETIME* t)
{
return (clock_t)((((float) t->dwHighDateTime) * 429496.7296) +
(((float) t->dwLowDateTime) * .0001));
}
//======================================================================================//
clock_t
times(struct tms* t)
{
FILETIME ct = { 0, 0 }, et = { 0, 0 }, st = { 0, 0 }, ut = { 0, 0 }, rt = { 0, 0 };
SYSTEMTIME realtime;
GetSystemTime(&realtime);
SystemTimeToFileTime(&realtime, &rt); // get real time in 10^-9 sec
if(t != 0)
{
GetProcessTimes(GetCurrentProcess(), &ct, &et, &st,
&ut); // get process time in 10^-9 sec
t->tms_utime = t->tms_cutime = filetime2msec(&ut);
t->tms_stime = t->tms_cstime = filetime2msec(&st);
}
return filetime2msec(&rt);
}
//======================================================================================//
#endif /* WIN32 */
//======================================================================================//
double
Timer::GetRealElapsed() const
{
if(!fValidTimes)
{
throw std::runtime_error("Timer::GetRealElapsed() - "
"Timer not stopped or times not recorded!");
}
std::chrono::duration<double> diff = fEndRealTime - fStartRealTime;
return diff.count();
}
//======================================================================================//
double
Timer::GetSystemElapsed() const
{
if(!fValidTimes)
{
throw std::runtime_error("Timer::GetSystemElapsed() - "
"Timer not stopped or times not recorded!");
}
double diff = fEndTimes.tms_stime - fStartTimes.tms_stime;
return diff / sysconf(_SC_CLK_TCK);
}
//======================================================================================//
double
Timer::GetUserElapsed() const
{
if(!fValidTimes)
{
throw std::runtime_error("Timer::GetUserElapsed() - "
"Timer not stopped or times not recorded!");
}
double diff = fEndTimes.tms_utime - fStartTimes.tms_utime;
return diff / sysconf(_SC_CLK_TCK);
}
//======================================================================================//
+5 -25
View File
@@ -26,10 +26,10 @@
#include "PTL/UserTaskQueue.hh"
#include "PTL/AutoLock.hh"
#include "PTL/ScopeDestructor.hh"
#include "PTL/TaskGroup.hh"
#include "PTL/ThreadData.hh"
#include "PTL/ThreadPool.hh"
#include "PTL/Utility.hh"
#include <cassert>
#include <chrono>
@@ -37,12 +37,11 @@
#include <iostream>
#include <map>
#include <stdexcept>
#include <system_error>
#include <thread>
#include <utility>
using namespace PTL;
namespace PTL
{
//======================================================================================//
UserTaskQueue::UserTaskQueue(intmax_t nworkers, UserTaskQueue* parent)
@@ -61,26 +60,6 @@ UserTaskQueue::UserTaskQueue(intmax_t nworkers, UserTaskQueue* parent)
for(intmax_t i = 0; i < nworkers + 1; ++i)
m_subqueues->emplace_back(new TaskSubQueue(m_ntasks));
}
#if defined(DEBUG)
if(GetEnv<int>("PTL_VERBOSE", 0) > 3)
{
RecursiveAutoLock l(TypeMutex<decltype(std::cout), RecursiveMutex>());
std::stringstream ss;
ss << ThreadPool::get_this_thread_id() << "> " << ThisThread::get_id() << " ["
<< __FUNCTION__ << ":" << __LINE__ << "] "
<< "this = " << this << ", "
<< "clone = " << std::boolalpha << m_is_clone << ", "
<< "thread = " << m_thread_bin << ", "
<< "insert = " << m_insert_bin << ", "
<< "hold = " << m_hold->load() << " @ " << m_hold << ", "
<< "tasks = " << m_ntasks->load() << " @ " << m_ntasks << ", "
<< "subqueue = " << m_subqueues << ", "
<< "size = " << true_size() << ", "
<< "empty = " << true_empty();
std::cout << ss.str() << std::endl;
}
#endif
}
//======================================================================================//
@@ -311,7 +290,6 @@ UserTaskQueue::InsertTask(task_pointer&& task, ThreadData* data, intmax_t subq)
if(insert_task(_n))
return _n;
}
return GetThreadBin();
}
//======================================================================================//
@@ -470,3 +448,5 @@ UserTaskQueue::ReleaseHold()
}
//======================================================================================//
} // namespace PTL
+6 -5
View File
@@ -27,12 +27,11 @@
#include "PTL/VUserTaskQueue.hh"
#include "PTL/TaskRunManager.hh"
#include "PTL/Utility.hh" // for PTL
#include <cstdint> // for intmax_t
#include <thread> // for thread
using namespace PTL;
#include <cstdint> // for intmax_t
#include <thread> // for thread
namespace PTL
{
//======================================================================================//
VUserTaskQueue::VUserTaskQueue(intmax_t nworkers)
@@ -48,3 +47,5 @@ VUserTaskQueue::VUserTaskQueue(intmax_t nworkers)
}
//======================================================================================//
} // namespace PTL