Geant4 11.1.1
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#include "PTL/TaskRunManager.hh"
22
23#ifndef G4GMAKE
24#include "PTL/Config.hh"
25#endif
26#include "PTL/TaskManager.hh"
27#include "PTL/ThreadPool.hh"
28#include "PTL/Threading.hh"
29#include "PTL/Utility.hh"
30
31#include <iostream>
32
33using namespace PTL;
34
35//======================================================================================//
36
38TaskRunManager::GetPrivateMasterRunManager()
39{
40 static pointer _instance = nullptr;
41 return _instance;
42}
43
44//======================================================================================//
45
47TaskRunManager::GetPrivateMasterRunManager(bool init, bool useTBB)
48{
49 auto& _v = GetPrivateMasterRunManager();
50 if(!init)
51 return _v;
52 if(!_v)
53 _v = new TaskRunManager(useTBB);
54 return _v;
55}
56
57//======================================================================================//
58
61{
62 auto& _v = GetPrivateMasterRunManager(true, useTBB);
63 return _v;
64}
65
66//======================================================================================//
67
70{
71 return GetMasterRunManager(useTBB);
72}
73
74//======================================================================================//
75
77: m_workers(std::thread::hardware_concurrency())
78{
79 if(!GetPrivateMasterRunManager())
80 GetPrivateMasterRunManager() = this;
81
82#if defined(PTL_USE_TBB)
83 auto _useTBB = GetEnv<bool>("PTL_FORCE_TBB", GetEnv<bool>("FORCE_TBB", useTBB));
84 if(_useTBB)
85 useTBB = true;
86#endif
87
88 // handle TBB
90 m_workers = GetEnv<uint64_t>("PTL_NUM_THREADS", m_workers);
91}
92
93//======================================================================================//
94
96{
97 if(GetPrivateMasterRunManager() == this)
98 GetPrivateMasterRunManager() = nullptr;
99}
100
101//======================================================================================//
102
103void
105{
106 m_workers = n;
107
108 // create threadpool if needed + task manager
109 if(!m_thread_pool)
110 {
111 if(m_verbose > 0)
112 std::cout << "TaskRunManager :: Creating thread pool..." << std::endl;
114 if(m_verbose > 0)
115 std::cout << "TaskRunManager :: Creating task manager..." << std::endl;
117 }
118 // or resize
119 else if(m_workers != m_thread_pool->size())
120 {
121 if(m_verbose > 0)
122 {
123 std::cout << "TaskRunManager :: Resizing thread pool from "
124 << m_thread_pool->size() << " to " << m_workers << " threads ..."
125 << std::endl;
126 }
128 }
129
130 // create the joiners
132 {
133 if(m_verbose > 0)
134 std::cout << "TaskRunManager :: Using TBB..." << std::endl;
135 }
136 else
137 {
138 if(m_verbose > 0)
139 std::cout << "TaskRunManager :: Using ThreadPool..." << std::endl;
140 }
141
142 m_is_initialized = true;
143 if(m_verbose > 0)
144 std::cout << "TaskRunManager :: initialized..." << std::endl;
145}
146
147//======================================================================================//
148
149void
151{
152 m_is_initialized = false;
153 if(m_thread_pool)
155 delete m_task_manager;
156 delete m_thread_pool;
157 m_task_manager = nullptr;
158 m_thread_pool = nullptr;
159}
160
161//======================================================================================//
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:151
void resize(size_type _n)
Definition: ThreadPool.hh:413
size_type size() const
Definition: ThreadPool.hh:252
size_type destroy_threadpool()
Definition: ThreadPool.cc:572
static void set_use_tbb(bool _v)
Definition: ThreadPool.cc:159
Definition: AutoLock.hh:255