-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_f.hpp
329 lines (230 loc) · 6.04 KB
/
parallel_f.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// === (C) 2020-2024 === parallel_f (tasks, queues, lists in parallel threads)
// Written by Denis Oliver Kropp <[email protected]>
#pragma once
#include <any>
#include <map>
#include <memory>
#include <mutex>
#include <queue>
#include <tuple>
#include "log.hpp"
#include "stats.hpp"
#include "system.hpp"
#include "vthread.hpp"
#include "Event.hxx"
#include "task.hpp"
#include "task_node.hpp"
#include "joinable.hpp"
namespace parallel_f {
static const std::any none;
/**
* Generates a task object using the provided callable and arguments.
*
* @param callable The function or callable object to be executed by the task.
* @param args The arguments to be passed to the callable.
*
* @return A shared pointer to a task object that wraps the callable and arguments.
*
* @throws None
*/
template <typename Callable, typename... Args>
auto make_task(Callable callable, Args... args)
{
return std::shared_ptr<task<Callable, Args...>> (new task<Callable,Args...>(callable, std::forward<Args>(args)...));
}
// parallel_f :: task_queue == implementation
class task_queue
{
private:
std::shared_ptr<task_node> first;
std::shared_ptr<task_node> last;
std::mutex mutex;
public:
task_queue() {}
void push(std::shared_ptr<task_base> task)
{
LOG_DEBUG("task_queue::push()\n");
std::unique_lock<std::mutex> lock(mutex);
if (first) {
auto node = std::make_shared<task_node>("task", task, 1);
last->add_to_notify(node);
last = node;
}
else {
auto node = std::make_shared<task_node>("first", task, 1);
first = node;
last = node;
}
}
joinable exec(bool detached = false)
{
LOG_DEBUG("task_queue::exec(%s)\n", detached ? "true" : "false");
std::unique_lock<std::mutex> lock(mutex);
auto f = first;
auto l = last;
first.reset();
last.reset();
lock.unlock();
f->notify();
if (!detached) {
l->join();
return joinable();
}
return joinable([f,l]() {
l->join();
});
}
};
class task_queue_simple : public lli::EventListener
{
private:
std::vector<std::shared_ptr<task_base>> tasks;
std::mutex lock;
public:
task_queue_simple() {}
void push(std::shared_ptr<task_base> task)
{
LOG_DEBUG("task_queue_simple::push()\n");
std::unique_lock<std::mutex> l(lock);
tasks.push_back(task);
}
void exec()
{
LOG_DEBUG("task_queue_simple::exec()\n");
std::unique_lock<std::mutex> l(lock);
std::vector<std::shared_ptr<task_base>> q = tasks;
tasks.clear();
l.unlock();
run(q);
}
private:
void run(std::vector<std::shared_ptr<task_base>>& q)
{
for (auto t : q) {
if (!t->finish()) {
std::mutex lock;
std::condition_variable cond;
bool finished = false;
std::unique_lock<std::mutex> l(lock);
t->finished.Attach(this, [&lock,&cond,&finished](int) {
std::unique_lock<std::mutex> l(lock);
finished = true;
cond.notify_one();
});
while (!finished && t->get_state() != task_base::task_state::FINISHED)
cond.wait(l);
}
}
}
};
// parallel_f :: task_list == implementation
typedef unsigned long long task_id;
class task_list
{
private:
task_id ids;
std::map<task_id,std::shared_ptr<task_node>> nodes;
std::mutex mutex;
std::shared_ptr<task_node> flush_join;
public:
task_list()
:
ids(0),
flush_join(0)
{
LOG_DEBUG("task_list::task_list(%p)\n", this);
}
~task_list()
{
LOG_DEBUG("task_list::~task_list(%p)\n", this);
}
task_id append(std::shared_ptr<task_base> task)
{
LOG_DEBUG("task_list::append( no dependencies )\n");
std::unique_lock<std::mutex> lock(mutex);
task_id id = ++ids;
nodes[id] = std::make_shared<task_node>("task", task, 1);
return id;
}
template <typename ...Deps>
task_id append(std::shared_ptr<task_base> task, Deps... deps)
{
LOG_DEBUG("task_list::append( %d dependencies )\n", sizeof...(deps));
std::unique_lock<std::mutex> lock(mutex);
task_id id = ++ids;
std::shared_ptr<task_node> node = std::make_shared<task_node>("task", task, (unsigned int)(1 + sizeof...(deps)));
std::list<task_id> deps_list({ deps... });
for (auto li : deps_list) {
LOG_DEBUG("task_list::append() <- id %llu\n", li);
auto dep = nodes.find(li);
if (dep != nodes.end())
(*dep).second->add_to_notify(node);
else
node->notify();
}
nodes[id] = node;
return id;
}
joinable finish(bool detached = false)
{
LOG_DEBUG("task_list::finish(%s)\n", detached ? "true" : "false");
std::unique_lock<std::mutex> lock(mutex);
if (flush_join) {
LOG_DEBUG("task_list::finish() joining previous flush...\n");
flush_join->join();
LOG_DEBUG("task_list::finish() joined previous flush.\n");
}
for (auto node : nodes) {
if (node.second != flush_join)
node.second->notify();
}
flush_join.reset();
if (!detached) {
for (auto node : nodes)
node.second->join();
nodes.clear();
return joinable();
}
auto n = nodes;
nodes.clear();
return joinable([n]() {
for (auto node : n)
node.second->join();
});
}
task_id flush()
{
LOG_DEBUG("task_list::flush()...\n");
std::unique_lock<std::mutex> lock(mutex);
std::shared_ptr<task_node> prev_flush_join = flush_join;
if (flush_join)
flush_join = std::make_shared<task_node>("flush", make_task([]() {}), (unsigned int)(1 + nodes.size() - 1));
else
flush_join = std::make_shared<task_node>("flush", make_task([]() {}), (unsigned int)(1 + nodes.size()));
if (prev_flush_join) {
LOG_DEBUG("task_list::flush() joining previous flush...\n");
prev_flush_join->join();
LOG_DEBUG("task_list::flush() joined previous flush.\n");
}
LOG_DEBUG("task_list::flush() flushing (notify) nodes...\n");
for (auto node : nodes) {
if (node.second != prev_flush_join) {
node.second->add_to_notify(flush_join);
node.second->notify();
}
}
LOG_DEBUG("task_list::flush() clearing nodes...\n");
nodes.clear();
LOG_DEBUG("task_list::flush() clearing nodes done.\n");
task_id flush_id = ++ids;
nodes[flush_id] = flush_join;
flush_join->notify();
LOG_DEBUG("task_list::flush() done.\n");
return flush_id;
}
size_t length() const
{
return nodes.size();
}
};
}