Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
VUserTaskQueue.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// Abstract base class for creating a task queue used by
23// ThreadPool
24// ---------------------------------------------------------------
25// Author: Jonathan Madsen
26// ---------------------------------------------------------------
27
28#pragma once
29
30#include "PTL/Globals.hh"
31#include "PTL/Threading.hh"
32#include "PTL/Types.hh"
33
34#include <cstddef>
35#include <set>
36#include <tuple>
37#include <type_traits>
38#include <utility>
39
40namespace PTL
41{
42class VTask;
43class VTaskGroup;
44class ThreadPool;
45class ThreadData;
46
48{
49public:
51 typedef std::atomic<intmax_t> AtomicInt;
52 typedef uintmax_t size_type;
53 typedef std::function<void()> function_type;
54 typedef std::set<ThreadId> ThreadIdSet;
55
56public:
57 // Constructor - accepting the number of workers
58 explicit VUserTaskQueue(intmax_t nworkers = -1);
59 // Virtual destructors are required by abstract classes
60 // so add it by default, just in case
61 virtual ~VUserTaskQueue();
62
63public:
64 // Virtual function for getting a task from the queue
65 // parameters:
66 // 1. int - get from specific sub-queue
67 // 2. int - number of iterations
68 // returns:
69 // VTask* - a task or nullptr
70 virtual task_pointer GetTask(intmax_t subq = -1, intmax_t nitr = -1) = 0;
71
72 // Virtual function for inserting a task into the queue
73 // parameters:
74 // 1. VTask* - task to insert
75 // 2. int - sub-queue to inserting into
76 // return:
77 // int - subqueue inserted into
78 virtual intmax_t InsertTask(task_pointer, ThreadData* = nullptr,
79 intmax_t subq = -1) = 0;
80
81 // Overload this function to hold threads
82 virtual void Wait() = 0;
83 virtual intmax_t GetThreadBin() const = 0;
84
85 virtual void resize(intmax_t) = 0;
86
87 // these are used for stanard checking
88 virtual size_type size() const = 0;
89 virtual bool empty() const = 0;
90
91 virtual size_type bin_size(size_type bin) const = 0;
92 virtual bool bin_empty(size_type bin) const = 0;
93
94 // these are for slower checking, default to returning normal size()/empty
95 virtual size_type true_size() const { return size(); }
96 virtual bool true_empty() const { return empty(); }
97
98 // a method of executing a specific function on all threads
100
102 function_type f) = 0;
103
104 intmax_t workers() const { return m_workers; }
105
106 virtual VUserTaskQueue* clone() = 0;
107
108 // operator for number of tasks
109 // prefix versions
110 // virtual uintmax_t operator++() = 0;
111 // virtual uintmax_t operator--() = 0;
112 // postfix versions
113 // virtual uintmax_t operator++(int) = 0;
114 // virtual uintmax_t operator--(int) = 0;
115
116public:
117 template <typename ContainerT, size_t... Idx>
118 static auto ContainerToTupleImpl(ContainerT&& container, mpl::index_sequence<Idx...>)
119 -> decltype(std::make_tuple(std::forward<ContainerT>(container)[Idx]...))
120 {
121 return std::make_tuple(std::forward<ContainerT>(container)[Idx]...);
122 }
123
124 template <std::size_t N, typename ContainerT>
125 static auto ContainerToTuple(ContainerT&& container)
126 -> decltype(ContainerToTupleImpl(std::forward<ContainerT>(container),
128 {
129 return ContainerToTupleImpl(std::forward<ContainerT>(container),
131 }
132
133 template <std::size_t N, std::size_t Nt, typename TupleT,
134 enable_if_t<(N == Nt), int> = 0>
135 static void TExecutor(TupleT&& _t)
136 {
137 if(std::get<N>(_t).get())
138 (*(std::get<N>(_t)))();
139 }
140
141 template <std::size_t N, std::size_t Nt, typename TupleT,
142 enable_if_t<(N < Nt), int> = 0>
143 static void TExecutor(TupleT&& _t)
144 {
145 if(std::get<N>(_t).get())
146 (*(std::get<N>(_t)))();
147 TExecutor<N + 1, Nt, TupleT>(std::forward<TupleT>(_t));
148 }
149
150 template <typename TupleT, std::size_t N = std::tuple_size<decay_t<TupleT>>::value>
151 static void Executor(TupleT&& __t)
152 {
153 TExecutor<0, N - 1, TupleT>(std::forward<TupleT>(__t));
154 }
155
156 template <typename Container,
157 typename std::enable_if<std::is_same<Container, task_pointer>::value,
158 int>::type = 0>
159 static void Execute(Container& obj)
160 {
161 if(obj.get())
162 (*obj)();
163 }
164
165 template <typename Container,
166 typename std::enable_if<!std::is_same<Container, task_pointer>::value,
167 int>::type = 0>
168 static void Execute(Container& tasks)
169 {
170 /*
171 for(auto& itr : tasks)
172 {
173 if(itr.get())
174 (*itr)();
175 }*/
176
177 size_type n = tasks.size();
178 size_type max_n = 4;
179 while(n > 0)
180 {
181 auto compute = (n > max_n) ? max_n : n;
182 switch(compute)
183 {
184 case 4: {
185 auto t = ContainerToTuple<4>(tasks);
186 Executor(t);
187 break;
188 }
189 case 3: {
190 auto t = ContainerToTuple<3>(tasks);
191 Executor(t);
192 break;
193 }
194 case 2: {
195 auto t = ContainerToTuple<2>(tasks);
196 Executor(t);
197 break;
198 }
199 case 1: {
200 auto t = ContainerToTuple<1>(tasks);
201 Executor(t);
202 break;
203 }
204 case 0: break;
205 }
206 // tasks.erase(tasks.begin(), tasks.begin() + compute);
207 n -= compute;
208 }
209 }
210
211protected:
212 intmax_t m_workers = 0;
213};
214
215} // namespace PTL
VTask is the abstract class stored in thread_pool.
Definition: VTask.hh:55
virtual void ExecuteOnSpecificThreads(ThreadIdSet tid_set, ThreadPool *tp, function_type f)=0
virtual task_pointer GetTask(intmax_t subq=-1, intmax_t nitr=-1)=0
virtual size_type true_size() const
static void TExecutor(TupleT &&_t)
virtual intmax_t InsertTask(task_pointer, ThreadData *=nullptr, intmax_t subq=-1)=0
virtual size_type bin_size(size_type bin) const =0
std::atomic< intmax_t > AtomicInt
static auto ContainerToTupleImpl(ContainerT &&container, mpl::index_sequence< Idx... >) -> decltype(std::make_tuple(std::forward< ContainerT >(container)[Idx]...))
virtual void ExecuteOnAllThreads(ThreadPool *tp, function_type f)=0
intmax_t workers() const
virtual bool empty() const =0
virtual void resize(intmax_t)=0
static auto ContainerToTuple(ContainerT &&container) -> decltype(ContainerToTupleImpl(std::forward< ContainerT >(container), mpl::make_index_sequence< N >{}))
static void Execute(Container &obj)
static void Execute(Container &tasks)
static void Executor(TupleT &&__t)
virtual VUserTaskQueue * clone()=0
std::set< ThreadId > ThreadIdSet
virtual void Wait()=0
virtual size_type size() const =0
virtual intmax_t GetThreadBin() const =0
virtual bool bin_empty(size_type bin) const =0
std::function< void()> function_type
virtual bool true_empty() const
impl::make_integer_sequence< size_t, NumT > make_index_sequence
Alias template make_index_sequence.
Definition: Globals.hh:165
Definition: AutoLock.hh:254
auto get(Tuple< Elements... > &t) -> decltype(get_height< sizeof...(Elements) - I - 1 >(t))
Definition: Tuple.hh:139
typename std::enable_if< B, T >::type enable_if_t
Definition: Globals.hh:55
Class template integer_sequence.
Definition: Globals.hh:110