Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
VTaskGroup.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 file
21//
22// Class Description:
23//
24// This file creates an abstract base class for the grouping the thread-pool
25// tasking system into independently joinable units
26//
27// ---------------------------------------------------------------
28// Author: Jonathan Madsen (Feb 13th 2018)
29// ---------------------------------------------------------------
30
31#pragma once
32
33#include "PTL/AutoLock.hh"
34#include "PTL/Threading.hh"
35#include "PTL/VTask.hh"
36
37#include <atomic>
38#include <cstdint>
39#include <cstdlib>
40#include <iostream>
41
42#include <deque>
43#include <list>
44#include <map>
45#include <memory>
46#include <unordered_map>
47#include <vector>
48
49namespace PTL
50{
51class ThreadPool;
52
54{
55public:
56 template <typename Tp>
57 using container_type = std::vector<Tp>;
58 template <typename Tp>
59 using list_type = std::vector<Tp>;
60
62 typedef std::thread::id tid_type;
64 typedef uintmax_t size_type;
65 typedef Mutex lock_t;
66 typedef std::atomic_intmax_t atomic_int;
67 typedef std::atomic_uintmax_t atomic_uint;
71
72public:
73 // Constructor and Destructors
74 explicit VTaskGroup(ThreadPool* tp = nullptr);
75 // Virtual destructors are required by abstract classes
76 // so add it by default, just in case
77 virtual ~VTaskGroup();
78
79 VTaskGroup(const this_type&) = delete;
80 VTaskGroup(this_type&& rhs) = default;
81
82 this_type& operator=(const this_type&) = delete;
83 this_type& operator=(this_type&& rhs) = default;
84
85public:
86 //------------------------------------------------------------------------//
87 // wait to finish
88 virtual void wait();
89
90 //------------------------------------------------------------------------//
91 // increment (prefix)
92 intmax_t operator++() { return ++(*m_tot_task_count); }
93 intmax_t operator++(int) { return (*m_tot_task_count)++; }
94 //------------------------------------------------------------------------//
95 // increment (prefix)
96 intmax_t operator--() { return --(*m_tot_task_count); }
97 intmax_t operator--(int) { return (*m_tot_task_count)--; }
98 //------------------------------------------------------------------------//
99 // size
100 intmax_t size() const { return m_tot_task_count->load(); }
101
102 // get the locks/conditions
103 std::shared_ptr<condition_t> task_cond() { return m_task_cond; }
104
105 // identifier
106 const uintmax_t& id() const { return m_id; }
107
108 // thread pool
109 void set_pool(ThreadPool* tp) { m_pool = tp; }
110 ThreadPool*& pool() { return m_pool; }
111 ThreadPool* pool() const { return m_pool; }
112
113 void clear();
114 virtual bool is_native_task_group() const { return true; }
115 virtual bool is_master() const { return this_tid() == m_main_tid; }
116
117 //------------------------------------------------------------------------//
118 // check if any tasks are still pending
119 virtual intmax_t pending() { return m_tot_task_count->load(); }
120
121 static void set_verbose(int level) { f_verbose = level; }
122
123protected:
124 //------------------------------------------------------------------------//
125 // get the thread id
126 static tid_type this_tid() { return std::this_thread::get_id(); }
127
128 //------------------------------------------------------------------------//
129 // get the task count
131 const atomic_int& task_count() const { return *m_tot_task_count; }
132
133protected:
134 // Private variables
135 uintmax_t m_id;
137 std::shared_ptr<atomic_int> m_tot_task_count = std::make_shared<atomic_int>(0);
138 std::shared_ptr<condition_t> m_task_cond = std::make_shared<condition_t>();
139 std::shared_ptr<lock_t> m_task_lock = std::make_shared<lock_t>();
142 static int f_verbose;
143};
144
145inline void
147{
148 for(auto& itr : vtask_list)
149 delete itr;
150 vtask_list.clear();
151}
152
153//--------------------------------------------------------------------------------------//
154
155} // namespace PTL
const atomic_int & task_count() const
Definition: VTaskGroup.hh:131
std::atomic_uintmax_t atomic_uint
Definition: VTaskGroup.hh:67
virtual bool is_master() const
Definition: VTaskGroup.hh:115
std::atomic_intmax_t atomic_int
Definition: VTaskGroup.hh:66
intmax_t operator--()
Definition: VTaskGroup.hh:96
task_type * task_pointer
Definition: VTaskGroup.hh:69
std::shared_ptr< atomic_int > m_tot_task_count
Definition: VTaskGroup.hh:137
const uintmax_t & id() const
Definition: VTaskGroup.hh:106
void set_pool(ThreadPool *tp)
Definition: VTaskGroup.hh:109
atomic_int & task_count()
Definition: VTaskGroup.hh:130
std::thread::id tid_type
Definition: VTaskGroup.hh:62
VTaskGroup(this_type &&rhs)=default
vtask_list_type vtask_list
Definition: VTaskGroup.hh:141
virtual ~VTaskGroup()
Definition: VTaskGroup.cc:76
std::vector< Tp > container_type
Definition: VTaskGroup.hh:57
ThreadPool * m_pool
Definition: VTaskGroup.hh:136
intmax_t operator++(int)
Definition: VTaskGroup.hh:93
uintmax_t size_type
Definition: VTaskGroup.hh:64
std::shared_ptr< condition_t > m_task_cond
Definition: VTaskGroup.hh:138
VTaskGroup(const this_type &)=delete
ThreadPool * pool() const
Definition: VTaskGroup.hh:111
virtual void wait()
Definition: VTaskGroup.cc:81
virtual intmax_t pending()
Definition: VTaskGroup.hh:119
VTaskGroup this_type
Definition: VTaskGroup.hh:61
Condition condition_t
Definition: VTaskGroup.hh:68
std::vector< Tp > list_type
Definition: VTaskGroup.hh:59
intmax_t operator++()
Definition: VTaskGroup.hh:92
uintmax_t m_id
Definition: VTaskGroup.hh:135
this_type & operator=(const this_type &)=delete
static void set_verbose(int level)
Definition: VTaskGroup.hh:121
tid_type m_main_tid
Definition: VTaskGroup.hh:140
static int f_verbose
Definition: VTaskGroup.hh:142
virtual bool is_native_task_group() const
Definition: VTaskGroup.hh:114
std::shared_ptr< lock_t > m_task_lock
Definition: VTaskGroup.hh:139
container_type< task_pointer > vtask_list_type
Definition: VTaskGroup.hh:70
this_type & operator=(this_type &&rhs)=default
static tid_type this_tid()
Definition: VTaskGroup.hh:126
intmax_t operator--(int)
Definition: VTaskGroup.hh:97
intmax_t size() const
Definition: VTaskGroup.hh:100
ThreadPool *& pool()
Definition: VTaskGroup.hh:110
std::shared_ptr< condition_t > task_cond()
Definition: VTaskGroup.hh:103
VTask is the abstract class stored in thread_pool.
Definition: VTask.hh:55
Definition: AutoLock.hh:254
std::condition_variable Condition
Definition: Threading.hh:189
std::mutex Mutex
Definition: Threading.hh:76