|
| 1 | +--- |
| 2 | +title: "Delta Encoding" |
| 3 | +author: "Justin LaPre" |
| 4 | +category: feature |
| 5 | +--- |
| 6 | + |
| 7 | +Delta encoding provides a solution for models that contain events which are not well suited for using reverse computation or consumes significant amounts of memory (making copy-state approaches infeasible). |
| 8 | +Delta encoding solves this issue by only computing state change deltas once an event has completed execution. |
| 9 | +These deltas are then compressed for reduced storage overheads. |
| 10 | +In addition, delta encoding is done on a per-event basis allowing both reverse computation and delta encoding to be mixed within a single model. |
| 11 | +Overall, the delta encoding approach provides the benefits of incremental state-saving but without requiring the specific identification of which state elements change. |
| 12 | +This feature is further described in [LaPre et al., 2015](https://dl.acm.org/citation.cfm?id=2888969). |
| 13 | + |
| 14 | +## Use Case |
| 15 | + |
| 16 | +Some model events are not well suited to reverse computation([LaPre et al., 2014](http://dl.acm.org/citation.cfm?id=2601397)). |
| 17 | +Two features are often difficult or inefficient to reverse: |
| 18 | +- Complex, nested loops, |
| 19 | +- Destructive value assignments. |
| 20 | + |
| 21 | +## API |
| 22 | + |
| 23 | +The API is as follows: |
| 24 | + |
| 25 | + tw_snapshot(lp, lp->type->state_sz) |
| 26 | + tw_snapshot_delta(lp, lp->type->state_sz) |
| 27 | + tw_snapshot_restore(lp, lp->type->state_sz, lp->pe->cur_event->delta_buddy, lp->pe->cur_event->delta_size) |
| 28 | + |
| 29 | +In addition, the following options are provided: |
| 30 | + |
| 31 | + --buddy-size=n delta encoding buddy system allocation (2^X) (default 0) |
| 32 | + --lz4-knob=n LZ4 acceleration factor (higher = faster) (default 17) |
| 33 | + |
| 34 | +### Command-Line Options |
| 35 | + |
| 36 | +The `--buddy-size` option allocates a block of memory for use by the delta encoding system. |
| 37 | +This flag **must** be set for all ROSS binaries that use delta encoding. |
| 38 | +A value of 20 will allocate 2^20 bytes (1 megabyte) of space and is usually a good starting point. |
| 39 | + |
| 40 | +The `--lz4-knob` option tunes the amount of LZ4 compression which is applied to the delta. |
| 41 | +A value of 1 provides maximum compression, but at the cost of speed. |
| 42 | +ROSS uses a default value of 17. |
| 43 | + |
| 44 | +### Functions |
| 45 | + |
| 46 | +`tw_snapshot()` and `tw_snapshot_delta()` should be called at the start and end, respectively, of an event handler. |
| 47 | +Be careful to ensure that `tw_snapshot_delta()` is called before *every* possible function return. |
| 48 | +In the reverse event handler, call `tw_snapshot_restore()` to return the state to its previous values. |
| 49 | + |
| 50 | +## Other Concerns |
| 51 | + |
| 52 | +Random number generation must also be accounted for, which must be done by the model in addition to any delta encoding calls. If an LP makes four RNG calls, it must make four RNG *uncalls*. This can be tracked by using the `rng_count` member in the `message` class; see the linked code example for an implementation |
| 53 | + |
| 54 | +## Example |
| 55 | + |
| 56 | +For a complete (and probably working) example, see [the phold-delta model](https://github.com/ROSS-org/ROSS-Models/tree/master/phold-delta). |
0 commit comments