Geant4-11
UserTaskQueue.hh
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 header
21// Class Description:
22// ---------------------------------------------------------------
23// Author: Jonathan Madsen
24// ---------------------------------------------------------------
25
26#pragma once
27
28#include "PTL/Globals.hh"
29#include "PTL/Threading.hh"
30#include "PTL/Types.hh"
31#include "PTL/VUserTaskQueue.hh"
32
33#include <atomic>
34#include <deque>
35#include <list>
36#include <memory>
37#include <queue>
38#include <random>
39#include <set>
40#include <stack>
41
42namespace PTL
43{
44class VTask;
45class TaskSubQueue; // definition in UserTaskQueue.icc
46
48{
49public:
50 typedef std::shared_ptr<VTask> task_pointer;
51 typedef std::vector<TaskSubQueue*> TaskSubQueueContainer;
52 typedef std::default_random_engine random_engine_t;
53 typedef std::uniform_int_distribution<int> int_dist_t;
54
55public:
56 // Constructor and Destructors
57 UserTaskQueue(intmax_t nworkers = -1, UserTaskQueue* = nullptr);
58 // Virtual destructors are required by abstract classes
59 // so add it by default, just in case
60 virtual ~UserTaskQueue() override;
61
62public:
63 // Virtual function for getting a task from the queue
64 virtual task_pointer GetTask(intmax_t subq = -1, intmax_t nitr = -1) override;
65 // Virtual function for inserting a task into the queue
66 virtual intmax_t InsertTask(task_pointer&&, ThreadData* = nullptr,
67 intmax_t subq = -1) override PTL_NO_SANITIZE_THREAD;
68
69 // if executing only tasks in threads bin
71
72 // Overload this function to hold threads
73 virtual void Wait() override {}
74 virtual void resize(intmax_t) override;
75
76 virtual bool empty() const override;
77 virtual size_type size() const override;
78
79 virtual size_type bin_size(size_type bin) const override;
80 virtual bool bin_empty(size_type bin) const override;
81
82 inline bool true_empty() const override;
83 inline size_type true_size() const override;
84
85 virtual void ExecuteOnAllThreads(ThreadPool* tp, function_type f) override;
86
88 function_type f) override;
89
90 virtual VUserTaskQueue* clone() override;
91
92 virtual intmax_t GetThreadBin() const override;
93
94protected:
95 intmax_t GetInsertBin() const;
96
97private:
98 void AcquireHold();
99 void ReleaseHold();
100
101private:
103 intmax_t m_thread_bin;
104 mutable intmax_t m_insert_bin;
105 std::atomic_bool* m_hold;
106 std::atomic_uintmax_t* m_ntasks;
109 std::vector<int> m_rand_list;
110 std::vector<int>::iterator m_rand_itr;
111};
112
113} // namespace PTL
114
115//======================================================================================//
116
117#include "PTL/UserTaskQueue.icc"
118
119//======================================================================================//
120
121inline bool
123{
124 return (m_ntasks->load(std::memory_order_relaxed) == 0);
125}
126
127//======================================================================================//
128
131{
132 return m_ntasks->load(std::memory_order_relaxed);
133}
134
135//======================================================================================//
136
139{
140 return (*m_subqueues)[bin]->size();
141}
142
143//======================================================================================//
144
145inline bool
147{
148 return (*m_subqueues)[bin]->empty();
149}
150
151//======================================================================================//
152
153inline bool
155{
156 for(const auto& itr : *m_subqueues)
157 if(!itr->empty())
158 return false;
159 return true;
160}
161
162//======================================================================================//
163
166{
167 size_type _n = 0;
168 for(const auto& itr : *m_subqueues)
169 _n += itr->size();
170 return _n;
171}
172
173//======================================================================================//
#define PTL_NO_SANITIZE_THREAD
Definition: Globals.hh:61
virtual void Wait() override
std::default_random_engine random_engine_t
virtual VUserTaskQueue * clone() override
std::atomic_uintmax_t * m_ntasks
std::vector< TaskSubQueue * > TaskSubQueueContainer
virtual size_type size() const override
virtual void resize(intmax_t) override
virtual task_pointer GetTask(intmax_t subq=-1, intmax_t nitr=-1) override
virtual intmax_t GetThreadBin() const override
virtual void ExecuteOnSpecificThreads(ThreadIdSet tid_set, ThreadPool *tp, function_type f) override
intmax_t GetInsertBin() const
UserTaskQueue(intmax_t nworkers=-1, UserTaskQueue *=nullptr)
virtual bool bin_empty(size_type bin) const override
std::atomic_bool * m_hold
virtual void ExecuteOnAllThreads(ThreadPool *tp, function_type f) override
TaskSubQueueContainer * m_subqueues
std::vector< int >::iterator m_rand_itr
virtual intmax_t InsertTask(task_pointer &&, ThreadData *=nullptr, intmax_t subq=-1) override PTL_NO_SANITIZE_THREAD
bool true_empty() const override
virtual bool empty() const override
virtual ~UserTaskQueue() override
virtual size_type bin_size(size_type bin) const override
std::uniform_int_distribution< int > int_dist_t
task_pointer GetThreadBinTask()
std::vector< int > m_rand_list
std::shared_ptr< VTask > task_pointer
size_type true_size() const override
std::set< ThreadId > ThreadIdSet
std::function< void()> function_type
Definition: AutoLock.hh:254
std::mutex Mutex
Definition: Threading.hh:77