A code generator for the CAL actor language

  • Slides: 45
Download presentation
A code generator for the CAL actor language Lars Wernli Supervisor: Joern Janneck, UC

A code generator for the CAL actor language Lars Wernli Supervisor: Joern Janneck, UC Berkeley Professor: Lothar Thiele, ETH Zuerich

What is Ptolemy II? continuous time finite-state machine discrete time Hierarchical, heterogeneous model

What is Ptolemy II? continuous time finite-state machine discrete time Hierarchical, heterogeneous model

Actor oriented design parameters input ports 1 3 ‘A’ ‘C’ 12 ‘’ N Data

Actor oriented design parameters input ports 1 3 ‘A’ ‘C’ 12 ‘’ N Data output ports 42 Actor FIRE tokens ‘L’ 99 state 42 41 tokens ‘P’ Actors decouple data and control

Actor oriented design parameters input ports 1 99 12 ‘’ 45 2 N Actor

Actor oriented design parameters input ports 1 99 12 ‘’ 45 2 N Actor token Data output ports state 45 42 4 41 tokens ‘P’ ‘L’ ‘A’ ‘C’ Actors decouple data and control

Actors in Ptolemy II Firing is divided into three phases: • prefire() 1 time

Actors in Ptolemy II Firing is divided into three phases: • prefire() 1 time – checks whether action can fire or not • fire() n times – calculates output tokens • postfire() 0 or 1 times – updates persistent state

