2011年12月28日水曜日

Compiling the Particle-based Simulation Description Language


On the former entry, I described simple N-Body simulation in the Particle-based Simulation Description Language as a embedded DSL and executed it with interpreter. In this entry, I introduce compiling and demonstrate it.

Implementation
The Particle-based Simulation Description Language is compiled to Haskell using Template Haskell, a GHC extension to Haskell that adds compile-time metaprogramming facilities.

With this transformation, a code corresponding to the N-Body simulation code written in plain Haskell on the former entry is produced, then it is compiled with GHC Haskell compiler.

In other words, the host language is the Particle-based Simulation Description Language and the target language is Haskell, though there is no clear line between the host and target languages because it is a embedded DSL.

Codes
Here the codes are, with the interpreter introduced on the former entry together, arranged as a module.

The structure of the module is as below:
- Examples/
- SimulationDSL/
  - SimulationDSL.hs
  - SimulationDSL/
    - Compiler/
    - Data/
    - Interpreter/
    - Language/
    - Test/

Execution
To execute, compile CompilerRun.hs in Examples directory and run the output file as below:
$ cd Examples
$ ghc -i../SimulationDSL --make -O2 CompilerRun.hs
$ ./CompilerRun

Performance
Comparing performance on three codes:
  • with compiler - CompilerRun
  • in plain Haskell - NBody
  • with interpreter - InterpreterRun
The performance:
$ time ./CompilerRun > /dev/null

real 0m0.949s
user 0m0.927s
sys 0m0.011s

$ time ./NBody > /dev/null

real 0m0.972s
user 0m0.950s
sys 0m0.011s

$ time ./InterpreterRun > /dev/null
real 0m2.932s
user 0m2.906s
sys 0m0.022s

As chart:


As intended, the same performance to the code in plain Haskell is given with compiler, which is 2x-3x faster than with interpreter.

Conclusion
In this entry, I introduced compiling the Particle-based Simulation Description Language to Haskell and showed improvement in performance of simple N-Body simulation.

While this time I chose Haskell as the target language for this embedded DSL, it is also possible to compile it to C adapting OpenMP or CUDA with parallel computation.


--

0 件のコメント: