Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Savina Fork Join (Create) #205

Open
wants to merge 3 commits into
base: mutation/full
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/benchmark/FJCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Typescript runtime implementation of Fork Join - Create benchmark programme
* of Savina benchmark suite.
* @author axmmisaka (github.com/axmmisaka)
*/

import {
Log,
Reactor,
App,
type TimeValue,
InPort,
OutPort
} from "../core/internal";

Log.global.level = Log.levels.ERROR;

const N = 4000;

export class ForkJoinReactor extends Reactor {
// private valueToCalculate;
triggerPort: InPort<number>;
constructor(parent: Reactor, name = "Innocent Reactor") {
super(parent);
this.triggerPort = new InPort(this);
this.addReaction(
[this.triggerPort],
[this.triggerPort],
(inp) => {
const val = inp.get();
if (val == null) {
throw new Error(`inp is absent for ${this._getFullyQualifiedName()}`)
}
const sint = Math.sin(val);
const res = sint * sint;
if (res <= 0) {
throw new Error(`this is kinda insane, ${res}`);
} else {
console.log(`I am ${this._getFullyQualifiedName()}. I finished calculating after ${this.util.getElapsedLogicalTime()}; ${this.util.getElapsedPhysicalTime()}. Result is ${res}`)
}
}
);
}
}

export class FJCreator extends Reactor {
forks: ForkJoinReactor[];
outp: OutPort<number>;

constructor(parent: Reactor) {
super(parent);
this.forks = [];
this.outp = new OutPort(this);
this.addMutation(
[this.startup],
[this.writable(this.outp)],
function (this, outp) {
console.log("startup triggered!")
for (let i = 0; i < N; ++i) {
const fork = this.addSibling(ForkJoinReactor, `FJReactor ${i}`);
// this.getReactor().forks.push(fork);
this.connect(outp.getPort(), fork.triggerPort);
console.log(`Fork ${i} created at physical time ${this.util.getElapsedPhysicalTime()}`)
}
outp.set(114.514);
}
)
}
}

export class FJHost extends App {
creator: FJCreator;
constructor(
name: string,
timeout: TimeValue | undefined = undefined,
keepAlive = false,
fast = false,
success?: () => void,
fail?: () => void
) {
super(timeout, keepAlive, fast, success, fail);
this.creator = new FJCreator(this);
}
}

const fj = new FJHost("FJ");
fj._start();
59 changes: 59 additions & 0 deletions src/core/reactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ export abstract class Reactor extends Component {
*/
private readonly _keyChain = new Map<Component, symbol>();

// This is the keychain for creation, i.e. if Reactor R's mutation created reactor B,
// then R is B's creator, even if they are siblings. R should have access to B,
// at least semantically......?
private readonly _creatorKeyChain = new Map<Component, symbol>();

/**
* This graph has in it all the dependencies implied by this container's
* ports, reactions, and connections.
Expand Down Expand Up @@ -387,6 +392,9 @@ export abstract class Reactor extends Component {
return owner._getKey(component, this._keyChain.get(owner));
}
}
return component
.getContainer()
._getKey(component, this._creatorKeyChain.get(component.getContainer()));
}

/**
Expand Down Expand Up @@ -467,6 +475,20 @@ export abstract class Reactor extends Component {
public delete(reactor: Reactor): void {
reactor._delete();
}

public addChild<R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
): R {
return this.reactor._addChild(constructor, ...args);
}

public addSibling<R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
): R {
return this.reactor._addSibling(constructor, ...args);
}
};

/**
Expand Down Expand Up @@ -1555,6 +1577,33 @@ export abstract class Reactor extends Component {
toString(): string {
return this._getFullyQualifiedName();
}

protected _addChild<R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
): R {
const newReactor = new constructor(this, ...args);
return newReactor;
}

protected _addSibling<R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
): R {
if (this._getContainer() == null) {
throw new Error(
`Reactor ${this} does not have a parent. Sibling is not well-defined.`
);
}
if (this._getContainer() === this) {
throw new Error(
`Reactor ${this} is self-contained. Adding sibling creates logical issue.`
);
}
const newReactor = this._getContainer()._addChild(constructor, ...args);
this._creatorKeyChain.set(newReactor, newReactor._key);
return newReactor;
}
}

/*
Expand Down Expand Up @@ -1795,6 +1844,16 @@ export interface MutationSandbox extends ReactionSandbox {

getReactor: () => Reactor; // Container

addChild: <R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
) => R;

addSibling: <R extends Reactor, G extends unknown[]>(
constructor: new (container: Reactor, ...args: G) => R,
...args: G
) => R;

// FIXME:
// forkJoin(constructor: new () => Reactor, ): void;
}
Expand Down