Skip to content

xbpar: Add DumpAsDot debug helper method #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/xbpar/PARGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
**********************************************************************************************************************/

#include <xbpar.h>
#include <unordered_map>
#include <unordered_set>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Construction / destruction
Expand Down Expand Up @@ -123,3 +125,105 @@ PARGraphNode* PARGraph::GetNodeByLabelAndIndex(uint32_t label, uint32_t index) c
{
return m_labeledNodes[label][index];
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Debugging

#include <cstdio>

std::string PARGraph::DumpAsDot() const
{
std::string ret;

// Need to collect all inbound edges because these are harder to obtain
std::unordered_map<PARGraphNode*, std::unordered_set<std::string>> inbound_edge_map;
for(auto n : m_nodes)
{
for(uint32_t i = 0; i < n->GetEdgeCount(); i++)
{
auto e = n->GetEdgeByIndex(i);
inbound_edge_map[e->m_destnode].insert(e->m_destport);
}
}

ret += "digraph pargraph {\n";
ret += "node [shape=record];\n";

// Write out nodes
for(auto n : m_nodes)
{
ret += "n" + std::to_string((uintptr_t)n) + " [label=\"";

// Inbound
if (inbound_edge_map[n].size())
{
ret += "{";
size_t count = 0;
for(auto i = inbound_edge_map[n].begin(); i != inbound_edge_map[n].end(); i++, count++)
{
ret += "<" + *i + "> ";
ret += *i;
if (count != inbound_edge_map[n].size() - 1)
ret += "|";
}
ret += "}|";
}

// Labels
ret += std::to_string(n->GetLabel());
if (n->GetAlternateLabelCount())
{
ret += " (";
for(size_t i = 0; i < n->GetAlternateLabelCount(); i++)
{
ret += std::to_string(n->GetAlternateLabel(i));
if (i != n->GetAlternateLabelCount() - 1)
ret += ", ";
}
ret += ")";
}

// Outbound
if (n->GetEdgeCount())
{
std::unordered_set<std::string> used_outbound_ports;

for(size_t i = 0; i < n->GetEdgeCount(); i++)
used_outbound_ports.insert(n->GetEdgeByIndex(i)->m_sourceport);

ret += "|{";
size_t count = 0;
for(auto i = used_outbound_ports.begin(); i != used_outbound_ports.end(); i++, count++)
{
ret += "<" + *i + "> ";
ret += *i;
if (count != used_outbound_ports.size() - 1)
ret += "|";
}
ret += "}";
}

ret += "\"];\n";
}

// Write out edges
for(auto n : m_nodes)
{
for(uint32_t i = 0; i < n->GetEdgeCount(); i++)
{
auto e = n->GetEdgeByIndex(i);

ret += "n" + std::to_string((uintptr_t)e->m_sourcenode) + ":\"" + e->m_sourceport;
ret += "\" -> ";
ret += "n" + std::to_string((uintptr_t)e->m_destnode) + ":\"" + e->m_destport;
ret += "\";\n";
}
}

ret += "}\n";

printf("%s", ret.c_str());
fflush(stdout);

return ret;
}
3 changes: 3 additions & 0 deletions src/xbpar/PARGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define PARGraph_h

#include <cstdint>
#include <string>
#include <vector>

class PARGraphNode;
Expand Down Expand Up @@ -52,6 +53,8 @@ class PARGraph
//Insertion
void AddNode(PARGraphNode* node);

std::string DumpAsDot() const;

protected:

typedef std::vector<PARGraphNode*> NodeVector;
Expand Down