-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubflow.hpp
154 lines (121 loc) · 3.47 KB
/
subflow.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
#include <bits/stdc++.h>
#include "task.hpp"
#include "PARAMS.hpp"
// #include "executor.hpp"
using namespace std;
class Subflow {
// friend class graph;
// friend class executor;
public:
vector<Task*> subflow;
string name;
Subflow(string name="default"){
this->name = name;
}
int total_task = 0;
// input -> vector of function pointers, output-> vector of pointers Tasks
vector<Task*> add(vector<void (*)()>input){
// vector<Task*> output;
for(int i=0;i<input.size();i++){
Task* A = new Task();
A->fun = bind(input[i]);
A->id = (this->total_task)++;
subflow.push_back(A);
}
return subflow;
}
vector<Task*> topological_sort_list;
// to mark visited while dfs
vector<int> color;
// task to id map
//returns false if there is a cycle,
// also populates the topological sort list.
bool dfs(int node_id){
// cout<<"\n"<<node_id<<" my successors: ";
if((this->color)[node_id]==0){
(this->color)[node_id]=1;
for(auto task: this->subflow[node_id]->successor){
int id = task->id;
bool flag = this->dfs(id);
if(!flag) return false;
}
// cout<<"\n";
if(this->subflow[node_id]->subflow!=NULL){
this->subflow[node_id]->subflow->build();
for(Task* subflow_task: this->subflow[node_id]->subflow->topological_sort_list){
this->topological_sort_list.push_back(subflow_task);
}
}
this->topological_sort_list.push_back(this->subflow[node_id]);
(this->color)[node_id] = 2;
return true;
}
// cycle detected
else if(color[node_id]==1){
return false;
}
return true;
}
void build(){
// building subflow tasks -> id map
int i=0;
for(auto task: this->subflow){
// task->id = i;
this->color.push_back(0);
i++;
}
bool flag=true;
for(int i=0;i<this->total_task;i++){
if(color[i]==0){
flag = flag & this->dfs(i);
}
}
if(!flag){
this->topological_sort_list.clear();
cout<<"given graph in Subflow:"<<this->name<<" has cycle, deadlock can not be resolved. Routine skipped\n";
// exit(1);
}
// reverse(topological_sort_list.begin(), topological_sort_list.end());
return;
}
void dump(ofstream &myfile, string parent_name){
// cout<<"dump called\n";
for(int node_id=0; node_id<this->total_task;node_id++){
myfile<<"subflow_"<<this->name<<"_"<<node_id<<"[fontcolor=darkgreen, shape=box];\n";
myfile<<parent_name<<"->"<<"subflow_"<<this->name<<"_"<<node_id<<"[fontcolor=darkgreen];\n";
bool flag=false;
for(auto task: this->subflow[node_id]->successor){
flag=true;
// printf("not\n");
myfile<<"subflow_"<<this->name<<"_"<<node_id<<"->"<<"subflow_"<<this->name<<"_"<<task->id<<";\n";
}
if(this->subflow[node_id]->subflow!=NULL){
this->subflow[node_id]->subflow->dump(myfile, "subflow_"+this->name+"_"+to_string(node_id));
}
if(!flag){
myfile<<"subflow_"<<this->name<<"_"<<node_id<<"[fontcolor=darkgreen];\n";
}
}
// myfile << "}";
}
template <typename B, typename E, typename S, typename C>
void for_each(B&& beg, E&& end, S&& inc, C&& cal){
int n = distance(beg, end);
n = n/inc;
while(n-- > 0){
Task *A = new Task();
A->fun = bind(cal, *beg);
A->id=(this->total_task)++;
advance(beg, inc);
subflow.push_back(A);
}
}
};
// int main(){
// Subflow reddy = Subflow("reddy");
// auto A = reddy.add({
// [](){cout<<"Mc";},
// [](){cout<<"Bc";}
// });
// A[1]->execute_task();
// }