Skip to content

Commit

Permalink
Fixes to cutouts and CFG lists
Browse files Browse the repository at this point in the history
  • Loading branch information
phschaad committed Apr 19, 2024
1 parent 5d9c761 commit d9dbab1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/renderer/canvas_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ export class CanvasManager {
// Also update destination point of edge
if (edge.dst_connector !== null) {
const e = parent_element?.data?.state?.edges[edge.id];
const dst_el = parent_graph.node(e?.dst);
const dst_el = parent_graph?.node(e?.dst);
if (dst_el) {
for (let i = 0; i < dst_el.in_connectors.length; i++) {
const dst_name = dst_el.in_connectors[i].data.name;
Expand Down Expand Up @@ -693,7 +693,7 @@ export class CanvasManager {
// Also update source point of edge
if (edge.src_connector !== null) {
const e = parent_element?.data?.state?.edges[edge.id];
const src_el = parent_graph.node(e?.src);
const src_el = parent_graph?.node(e?.src);
if (src_el) {
for (let i = 0; i < src_el.out_connectors.length; i++) {
const out_name = src_el.out_connectors[i].data.name;
Expand Down
117 changes: 69 additions & 48 deletions src/renderer/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { memletTreeComplete } from '../utils/sdfg/memlet_trees';
import {
check_and_redirect_edge, deletePositioningInfo, deleteSDFGNodes,
deleteCFGBlocks, findExitForEntry, findGraphElementByUUID,
getPositioningInfo, get_uuid_graph_element, findRootCFG
getPositioningInfo, getGraphElementUUID, findRootCFG
} from '../utils/sdfg/sdfg_utils';
import {
traverseSDFGScopes
Expand Down Expand Up @@ -112,7 +112,7 @@ type JsonSDFGElemFunction = (
export type CFGListType = {
[id: string]: {
jsonObj: JsonSDFGControlFlowRegion,
graph: DagreGraph,
graph: DagreGraph | null,
}
};

Expand Down Expand Up @@ -902,6 +902,7 @@ export class SDFGRenderer extends EventEmitter {
// Create the initial SDFG layout
// Loading animation already started in the file_read_complete function in sdfv.ts
// to also include the JSON parsing step.
this.updateCFGList();
this.relayout();

// Set mouse event handlers
Expand Down Expand Up @@ -1016,42 +1017,57 @@ export class SDFGRenderer extends EventEmitter {
this.canvas_manager?.draw_async();
}

public setSDFG(new_sdfg: JsonSDFG, layout: boolean = true): void {
this.sdfg = new_sdfg;

if (layout) {
this.add_loading_animation();
setTimeout(() => {
this.relayout();
}, 10);

this.draw_async();
}

// Update info box
if (this.selected_elements.length === 1) {
const uuid = get_uuid_graph_element(this.selected_elements[0]);
if (this.graph)
this.sdfv_instance.fill_info(
findGraphElementByUUID(this.cfgList, uuid)
);
}

public updateCFGList() {
// Update SDFG metadata
this.cfgTree = {};
this.cfgList = {};
this.cfgList[0] = {
jsonObj: this.sdfg,
graph: null,
};

this.doForAllSDFGElements(
(_oGroup, oInfo, obj) => {
const cfgId = (obj as JsonSDFGControlFlowRegion).cfg_list_id;
if (obj.type === SDFGElementType.NestedSDFG &&
obj.attributes.sdfg)
obj.attributes.sdfg) {
this.cfgTree[obj.attributes.sdfg.cfg_list_id] =
oInfo.sdfg.cfg_list_id;
else if (cfgId !== undefined && cfgId >= 0)
} else if (cfgId !== undefined && cfgId >= 0) {
this.cfgTree[cfgId] = oInfo.cfgId;
this.cfgList[cfgId] = {
jsonObj: obj as JsonSDFGControlFlowRegion,
graph: null,
};
}
}
);
}

public setSDFG(new_sdfg: JsonSDFG, layout: boolean = true): void {
this.sdfg = new_sdfg;

// Update info box
if (this.selected_elements.length === 1) {
const uuid = getGraphElementUUID(this.selected_elements[0]);
if (this.graph)
this.sdfv_instance.fill_info(
findGraphElementByUUID(this.cfgList, uuid)
);
}

if (layout) {
this.updateCFGList();

this.add_loading_animation();
setTimeout(() => {
this.relayout();
}, 10);

this.draw_async();
}
}

// Set mouse events (e.g., click, drag, zoom)
public set_mouse_handlers(): void {
const canvas = this.canvas;
Expand Down Expand Up @@ -1136,7 +1152,8 @@ export class SDFGRenderer extends EventEmitter {
if (!this.ctx)
throw new Error('No context found while performing layouting');

this.cfgList = {};
for (const cfgId in this.cfgList)
this.cfgList[cfgId].graph = null;
this.graph = relayoutStateMachine(
this.ctx, this.sdfg, this.sdfg, this.cfgList,
this.state_parent_list, !SDFVSettings.showAccessNodes, undefined
Expand Down Expand Up @@ -2708,7 +2725,7 @@ export class SDFGRenderer extends EventEmitter {
// Do not move element individually if it is
// moved together with its parent state
const state_parent =
this.cfgList[list_id].graph.node(
this.cfgList[list_id].graph?.node(
el.parent_id!.toString()
);
if (state_parent &&
Expand Down Expand Up @@ -3028,7 +3045,7 @@ export class SDFGRenderer extends EventEmitter {
if (obj instanceof AccessNode) {
if (obj.hovered && hover_changed) {
traverseSDFGScopes(
this.cfgList[obj.sdfg.cfg_list_id].graph,
this.cfgList[obj.sdfg.cfg_list_id].graph!,
(node: any) => {
// If node is a state, then visit sub-scope
if (node instanceof State)
Expand All @@ -3044,7 +3061,7 @@ export class SDFGRenderer extends EventEmitter {
}
else if (!obj.hovered && hover_changed) {
traverseSDFGScopes(
this.cfgList[obj.sdfg.cfg_list_id].graph,
this.cfgList[obj.sdfg.cfg_list_id].graph!,
(node: any) => {
// If node is a state, then visit sub-scope
if (node instanceof State)
Expand Down Expand Up @@ -3269,11 +3286,11 @@ export class SDFGRenderer extends EventEmitter {
this.emit(
'add_element',
this.add_type,
get_uuid_graph_element(
getGraphElementUUID(
foreground_elem
),
undefined,
get_uuid_graph_element(start),
getGraphElementUUID(start),
this.add_edge_start_conn ?
this.add_edge_start_conn.data.name :
undefined,
Expand All @@ -3292,7 +3309,7 @@ export class SDFGRenderer extends EventEmitter {
this.emit(
'add_element',
this.add_type,
get_uuid_graph_element(
getGraphElementUUID(
foreground_elem
),
this.add_mode_lib || undefined
Expand All @@ -3303,7 +3320,7 @@ export class SDFGRenderer extends EventEmitter {
this.emit(
'add_element',
this.add_type,
get_uuid_graph_element(
getGraphElementUUID(
foreground_elem
)
);
Expand Down Expand Up @@ -3707,19 +3724,25 @@ export class SDFGRenderer extends EventEmitter {
// Add all contents of the CFG.
const cfg: JsonSDFGControlFlowRegion = cfgNode.data.block;
const ownCfgId = cfg.cfg_list_id;
cfgs.add(ownCfgId);
if (!(ownCfgId in blocks))
blocks[ownCfgId] = new Set();

for (const blockId of cfgNode.data.graph.nodes()) {
const block = cfgNode.data.graph.node(blockId);
if (block instanceof ControlFlowRegion) {
const nCfg: JsonSDFGControlFlowRegion = block.data.block;
const nCfgId = nCfg.cfg_list_id;
cfgs.add(nCfgId);
addCutoutCFG(ownCfgId, block);
} else {
addCutoutState(ownCfgId, block);
if (cfgNode.data.graph) {
for (const blockId of cfgNode.data.block.nodes.keys()) {
const block = cfgNode.data.graph.node(blockId);
if (block instanceof ControlFlowRegion) {
const nCfg: JsonSDFGControlFlowRegion = block.data.block;
const nCfgId = nCfg.cfg_list_id;
cfgs.add(nCfgId);
addCutoutCFG(ownCfgId, block);
} else {
addCutoutState(ownCfgId, block);
}
}
} else {
for (const blockId of cfgNode.data.block.nodes.keys())
blocks[ownCfgId].add(blockId);
}

blocks[cfgId].add(cfgNode.id);
Expand Down Expand Up @@ -3747,7 +3770,8 @@ export class SDFGRenderer extends EventEmitter {
// Clear selection and redraw
this.deselect();

if (Object.keys(nodes).length === 0) { // Nothing to cut out
if (Object.keys(nodes).length + Object.keys(blocks).length === 0) {
// Nothing to cut out
this.draw_async();
return;
}
Expand Down Expand Up @@ -4045,11 +4069,8 @@ function relayoutStateMachine(
(g as any).width = bb.width;
(g as any).height = bb.height;

// Add CFG to global store.
cfgList[stateMachine.cfg_list_id] = {
jsonObj: stateMachine,
graph: g,
};
// Add CFG graph to global store.
cfgList[stateMachine.cfg_list_id].graph = g;

return g;
}
Expand Down
18 changes: 13 additions & 5 deletions src/utils/sdfg/sdfg_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
JsonSDFGState
} from '../../index';
import {
ControlFlowRegion,
Edge,
SDFGElement,
SDFGElementType,
Expand Down Expand Up @@ -42,18 +43,23 @@ export function findExitForEntry(
*
* @returns String containing the UUID
*/
export function get_uuid_graph_element(element: SDFGElement | null): string {
export function getGraphElementUUID(element: SDFGElement | null): string {
const undefined_val = -1;
const cfgId = (
element instanceof ControlFlowRegion ?
element.data.block.cfg_list_id :
element?.cfg?.cfg_list_id
) ?? undefined_val;
if (element instanceof State) {
return (
element.sdfg.cfg_list_id + '/' +
cfgId + '/' +
element.id + '/' +
undefined_val + '/' +
undefined_val
);
} else if (element instanceof SDFGNode) {
return (
element.sdfg.cfg_list_id + '/' +
cfgId + '/' +
element.parent_id + '/' +
element.id + '/' +
undefined_val
Expand All @@ -63,7 +69,7 @@ export function get_uuid_graph_element(element: SDFGElement | null): string {
if (element.parent_id !== null && element.parent_id !== undefined)
parent_id = element.parent_id;
return (
element.sdfg.cfg_list_id + '/' +
cfgId + '/' +
parent_id + '/' +
undefined_val + '/' +
element.id
Expand Down Expand Up @@ -114,9 +120,11 @@ export function findGraphElementByUUID(
return null;

const graph = cfgList[cfgId].graph;
if (graph === null)
return null;

let state = null;
if (stateId !== '-1' && graph !== undefined)
if (stateId !== '-1')
state = graph.node(stateId);

let element = null;
Expand Down
3 changes: 2 additions & 1 deletion src/utils/sdfg/traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
* signature as `func`).
**/
export function traverseSDFGScopes(
sdfg: DagreGraph, func: CallableFunction, postSubscopeFunc?: CallableFunction
sdfg: DagreGraph, func: CallableFunction,
postSubscopeFunc?: CallableFunction
): void {
function scopesRecursive(
graph: DagreGraph, nodes: string[], processedNodes?: Set<string>
Expand Down

0 comments on commit d9dbab1

Please sign in to comment.