Skip to content

Implement Communication through Shared Scripts #49

Open

Description

@AndreVanDelft

Process communication though shared scripts is a generalisation of the script refinement.
One way to view this is: a called script may have multiple synchronous callers.
There are some decisions outstanding, with respect to syntax and semantics.
Below I hope to address all issues.

(work in progress)

Syntax

a, b                    = x // 2 partners
a, b, c                 = x // 3 partners

a(i:Int), b(??j: Int)   = x // parameters
a(i:Int), b(??i: _)     = x // parameters; type of second i not to be named

a...                    = x // one or more a's communicate
a..., b                 = x // one or more a's, and one b communicate
a..?, b                 = x // zero or more a's, and one b communicate
a?, b                   = x // zero or one a, and one b communicate

a<-(i:Int), a->(??i: _) = x // arrows are allowed as trailer of name
a<-->(??i: Int)         = x // shorthand form of the previous line
a<-...->(??i: Int)      = x // shorthand for a<-(i:Int), a->(??i: _)...
a<-..?->(??i: Int)      = x // shorthand for a<-(i:Int), a->(??i: _)..?

For a shared parameters, only one communicating script shall define its real type;
this communication script one shall not have arity "zero or one" (a?) or "zero or more" (a..?).
In the other communication scripts the type shall be specified as _.
This way the programmer shows he knows that the parameter is shared.

A declared script that does not communicate with another one, will by default use the efficient script call mechanism, although the communication mechanism would in principle also apply. By having += rather than = in the declaration, the communication mechanism will be used instead. This way such a script may also occur in other communications, e.g.,

  a,b = x
  a   += y

Semantics

In the execution cycle, with a just higher priority than normal code fragments execution, it is checked whether a set of communication scripts is activate that could constitute a communication.

A set of active communication scripts may do so if:

  • they belong to the same owner object
  • they have been activated in parallel, i.e. pairwise they pend to different child nodes of parallel operator nodes:&, &&, |, ||, (** **), root (the latter two are anchors for process launching).
  • if they have parameters of the same name, then those that are not actually output parameters (incl possibly adapting parameters) must all have an equal value.

Note that a given communication script may be eligible for multiple communications. E.g., for one a there may be two distinct a,b communications possible, in case there are 2 b's active. Also there may be another communication a,c available, and a c might be active.

It is not yet clear how to deal with this. Maybe it is best to find the first applicable communication, and then assume that this will be the one and only one. However, in a... we want to have a maximal set; that is: if possible the applicable communication should be extended with additional a's, as long as these belong to the same owner etc.

In case there are multiple scripts that share a parameter name, which one to take for the formal value? If none of these are not output parameters then all values should be equal; take that value.
Else take the parameter of the leftmost named script that has the real type for the parameter specified; in case that is of the form a... then any of those communication scripts will do.

Implementation

Parser

The tasks to be done in the parser seem relatively straightforward.
Special attention will be needed to check parameter names: if one communication script has a parameter with the same name as an earlier communication script, then, and only then, its type must be specified as _.

DSL

This may be the hardest part of implementing communication.

a(i:Int), b(j:Int) = println: (i+j)
a(i:Int), c = ...

would, among others, result in:

def _communication_a_b: Script[Unit] = subscript.DSL._communication_2(this) {
  (partner0: CommScript) =>
  (partner1: CommScript) =>
  (_node: subscript.vm.ScriptTrait[Any]) =>
      implicit val script = _node
      subscript.DSL._tiny[Any] (_node => {
        implicit val here = _node
        println(subscript.DSL._maybeVarCall("i"), subscript.DSL._maybeVarCall("j"))
      }, true)
}

