-
Notifications
You must be signed in to change notification settings - Fork 2
/
makeSet.h
86 lines (73 loc) · 2.39 KB
/
makeSet.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
#ifndef MAKE_SET_H
#define MAKE_SET_H
#include <set>
#include <vector>
void visit(std::vector<std::set<int>> const &dag, int idx, bool visited[], std::set<int>& reachset) {
reachset.insert(idx);
visited[idx] = true;
for (auto f : dag[idx]) {
if (!visited[f])
visit(dag, f, visited, reachset);
}
}
void insertToLevel(std::vector< std::vector<int> >& levels, int const node, int const currLevel) {
if (levels.size() <= currLevel) {
levels.emplace_back(); // construct a new level
}
levels[currLevel].push_back(node);
}
// Function that returns the DAG. visited matrix is later used to generate reachset
std::vector<std::set<int>> makeDAG(int* &col, int* &row, double* &val, int n, bool* &visited) {
std::vector<std::set<int>> dag(n);
for (int i = 0; i < n; ++i) {
visited[i] = false;
}
// Making the dag
for (int i = 1; i < n; ++i) {
for (int j = col[i-1]; j < col[i]; ++j) {
if (row[j] != i - 1) // Do not create a self-loop
dag[i-1].insert(row[j]);
}
}
return dag;
}
// Function that generates the reahset from the DAG
std::set<int> makeReachset(std::vector<std::set<int>> const &dag, bool* &visited, int n, double* &x) {
std::set<int> reachset;
// Creating dependencies using DFS
for (int i = 0; i < n; ++i) {
if (x[i] != 0) {
if (!visited[i]) {
visit(dag, i, visited, reachset);
}
}
}
return reachset;
}
// Creating levels and tasks
std::vector< std::vector<int> > makeLevelSet(std::vector<std::set<int>> const &dag, std::set<int> reachset, int n) {
std::vector<int> indegree(n); // Keep a count of incoming edges to record root nodes
std::vector< std::vector<int> > levels; // Store levels of parallelism. Tasks inside same level can be performed parallely; each 2 levels need to sync
int currLevel = 0;
for (auto f : reachset) {
for (auto j : dag[f]) {
++indegree[j];
}
}
while (!reachset.empty()) {
for (auto f : reachset) {
if (indegree[f] == 0) {
insertToLevel(levels, f, currLevel);
}
}
for (auto f : levels[currLevel]) {
reachset.erase(f);
for (auto j : dag[f]) {
--indegree[j];
}
}
++currLevel;
}
return levels;
}
#endif