functional-dag
Loading...
Searching...
No Matches
dag_impl.hpp
1#pragma once
2// ---------------------------------------------
3// ___ .___
4// |_ \ __| _/____ ____
5// / \ ______ / __ |\__ \ / ___\
6// / /\ \ /_____/ / /_/ | / __ \_/ /_/ >
7// /_/ \__\ \____ |(____ /\___ /
8// \/ \//_____/
9// ---------------------------------------------
10// @author ndepalma@alum.mit.edu
11
12#include <vector>
13#include <map>
14#include <iostream>
15#include <unordered_set>
16
17#include "functional_dag/dag_interface.hpp"
18#include "functional_dag/impl/dag_fanout_impl.hpp"
19
20namespace fn_dag {
21
27 template<typename IDType>
28 class _dag_base {
29 public:
31 virtual ~_dag_base() = default;
32
36 virtual bool dag_contains(IDType _id) = 0;
37
39 virtual void print() = 0;
40
44 virtual IDType get_id() = 0;
45
47 virtual void push_once() = 0;
48 };
49
58 template<typename OriginType, typename IDType>
59 class dag : public _dag_base<IDType> {
60 private:
61 const IDType m_id; // The ID of the DAG itself
62 dag_source<OriginType> *m_source; // The source generator that creates data
63 dag_fanout_node<OriginType, IDType> m_children; // The children of the source to propagate data across
64 unordered_set<IDType> m_children_ids; // An optimization: a quick O(1) set lookup of the children IDs
65 const _dag_context &g_context; // The shared state across all of the children of this node.
66 thread m_thread; // Thread to run on if this DAG runs multi-threaded.
67
68 public:
78 const _dag_context &_context,
79 bool _startThread) :
80 m_id(_id),
81 m_source(_lsource),
82 m_children(_context),
83 m_children_ids(),
84 g_context(_context) {
85 if(_startThread)
86 m_thread = std::thread(&dag::start_source, this);
87 }
88
90 ~dag() {
91 if(m_thread.joinable())
92 m_thread.join();
93 delete m_source;
94 }
95
103 return m_id;
104 }
105
114 m_children.fan_out(_raw_dat);
115 }
116
126 return m_children_ids.count(_id) > 0;
127 }
128
139 template<typename In, typename Out>
143 m_children_ids.insert(_newID);
144 m_children.add_node_to_subdag(new_node, _on_node, m_id);
145 }
146
148 void print() {
149 *g_context.log << "->" << m_id << std::endl;
150 m_children.print(g_context.indent_str);
151 }
152
158 void push_once() {
159 OriginType *dat = m_source->update();
160 if(dat != nullptr)
161 m_children.fan_out(dat);
162 }
163
164 private:
169 void start_source() {
170 while(!g_context.filter_off)
171 push_once();
172 }
173 };
174};
The main DAG function that encapulates generation and mapping of the data across the DAG.
Definition dag_impl.hpp:59
void push_once()
Runs the generator and begins propagating the data to it's children.
Definition dag_impl.hpp:158
~dag()
Default deconstructor.
Definition dag_impl.hpp:90
dag(IDType _id, dag_source< OriginType > *_lsource, const _dag_context &_context, bool _startThread)
Constructor of the DAG.
Definition dag_impl.hpp:76
void add_filter(IDType _newID, dag_node< In, Out > *_new_filter, IDType _on_node)
Adds a new function to the DAG.
Definition dag_impl.hpp:140
void print()
Simple print function to print the ID of this DAG and it's children.
Definition dag_impl.hpp:148
IDType get_id()
Simple getter for the ID of the DAG itself.
Definition dag_impl.hpp:102
bool dag_contains(IDType _id)
Checks whether this DAG contains a specific ID.
Definition dag_impl.hpp:125
void manual_pump(OriginType *_raw_dat)
Manually pumps some data across the children of the DAG.
Definition dag_impl.hpp:113