-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.h
58 lines (44 loc) · 1.21 KB
/
graph.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
#ifndef _GRAPH_H_
#define _GRAPH_H_
#include <condition_variable>
#include <memory>
#include <queue>
#include <thread>
#include <vector>
#include "context.h"
#include "node.h"
namespace graph_executor {
// Virtual base class of execution graph.
class Graph {
public:
Graph(int num_threads, std::vector<std::unique_ptr<Node>> &&nodes,
std::vector<std::unique_ptr<Context>> &&contexts)
: nodes_(std::move(nodes)), contexts_(std::move(contexts)) {
SetUp(num_threads);
}
virtual ~Graph() { TearDown(); };
// Not copyable. Not movable.
Graph(const Graph &other) = delete;
Graph(Graph &&other) = delete;
void Execute(int num_executions = 1);
private:
// Set up threads.
void SetUp(int num_threads);
// TearDown threads.
void TearDown();
// Worker thread.
void WorkerThread(int thread_id);
bool active_;
int concurrent_execution_;
std::mutex mutex_;
std::condition_variable client_cv_;
std::condition_variable worker_cv_;
std::queue<Node *> queue_;
std::vector<std::thread> threads_;
NoOpNode *input_node_;
NoOpNode *output_node_;
std::vector<std::unique_ptr<Node>> nodes_;
std::vector<std::unique_ptr<Context>> contexts_;
};
} // namespace graph_executor
#endif