pystencils: Speeding up stencil computations on CPUs and GPUs
09-05, 16:30–16:45 (UTC), Track 3 (Oteiza)

pystencils speeds up stencil computations on numpy arrays using a sympy-based high level description, that is compiled into optimized C code.

Interactive Notebooks are available here.

Many operations on structured arrays can be formulated as stencil codes, where the update of one array cell depends only on values in its local neighborhood. Stencil codes arise in many different fields, for example in image processing or in computational fluid dynamics by discretizing partial differential equations (PDEs) using finite differences or finite volume schemes.

We present the pystencils package that allows for fast execution of stencil codes on numpy arrays using code generation techniques.
The stencil is formulated in sympy and transformed into an intermediate representation (IR).
pystencils comes with a set of optimizing transformations that can be applied on this IR, for example cache blocking or explicit SIMD vectorization with intrinsics. The intermediate representation is transformed into C or CUDA code and automatically loaded as a C extension module. This approach yields highly efficient implementations, outperforming current acceleration techniques like Cython or numba. Additionally, together with the waLBerla package, the resulting stencil codes can be run on large computing clusters, using MPI parallelization.

pystencils also comes with functions to automatically derive the sympy-based stencil representation from a continuous PDE. Symbolic, continuous differential operators are automatically discretized by finite difference schemes of arbitrary order.

We show two examples of large-scale setups run with pystencils: a phase-field method simulating solidification of alloys and a CFD simulation based on the lattice-Boltzmann method.

Project Homepage / Git

Abstract as a tweet

pystencils: Fast stencil computations on numpy arrays

Python Skill Level


Domain Expertise



Image Processing, Jupyter, Materials Science, Simulation

Martin Bauer is a PhD student at the chair for system simulation at the University Erlangen-Nuremberg.
His research interests are CFD simulations with the lattice Boltzmann method, meta-programming techniques and high performance computing. He is one of the core developers of the waLBerla lattice Boltzmann framework.