Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Embedded Domain-Specific Languages

Embedded Domain-Specific Languages

Talk at Sydney Area Workshop on Data Science and Programming Languages (SAWDAP), December 2014

Tony Sloane

April 27, 2015
Tweet

More Decks by Tony Sloane

Other Decks in Programming

Transcript

  1. 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 <html > <head > <title >SAWDAP Workshop Program </title > </head > <body > ... </body > </html > 3 / 16
  2. Structured Data Processing Relational database tables Structured Query Language (SQL)

    SELECT * FROM Purchases WHERE price >= 50.00 ORDER BY description; 4 / 16
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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