Skip to content

Commit

Permalink
prototype; need cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
axmmisaka committed Jul 19, 2023
1 parent 28f618b commit 005e493
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
19 changes: 18 additions & 1 deletion src/benchmark/Sieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class Filter extends Reactor {
if (size < numberOfPrimes) {
seen.push(p);
console.log(`Found new prime number ${p}`);
if (!primes.has(p)) {
;
} else {
primes.delete(p);
}
} else {
// Potential prime found.
if (!hasChild.get()) {
Expand All @@ -100,6 +105,7 @@ class Filter extends Reactor {
const port = (out as unknown as WritablePort<number>).getPort();
console.log("connecting......");
this.connect(port, n.inp);
printSieveGraph();
// FIXME: this updates the dependency graph, but it doesn't redo the topological sort
// For a pipeline like this one, it is not necessary, but in general it is.
// Can we avoid redoing the entire sort?
Expand Down Expand Up @@ -136,9 +142,20 @@ class Sieve extends App {
}


const sieve = new Sieve("Sieve");
const sieve = new Sieve("Sieve", undefined, undefined, undefined, ()=>{globalThis.graphDebugLogger?.write("debug0.json")});

const printSieveGraph = (): void => {
const graph = sieve["_getPrecedenceGraph"]();
const hierarchy = sieve._getNodeHierarchyLevels();
const str = graph.toMermaidString(undefined, hierarchy);
const time = sieve["util"].getElapsedLogicalTime();
console.log(str);
console.log(time);
}

globalThis.graphDebugLogger = new GraphDebugLogger(sieve);
globalThis.recording = false;

const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 9972, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]);

sieve._start();
23 changes: 13 additions & 10 deletions src/core/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {Reaction} from "./reaction";
import type {Sortable, Variable} from "./types";
import {GraphDebugLogger, Log} from "./util";


// TODO: find a way to to this with decorators.
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/no-explicit-any
const debugLoggerDecorator = (target: any, context: ClassMethodDecoratorContext) => {
if (context.kind === "method") {
Expand All @@ -23,21 +23,24 @@ const debugLoggerDecorator = (target: any, context: ClassMethodDecoratorContext)
}

declare global {
// eslint-disable-next-line no-var
var graphDebugLogger: GraphDebugLogger | undefined;
// eslint-disable-next-line no-var
var recording: boolean;
}

const debugHelper = (): void => {
const debugHelper = (stacktrace: Error): void => {
if (globalThis.recording) { return; }
// If recording now, do not record any subsequent operations,
// as recursion hell might involve when calling `capture`,
// causing infinite loop.
globalThis.recording = true;
if (globalThis.graphDebugLogger == null) {
return
}

const err = new Error();
console.log(err.stack);
const debuglogger = globalThis.graphDebugLogger;
debuglogger.capture(err);
debuglogger.capture(stacktrace);
globalThis.recording = false;
};

Expand Down Expand Up @@ -87,7 +90,7 @@ export class PrecedenceGraph<T> {
* @param node
*/
addNode(node: T): void {
debugHelper();
debugHelper(new Error("addNode"));
if (!this.adjacencyMap.has(node)) {
this.adjacencyMap.set(node, new Set());
}
Expand Down Expand Up @@ -178,8 +181,7 @@ export class PrecedenceGraph<T> {
* @param downstream The node at which the directed edge ends.
*/
addEdge(upstream: T, downstream: T): void {
debugHelper();
console.log("!");
debugHelper(new Error("addEdge"));
const deps = this.adjacencyMap.get(downstream);
if (deps == null) {
this.adjacencyMap.set(downstream, new Set([upstream]));
Expand Down Expand Up @@ -232,7 +234,7 @@ export class PrecedenceGraph<T> {
* @param edgesWithIssue An array containing arrays with [origin, effect].
* Denotes edges in the graph that causes issues to the execution, will be visualized as `--x` in mermaid.
*/
toMermaidString(edgesWithIssue?: Array<[T, T]>, hierarchy?: HierarchyGraphLevel<T>): string {
toMermaidString(edgesWithIssue?: Array<[T, T]>, hierarchy?: HierarchyGraphLevel<T>, uniqueNames?: boolean): string {
if (edgesWithIssue == null) edgesWithIssue = [];
let result = "graph\n";
const nodeToSymbolString = new Map<T, string>();
Expand All @@ -250,9 +252,10 @@ export class PrecedenceGraph<T> {

if (hierarchy != null) {
let counter = 0;
let subgraphCounter = 0;
const recurse = (h: HierarchyGraphLevel<T>, level: number): void => {
const indent = " ".repeat(level);
result += level === 0 ? "" : `${indent}subgraph "${h.name}"\n`;
result += level === 0 ? "" : `${indent}subgraph "${(uniqueNames ?? false) ? h.name : `sg${subgraphCounter++}`}"\n`;
for (const v of h.nodes) {
result += `${indent} ${counter}["${getNodeString(v, String(counter))}"]\n`
nodeToSymbolString.set(v, `${counter++}`);
Expand Down

0 comments on commit 005e493

Please sign in to comment.