18 #include <unordered_set>
25 #include <sys/types.h>
27 #define NO_PREVIOUS_CYCLE_INPUT
29 void genCGRAVisual(std::string exe_path, std::shared_ptr<CGRA> cgra,
int II)
31 std::string par_js_path = exe_path +
"/../../../src/visual/visual.partial.js";
32 std::ifstream par_js(par_js_path);
35 std::string context_f_path = exe_path +
"/../../../output/context-div.js";
36 std::ofstream context_f(context_f_path, std::ios::trunc);
39 std::string f_path = exe_path +
"/../../../output/CGRA.js";
40 std::ofstream f(f_path, std::ios::trunc);
44 const auto& mrrg = cgra->getMRRG(II);;
45 if(!mrrg.initiationInterval())
49 context_f <<
"document.write(\"\n";
50 context_f <<
" <div class=\\\"tab\\\">\\\n";
51 context_f <<
" <button class=\\\"tablinks\\\" onclick=\\\"showContext(event, 'context_0')\\\" id=\\\"defaultOpen\\\">Context 0</button>\\\n";
52 for(
int i = 1; i < mrrg.initiationInterval(); ++i)
53 context_f <<
" <button class=\\\"tablinks\\\" onclick=\\\"showContext(event, 'context_" << i <<
"')\\\">Context " << i <<
"</button>\\\n";
54 context_f <<
" </div>\\\n";
55 for(
int i = 0; i < mrrg.initiationInterval(); ++i)
56 context_f <<
" <div id=\\\"context_" << i <<
"\\\" class=\\\"tabcontent\\\"></div>\\\n";
57 context_f <<
"\");\n";
60 f <<
"function draw() {\n";
61 for(
int i = 0; i < mrrg.initiationInterval(); ++i)
62 f <<
" draw_helper(" << i <<
");\n";
65 std::vector<std::set<std::pair<int, std::string>, std::greater<std::pair<int, std::string>>>> cluster_type;
67 int max_module_depth = 0;
68 for(
int i = 0; i < mrrg.initiationInterval(); ++i)
70 cluster_type.push_back(
std::set<std::pair<int, std::string>, std::greater<std::pair<int, std::string>>>());
71 for(
auto n = mrrg.allNodesByCycle().at(i).begin(); n != mrrg.allNodesByCycle().at(i).end(); n++)
73 std::string s = n->second->getFullName();
75 std::string::size_type pre_pos = 0;
78 auto pos = s.find(
'.', pre_pos);
80 if(pos == std::string::npos)
83 cluster_type[i].insert(std::make_pair(++count, s.substr(0, pos)));
85 if(count > max_module_depth)
86 max_module_depth = count;
91 f <<
"var clusterColor = [ '#6698FF', '#27CEC8', '#30FF89', '#EEFF99' ];\n";
96 f <<
"var clusterTypes = [];\n";
97 for(
int i = 0; i < mrrg.initiationInterval(); ++i)
99 f <<
"clusterTypes.push([\n";
100 for(
auto & s : cluster_type[i])
102 f <<
"[\"" << s.second <<
"\", " << s.first <<
"],\n";
107 #ifndef NO_PREVIOUS_CYCLE_INPUT
108 std::multimap<MRRGNode*, MRRGNode*> input_nodes;
112 f <<
"var nodes = [];\n";
113 for(
int i = 0; i < mrrg.initiationInterval(); ++i)
115 std::set<std::string> nodes;
116 f <<
"nodes.push(new vis.DataSet([\n";
117 for(
auto n = mrrg.allNodesByCycle().at(i).begin(); n != mrrg.allNodesByCycle().at(i).end(); n++)
120 f <<
"{ id: '" << n->second->getFullName() <<
"', label: '" << n->second->getFullName() <<
"', shape: 'box'},\n";
122 f <<
"{ id: '" << n->second->getFullName() <<
"', label: '" << n->second->getFullName() <<
"'},\n";
123 nodes.insert(n->second->getFullName());
126 for(
auto n = mrrg.allNodesByCycle().at(i).begin(); n != mrrg.allNodesByCycle().at(i).end(); ++n)
128 for(
auto fanout = n->second->fanout.begin(); fanout != n->second->fanout.end(); fanout++)
130 #ifndef NO_PREVIOUS_CYCLE_INPUT
131 if((*fanout)->cycle != n->second->cycle)
132 input_nodes.insert(std::make_pair(*fanout, n->second));
134 if(nodes.find((*fanout)->getFullName()) == nodes.end())
136 f <<
"{ id: '" << **fanout <<
"' , label: '" << **fanout <<
"', color: '#B464FF'},\n";
137 nodes.insert((*fanout)->getFullName());
145 f <<
"var edges = [];\n";
146 for(
int i = 0; i < mrrg.initiationInterval(); ++i)
148 f <<
"edges.push(new vis.DataSet([\n";
149 for(
auto n = mrrg.allNodesByCycle().at(i).begin(); n != mrrg.allNodesByCycle().at(i).end(); ++n)
151 for(
auto fanout = n->second->fanout.begin(); fanout != n->second->fanout.end(); fanout++)
153 f <<
"{ from: '" << (*(n->second)) <<
"', to: '" << (**fanout) <<
"', arrows: 'to' },\n";
159 #ifndef NO_PREVIOUS_CYCLE_INPUT
160 for(
const auto & input_node : input_nodes)
162 f <<
"nodes[" << input_node.first->cycle <<
"].update([" <<
"{ id: '" << input_node.second->getFullName() <<
"', label: '" << input_node.second->getFullName() <<
"', color: 'grey'}]);\n";
163 f <<
"edges[" << input_node.first->cycle <<
"].add([" <<
"{ from: '" << input_node.second->getFullName() <<
"', to: '" << input_node.first->getFullName() <<
"', arrows: 'to' }]);\n";
171 std::string exe_path,
const MRRG& mrrg,
175 const auto vis_output_dir = exe_path +
"/../../../output";
176 const auto vis_src_dir = exe_path +
"/../../../src/visual";
178 if (mkdir(vis_output_dir.c_str(), 0777) && errno != EEXIST) {
182 std::string par_js_path = vis_src_dir +
"/visual.partial.js";
183 std::ifstream par_js(par_js_path);
186 std::string context_f_path = vis_output_dir +
"/context-div.js";
187 std::ofstream context_f(context_f_path, std::ios::trunc);
190 std::string f_path = vis_output_dir +
"/CGRA.js";
191 std::ofstream f(f_path, std::ios::trunc);
194 std::string cgra_html_path_out = vis_output_dir +
"/CGRA.html";
195 std::ofstream cgra_html_out(cgra_html_path_out, std::ios::trunc);
196 if(cgra_html_out.fail())
198 std::string cgra_html_path_in = vis_src_dir +
"/CGRA.html";
199 std::ifstream cgra_html_in(cgra_html_path_in);
200 if(cgra_html_in.fail())
207 cgra_html_out << cgra_html_in.rdbuf();
210 context_f <<
"document.write(\"\\\n";
211 context_f <<
" <div class=\\\"tab\\\">\\\n";
212 context_f <<
" <button class=\\\"tablinks\\\" onclick=\\\"showContext(event, 'context_0')\\\" id=\\\"defaultOpen\\\">Context 0</button>\\\n";
214 context_f <<
" <button class=\\\"tablinks\\\" onclick=\\\"showContext(event, 'context_" << i <<
"')\\\">Context " << i <<
"</button>\\\n";
215 context_f <<
" <button class=\\\"tablinks\\\" onclick=\\\"showContext(event, 'context_" << mrrg.
initiationInterval() <<
"')\\\">Mapping</button>\\\n";
217 context_f <<
" </div>\\\n";
220 context_f <<
" <div id=\\\"context_" << i <<
"\\\" class=\\\"tabcontent\\\"></div>\\\n";
221 context_f <<
" <div id=\\\"context_" << mrrg.
initiationInterval() <<
"\\\" class=\\\"tabcontent\\\"></div>\\\n";
223 context_f <<
"\");\n";
226 f <<
"function draw() {\n";
228 f <<
" draw_helper(" << i <<
");\n";
231 std::vector<std::set<std::pair<int, std::string>, std::greater<std::pair<int, std::string>>>> cluster_type;
234 std::unordered_map<std::string, std::pair<double, double>> cluster_position;
237 int max_module_depth = 0;
240 cluster_type.push_back(
std::set<std::pair<int, std::string>, std::greater<std::pair<int, std::string>>>());
243 std::string s = n->second->getFullName();
245 std::string::size_type pre_pos = 2;
248 auto pos = s.find(
'.', pre_pos);
249 if(pos == std::string::npos)
252 cluster_type[i].insert(std::make_pair(++count, s.substr(0, pos)));
255 auto posInfo = n->second->parent->getSubModulePosition(s.substr(pre_pos, pos-pre_pos));
258 cluster_position.emplace(s.substr(0, pos), posInfo.second);
263 if(count > max_module_depth)
264 max_module_depth = count;
269 f <<
"var clusterColor = [ '#6698FF', '#27CEC8', '#30FF89', '#EEFF99' ];\n";
274 f <<
"var clusterTypes = [];\n";
277 f <<
"clusterTypes.push([\n";
278 for(
auto & s : cluster_type[i])
280 f <<
"[\"" << s.second <<
"\", " << s.first;
283 if (cluster_position.find(s.second) != cluster_position.end()) {
288 f <<
", " << xPos <<
", " << yPos;
297 f <<
"clusterTypes.push([\n";
300 for(
auto & s: cluster_type[i])
302 f <<
"[\"" << s.second <<
"\", " << s.first;
304 if (cluster_position.find(s.second) != cluster_position.end()) {
310 xPos += std::stoi(s.second)*10000;
311 f <<
", " << xPos <<
", " << yPos;
319 #ifndef NO_PREVIOUS_CYCLE_INPUT
320 std::multimap<MRRGNode*, MRRGNode*> input_nodes;
324 f <<
"var nodes = [];\n";
327 std::set<std::string> nodes;
328 f <<
"nodes.push(new vis.DataSet([\n";
331 std::string name = n->second->getFullName();
333 f <<
"{ id: '" << name <<
"', label: '" << name <<
"', shape: 'box'";
335 f <<
"{ id: '" << name <<
"', label: '" << name <<
"'";
338 auto dotIndex = name.rfind(
".");
339 std::pair<bool, std::pair<double, double>> posInfo;
340 if (dotIndex != std::string::npos) {
341 posInfo = n->second->parent->getNodePosition(name.substr(dotIndex+1));
344 auto colon_index = name.find(
":");
345 posInfo = n->second->parent->getNodePosition(name.substr(colon_index+1));
352 f <<
", x:" << xPos <<
", y:" << yPos <<
", physics:false, fixed:true";
359 for(
auto fanout = n->second->fanout.begin(); fanout != n->second->fanout.end(); fanout++)
361 #ifndef NO_PREVIOUS_CYCLE_INPUT
362 if((*fanout)->cycle != n->second->cycle)
363 input_nodes.insert(std::make_pair(*fanout, n->second));
365 std::string name = (*fanout)->getFullName();
366 if(nodes.find(name) == nodes.end()) {
367 f <<
"{ id: '" << **fanout <<
"' , label: '" << **fanout <<
"', color: '#B464FF'";
370 auto posInfo = (*fanout)->parent->getNodePosition(name.substr(name.rfind(
".")+1));
375 if (std::stoi(name) != i) {
376 xPos += (i-std::stoi(name))*200;
379 f <<
", x:" << xPos <<
", y:" << yPos <<
", physics:false, fixed:true";
389 f <<
"nodes.push(new vis.DataSet([\n";
390 for (
const auto& mg_ndesc_and_node : mg.
allNodes()) {
391 const auto& mg_node = mg.
getNodeRef(mg_ndesc_and_node.first);
392 const auto& mrrg_node = mrrg.
getNodeRef(toMRRG.at(mg_ndesc_and_node.first));
395 f <<
"{ id: '" << name <<
"|" << mg_node.op_node_desc->getName() <<
"', label: '" << name <<
"|" << mg_node.op_node_desc->getName() <<
"'";
397 if (mrrg_node.parent->getName().find(
"mux") != std::string::npos) {
398 f <<
", image: '../src/visual/Multiplexer.png', shape: 'image'";
400 else if (mrrg_node.parent->getName().find(
"ALU") != std::string::npos || mrrg_node.parent->getName().find(
"func") != std::string::npos) {
401 f <<
", image: '../src/visual/ALU.png', shape: 'image'";
404 f <<
", shape: 'box'";
407 int xPos = -1, yPos = -1;
408 auto dotIndex = name.rfind(
".");
409 std::pair<bool, std::pair<double, double>> posInfo;
410 if (dotIndex != std::string::npos) {
411 posInfo = mrrg_node.parent->getNodePosition(name.substr(dotIndex+1));
414 posInfo = mrrg_node.parent->getNodePosition(name.substr(2));
420 xPos += std::stoi(name)*10000;
422 f <<
", x:" << xPos <<
", y:" << yPos <<
", physics:false, fixed:true";
441 f <<
"var edges = [];\n";
444 f <<
"edges.push(new vis.DataSet([\n";
447 for(
auto fanout = n->second->fanout.begin(); fanout != n->second->fanout.end(); fanout++)
449 f <<
"{ from: '" << (*(n->second)) <<
"', to: '" << (**fanout) <<
"', arrows: 'to' },\n";
456 f <<
"edges.push(new vis.DataSet([\n";
458 for (
auto & fanout: mg.
fanout(node.first)) {
464 #ifndef NO_PREVIOUS_CYCLE_INPUT
465 for(
const auto & input_node : input_nodes)
467 f <<
"nodes[" << input_node.first->cycle <<
"].update([" <<
"{ id: '" << input_node.second->getFullName() <<
"', label: '" << input_node.second->getFullName() <<
"', color: 'grey'}]);\n";
468 f <<
"edges[" << input_node.first->cycle <<
"].add([" <<
"{ from: '" << input_node.second->getFullName() <<
"', to: '" << input_node.first->getFullName() <<
"', arrows: 'to' }]);\n";
472 f <<
"var nodeMapped = []; while(nodeMapped.push([]) <= " << mrrg.
initiationInterval() <<
");\n";
475 for(
const auto & mrrg_node : map.second)
477 f <<
"nodeMapped[" << mrrg.
getNodeRef(mrrg_node).
cycle <<
"].push({ id: '" << mrrg.
getNodeRef(mrrg_node).
getFullName() <<
"', color: 'red', title: '" << map.first->name <<
"' });\n";
494 return {pos.first*width, pos.second*height};