Writing a Ptolemy actor int sum = 0, _sum; prefire() { return N. has.

Writing a Ptolemy actor int sum = 0, _sum; prefire() { return N. has. Token(); } fire() { _sum = sum; int n = N. get. Token(); if (Data. has. Tokens(n)) { _sum = _sum + n; for (int i = 0; i < n; i++) Out 2. put. Token(Data. get. Token()); Out 1. put. Token(_sum); } else { // what to do with the value of n? } } postfire() { sum = _sum; }

Writing a Ptolemy actor int sum = 0, _sum; prefire() { return N. has.

Writing a Ptolemy actor int sum = 0, _sum; prefire() { return N. has. Token(); } fire() { _sum = sum; int n = N. get. Token(); if (Data. has. Tokens(n)) { _sum = _sum + n; for (int i = 0; i < n; i++) Out 2. put. Token(Data. get. Token()); Out 1. put. Token(_sum); } else { // what to do with the value of n? } } postfire() { sum = _sum; }

What is CAL? CAL is a textual language for writing dataflow actors. The actor

What is CAL? CAL is a textual language for writing dataflow actors. The actor just introduced written in CAL: Integer sum : = 0; action N: [n], Data: [d] repeat n ==> Out 1: [sum], Out 2: [d] repeat n do sum : = sum + n; end

Motivation for using CAL • • makes writing actors more accessible reduces amount of

Motivation for using CAL • • makes writing actors more accessible reduces amount of code to be written reduces error probability allows information extraction for model analysis • CAL actors may be reused by other platforms, or new versions of Ptolemy

Design goal for CAL is intended to be retargeted to a variety of platforms

Design goal for CAL is intended to be retargeted to a variety of platforms • make retargeting as simple as possible – modular compiler design – modular code generation

CAL compilation —the big picture. parsing CAL source text Caltrop AST target platform transformation,

CAL compilation —the big picture. parsing CAL source text Caltrop AST target platform transformation, annotation code generation CAL(0) CAL(1) Java platforms CAL(n) Cal. Core Pålsjö/Koala Leg. OS Ptolemy II Moses JGraf. Chart

Generic and specific actor Cal. Core platform specific code generator generic code generator •

Generic and specific actor Cal. Core platform specific code generator generic code generator • Code generator is easy to retarget • Actor core can be reused by other platforms

Code generator and target code design • Design goals 1. Make retargeting the code

Code generator and target code design • Design goals 1. Make retargeting the code generator as simple as possible 2. Reusability of generated code 3. Optimize for speed 1. Challenges 1. specify an interface for generic part of the actor 2. matching the generic actor interface to Ptolemy API

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy mark. As. Changed(this) fire() { listener. rollback. All(); … } postfire() { … listener. commit. All(); } change listener generic variable interface assign(45) Ptolemy specific variable object State: 42 Shadow State: 45

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy rollback. All() fire() { listener. rollback. All(); … } postfire() { … listener. commit. All(); } rollback() change listener generic variable interface Ptolemy specific variable object State: 42 Shadow State: 45

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy mark. As. Changed(this) fire() { listener. rollback. All(); … } postfire() { … listener. commit. All(); } change listener generic variable interface assign(47) Ptolemy specific variable object State: 42 Shadow State: 47

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy fire() { listener. rollback. All(); … } postfire() { … listener. commit. All(); } change listener generic variable interface Ptolemy specific variable object State: 42 Shadow State: 47

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy commit. All() fire() { listener. rollback. All(); … } postfire() { … listener. commit. All(); } change listener commit() generic variable interface Ptolemy specific variable object State: 47 42 Shadow State: 47

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy

State shadowing • Problem: state changing firing in CAL vs state-invariant fire() in Ptolemy fire() { listener. rollback. All(); … } postfire() { … listener. commit. All(); } change listener generic variable interface Ptolemy specific variable object State: 42 47 Shadow State:

Achievements • code generation for full-fledged language - higher-order function closures procedural closures set/list/map

Achievements • code generation for full-fledged language - higher-order function closures procedural closures set/list/map comprehensions input port patterns regular action selectors … • reusability of generated code • code generator easy to retarget to other Java platforms

Achievements • generated actors run with acceptable speed • facilitate retargeting to other languages

Achievements • generated actors run with acceptable speed • facilitate retargeting to other languages (such as C) – design template for code generators • Pålsjö/Koala LTH – reusable infrastructure

Future work – Implement type checking – Describe the transformations on the AST in

Future work – Implement type checking – Describe the transformations on the AST in XML – Retarget the code generator to other platforms (Leg. OS UCB, Moses ETH? ) – Model compilation using CAL actor • Network + actors schedule • Network + actors + schedule actor

It’s time for a demo

It’s time for a demo

Atomic actors in Ptolemy • • • implemented in Java domain polymorph ports parameters

Atomic actors in Ptolemy • • • implemented in Java domain polymorph ports parameters split-phase-firing: – prefire() – postfire()

Atomic actors in Ptolemy • • • implemented in Java domain polymorph ports parameters

Atomic actors in Ptolemy • • • implemented in Java domain polymorph ports parameters split-phase-firing: – prefire() – postfire()

The Ptolemy II GUI

The Ptolemy II GUI

Models in Ptolemy II • actor based • heterogeneous systems • hierarchical • composite

Models in Ptolemy II • actor based • heterogeneous systems • hierarchical • composite actors treated like atomic • directors decouple behavior & control flow

Writing Ptolemy actors in Java. . • . . requires certain knowledge about the

Writing Ptolemy actors in Java. . • . . requires certain knowledge about the Ptolemy II API • . . results in platform specific classes • . . is error-prone • . . is often redundant • . . makes it hard to extract information from the actors Specifying actors in Java is problematic

Writing Ptolemy actors in Java. . • . . requires certain knowledge about the

Writing Ptolemy actors in Java. . • . . requires certain knowledge about the Ptolemy II API • . . results in platform specific classes • . . is error-prone • . . is often redundant • . . makes it hard to extract information from the actors Specifying actors in Java is problematic

A better approach We should be able to generate actors from a more abstract

A better approach We should be able to generate actors from a more abstract description. • Benefits: – makes writing actors more accessible – actors may be retargeted to other platforms, or new versions of Ptolemy – reduces error probability – reduces amount of code to be written – actors get more readable and analyzable

Can you guess what this does? actor B () Double Input ==> Double Output:

Can you guess what this does? actor B () Double Input ==> Double Output: Integer n : = 0; Double sum : = 0; action [a] ==> [sum / n] DO n : = n + 1; sum : = sum + a; end

Can you guess what this does? actor B () Double Input ==> Double Output:

Can you guess what this does? actor B () Double Input ==> Double Output: Integer n : = 0; Double sum : = 0; action [a] ==> [sum / n] : n : = n + 1; sum : = sum + a; end

What about this? actor Prime. Sieve () Integer Input ==> Integer Output: [Integer -->

What about this? actor Prime. Sieve () Integer Input ==> Integer Output: [Integer --> Boolean] filter : = lambda (Integer a) --> Boolean : false end; function divides (Integer a, Integer b) --> Boolean : b mod a = 0 end action [a] ==> [] guard filter(a) end action [a] ==> [a] guard not filter(a) var [Integer --> Boolean] f = filter do filter : = lambda(Integer b) --> Boolean: f(b) or divides(a, b) end; end

Actor. Core vs Ptolemy API • state management – fire vs firen/postfire – state

Actor. Core vs Ptolemy API • state management – fire vs firen/postfire – state changing computation vs state-invariant fire • input ports – random access to input channels vs sequential read methods

The runtime environment 1. Variable objects & change listener – – Support state shadowing

The runtime environment 1. Variable objects & change listener – – Support state shadowing Provide a generic interface to the Ptolemy Token and Parameter objects 2. Port wrappers – – Emulate random access input ports Provide a generic interface to the Ptolemy Typed. IOPorts Factory – – Creates wrapping objects facilitates decoupling between Actor. Core and Ptolemy API

Three implementation details • Actors at runtime 1. How the Pt. Actor passes Ptolemy

Three implementation details • Actors at runtime 1. How the Pt. Actor passes Ptolemy objects to the Actor. Core via factory 2. How CAL scopes are represented in the Actor. Core • The code generator 3. How the code generator uses the visitor pattern to traverse the AST

1. Actors and the Factory

1. Actors and the Factory

2. CAL scopes actor Deadlock. Prime. Sieve () Integer Input ==> Integer Output: [Integer

2. CAL scopes actor Deadlock. Prime. Sieve () Integer Input ==> Integer Output: [Integer --> Boolean] filter : = lambda (Integer a) --> Boolean : false end; action [a] ==> [a] guard not filter(a) var [Integer --> Boolean] f = filter do filter : = lambda(Integer b) --> Boolean: f(b) or (lambda (Integer a, Integer b)--> Boolean : b mod a = 0; end)(a, b) end end

2. Structure of the Actor. Core

2. Structure of the Actor. Core

3. The visitor pattern e. arg. Tuple. accept(this); // generate some code … e.

3. The visitor pattern e. arg. Tuple. accept(this); // generate some code … e. function. accept(this); // generate more code … accept(this) visitor. visit. Application(this); visitor. visit. Tuple(this); visit. Application(this)

Problems solved • matching CAL to Ptolemy – single atomic action vs prefire/firen/postfire –

Problems solved • matching CAL to Ptolemy – single atomic action vs prefire/firen/postfire – state changing computation vs state-invariant fire – Cal. Core scopes vs Java scopes – random access to channels vs sequential read methods

Further work – Implement type checking – Describe the transformations on the AST in

Further work – Implement type checking – Describe the transformations on the AST in XML – Network + actors schedule – Network + actors + schedule actor – Retarget the code generator to other platforms (Moses ETH)

continuous time finite-state machine discrete time Hierarchical, heterogeneous model

continuous time finite-state machine discrete time Hierarchical, heterogeneous model

Generic and specific code generator

Generic and specific code generator

The CAL compiler

The CAL compiler