object Communication_a_b extends Communication {
  override def communication: Script[Unit] = _communication_a_b
  override def nPartnerScripts: Int = 2 
}
class CommScript_a(owner: Any, i:Int) extends CommScript {
  override def symbol = Symbol("a")
  override def communications = (Communication_a_b, 0, ARITY_ONE) ::
                                (Communication_a_c, 0, ARITY_ONE) :: Nil
  override def parameters = Map(`i -> i)
}

def a(i:Int) = new CommScript_a(this, i)

subscript.DSL._maybeVarCall now also has to deal with formal parameters of the communicating partners.

The structure that contains the call to println should be a def; an object or class would yield a wrong this value.

The meaning of (Communication_a_b, 0, ARITY_ONE, true) is:

  • refer to object Communication_a_b
  • this CommScript_a is the first partner script there (index = 0)
  • the arity there is 1, rather than 0 or 1, 0 or more, 1 or more.

The parameters method reveals this script's parameters. With this information it should be possible to sort out what communicating scripts have compatible parameters, and also how to transfer these parameters (note: output parameters are wrapped in ActualOutputParameter).
Also the fact that script has parameters that might be used, so the communication method will contain (partner0: CommScript) => in order to get access to this data through _maybeVarCall.

Establishing communication

The script executor or a more separate engine will establish communications.

An activated, not suspended, communication script for which no communication has yet been established, is called 'free'. As soon as it is part of an established communication, it is called a 'partner'.

When

The engine will know newly activated, deactivated, suspended and resumed communication scripts. In case there are newly activated or suspended code fragments, it should be established whether new communications need to be activated, just before a normal code fragment would be executed.
For that purpose the script executor keeps track of two sets: activatedOrResumedCommunicationScripts and deactivatedOrSuspendedCommunicationScripts (or maybe 4 sets).

Each time activatedOrResumedCommunicationScripts goes from empty to non-empty, a special message 'EstablishCommunicationsis put in the queue; this has a slightly higher priority thanCFToBeExecuted`.

Owner object

Communicating scripts must have the same owner object. This will go "automatically" since generated structures such as CommScript_a are local to the owner object.

Parallelism detection

Among others the engine should see whether a given set of communication scripts are active in parallel branches of the call graph. In Scriptic a complicated pruning algorithm was used for this. Maybe this will now become easier by collecting for each such communicating script a hash map with

  • keys: the ancestor nodes that are a parallel operator
  • values: the children of those parallel operators that are also an ancestor of the communicating script

Complexity and efficiency

The engine has some options for dealing with complexity.
To try all possibilities out may be order of 2^n, with n the number of free communication scripts.
In principle the application programmer should make sure that the number of possibilities will be limited, so the engine does not need to care for this aspect.

The engine should each time only try to establish communications where newly activated or resumed free communication scripts are involved. This way no unnecessary doubling of earlier attempts will happen. This is also the rationale for having the set activatedOrResumedCommunicationScripts.
The other set, deactivatedOrSuspendedCommunicationScripts, may or may not be used.

Call graph messages

The current call graph messages will become more complicated, since we will have a real call graph instead of a call tree.

Messages that went simply upward (AAStarted, AAHappened, ...) now need to multiply at Communication nodes (having upward branches); and likewise they need continuations at nodes (e.g. &) that have downward branches.

Conversely, messages that went simply downward (Exclude, Suspend, Resume) now need to do a continuation at Communication nodes.

So for each of these messages a continuation variant is needed. The simplest way would be to add a flag to the original message class. Setting this flag makes the message priority slightly lower.

Neutral Elements Issues

Neutral elements that are at the top level of a communicating script may cause seeming contradictions.
E.g.,

a,b = [] // explicit neutral element
a,c = if p then x // implicit
a,d = e // possible, depending on e

in all three cases the communication may state it behaves neutrally, i.e. as a 0 or 1. But what if a has been called from an and-like context (as in a&x), whereas the other party (b, c, or d) has been called from an or-like context? That seems a contradiction. The compiler may yield warnings in the first two cases, but the third case (a,d = e) it has not enough information. In such cases the developer should have known better. The implementation is free to choose any communication partner to determine whether context for the neutral behavior is and-like or or-like.

Misc

The currently existing methods available to code fragments that check for matching parameters and result values should do so for all communicating partners.

Activity

  1. anatoliykmetyuk commented on Jun 18, 2016

    @anatoliykmetyuk
    Contributor

    Communication introduces a new feature to the call graph - it becomes precisely a graph and no longer a tree, so some troubles may arise. For example:

    • The behaviour of the optional breaks is based on the indices of the graph nodes. E.g. a & b & break? & c will not activate c until action is taken in b. The fact of action taking place in b is determined by the index of the AAHappened that arrives at &. Now if a communicates with c and [a & break? & b] & [c & break? & d], the assumptions about the indices of the children of either a or c may be broken and I’m not sure how this line will behave.
    • Concurrent message propagation. As you say, the messages may split at some points. Will the current continuation mechanism be able to handle such duplicates with a mere flag that lowers the priority? How exactly? Say, in a & b the Continuation for & will encounter 2 AAHappened of the same nature - how is it supposed to behave and how will it behave in reality?
      As far as I remember, first, each AAHappened schedules a Continuation on an n-ary node it passes and, second, AAHappened has a larger priority than Continuation. So I suspect we’ll have two parallel AAHappened passing through &, each scheduling a Continuation, and a duplicate AAHappened will get us a duplicate Continuation. The first Continuation will consume the two AAHappened’s and make its decisions. The decisions can include sending a Success or Exclude messages, which both have a higher priority than Continuation. So it can be that when the second Continuation kicks in, the executer will die with an exception since the node it continues is already deactivated.

    I think on the message splitting and merging part we need a separate system. A check to be done before handling any message. It may involve a function (Seq[Message], Node) => Boolean that will determine whether a set of messages should be merged or splitted in a given node. Before a message is handled, we look at the queue for the messages of same type following immediately after this one (since they have the same priority, it is easy to select them; may be 0) and apply this function to see whether to merge/split them.

    Also, a test suite should be designed like that of the OperatorsSuite to test all the possible scenarios in combination with other operators - particularly breaks.

    I also think it is about time to make some tests and specifications for the script executor itself. Hardly we can test the internal machinery by the OperatorsSuite, only the external observation.

  2. AndreVanDelft commented on Jun 18, 2016

    @AndreVanDelft
    ContributorAuthor
    • About your first point: I do not see a problem. Note that there are distinct nodes for the calls (a and c) and for the callee (a_c); the relevant indexes for the children of the &'s may thus well differ.
    • Two or more AAHappened referring to the same AA, will after merge&continuation become 1 again. "...each scheduling a continuation..." - no; the purpose of such a "merging continuation" is that if there is already one, no second one is created; rather they are merged.
  3. anatoliykmetyuk commented on Jun 18, 2016

    @anatoliykmetyuk
    Contributor

    About your first point: I do not see a problem. Note that there are distinct nodes for the calls (a and c) and for the callee (a_c); the relevant indexes for the children of the &'s may thus well differ.

    In the line I referenced, there is a snippet “ a.child.index > node.indexChild_optionalBreak_secondLast” it basically tries to identify the (shared) source of the message by looking at whether the source’s index is greater then the remembered index, I believe the one of the call node, and we have two of them.

    Two or more AAHappened referring to the same AA, will after merge&continuation become 1 again. "...each scheduling a continuation..." - no; the purpose of such a "merging continuation" is that if there is already one, no second one is created; rather they are merged.

    If there’s just one AAHappened at the point of Continuation, it should be created somewhere. Mere priority lowering won’t do here, since there may be many pairs of communicating nodes under the same operand. We need some logic to identify the messages from these pairs, to determine who should be merged with who.

    —
    You are receiving this because you commented.
    Reply to this email directly, view it on GitHub #49 (comment), or mute the thread https://github.com/notifications/unsubscribe/ACfmHfbKDIwRTMD8w8h9jA8s2S4jiXh8ks5qNDTQgaJpZM4I0dac.

  4. AndreVanDelft commented on Jun 18, 2016

    @AndreVanDelft
    ContributorAuthor

    of the call node, and we have two of them

    The child indexes are unique, since a child is a caller, i.e. either a or c, not both. This is one of the good things of having separate nodes for caller & callee. So the indexes are really different.
    About 25 years ago, there was only one node rather than two for a call, and that caused some problems that I don't remember well.

    We need some logic to identify the messages from these pairs

    The logic for the AAHappened continuation messages will be fairly simple:

    • for each kind of message such as AAHappened that requires special continuations there is a hash map of node->continuationMessage
    • Such a continuation message has a set of nodes (e.g. that had a AAHappened). The set does the merging, since its elements are unique.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions