Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
TaskRunManager.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#include "PTL/TaskRunManager.hh"
23#include "PTL/AutoLock.hh"
24#include "PTL/Task.hh"
25#include "PTL/TaskGroup.hh"
26#include "PTL/TaskManager.hh"
27#include "PTL/ThreadPool.hh"
28#include "PTL/Threading.hh"
29#include "PTL/Utility.hh"
30
31#include <cstdlib>
32#include <cstring>
33#include <iterator>
34
35using namespace PTL;
36
37//======================================================================================//
38
40TaskRunManager::GetPrivateMasterRunManager(bool init, bool useTBB)
41{
42 static pointer _instance = (init) ? new TaskRunManager(useTBB) : nullptr;
43 return _instance;
44}
45
46//======================================================================================//
47
50{
51 static pointer& _instance = GetPrivateMasterRunManager(true, useTBB);
52 return _instance;
53}
54
55//======================================================================================//
56
59{
60 return GetMasterRunManager(useTBB);
61}
62
63//======================================================================================//
64
66: m_is_initialized(false)
67, m_verbose(0)
68, m_workers(std::thread::hardware_concurrency())
69, m_task_queue(nullptr)
70, m_thread_pool(nullptr)
71, m_task_manager(nullptr)
72{
73 if(!GetPrivateMasterRunManager(false))
74 {
75 GetPrivateMasterRunManager(false) = this;
76 }
77
78#ifdef PTL_USE_TBB
79 auto _useTBB = GetEnv<bool>("FORCE_TBB", useTBB);
80 if(_useTBB)
81 useTBB = true;
82#endif
83
84 // handle TBB
86 m_workers = GetEnv<uint64_t>("PTL_NUM_THREADS", m_workers);
87}
88
89//======================================================================================//
90
92
93//======================================================================================//
94
95void
97{
98 m_workers = n;
99
100 // create threadpool if needed + task manager
101 if(!m_thread_pool)
102 {
103 if(m_verbose > 0)
104 std::cout << "TaskRunManager :: Creating thread pool..." << std::endl;
106 if(m_verbose > 0)
107 std::cout << "TaskRunManager :: Creating task manager..." << std::endl;
109 }
110 // or resize
111 else if(m_workers != m_thread_pool->size())
112 {
113 if(m_verbose > 0)
114 {
115 std::cout << "TaskRunManager :: Resizing thread pool from "
116 << m_thread_pool->size() << " to " << m_workers << " threads ..."
117 << std::endl;
118 }
120 }
121
122 // create the joiners
124 {
125 if(m_verbose > 0)
126 std::cout << "TaskRunManager :: Using TBB..." << std::endl;
127 }
128 else
129 {
130 if(m_verbose > 0)
131 std::cout << "TaskRunManager :: Using ThreadPool..." << std::endl;
132 }
133
134 m_is_initialized = true;
135 if(m_verbose > 0)
136 std::cout << "TaskRunManager :: initialized..." << std::endl;
137}
138
139//======================================================================================//
140
141void
143{
144 m_is_initialized = false;
146 delete m_task_manager;
147 delete m_thread_pool;
148 m_task_manager = nullptr;
149 m_thread_pool = nullptr;
150}
151
152//======================================================================================//
TaskManager * m_task_manager
static TaskRunManager * GetMasterRunManager(bool useTBB=false)
virtual void Initialize(uint64_t n=std::thread::hardware_concurrency())
virtual void Terminate()
ThreadPool * m_thread_pool
TaskRunManager(bool useTBB=false)
VUserTaskQueue * m_task_queue
static TaskRunManager * GetInstance(bool useTBB=false)
TaskRunManager * pointer
static bool using_tbb()
Definition: ThreadPool.cc:92
void resize(size_type _n)
Definition: ThreadPool.hh:287
static void set_use_tbb(bool val)
Definition: ThreadPool.cc:100
size_type size() const
Definition: ThreadPool.hh:151
size_type destroy_threadpool()
Definition: ThreadPool.cc:366
Definition: AutoLock.hh:254