-
Notifications
You must be signed in to change notification settings - Fork 707
/
thread_management.h
1453 lines (1329 loc) · 55.4 KB
/
thread_management.h
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2000 - 2023 by the deal.II authors
//
// This file is part of the deal.II library.
//
// Part of the source code is dual licensed under Apache-2.0 WITH
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
// governing the source code and code contributions can be found in
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
//
// ------------------------------------------------------------------------
#ifndef dealii_thread_management_h
#define dealii_thread_management_h
#include <deal.II/base/config.h>
#include <deal.II/base/exceptions.h>
#include <deal.II/base/multithread_info.h>
#include <deal.II/base/mutex.h>
#include <deal.II/base/template_constraints.h>
#ifdef DEAL_II_WITH_TASKFLOW
# include <taskflow/taskflow.hpp>
#endif
#include <atomic>
#include <functional>
#include <future>
#include <list>
#include <memory>
#include <thread>
#include <tuple>
#include <utility>
#include <vector>
#ifdef DEAL_II_HAVE_CXX20
# include <concepts>
#endif
#ifdef DEAL_II_WITH_TBB
# include <tbb/task_group.h>
#endif
DEAL_II_NAMESPACE_OPEN
/**
* @addtogroup threads
* @{
*/
/**
* A namespace for the implementation of thread management in deal.II. Most of
* the content of this namespace is discussed in detail in one of the reports
* linked to from the documentation page of deal.II.
*
* @ingroup threads
*/
namespace Threads
{
/**
* Split the range <code>[begin,end)</code> into <code>n_intervals</code>
* subintervals of equal size. The last interval will be a little bit
* larger, if the number of elements in the whole range is not exactly
* divisible by <code>n_intervals</code>. The type of the iterators has to
* fulfill the requirements of a forward iterator, i.e.
* <code>operator++</code> must be available, and of course it must be
* assignable.
*
* A list of subintervals is returned as a vector of pairs of iterators,
* where each pair denotes the range <code>[begin[i],end[i])</code>.
*
* @ingroup threads
*/
template <typename ForwardIterator>
std::vector<std::pair<ForwardIterator, ForwardIterator>>
split_range(const ForwardIterator &begin,
const ForwardIterator &end,
const unsigned int n_intervals);
/**
* Split the interval <code>[begin,end)</code> into subintervals of (almost)
* equal size. This function works mostly as the one before, with the
* difference that instead of iterators, now values are taken that define
* the whole interval.
*
* @ingroup threads
*/
std::vector<std::pair<unsigned int, unsigned int>>
split_interval(const unsigned int begin,
const unsigned int end,
const unsigned int n_intervals);
/**
* @cond internal
*/
/**
* A namespace in which helper functions and the like for the threading
* subsystem are implemented. The members of this namespace are not meant
* for public use.
*/
namespace internal
{
/**
* @internal
*
* If in a sub-thread an exception is thrown, it is not propagated to the
* main thread. Therefore, the exception handler that is provided by the
* applications main function or some of its other parts will not be able
* to catch these exceptions. Therefore, we have to provide an exception
* handler in the top function of each sub-thread that at least catches
* the exception and prints some information, rather than letting the
* operating system to just kill the program without a message. In each of
* the functions we use as entry points to new threads, we therefore
* install a try-catch block, and if an exception of type
* <code>std::exception</code> is caught, it passes over control to this
* function, which will then provide some output.
*/
[[noreturn]] void
handle_std_exception(const std::exception &exc);
/**
* @internal
*
* Same as above, but the type of the exception is not derived from
* <code>std::exception</code>, so there is little way to provide
* something more useful.
*/
[[noreturn]] void
handle_unknown_exception();
} // namespace internal
/**
* @endcond
*/
} // namespace Threads
/* ----------- implementation of functions in namespace Threads ---------- */
#ifndef DOXYGEN
namespace Threads
{
template <typename ForwardIterator>
std::vector<std::pair<ForwardIterator, ForwardIterator>>
split_range(const ForwardIterator &begin,
const ForwardIterator &end,
const unsigned int n_intervals)
{
using IteratorPair = std::pair<ForwardIterator, ForwardIterator>;
// in non-multithreaded mode, we often have the case that this
// function is called with n_intervals==1, so have a shortcut here
// to handle that case efficiently
if (n_intervals == 1)
return (std::vector<IteratorPair>(1, IteratorPair(begin, end)));
// if more than one interval requested, do the full work
const unsigned int n_elements = std::distance(begin, end);
const unsigned int n_elements_per_interval = n_elements / n_intervals;
const unsigned int residual = n_elements % n_intervals;
std::vector<IteratorPair> return_values(n_intervals);
return_values[0].first = begin;
for (unsigned int i = 0; i < n_intervals; ++i)
{
if (i != n_intervals - 1)
{
return_values[i].second = return_values[i].first;
// note: the cast is performed to avoid a warning of gcc
// that in the library `dist>=0' is checked (dist has a
// template type, which here is unsigned if no cast is
// performed)
std::advance(return_values[i].second,
static_cast<signed int>(n_elements_per_interval));
// distribute residual in division equally among the first
// few subintervals
if (i < residual)
++return_values[i].second;
return_values[i + 1].first = return_values[i].second;
}
else
return_values[i].second = end;
}
return return_values;
}
} // namespace Threads
#endif // DOXYGEN
namespace Threads
{
namespace internal
{
/**
* @internal
*
* Given an arbitrary type RT, store an element of it and grant
* access to it through functions get() and set(). There are
* specializations for reference types (which need to be stored as
* pointers to the object being referenced), and for type void.
*
* This function is not dissimilar to the `std::promise`/`std::future`
* combination of classes. The difference is that a `std::promise`
* can only be read once via `std::future::get()` (presumably this
* design is due to the fact that `std::future::get()` can throw
* an exception previously stored in the `std::promise`). On
* the other hand, this class makes the result available for
* as many times as desired. It also doesn't store any exceptions
* (though they will be forwarded by the classes using the current
* class).
*/
template <typename RT>
struct return_value
{
private:
RT value;
bool value_is_initialized;
public:
using reference_type = RT &;
inline return_value()
: value()
, value_is_initialized(false)
{}
inline reference_type
get()
{
Assert(
value_is_initialized,
ExcMessage(
"You cannot read the return value of a thread or task "
"if that value has not been set. This happens, for example, if "
"a task or thread threw an exception."));
return value;
}
inline void
set(RT &&v)
{
value = std::move(v);
}
/**
* Set the value from the given `std::future` object. If the future
* object holds an exception, the set will not happen and this function
* instead throws the exception stored in the future object.
*/
inline void
set_from(std::future<RT> &v)
{
// Get the value from the std::future object. If the future holds
// an exception, then the assignment fails, we exit the function via the
// exception right away, and value_is_initialized is not set to true --
// that's something we can check later on.
value = std::move(v.get());
value_is_initialized = true;
}
};
/**
* @internal
*
* Given an arbitrary type RT, store an element of it and grant access to
* it through functions get() and set(). This is the specialization for
* reference types: since references cannot be set after construction time,
* we store a pointer instead, which holds the address of the object being
* referenced.
*
* This function is not dissimilar to the `std::promise`/`std::future`
* combination of classes. The difference is that a `std::promise`
* can only be read once via `std::future::get()` (presumably this
* design is due to the fact that `std::future::get()` can throw
* an exception previously stored in the `std::promise`). On
* the other hand, this class makes the result available for
* as many times as desired. It also doesn't store any exceptions
* (though they will be forwarded by the classes using the current
* class).
*/
template <typename RT>
struct return_value<RT &>
{
private:
RT *value;
bool value_is_initialized;
public:
using reference_type = RT &;
inline return_value()
: value(nullptr)
, value_is_initialized(false)
{}
inline reference_type
get() const
{
Assert(
value_is_initialized,
ExcMessage(
"You cannot read the return value of a thread or task "
"if that value has not been set. This happens, for example, if "
"a task or thread threw an exception."));
return *value;
}
inline void
set(RT &v)
{
value = &v;
}
/**
* Set the value from the given `std::future` object. If the future
* object holds an exception, the set will not happen and this function
* instead throws the exception stored in the future object.
*/
inline void
set_from(std::future<RT &> &v)
{
// Get the value from the std::future object. If the future holds
// an exception, then the assignment fails, we exit the function via the
// exception right away, and value_is_initialized is not set to true --
// that's something we can check later on.
value = &v.get();
value_is_initialized = true;
}
};
/**
* @internal
*
* Given an arbitrary type RT, store an element of it and grant access to
* it through functions get() and set(). This is the specialization for
* type void: there is obviously nothing to store, so no function set(),
* and a function get() that returns void.
*
* This function is not dissimilar to the `std::promise`/`std::future`
* combination of classes. The difference is that a `std::promise`
* can only be read once via `std::future::get()` (presumably this
* design is due to the fact that `std::future::get()` can throw
* an exception previously stored in the `std::promise`). On
* the other hand, this class makes the result available for
* as many times as desired. It also doesn't store any exceptions
* (though they will be forwarded by the classes using the current
* class).
*/
template <>
struct return_value<void>
{
using reference_type = void;
static inline void
get()
{}
/**
* This function does nothing, because the `std::future` object
* does not actually hold a return value. However, if the future
* object holds an exception, the set will not happen and this function
* instead throws the exception stored in the future object.
*/
inline void
set_from(std::future<void> &)
{}
};
} // namespace internal
namespace internal
{
/**
* A general template that returns std::ref(t) if t is of reference
* type, and t otherwise.
*
* The case that t is of reference type is handled in a partial
* specialization declared below.
*/
template <typename T>
struct maybe_make_ref
{
static T
act(T &t)
{
return t;
}
};
/**
* A general template that returns std::ref(t) if t is of reference
* type, and t otherwise.
*
* The case that t is of reference type is handled in this partial
* specialization.
*/
template <typename T>
struct maybe_make_ref<T &>
{
static std::reference_wrapper<T>
act(T &t)
{
return std::ref(t);
}
};
/**
* Set the value of a std::promise object by evaluating the action.
*
* @dealiiConceptRequires{(std::invocable<Function> &&
* std::convertible_to<std::invoke_result_t<Function>, RT>)}
*/
template <typename RT, typename Function>
DEAL_II_CXX20_REQUIRES(
(std::invocable<Function> &&
std::convertible_to<std::invoke_result_t<Function>, RT>))
void evaluate_and_set_promise(Function &function, std::promise<RT> &promise)
{
promise.set_value(function());
}
/**
* Set the value of a std::promise object by evaluating the
* action. This function is a specialization of the previous one
* for the case where the return type is `void`. Consequently, we
* can't set a value. But we do evaluate the function object and
* call `std::promise::set_value()` without argument.
*
* @dealiiConceptRequires{(std::invocable<Function>)}
*/
template <typename Function>
DEAL_II_CXX20_REQUIRES((std::invocable<Function>))
void evaluate_and_set_promise(Function &function,
std::promise<void> &promise)
{
function();
promise.set_value();
}
} // namespace internal
/**
* This class describes a task object, i.e., what one obtains by calling
* Threads::new_task(). The idea is that Threads::new_task() allows one to run
* a function whenever the C++ run-time system finds it convenient --
* typically, when there is an idle processor available. This can be used to
* run things in the background when there is no immediate need for the
* result, or if there are other things that could well be done in parallel.
* Whenever the result of that background task is needed, one can call either
* join() to just wait for the task to finish, or return_value() to obtain the
* value that was returned by the function that was run on that background
* task.
*
* This class is conceptually similar to the
* [`std::future`](https://en.cppreference.com/w/cpp/thread/future) class that
* is returned by
* [`std::async`](https://en.cppreference.com/w/cpp/thread/async) (which is
* itself similar to what Threads::new_task() does). The principal conceptual
* difference is that one can only call `std::future::get()` once, whereas one
* can call Threads::Task::return_value() as many times as desired. It is,
* thus, comparable to the
* [`std::shared_future`](https://en.cppreference.com/w/cpp/thread/shared_future)
* class. However, `std::shared_future` can not be used for types that can not
* be copied -- a particular restriction for `std::unique_ptr`, for example.
*
* @ingroup threads
*/
template <typename RT = void>
class Task
{
public:
/**
* Construct a task object, given a function object to execute on
* the task, and then schedule this function for
* execution. However, when MultithreadInfo::n_threads() returns
* 1, i.e., if the deal.II runtime system has been configured to
* only use one thread, then just execute the given function
* object.
*
* @post Using this constructor automatically makes the task object
* joinable().
*/
Task(const std::function<RT()> &function_object)
{
if (MultithreadInfo::n_threads() > 1)
{
#ifdef DEAL_II_WITH_TASKFLOW
task_data = std::make_shared<TaskData>(
MultithreadInfo::get_taskflow_executor().async(function_object));
#elif defined(DEAL_II_WITH_TBB)
// Create a promise object and from it extract a future that
// we can use to refer to the outcome of the task. For reasons
// explained below, we can't just create a std::promise object,
// but have to make do with a pointer to such an object.
std::unique_ptr<std::promise<RT>> promise =
std::make_unique<std::promise<RT>>();
task_data =
std::make_shared<TaskData>(std::move(promise->get_future()));
// Then start the task, using a task_group object (for just this one
// task) that is associated with the TaskData object. Note that we
// have to *copy* the function object being executed so that it is
// guaranteed to live on the called thread as well -- the copying is
// facilitated by capturing the 'function_object' variable by value.
//
// We also have to *move* the promise object into the new task's
// memory space because promises can not be copied and we can't refer
// to it by reference because it's a local variable of the current
// (surrounding) function that may go out of scope before the promise
// is ultimately set. This leads to a conundrum: if we had just
// declared 'promise' as an object of type std::promise, then we could
// capture it in the lambda function via
// [..., promise=std::move(promise)]() {...}
// and set the promise in the body of the lambda. But setting a
// promise is a non-const operation on the promise, and so we would
// actually have to declare the lambda function as 'mutable' because
// by default, lambda captures are 'const'. That is, we would have
// to write
// [..., promise=std::move(promise)]() mutable {...}
// But this leads to other problems: It turns out that the
// tbb::task_group::run() function cannot take mutable lambdas as
// argument :-(
//
// We work around this issue by not declaring the 'promise' variable
// as an object of type std::promise, but as a pointer to such an
// object. This pointer we can move, and the *pointer* itself can
// be 'const' (meaning we can leave the lambda as non-mutable)
// even though we modify the object *pointed to*. One would think
// that a std::unique_ptr would be the right choice for this, but
// that's not true: the resulting lambda function can then be
// non-mutable, but the lambda function object is not copyable
// and at least some TBB variants require that as well. So
// instead we move the std::unique_ptr used above into a
// std::shared_ptr to be stored within the lambda function object.
task_data->task_group->run(
[function_object,
promise =
std::shared_ptr<std::promise<RT>>(std::move(promise))]() {
try
{
internal::evaluate_and_set_promise(function_object, *promise);
}
catch (...)
{
try
{
// store anything thrown in the promise
promise->set_exception(std::current_exception());
}
catch (...)
{
// set_exception() may throw too. But ignore this on
// the task.
}
}
});
#else
// If no threading library is supported, just fall back onto C++11
// facilities. The problem with this is that the standard does
// not actually say what std::async should do. The first
// argument to that function can be std::launch::async or
// std::launch::deferred, or both. The *intent* of the standard's
// authors was probably that if one sets it to
// std::launch::async | std::launch::deferred,
// that the task is run in a thread pool. But at least as of
// 2021, GCC doesn't do that: It just runs it on a new thread.
// If one chooses std::launch::deferred, it runs the task on
// the same thread but only when one calls join() on the task's
// std::future object. In the former case, this leads to
// oversubscription, in the latter case to undersubscription of
// resources. We choose oversubscription here.
//
// The issue illustrates why relying on external libraries
// with task schedulers is the way to go.
task_data = std::make_shared<TaskData>(
std::async(std::launch::async | std::launch::deferred,
function_object));
#endif
}
else
{
// Only one thread allowed. So let the task run to completion
// and just emplace a 'ready' future.
//
// The design of std::promise/std::future is unclear, but it
// seems that the intent is to obtain the std::future before
// we set the std::promise. So create the TaskData object at
// the top and then run the task and set the returned
// value. Since everything here happens sequentially, it
// really doesn't matter in which order all of this is
// happening.
std::promise<RT> promise;
task_data = std::make_shared<TaskData>(promise.get_future());
try
{
internal::evaluate_and_set_promise(function_object, promise);
}
catch (...)
{
try
{
// store anything thrown in the promise
promise.set_exception(std::current_exception());
}
catch (...)
{
// set_exception() may throw too. But ignore this on
// the task.
}
}
}
}
/**
* Default constructor. You can't do much with a task object constructed
* this way, except for assigning it a task object that holds data created
* by the Threads::new_task() functions.
*
* @post Using this constructor leaves the object in an unjoinable state,
* i.e., joinable() will return false.
*/
Task() = default;
/**
* Copy constructor. At the end of this operation, both the original and the
* new object refer to the same task, and both can ask for the returned
* object. That is, if you do
* @code
* Threads::Task<T> t1 = Threads::new_task(...);
* Threads::Task<T> t2 (t1);
* @endcode
* then calling `t2.return_value()` will return the same object (not just an
* object with the same value, but in fact the same address!) as
* calling `t1.return_value()`.
*/
Task(const Task &other) = default;
/**
* Move constructor. At the end of this operation, the original object no
* longer refers to a task, and the new object refers to the same task
* as the original one originally did. That is, if you do
* @code
* Threads::Task<T> t1 = Threads::new_task(...);
* Threads::Task<T> t2 (std::move(t1));
* @endcode
* then calling `t2.return_value()` will return the object computed by
* the task, and `t1.return_value()` will result in an error because `t1`
* no longer refers to a task and consequently does not know anything
* about a return value.
*/
Task(Task &&other) noexcept = default;
/**
* Copy operator. At the end of this operation, both the right hand and the
* left hand object refer to the same task, and both can ask for the
* returned object. That is, if you do
* @code
* Threads::Task<T> t1 = Threads::new_task(...);
* Threads::Task<T> t2;
* t2 = t1;
* @endcode
* then calling `t2.return_value()` will return the same object (not just an
* object with the same value, but in fact the same address!) as
* calling `t1.return_value()`.
*/
Task &
operator=(const Task &other) = default;
/**
* Move operator. At the end of this operation, the right hand side object
* no longer refers to a task, and the left hand side object refers to the
* same task as the right hand side one originally did. That is, if you do
* @code
* Threads::Task<T> t1 = Threads::new_task(...);
* Threads::Task<T> t2;
* t2 = std::move(t1);
* @endcode
* then calling `t2.return_value()` will return the object computed by
* the task, and `t1.return_value()` will result in an error because `t1`
* no longer refers to a task and consequently does not know anything
* about a return value.
*/
Task &
operator=(Task &&other) noexcept = default;
/**
* Join the task represented by this object, i.e. wait for it to finish.
*
* A task can be joined multiple times (while the first join() operation
* may block until the task has completed running, all successive attempts
* to join will return immediately).
*
* If the operation that was executed on the task with which this
* object was initialized throws an exception instead of returning
* regularly, then calling the current join() function will first
* wait for that task to finish, and then in turn throw the
* exception that the task operation had thrown originally. This
* allows for the propagation of exceptions from tasks executed on
* a separate thread to the calling thread.
*
* (This behavior differs from that of
* [`std::future`](https://en.cppreference.com/w/cpp/thread/future),
* where the `std::future::wait()` function only waits for
* completion of the operation, whereas the exception is
* propagated only once one calls `std::future::get()`. However,
* this is awkward when putting `void` functions onto separate
* tasks because these do not actually return anything;
* consequently, it is more natural to call `std::task::wait()`
* for such tasks than the `std::task::get()` function since the
* latter does not, actually, return anything that could be
* gotten.)
*
* @pre You can't call this function if you have used the default
* constructor of this class and have not assigned a task object to it. In
* other words, the function joinable() must return true.
*/
void
join() const
{
// Make sure we actually have a task that we can wait for.
AssertThrow(joinable(), ExcNoTask());
task_data->wait();
}
/**
* Return whether the current object can be joined. You can join a task
* object once a task (typically created with Threads::new_task()) has
* actually been assigned to it. On the other hand, the function returns
* false if the object has been default constructed.
*
* A task can be joined multiple times (while the first join() operation
* may block until the task has completed running, all successive attempts
* to join will return immediately). Consequently, if this function
* returns true, it will continue to return true until the task object it
* reports on is assigned to from another object.
*/
bool
joinable() const
{
return (task_data != nullptr);
}
/**
* Get the return value of the function of the task. Since it is
* only available once the thread finishes, this function
* internally also calls join(). You can call this function
* multiple times as long as the object refers to the same task,
* and expect to get the same return value every time. (With the
* exception of the case where the returned object has been moved;
* see below.)
*
* @note The function returns a <i>non-@p const reference</i> to
* the returned object, instead of the returned object. This
* allows writing code such as
* @code
* Threads::Task<int> t = Threads::new_task (...function returning an
* int...); t.return_value() = 42; // overwrite returned value int i =
* t.return_value(); // i is now 42
* @endcode
* You will rarely have a need to write such code. On the other hand,
* the function needs to return a writable (non-@p const) reference to
* support code such as this:
* @code
* std::unique_ptr<int> create_int (const std::string &s) { ... }
*
* void f()
* {
* Threads::Task<std::unique_ptr<int>>
* t = Threads::new_task (&create_int, "42");
*
* std::unique_ptr<int> i = std::move(t.return_value());
* ...
* }
* @endcode
* Here, it is necessary to `std::move` the returned object (namely,
* the <code>std::unique_ptr</code> object) because
* <code>std::unique_ptr</code> objects can not be copied. In other words,
* to get the pointer out of the object returned from the task, it needs
* to be moved, and in order to be moved, the current function needs to
* return a writable (non-@p const) reference.
*
* This function internally calls the join() member function. As a
* consequence, and as explained there, if the packaged task
* throws an exception that is then re-thrown by the join()
* function and consequently also the current function if you have
* not previously called join().
*
* @pre You can't call this function if you have used the default
* constructor of this class and have not assigned a task object to it. In
* other words, the function joinable() must return true.
*/
typename internal::return_value<RT>::reference_type
return_value()
{
// Make sure we actually have a task that we can wait for.
AssertThrow(joinable(), ExcNoTask());
// Then return the promised object. If necessary, wait for the promise to
// be set.
return task_data->get();
}
/**
* @addtogroup Exceptions
* @{
*/
/**
* Exception
*/
DeclExceptionMsg(ExcNoTask,
"The current object is not associated with a task that "
"can be joined. It may have been detached, or you "
"may have already joined it in the past.");
/** @} */
private:
/**
* A data structure that holds a std::future into which the task deposits
* its return value. Since one can only call std::future::get() once,
* we do so in the get() member function and then move the returned object
* into the `returned_object` member variable from where we can read it
* multiple times and from where it can also be moved away if it is not
* copyable.
*/
class TaskData
{
public:
/**
* Constructor. Initializes an std::future object and assumes
* that the task so set has not finished yet.
*/
TaskData(std::future<RT> &&future) noexcept
: future(std::move(future))
, task_has_finished(false)
#ifdef DEAL_II_WITH_TBB
, task_group(std::make_unique<tbb::task_group>())
#endif
{}
/**
* There can only be one TaskData object referencing
* a task. Make sure that these objects are not copied.
*/
TaskData(const TaskData &) = delete;
/**
* There can only be one TaskData object referencing
* a task. Make sure that these objects are not moved.
*/
TaskData(TaskData &&) = delete;
/**
* There can only be one TaskData object referencing
* a task. Make sure that these objects are not copied.
*/
TaskData &
operator=(const TaskData &) = delete;
/**
* There can only be one TaskData object referencing
* a task. Make sure that these objects are not moved.
*/
TaskData &
operator=(TaskData &&) = delete;
/**
* Destructor. Wait for the results to be ready. This ensures that the
* last Task object holding a shared pointer to the current TaskData
* object blocks until the task has actually finished -- in essence,
* this makes sure that one cannot just abandon a task completely
* by letting all Task objects that point to it go out of scope.
*/
~TaskData() noexcept
{
// Explicitly wait for the results to be ready. This class stores
// a std::future object, and we could just let the compiler generate
// the destructor which would then call the destructor of std::future
// which *may* block until the future is ready. As explained in
// https://en.cppreference.com/w/cpp/thread/future/~future
// this is only a *may*, not a *must*. (The standard does not
// appear to say anything about it at all.) As a consequence,
// let's be explicit about waiting.
//
// One of the corner cases we have to worry about is that if a task
// ends by throwing an exception, then wait() will re-throw that
// exception on the thread that calls it, the first time around
// someone calls wait() (or the return_value() function of the
// surrounding class). So if we get to this constructor and an exception
// is thrown by wait(), then that means that the last Task object
// referring to a task is going out of scope with nobody having
// ever checked the return value of the task itself. In that case,
// one could argue that they would also not have cared about whether
// an exception is thrown, and that we should simply ignore the
// exception. This is what we do here. It is also the simplest solution,
// because we don't know what one should do with the exception to begin
// with: destructors aren't allowed to throw exceptions, so we can't
// just rethrow it here if one had been triggered.
try
{
wait();
}
catch (...)
{}
}
/**
* Wait for the std::future object to be ready, i.e., for the
* time when the std::promise receives its value. If this has
* already happened, this function can follow a fast path.
*/
void
wait()
{
// If we have previously already moved the result, then we don't
// need a lock and can just return.
if (task_has_finished)
return;
// Else, we need to go under a lock and try again. A different thread
// may have waited and finished the task since then, so we have to try
// a second time. (This is Schmidt's double-checking pattern.)
std::lock_guard<std::mutex> lock(mutex);
if (task_has_finished)
return;
else
{
#ifdef DEAL_II_WITH_TASKFLOW
// We want to call executor.corun_until() to keep scheduling tasks
// until the task we are waiting for has actually finished. The
// problem is that TaskFlow documents that you can only call
// corun_until() on a worker of the executor. In other words, we
// can call it from *inside* other tasks, but not from the main
// thread (or other threads that might have been created outside
// of TaskFlow).
//
// Fortunately, we can check whether we are on a worker thread:
if (MultithreadInfo::get_taskflow_executor().this_worker_id() >= 0)
MultithreadInfo::get_taskflow_executor().corun_until([this]() {
return (future.wait_for(std::chrono::seconds(0)) ==
std::future_status::ready);
});
else
// We are on a thread not managed by TaskFlow. In that case, we
// can simply stop the current thread to wait for the task to
// finish (i.e., for the std::future object to become ready). We
// can do this because we need not fear that this leads to a
// deadlock: The current threads is waiting for completion of a
// task that is running on a completely different set of
// threads, and so not making any progress here can not deprive
// these other threads of the ability to schedule their tasks.
//
// Indeed, this is even true if the current thread is a worker
// of one executor and we are waiting for a task running on a
// different executor: The current task being stopped may block
// the current executor from scheduling more tasks, but it is
// unrelated to the tasks of the scheduler for which we are
// waiting for something, and so that other executor will
// eventually get arond to scheduling the task we are waiting
// for, at which point the current task will also complete.
future.wait();
#elif defined(DEAL_II_WITH_TBB)
// If we build on the TBB, then we can't just wait for the
// std::future object to get ready. Apparently the TBB happily
// enqueues a task into an arena and then just sits on it without
// ever executing it unless someone expresses an interest in the
// task. The way to avoid this is to add the task to a
// tbb::task_group, and then here wait for the single task
// associated with that task group.
//
// This also makes sense from another perspective. Imagine that
// we allow at most N threads, and that we create N+1 tasks in such
// a way that the first N all wait for the (N+1)st task to finish.
// (See the multithreading/task_17 test for an example.) If they
// all just sit in their std::future::wait() function, nothing
// is ever going to happen because the scheduler sees that N tasks
// are currently running and is never informed that all they're
// doing is wait for another task to finish. What *needs* to