Skip to content

Commit 7d910e6

Browse files
committed
prototype; need cleanup
1 parent 4aa7162 commit 7d910e6

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/benchmark/Sieve.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class Filter extends Reactor {
8888
if (size < numberOfPrimes) {
8989
seen.push(p);
9090
console.log(`Found new prime number ${p}`);
91+
if (!primes.has(p)) {
92+
;
93+
} else {
94+
primes.delete(p);
95+
}
9196
} else {
9297
// Potential prime found.
9398
if (!hasChild.get()) {
@@ -100,6 +105,7 @@ class Filter extends Reactor {
100105
const port = (out as unknown as WritablePort<number>).getPort();
101106
console.log("connecting......");
102107
this.connect(port, n.inp);
108+
printSieveGraph();
103109
// FIXME: this updates the dependency graph, but it doesn't redo the topological sort
104110
// For a pipeline like this one, it is not necessary, but in general it is.
105111
// Can we avoid redoing the entire sort?
@@ -136,9 +142,20 @@ class Sieve extends App {
136142
}
137143

138144

139-
const sieve = new Sieve("Sieve");
145+
const sieve = new Sieve("Sieve", undefined, undefined, undefined, ()=>{globalThis.graphDebugLogger?.write("debug0.json")});
146+
147+
const printSieveGraph = (): void => {
148+
const graph = sieve["_getPrecedenceGraph"]();
149+
const hierarchy = sieve._getNodeHierarchyLevels();
150+
const str = graph.toMermaidString(undefined, hierarchy);
151+
const time = sieve["util"].getElapsedLogicalTime();
152+
console.log(str);
153+
console.log(time);
154+
}
140155

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

159+
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]);
160+
144161
sieve._start();

src/core/graph.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {Reaction} from "./reaction";
88
import type {Sortable, Variable} from "./types";
99
import {GraphDebugLogger, Log} from "./util";
1010

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

2525
declare global {
26+
// eslint-disable-next-line no-var
2627
var graphDebugLogger: GraphDebugLogger | undefined;
28+
// eslint-disable-next-line no-var
2729
var recording: boolean;
2830
}
2931

30-
const debugHelper = (): void => {
32+
const debugHelper = (stacktrace: Error): void => {
3133
if (globalThis.recording) { return; }
34+
// If recording now, do not record any subsequent operations,
35+
// as recursion hell might involve when calling `capture`,
36+
// causing infinite loop.
3237
globalThis.recording = true;
3338
if (globalThis.graphDebugLogger == null) {
3439
return
3540
}
3641

37-
const err = new Error();
38-
console.log(err.stack);
3942
const debuglogger = globalThis.graphDebugLogger;
40-
debuglogger.capture(err);
43+
debuglogger.capture(stacktrace);
4144
globalThis.recording = false;
4245
};
4346

@@ -87,7 +90,7 @@ export class PrecedenceGraph<T> {
8790
* @param node
8891
*/
8992
addNode(node: T): void {
90-
debugHelper();
93+
debugHelper(new Error("addNode"));
9194
if (!this.adjacencyMap.has(node)) {
9295
this.adjacencyMap.set(node, new Set());
9396
}
@@ -178,8 +181,7 @@ export class PrecedenceGraph<T> {
178181
* @param downstream The node at which the directed edge ends.
179182
*/
180183
addEdge(upstream: T, downstream: T): void {
181-
debugHelper();
182-
console.log("!");
184+
debugHelper(new Error("addEdge"));
183185
const deps = this.adjacencyMap.get(downstream);
184186
if (deps == null) {
185187
this.adjacencyMap.set(downstream, new Set([upstream]));
@@ -232,7 +234,7 @@ export class PrecedenceGraph<T> {
232234
* @param edgesWithIssue An array containing arrays with [origin, effect].
233235
* Denotes edges in the graph that causes issues to the execution, will be visualized as `--x` in mermaid.
234236
*/
235-
toMermaidString(edgesWithIssue?: Array<[T, T]>, hierarchy?: HierarchyGraphLevel<T>): string {
237+
toMermaidString(edgesWithIssue?: Array<[T, T]>, hierarchy?: HierarchyGraphLevel<T>, uniqueNames?: boolean): string {
236238
if (edgesWithIssue == null) edgesWithIssue = [];
237239
let result = "graph\n";
238240
const nodeToSymbolString = new Map<T, string>();
@@ -250,9 +252,10 @@ export class PrecedenceGraph<T> {
250252

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

0 commit comments

Comments
 (0)