#isl #loops #image-processing #stencil

rs_isl

2-dimensional generic Iterative Stencil Loops

2 releases

0.1.2 Oct 4, 2024
0.1.1 Oct 4, 2024
0.1.0 Oct 4, 2024

#76 in Simulation

Download history 343/week @ 2024-10-04 25/week @ 2024-10-11

368 downloads per month

MIT/Apache

19KB
365 lines

rs_isl

rs_isl is an implementation of Iterative Stencil Loops in Rust. ISLs can be used in a variety of scenarios such as image processing, fluid simulation and the calculation of PDEs. For more information see Wikipedia.

Example


lib.rs:

rs_isl

Implementation of Iterative Stencil Loops

Runs a simulation over a 2-dimensional array specified by the given parameters.

Features:

  • generic array elements
  • parallelization using threads
  • custom definition of neighbouring elements

For further information on ISLs see: https://wikipedia.org/wiki/Iterative_Stencil_Loops

Usage

An example which creates a wave-like motion from left to right through the grid.

use rs_isl::IslParams;
use rs_isl::run_isl;

// grid with a size of 4x2, where cells only access their left neighbour
let size = (4, 2);
let neighbours = vec![(-1, 0)];

// closure that calculates the new value based on the cell's own value and it's neighbours
let op = |_num: &f64, nb: Vec<Option<&f64>>| {
    if nb.first().unwrap().is_some() {
        let f = nb[0].unwrap();
        // if the cell's neighbour has the value 1.0, we take that, otherwise we return 0.0
        if *f != 0.0 {
            return 1.0;
        }
    }
    0.0
};

// closure that determines each cell's initial value based on it's position
let init = |x: usize, _y: usize| {
    // return 1.0 if the cell is located on the left boundary of the grid
    if x == 0 {
        return 1.0;
    }
    0.0
};

// create parameters
let params = IslParams::new(
    size,
    op,
    // number of threads, the grids size (x*y) must be divisible by this value
    1,
    init,
    // number of steps for which the simulation will be run
    4,
    // number of output steps, these will be evenly distributed through the simulation
    4,
    neighbours,
    // type of returned data
    rs_isl::OutputType::String,
);

// run ISL
let data = run_isl(params);

// extract the data
match data.unwrap() {
    rs_isl::IslOutput::RawData(vec) => println!("{:?}", vec),
    rs_isl::IslOutput::String(vec) => {
        for line in vec {
            println!("{}", line)
        }
    }
}

Dependencies

~0.4–5MB
~11K SLoC