|
| 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 = 4000; |
| 19 | + |
| 20 | +export class ForkJoinReactor extends Reactor { |
| 21 | + // private valueToCalculate; |
| 22 | + triggerPort: InPort<number>; |
| 23 | + constructor(parent: Reactor, name = "Innocent Reactor") { |
| 24 | + super(parent); |
| 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._getFullyQualifiedName()}`) |
| 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._getFullyQualifiedName()}. I finished calculating after ${this.util.getElapsedLogicalTime()}; ${this.util.getElapsedPhysicalTime()}. 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); |
| 52 | + this.forks = []; |
| 53 | + this.outp = new OutPort(this); |
| 54 | + this.addMutation( |
| 55 | + [this.startup], |
| 56 | + [this.writable(this.outp)], |
| 57 | + function (this, outp) { |
| 58 | + console.log("startup triggered!") |
| 59 | + for (let i = 0; i < N; ++i) { |
| 60 | + const fork = this.addSibling(ForkJoinReactor, `FJReactor ${i}`); |
| 61 | + // this.getReactor().forks.push(fork); |
| 62 | + this.connect(outp.getPort(), fork.triggerPort); |
| 63 | + console.log(`Fork ${i} created at physical time ${this.util.getElapsedPhysicalTime()}`) |
| 64 | + } |
| 65 | + outp.set(114.514); |
| 66 | + } |
| 67 | + ) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +export class FJHost extends App { |
| 72 | + creator: FJCreator; |
| 73 | + constructor( |
| 74 | + name: string, |
| 75 | + timeout: TimeValue | undefined = undefined, |
| 76 | + keepAlive = false, |
| 77 | + fast = false, |
| 78 | + success?: () => void, |
| 79 | + fail?: () => void |
| 80 | + ) { |
| 81 | + super(timeout, keepAlive, fast, success, fail); |
| 82 | + this.creator = new FJCreator(this); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +const fj = new FJHost("FJ"); |
| 87 | +fj._start(); |
0 commit comments