-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathNNPIUtils.cpp
105 lines (95 loc) · 3.17 KB
/
NNPIUtils.cpp
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
/*
* Copyright (c) Glow Contributors. See CONTRIBUTORS file.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "NNPIUtils.h"
#include "DebugMacros.h"
#include <fstream>
#include <sstream>
unsigned DotWriter::graphId_(0);
std::map<std::string, std::set<std::string>> DotWriter::subGraphNodes_;
std::map<std::string, std::string> DotWriter::subGraphLabels_;
std::set<std::string> DotWriter::edges_;
static const std::string &getColorString(unsigned i) {
// Taking colors from the SVG scheme
static const std::vector<std::string> nodeColors = {
"mistyrose", // function
"lightgreen", // host resource
"lightblue", // normal device resource
"plum", // static device resource
"lightcoral", // p2p device resource
"wheat", // drt device resource
"lightgray", // reserved
"sandybrown", // reserved
"turquoise", // reserved
"seagreen", // reserved
};
return nodeColors.at(i % nodeColors.size());
}
void DotWriter::clear() {
DotWriter::subGraphNodes_.clear();
DotWriter::subGraphLabels_.clear();
DotWriter::edges_.clear();
}
void DotWriter::addNode(std::string name, std::string label, unsigned color,
std::string subGraph) {
std::ostringstream os;
os << name << " [\n";
os << "\tlabel = \"" << label << "\"\n";
os << "\tstyle=\"filled,rounded\"\n";
os << "\tfillcolor=" << getColorString(color) << "\n";
os << "];\n";
if (!subGraph.empty()) {
subGraphNodes_[subGraph].insert(os.str() /*name*/);
}
}
void DotWriter::addEdge(std::string src, std::string dst) {
edges_.insert(src + " -> " + dst + ";\n");
}
void DotWriter::writeToFile(std::string filename) {
if (filename.empty()) {
filename = "dot_graph";
}
filename = filename + std::to_string(graphId_++) + ".dot";
std::ofstream outFile(filename);
if (!outFile.is_open()) {
LOG(INFO) << "Failed to write dor file: " << filename;
return;
}
outFile << "digraph {\n";
outFile << "\tedge[color = black];\n";
outFile << "\trank = TB;\n";
outFile << "\tnode[shape = Mrecord, penwidth=2];\n";
outFile << "\n";
for (const auto &sg : subGraphNodes_) {
outFile << "subgraph " << "cluster_" << sg.first << " {\n";
outFile << "\tlabel = \"" << subGraphLabels_.at(sg.first) << "\";\n";
for (const auto &n : sg.second) {
outFile << n; //<< ";\n";
}
outFile << "}\n";
}
for (const auto &e : edges_) {
outFile << e;
}
outFile << "\n";
outFile << "\t}\n";
}
void DotWriter::addSubGraph(std::string name, std::string label) {
subGraphLabels_[name] = label;
}
std::string DotWriter::getHexStr(uint64_t h) {
std::ostringstream os;
os << std::hex << h;
return os.str();
}