Skip to content

lunduniversity/scalades

Repository files navigation

scalades

scalades is a library for empirical investigation of real-world systems using stochastic Descrete-Event Simulation (DES) models in the Scala programming language.

This library is work in progress in its early stages, so the api is likely to change. Contributions are welcome! Contact [email protected]

scalades follow the terminology and principles of on Stochastic Discrete Event Simulation taught in this course on Simulation at Lund University.

How to use scalades

  • Pre-requisites: install sbt (requires JDK)

  • Clone this repo or download and extract this zip

  • Navigate to the scalades folder and do this in terminal:

$ sbt
sbt:scalades> runMain scalades.example.runMM1 9 10 10000 

You should get an output similar to:

[info] running scalades.example.runMM1 9 10 10000
---- SIMULATION START -----
Number of samples: 9828
Average queue length: 10.33536833536831
Execution time: 0.543 seconds
Last event at time stamp: 9999.9100217340560922411402 time units
SIMULATION END AFTER 10000.0 time units
[success] Total time: 1 s, completed Mar 24, 2021, 11:30:21 PM

Design approach

The main goal of scalades is to provide an easy-to-use library that supports the modelling of systems in the paradigm of stochastic Discrete Event Simulation.

Message passing

The world is modelled using events that are happening when processes send signals among each other. Each event has a decimal timestamp ordered in an event queue, with the event with the soonest time stamp first. The simulation time can directly jump to the time of the occurring next event, allowing efficient simulation of real-world systems in pseudo-time instead of real time.

Non-parallell, co-operative concurrency

Although a process can model behaviour that happens concurrently in the real world, the simulation model itself is executed in one single thread with co-operative, non-parallell concurrency through scheduling of process execution based on synchronous, sequential event dispatching. The Simulation class is responsible for the event dispatching.

Each simulation in a single thread executed in parallell

Multiple simulations can be executed independently of each other in different threads to allow efficient exploration of different simulation parameters in parallell using many CPU threads.

Utilizing the modelling power and execution efficiency of Scala 3

The scalades library is implemented in Scala 3, utilizing new language constructs such as enums for enumerating signals and states, opaque type aliases for unboxed Time values, extension methods and export clauses to shape the api, and contextual abstraction to provide a given simulation context to the implicit using by its processes.

We have conducted a series of benchmarks, indicating that Scala Native is a very promising runtime for scalades that may be even faster than the JVM. When Scala Native is available for Scala 3 our intention is to publish this library also for bare metal execution.

For more information on Scala native performance and further benchmarking using scalades in Scala Native, see this blog post by Wojciech Mazur:Revisiting Scala Native performance

TODO

  • Should Time be a Double or BigDecimal? Floating-point numbers are strange when comparing with >= etc so precision might be an issue especially in long simulations where durations are much smaller than the simulation time stamps. But BigDecimal is significantly slower than Double. This is a trade-off between precision and performance that is interesting to explore in benchmarks.

  • What are the most important use cases left to implement for the Simulation course at Lund university?

  • add help for contributors

  • add more examples

  • do more benchmarks for performance evaluation

  • try out using Scala's Future to run several simulations in parallell on different CPU-threads