Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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/TaskSubQueue.hh"
30#include "PTL/Threading.hh"
31#include "PTL/VTask.hh"
32#include "PTL/VUserTaskQueue.hh"
33
34#include <atomic>
35#include <cstdint>
36#include <memory>
37#include <random>
38#include <vector>
39
40namespace PTL
41{
43{
44public:
45 using task_pointer = std::shared_ptr<VTask>;
46 using TaskSubQueueContainer = std::vector<TaskSubQueue*>;
47 using random_engine_t = std::default_random_engine;
48 using int_dist_t = std::uniform_int_distribution<int>;
49
50public:
51 // Constructor and Destructors
52 UserTaskQueue(intmax_t nworkers = -1, UserTaskQueue* = nullptr);
53 // Virtual destructors are required by abstract classes
54 // so add it by default, just in case
55 ~UserTaskQueue() override;
56
57public:
58 // Virtual function for getting a task from the queue
59 task_pointer GetTask(intmax_t subq = -1, intmax_t nitr = -1) override;
60 // Virtual function for inserting a task into the queue
61 intmax_t InsertTask(task_pointer&&, ThreadData* = nullptr,
62 intmax_t subq = -1) override PTL_NO_SANITIZE_THREAD;
63
64 // if executing only tasks in threads bin
66
67 // Overload this function to hold threads
68 void Wait() override {}
69 void resize(intmax_t) override;
70
71 bool empty() const override;
72 size_type size() const override;
73
74 size_type bin_size(size_type bin) const override;
75 bool bin_empty(size_type bin) const override;
76
77 bool true_empty() const override;
78 size_type true_size() const override;
79
80 void ExecuteOnAllThreads(ThreadPool* tp, function_type f) override;
81
83 function_type f) override;
84
85 VUserTaskQueue* clone() override;
86
87 intmax_t GetThreadBin() const override;
88
89protected:
90 intmax_t GetInsertBin() const;
91
92private:
93 void AcquireHold();
94 void ReleaseHold();
95
96private:
97 bool m_is_clone;
98 intmax_t m_thread_bin;
99 mutable intmax_t m_insert_bin;
100 std::atomic_bool* m_hold = nullptr;
101 std::atomic_uintmax_t* m_ntasks = nullptr;
102 Mutex* m_mutex = nullptr;
103 TaskSubQueueContainer* m_subqueues = nullptr;
104 std::vector<int> m_rand_list = {};
105 std::vector<int>::iterator m_rand_itr = {};
106};
107
108//======================================================================================//
109
110inline bool
112{
113 return (m_ntasks->load(std::memory_order_relaxed) == 0);
114}
115
116//======================================================================================//
117
120{
121 return m_ntasks->load(std::memory_order_relaxed);
122}
123
124//======================================================================================//
125
128{
129 return (*m_subqueues)[bin]->size();
130}
131
132//======================================================================================//
133
134inline bool
136{
137 return (*m_subqueues)[bin]->empty();
138}
139
140//======================================================================================//
141
142inline bool
144{
145 for(const auto& itr : *m_subqueues)
146 if(!itr->empty())
147 return false;
148 return true;
149}
150
151//======================================================================================//
152
155{
156 size_type _n = 0;
157 for(const auto& itr : *m_subqueues)
158 _n += itr->size();
159 return _n;
160}
161
162//======================================================================================//
163} // namespace PTL
#define PTL_NO_SANITIZE_THREAD
Definition Globals.hh:47
VUserTaskQueue * clone() override
size_type true_size() const override
void resize(intmax_t) override
void Wait() override
task_pointer GetTask(intmax_t subq=-1, intmax_t nitr=-1) override
intmax_t GetThreadBin() const override
void ExecuteOnSpecificThreads(ThreadIdSet tid_set, ThreadPool *tp, function_type f) override
intmax_t GetInsertBin() const
std::vector< TaskSubQueue * > TaskSubQueueContainer
UserTaskQueue(intmax_t nworkers=-1, UserTaskQueue *=nullptr)
bool bin_empty(size_type bin) const override
void ExecuteOnAllThreads(ThreadPool *tp, function_type f) override
std::default_random_engine random_engine_t
intmax_t InsertTask(task_pointer &&, ThreadData *=nullptr, intmax_t subq=-1) override PTL_NO_SANITIZE_THREAD
size_type bin_size(size_type bin) const override
bool true_empty() const override
std::shared_ptr< VTask > task_pointer
bool empty() const override
~UserTaskQueue() override
std::uniform_int_distribution< int > int_dist_t
task_pointer GetThreadBinTask()
size_type size() const override
std::set< ThreadId > ThreadIdSet
std::function< void()> function_type
std::mutex Mutex
Definition Threading.hh:57