Skip to content

Commit 0659998

Browse files
committed
Add working and non working examples
1 parent 7413e83 commit 0659998

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

src/benchmark/FJCreate.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Typescript runtime implementation of Fork Join - Create benchmark programme
3+
* of Savina benchmark suite.
4+
* @author axmmisaka (github.com/axmmisaka)
5+
*/
6+
7+
import {
8+
Log,
9+
Reactor,
10+
App,
11+
type TimeValue,
12+
InPort,
13+
OutPort
14+
} from "../core/internal";
15+
16+
Log.global.level = Log.levels.ERROR;
17+
18+
const N = 400;
19+
20+
export class ForkJoinReactor extends Reactor {
21+
// private valueToCalculate;
22+
triggerPort: InPort<number>;
23+
constructor(parent: Reactor, name = "Innocent Reactor") {
24+
super(parent, name);
25+
this.triggerPort = new InPort(this);
26+
this.addReaction(
27+
[this.triggerPort],
28+
[this.triggerPort],
29+
(inp) => {
30+
const val = inp.get();
31+
if (val == null) {
32+
throw new Error(`inp is absent for ${this._name}`)
33+
}
34+
const sint = Math.sin(val);
35+
const res = sint * sint;
36+
if (res <= 0) {
37+
throw new Error(`this is kinda insane, ${res}`);
38+
} else {
39+
console.log(`I am ${this._name}. I finished calculating at time ${this.util.getCurrentLogicalTime()}. Result is ${res}`)
40+
}
41+
}
42+
);
43+
}
44+
}
45+
46+
export class FJCreator extends Reactor {
47+
forks: ForkJoinReactor[];
48+
outp: OutPort<number>;
49+
50+
constructor(parent: Reactor) {
51+
super(parent, "FJCreator");
52+
this.forks = [];
53+
this.outp = new OutPort(this);
54+
for (let i = 0; i < N; ++i) {
55+
this.addMutation(
56+
[this.startup],
57+
[],
58+
() => {
59+
const fork = this._uncheckedAddSibling(ForkJoinReactor, `FJReactor ${i}`);
60+
this.forks.push(fork);
61+
this._connect(this.outp, fork.triggerPort);
62+
}
63+
)
64+
}
65+
}
66+
}
67+
68+
export class FJHost extends App {
69+
creator: FJCreator;
70+
constructor(
71+
name: string,
72+
timeout: TimeValue | undefined = undefined,
73+
keepAlive = false,
74+
fast = false,
75+
success?: () => void,
76+
fail?: () => void
77+
) {
78+
super(timeout, keepAlive, fast, success, fail, name);
79+
this.creator = new FJCreator(this);
80+
}
81+
}
82+
83+
const fj = new FJHost("FJ");
84+
fj._start();

src/benchmark/FJCreateNaive.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Typescript runtime implementation of Fork Join - Create benchmark programme
3+
* of Savina benchmark suite.
4+
* @author axmmisaka (github.com/axmmisaka)
5+
*/
6+
7+
import {
8+
Log,
9+
Reactor,
10+
App,
11+
type TimeValue,
12+
InPort,
13+
OutPort
14+
} from "../core/internal";
15+
16+
Log.global.level = Log.levels.ERROR;
17+
18+
const N = 400;
19+
20+
export class ForkJoinReactor extends Reactor {
21+
// private valueToCalculate;
22+
triggerPort: InPort<number>;
23+
constructor(parent: Reactor, name = "Innocent Reactor") {
24+
super(parent, name);
25+
this.triggerPort = new InPort(this);
26+
this.addReaction(
27+
[this.startup]/* [this.triggerPort] */,
28+
[this.triggerPort],
29+
(inp) => {
30+
const val = 69.420 /* inp.get() */;
31+
if (val == null) {
32+
throw new Error(`inp is absent for ${this._name}`)
33+
}
34+
const sint = Math.sin(val);
35+
const res = sint * sint;
36+
if (res <= 0) {
37+
throw new Error(`this is kinda insane, ${res}`);
38+
} else {
39+
console.log(`I am ${this._name}. I finished calculating at time ${this.util.getCurrentLogicalTime()}. Result is ${res}`)
40+
}
41+
}
42+
);
43+
}
44+
}
45+
46+
export class FJCreator extends App {
47+
forks: ForkJoinReactor[];
48+
outp: OutPort<number>;
49+
50+
constructor(
51+
name: string,
52+
timeout: TimeValue | undefined = undefined,
53+
keepAlive = false,
54+
fast = false,
55+
success?: () => void,
56+
fail?: () => void
57+
) {
58+
super(timeout, keepAlive, fast, success, fail, name);
59+
this.forks = [];
60+
this.outp = new OutPort(this);
61+
const key = this._getKey(this.outp);
62+
(this.outp.asWritable(key)).set(69.420);
63+
for (let i = 0; i < N; ++i) {
64+
const fork = this._uncheckedAddChild(ForkJoinReactor, `FJReactor ${i}`);
65+
this.forks.push(fork);
66+
// this._connect(this.outp, fork.triggerPort);
67+
}
68+
}
69+
}
70+
71+
const fj = new FJCreator("FJ");
72+
fj._start()

0 commit comments

Comments
 (0)