Skip to content

Commit 5d3662b

Browse files
update observer
1 parent 7d4c004 commit 5d3662b

7 files changed

Lines changed: 222 additions & 129 deletions

File tree

examples/simple.cpp

Lines changed: 19 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,24 @@
77

88
int main(){
99

10-
tf::Taskflow taskflow ("Error 1: no source");
11-
12-
auto E = taskflow.emplace([](){}).name("E");
13-
auto C = taskflow.emplace([](){return ::rand()%2; }).name("C");
14-
auto D = taskflow.emplace([](){}).name("D");
15-
auto D_aux = taskflow.emplace([](){}).name("D_aux");
16-
auto F = taskflow.emplace([](){}).name("F");
17-
E.precede(D);
18-
C.precede(D_aux, F);
19-
D_aux.precede(D);
20-
21-
taskflow.dump(std::cout);
22-
23-
//tf::Taskflow taskflow1("taskflow1");
24-
//tf::Taskflow taskflow2("taskflow2");
25-
26-
//auto [A, B] = taskflow1.emplace(
27-
// [] () { std::cout << "TaskA"; },
28-
// [] () { std::cout << "TaskB"; }
29-
//);
30-
//A.precede(B);
31-
//
32-
//auto [C, D] = taskflow2.emplace(
33-
// [] () { std::cout << "TaskC"; },
34-
// [] (tf::Subflow& sf) {
35-
// std::cout << "TaskD";
36-
// auto [D1, D2] = sf.emplace(
37-
// [] () { std::cout << "D1"; },
38-
// [] () { std::cout << "D2"; }
39-
// );
40-
// D1.precede(D2);
41-
// }
42-
//);
43-
//C.precede(D);
44-
45-
//auto E = taskflow2.composed_of(taskflow1);
46-
//D.precede(E);
47-
48-
//executor.run(taskflow2).wait();
49-
50-
//taskflow2.dump(std::cout);
51-
52-
//auto A = taskflow.emplace([]() { std::cout << "TaskA\n"; });
53-
//auto B = taskflow.emplace([]() { std::cout << "TaskB\n"; });
54-
//auto C = taskflow.emplace([]() { std::cout << "TaskC\n"; });
55-
//auto D = taskflow.emplace([]() { std::cout << "TaskD\n"; });
56-
57-
//A.precede(B); // B runs after A // +---+
58-
//A.precede(C); // C runs after A // +---->| B |-----+
59-
//B.precede(D); // D runs after B // | +---+ |
60-
//C.precede(D); // D runs after C // +---+ +-v-+
61-
// // | A | | D |
62-
// // +---+ +-^-+
63-
//executor.run(taskflow).wait(); // | +---+ |
64-
// // +---->| C |-----+
65-
//return 0; // +---+
10+
tf::Executor executor;
11+
tf::Taskflow taskflow("simple");
12+
13+
auto A = taskflow.emplace([]() { std::cout << "TaskA\n"; });
14+
auto B = taskflow.emplace([]() { std::cout << "TaskB\n"; });
15+
auto C = taskflow.emplace([]() { std::cout << "TaskC\n"; });
16+
auto D = taskflow.emplace([]() { std::cout << "TaskD\n"; });
17+
18+
A.precede(B); // B runs after A // +---+
19+
A.precede(C); // C runs after A // +---->| B |-----+
20+
B.precede(D); // D runs after B // | +---+ |
21+
C.precede(D); // D runs after C // +---+ +-v-+
22+
// | A | | D |
23+
// +---+ +-^-+
24+
executor.run(taskflow).wait(); // | +---+ |
25+
// +---->| C |-----+
26+
// +---+
27+
28+
return 0;
6629
}
6730

taskflow/core/executor.hpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@
2121

2222
namespace tf {
2323

24+
25+
/** @class WorkerView
26+
27+
@brief class to access worker information from the observer interface
28+
29+
*/
30+
class WorkerView {
31+
32+
friend class Executor;
33+
34+
public:
35+
36+
37+
private:
38+
39+
Worker* _worker;
40+
41+
};
42+
43+
44+
// ----------------------------------------------------------------------------
45+
// Executor Definition
46+
// ----------------------------------------------------------------------------
47+
48+
2449
/** @class Executor
2550
2651
@brief execution interface for running a taskflow graph
@@ -181,7 +206,7 @@ class Executor {
181206
Each executor manages at most one observer at a time through std::unique_ptr.
182207
Createing multiple observers will only keep the lastest one.
183208
184-
@tparam Observer observer type derived from tf::ExecutorObserverInterface
209+
@tparam Observer observer type derived from tf::ObserverInterface
185210
@tparam ArgsT... argument parameter pack
186211
187212
@param args arguments to forward to the constructor of the observer
@@ -227,7 +252,7 @@ class Executor {
227252
std::atomic<size_t> _num_thieves[NUM_DOMAINS];
228253
std::atomic<bool> _done {0};
229254

230-
std::unique_ptr<ExecutorObserverInterface> _observer;
255+
std::unique_ptr<ObserverInterface> _observer;
231256

232257
PerThread& _per_thread() const;
233258

@@ -627,9 +652,17 @@ inline bool Executor::_wait_for_task(Worker& worker, Node*& t) {
627652
// Function: make_observer
628653
template<typename Observer, typename... Args>
629654
Observer* Executor::make_observer(Args&&... args) {
655+
656+
// must remove the existing observer before creating a new one
657+
if(_observer) {
658+
TF_THROW("observer already exists; remove the existing observer first");
659+
}
660+
630661
// use a local variable to mimic the constructor
631662
auto tmp = std::make_unique<Observer>(std::forward<Args>(args)...);
632663
tmp->set_up(_workers.size());
664+
665+
// transfer the ownership
633666
_observer = std::move(tmp);
634667
return static_cast<Observer*>(_observer.get());
635668
}

taskflow/core/graph.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../utility/object_pool.hpp"
66
#include "../utility/traits.hpp"
77
#include "../utility/passive_vector.hpp"
8+
#include "../utility/singleton.hpp"
89
#include "../nstd/variant.hpp"
910

1011
#if defined(__CUDA__) || defined(__CUDACC__)
@@ -144,18 +145,19 @@ class Node {
144145
ConditionWork, // conditional tasking
145146
ModuleWork // composable tasking
146147
>;
147-
148+
149+
public:
150+
148151
// variant index
149-
constexpr static auto STATIC_WORK = get_index_v<StaticWork, handle_t>;
150-
constexpr static auto DYNAMIC_WORK = get_index_v<DynamicWork, handle_t>;
151-
constexpr static auto CONDITION_WORK = get_index_v<ConditionWork, handle_t>;
152-
constexpr static auto MODULE_WORK = get_index_v<ModuleWork, handle_t>;
152+
constexpr static auto PLACEHOLDER_WORK = get_index_v<nstd::monostate, handle_t>;
153+
constexpr static auto STATIC_WORK = get_index_v<StaticWork, handle_t>;
154+
constexpr static auto DYNAMIC_WORK = get_index_v<DynamicWork, handle_t>;
155+
constexpr static auto CONDITION_WORK = get_index_v<ConditionWork, handle_t>;
156+
constexpr static auto MODULE_WORK = get_index_v<ModuleWork, handle_t>;
153157

154158
#ifdef TF_ENABLE_CUDA
155159
constexpr static auto CUDAFLOW_WORK = get_index_v<cudaFlowWork, handle_t>;
156160
#endif
157-
158-
public:
159161

160162
template <typename ...Args>
161163
Node(Args&&... args);

0 commit comments

Comments
 (0)