Slide 1

Slide 1 text

Embedded Domain-Specific Languages Anthony M. Sloane Programming Languages Research Group Department of Computing Macquarie University [email protected] @inkytonik 1 / 16

Slide 2

Slide 2 text

General-Purpose Language (GPL) 2 / 16

Slide 3

Slide 3 text

Domain-Specific Language (DSL) A language that is focused on a particular problem domain Notations that domain experts know Constructs and abstractions that may not be GPL-compatible Analysis, verification, optimisation, transformation can rely on domain knowledge Not necessarily executable SAWDAP Workshop Program ... 3 / 16

Slide 4

Slide 4 text

Structured Data Processing Relational database tables Structured Query Language (SQL) SELECT * FROM Purchases WHERE price >= 50.00 ORDER BY description; 4 / 16

Slide 5

Slide 5 text

Hadoop: API for the Map-Reduce Domain Apache, hadoop.apache.org Main access via Java API Hive DSL (hive.apache.org) compiles SQL-like code to Hadoop 5 / 16

Slide 6

Slide 6 text

External vs Internal Domain-Specific Language External DSL DSL Program DSL Compiler Target Program Target Program Problem Input Problem Output Internal/Embedded DSL (Shallow) Host Program Host Compiler Target Program DSL Program Host Library DSL Library 6 / 16

Slide 7

Slide 7 text

Kiama: Shallow Embedding of Software Language Processing in Scala val beta = rule { case App (Lam (x, t, e1), e2) => Let (x, t, e2 , e1) } val lambda = beta + arithop + subsNum + subsVar + subsApp + subsLam + subsOpn def innermost (s : => Strategy) : Strategy = all (innermost (s) <* attempt (s <* innermost (s))) lazy val s : Strategy = innermost (lambda) Macquarie University, kiama.googlecode.com 7 / 16

Slide 8

Slide 8 text

Internal/Embedded DSL (Deep) Host Program Host Compiler DSL Compiler DSL Program Problem Config DSL Compiler Target Program Target Program Problem Input Problem Output 8 / 16

Slide 9

Slide 9 text

Scoobi: Deep Embedding of Hadoop Computations val lines = fromTextFile ("hdfs ://in /...") val counts = lines.mapFlatten (_.split (" ")) .map (word => (word , 1)) .groupByKey .combine (Sum.int) counts.toTextFile ("hdfs :// out /...", overwrite=true). persist ( ScoobiConfiguration ()) Uses Kiama for analysis and optimisation NICTA, github.com/NICTA/scoobi 9 / 16

Slide 10

Slide 10 text

Abstract vs Concrete Domains Hadoop-based DSLs data represented by standard primitives and generic collections operations are generic mapping, reducing, etc over collections solutions to many problems from many concrete domains can be expressed in this way solutions are abstract compared to the problem domain Concrete DSLs based on data types found in the domain operations are custom for the domain targeted at a particular concrete domain solutions are highly specific to the domain 10 / 16

Slide 11

Slide 11 text

Chisel: A More Concrete Domain-Specific Language Deep embedding of hardware specification class GCD extends Module { val io = new Bundle { val a = UInt(INPUT , 16) val b = UInt(INPUT , 16) val e = Bool(INPUT) val z = UInt(OUTPUT , 16) val v = Bool(OUTPUT) } val x = Reg(UInt ()) val y = Reg(UInt ()) when (x > y) { x := x - y } unless (x > y) { y := y - x } when (io.e) { x := io.a; y := io.b } io.z := x io.v := y === UInt (0) } Berkeley, chisel.eecs.berkeley.edu 11 / 16

Slide 12

Slide 12 text

Delite: High Performance DSL Implementation Stanford, EPFL, stanford-ppl.github.io/Delite/ 12 / 16

Slide 13

Slide 13 text

OptiMesh: DSL for Mesh-based Computation Delite-based implementation customises a generic computation to a particular mesh structure, including analysis for parallelisation. for (edge <- edges (mesh)) { val flux = flux_calc (edge) val v0 = head (edge) val v1 = tail (edge) Flux (v0) += flux Flux (v1) -= flux } Supports complex partial differentiation equation systems over these mesh structures. 13 / 16

Slide 14

Slide 14 text

Delite-based DSL Performance Compared to C++ OptiQL: SQL-like queries using a standard benchmark OptiGraph: static graph analysis on Google’s PageRank algorithm OptiMesh: shallow water simulation 14 / 16

Slide 15

Slide 15 text

Conclusion Programming language people know how to embed languages Start with shallow embedding while language is designed Move to deep embedding to add analysis, optimisation, code generation Domain experts know the problem domain Identify the key concepts, data and operations for their problems Together Rapidly prototype embedded languages for new domains Develop optimised implementations as applications grow 15 / 16

Slide 16

Slide 16 text

Other Credits Relational database table and SQL example: Wikipedia FORTRAN board game: Christian Hammond on Flickr MapReduce flow diagram: Lukas Kaestner on Flickr Delite figures from Delite: A Compiler Architecture for Performance-Oriented Embedded Domain-Specific Languages by Sujeeth et al, ACM Transactions on Embedded Systems, 13(4), March 2014. 16 / 16