Geant4-11
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_workers(std::thread::hardware_concurrency())
67{
69 {
70 GetPrivateMasterRunManager(false) = this;
71 }
72
73#if defined(PTL_USE_TBB)
74 auto _useTBB = GetEnv<bool>("PTL_FORCE_TBB", GetEnv<bool>("FORCE_TBB", useTBB));
75 if(_useTBB)
76 useTBB = true;
77#endif
78
79 // handle TBB
81 m_workers = GetEnv<uint64_t>("PTL_NUM_THREADS", m_workers);
82}
83
84//======================================================================================//
85
86void
88{
89 m_workers = n;
90
91 // create threadpool if needed + task manager
92 if(!m_thread_pool)
93 {
94 if(m_verbose > 0)
95 std::cout << "TaskRunManager :: Creating thread pool..." << std::endl;
97 if(m_verbose > 0)
98 std::cout << "TaskRunManager :: Creating task manager..." << std::endl;
100 }
101 // or resize
102 else if(m_workers != m_thread_pool->size())
103 {
104 if(m_verbose > 0)
105 {
106 std::cout << "TaskRunManager :: Resizing thread pool from "
107 << m_thread_pool->size() << " to " << m_workers << " threads ..."
108 << std::endl;
109 }
111 }
112
113 // create the joiners
115 {
116 if(m_verbose > 0)
117 std::cout << "TaskRunManager :: Using TBB..." << std::endl;
118 }
119 else
120 {
121 if(m_verbose > 0)
122 std::cout << "TaskRunManager :: Using ThreadPool..." << std::endl;
123 }
124
125 m_is_initialized = true;
126 if(m_verbose > 0)
127 std::cout << "TaskRunManager :: initialized..." << std::endl;
128}
129
130//======================================================================================//
131
132void
134{
135 m_is_initialized = false;
136 if(m_thread_pool)
138 delete m_task_manager;
139 delete m_thread_pool;
140 m_task_manager = nullptr;
141 m_thread_pool = nullptr;
142}
143
144//======================================================================================//
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)
static pointer & GetPrivateMasterRunManager(bool init, bool useTBB=false)
VUserTaskQueue * m_task_queue
static TaskRunManager * GetInstance(bool useTBB=false)
static bool using_tbb()
Definition: ThreadPool.cc:94
void resize(size_type _n)
Definition: ThreadPool.hh:342
static void set_use_tbb(bool val)
Definition: ThreadPool.cc:102
size_type size() const
Definition: ThreadPool.hh:163
size_type destroy_threadpool()
Definition: ThreadPool.cc:364
Definition: AutoLock.hh:254