Caching intermediate results

Sometimes when performing complex simulations micromodels are run with the same data more than once and it can be useful to cache the results to save computing time.

A simple caching component can be found in the examples/python folder named cache.py. This cache can be inserted between two models to cache results from the micro model.

docs/source/examples/ecac_python
ymmsl_version: v0.2

imports:
- from eca_programs import implementation elemental_ca_macro_python
- from eca_programs import implementation elemental_ca_micro_python
- from eca_programs import implementation cache_python

models:
  elementary_cellular_automata:
    description: A simple cellular automaton model with cache
    components:
      macro:
        ports:
          o_i: state_out
          s: state_in
        description: Macro model containing only the time stepping loop
        implementation: elemental_ca_macro_python

      cache:
        ports:
          f_init: front_in
          o_i: back_out
          s: back_in
          o_f: front_out
        description: A cache that stores state update results
        implementation: cache_python

      micro:
        ports:
          f_init: initial_state
          o_f: final_state
        description: Micro model computing the state update
        implementation: elemental_ca_micro_python

    conduits:
      macro.state_out: cache.front_in
      cache.back_out: micro.initial_state
      micro.final_state: cache.back_in
      cache.front_out: macro.state_in

resources:
  elementary_cellular_automata.macro:
    threads: 1
  elementary_cellular_automata.cache:
    threads: 1
  elementary_cellular_automata.micro:
    threads: 1

The cache can be used by inserting it between two components, attach the back side to the model you want cached, and the front to the caller.

One setting is available for the cache, determining how many responses the cache will remember. Setting this too high for data-intensive applications will result in a lot of memory usage. If this setting is not set a cache size of 128 is used.

docs/source/examples/eca_settings.ymmsl
ymmsl_version: v0.2
description: |
  Settings for the cellular automaton example
settings:
  muscle_local_log_level: INFO
  muscle_remote_log_level: WARNING

  macro.max_steps: 1000
  macro.size: 400
  micro.rule: 57
  cache.size: 10

Example caching results

An example elementary cellular automata can be run with and without cache to see the impact.

To run the examples with and without cache you can execute the following

# without cache
muscle_manager --start-all eca_implementations.ymmsl eca_python.ymmsl eca_settings.ymmsl
# with cache
muscle_manager --start-all eca_implementations.ymmsl ecac_python.ymmsl eca_settings.ymmsl
_images/plot_performance_timeline_no_cache.png

timeline without cache

_images/plot_performance_timeline_cache.png

timeline with cache

As you can see after 1.25 seconds the simulation reached a stable state, and the cache takes over from the micromodel, saving compute time in the process

_images/ca_result.png

cellular automata simulation

The simulations finds a stable state after 400~ iterations, after which the cache can take over from the micro model