File size: 8,849 Bytes
be94e5d |
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018 Intel Corporation
#include "precomp.hpp"
#include <iostream> // cout
#include <sstream> // stringstream
#include <fstream> // ofstream
#include <map>
#include <ade/passes/check_cycles.hpp>
#include <opencv2/gapi/gproto.hpp>
#include "compiler/gmodel.hpp"
#include "compiler/gislandmodel.hpp"
#include "compiler/passes/passes.hpp"
namespace cv { namespace gimpl { namespace passes {
// TODO: FIXME: Ideally all this low-level stuff with accessing ADE APIs directly
// should be incapsulated somewhere into GModel, so here we'd operate not
// with raw nodes and edges, but with Operations and Data it produce/consume.
void dumpDot(const ade::Graph &g, std::ostream& os)
{
GModel::ConstGraph gr(g);
const std::unordered_map<cv::GShape, std::string> data_labels = {
{cv::GShape::GMAT, "GMat"},
{cv::GShape::GSCALAR, "GScalar"},
{cv::GShape::GARRAY, "GArray"},
{cv::GShape::GOPAQUE, "GOpaque"},
{cv::GShape::GFRAME, "GFrame"},
};
auto format_op_label = [&gr](ade::NodeHandle nh) -> std::string {
std::stringstream ss;
const cv::GKernel k = gr.metadata(nh).get<Op>().k;
ss << k.name << "_" << nh;
return ss.str();
};
auto format_op = [&format_op_label](ade::NodeHandle nh) -> std::string {
return "\"" + format_op_label(nh) + "\"";
};
auto format_obj = [&gr, &data_labels](ade::NodeHandle nh) -> std::string {
std::stringstream ss;
const auto &data = gr.metadata(nh).get<Data>();
ss << data_labels.at(data.shape) << "_" << data.rc;
return ss.str();
};
auto format_log = [&gr](ade::NodeHandle nh, const std::string &obj_name) {
std::stringstream ss;
const auto &msgs = gr.metadata(nh).get<Journal>().messages;
ss << "xlabel=\"";
if (!obj_name.empty()) { ss << "*** " << obj_name << " ***:\n"; };
for (const auto &msg : msgs) { ss << msg << "\n"; }
ss << "\"";
return ss.str();
};
// FIXME:
// Unify with format_log
auto format_log_e = [&gr](ade::EdgeHandle nh) {
std::stringstream ss;
const auto &msgs = gr.metadata(nh).get<Journal>().messages;
for (const auto &msg : msgs) { ss << "\n" << msg; }
return ss.str();
};
auto sorted = gr.metadata().get<ade::passes::TopologicalSortData>();
os << "digraph GAPI_Computation {\n";
// Prior to dumping the graph itself, list Data and Op nodes individually
// and put type information in labels.
// Also prepare list of nodes in islands, if any
std::map<std::string, std::vector<std::string> > islands;
for (auto &nh : sorted.nodes())
{
const auto node_type = gr.metadata(nh).get<NodeType>().t;
if (NodeType::DATA == node_type)
{
const auto obj_data = gr.metadata(nh).get<Data>();
const auto obj_name = format_obj(nh);
os << obj_name << " [label=\"" << obj_name << "\n" << obj_data.meta << "\"";
if (gr.metadata(nh).contains<Journal>()) { os << ", " << format_log(nh, obj_name); }
os << " ]\n";
if (gr.metadata(nh).contains<Island>())
islands[gr.metadata(nh).get<Island>().island].push_back(obj_name);
}
else if (NodeType::OP == gr.metadata(nh).get<NodeType>().t)
{
const auto obj_name = format_op(nh);
const auto obj_name_label = format_op_label(nh);
os << obj_name << " [label=\"" << obj_name_label << "\"";
if (gr.metadata(nh).contains<Journal>()) { os << ", " << format_log(nh, obj_name_label); }
os << " ]\n";
if (gr.metadata(nh).contains<Island>())
islands[gr.metadata(nh).get<Island>().island].push_back(obj_name);
}
}
// Then, dump Islands (only nodes, operations and data, without links)
for (const auto &isl : islands)
{
os << "subgraph \"cluster " + isl.first << "\" {\n";
for(auto isl_node : isl.second) os << isl_node << ";\n";
os << "label=\"" << isl.first << "\";";
os << "}\n";
}
// Now dump the graph
for (auto &nh : sorted.nodes())
{
// FIXME: Alan Kay probably hates me.
switch (gr.metadata(nh).get<NodeType>().t)
{
case NodeType::DATA:
{
const auto obj_name = format_obj(nh);
for (const auto &eh : nh->outEdges())
{
os << obj_name << " -> " << format_op(eh->dstNode())
<< " [ label = \"in_port: "
<< gr.metadata(eh).get<Input>().port;
if (gr.metadata(eh).contains<Journal>()) { os << format_log_e(eh); }
os << "\" ] \n";
}
}
break;
case NodeType::OP:
{
for (const auto &eh : nh->outEdges())
{
os << format_op(nh) << " -> " << format_obj(eh->dstNode())
<< " [ label = \"out_port: "
<< gr.metadata(eh).get<Output>().port
<< " \" ]; \n";
}
}
break;
default: GAPI_Error("InternalError");
}
}
// And finally dump a GIslandModel (not connected with GModel directly,
// but projected in the same .dot file side-by-side)
auto pIG = gr.metadata().get<IslandModel>().model;
GIslandModel::Graph gim(*pIG);
for (auto nh : gim.nodes())
{
switch (gim.metadata(nh).get<NodeKind>().k)
{
case NodeKind::ISLAND:
{
const auto island = gim.metadata(nh).get<FusedIsland>().object;
const auto isl_name = "\"" + island->name() + "\"";
for (auto out_nh : nh->outNodes())
{
os << isl_name << " -> \"slot:"
<< format_obj(gim.metadata(out_nh).get<DataSlot>()
.original_data_node)
<< "\"\n";
}
}
break;
case NodeKind::SLOT:
{
const auto obj_name = format_obj(gim.metadata(nh).get<DataSlot>()
.original_data_node);
for (auto cons_nh : nh->outNodes())
{
if (gim.metadata(cons_nh).get<NodeKind>().k == NodeKind::ISLAND) {
os << "\"slot:" << obj_name << "\" -> \""
<< gim.metadata(cons_nh).get<FusedIsland>().object->name()
<< "\"\n";
} // other data consumers -- sinks -- are processed separately
}
}
break;
case NodeKind::EMIT:
{
for (auto out_nh : nh->outNodes())
{
const auto obj_name = format_obj(gim.metadata(out_nh).get<DataSlot>()
.original_data_node);
os << "\"emit:" << nh << "\" -> \"slot:" << obj_name << "\"\n";
}
}
break;
case NodeKind::SINK:
{
for (auto in_nh : nh->inNodes())
{
const auto obj_name = format_obj(gim.metadata(in_nh).get<DataSlot>()
.original_data_node);
os << "\"slot:" << obj_name << "\" -> \"sink:" << nh << "\"\n";
}
}
break;
default:
GAPI_Error("InternalError");
break;
}
}
os << "}" << std::endl;
}
void dumpDot(ade::passes::PassContext &ctx, std::ostream& os)
{
dumpDot(ctx.graph, os);
}
void dumpDotStdout(ade::passes::PassContext &ctx)
{
dumpDot(ctx, std::cout);
}
void dumpDotToFile(ade::passes::PassContext &ctx, const std::string& dump_path)
{
std::ofstream dump_file(dump_path);
if (dump_file.is_open())
{
dumpDot(ctx, dump_file);
dump_file << std::endl;
}
}
void dumpGraph(ade::passes::PassContext &ctx, const std::string& dump_path)
{
dump_path.empty() ? dumpDotStdout(ctx) : dumpDotToFile(ctx, dump_path);
}
}}} // cv::gimpl::passes
|