Skip to main content

Speeding up stencil computations on CPUs and GPUs

Project description

pystencils

Binder Docs pypi-package pipeline status coverage report

Run blazingly fast stencil codes on numpy arrays.

pystencils uses sympy to define stencil operations, that can be executed on numpy arrays. Exploiting the stencil structure makes pystencils run faster than normal numpy code and even as Cython and numba, as demonstrated in this notebook.

Here is a code snippet that computes the average of neighboring cells:

import pystencils as ps
import numpy as np

f, g = ps.fields("f, g : [2D]")
stencil = ps.Assignment(g[0, 0],
                       (f[1, 0] + f[-1, 0] + f[0, 1] + f[0, -1]) / 4)
kernel = ps.create_kernel(stencil).compile()

f_arr = np.random.rand(1000, 1000)
g_arr = np.empty_like(f_arr)
kernel(f=f_arr, g=g_arr)

pystencils is mostly used for numerical simulations using finite difference or finite volume methods. It comes with automatic finite difference discretization for PDEs:

import pystencils as ps
import sympy as sp

c, v = ps.fields("c, v(2): [2D]")
adv_diff_pde = ps.fd.transient(c) - ps.fd.diffusion(c, sp.symbols("D")) + ps.fd.advection(c, v)
discretize = ps.fd.Discretization2ndOrder(dx=1, dt=0.01)
discretization = discretize(adv_diff_pde)

Installation

pip install pystencils[interactive]

Without [interactive] you get a minimal version with very little dependencies.

All options:

  • gpu: use this if an NVIDIA or AMD GPU is available and CUDA or ROCm is installed
  • alltrafos: pulls in additional dependencies for loop simplification e.g. libisl
  • bench_db: functionality to store benchmark result in object databases
  • interactive: installs dependencies to work in Jupyter including image I/O, plotting etc.
  • doc: packages to build documentation

Options can be combined e.g.

pip install pystencils[interactive, gpu, doc]

pystencils is also fully compatible with Windows machines. If working with visual studio and cupy makes sure to run example files first to ensure that cupy can find the compiler's executable.

Documentation

Read the docs here and check out the Jupyter notebooks in doc/notebooks. The Changelog of pystencils can be found here.

Authors

Many thanks go to the contributors of pystencils.

Please cite us

If you use pystencils in a publication, please cite the following articles:

Overview:

Performance Modelling:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pystencils-1.4.tar.gz (292.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pystencils-1.4-py3-none-any.whl (264.1 kB view details)

Uploaded Python 3

File details

Details for the file pystencils-1.4.tar.gz.

File metadata

  • Download URL: pystencils-1.4.tar.gz
  • Upload date:
  • Size: 292.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pystencils-1.4.tar.gz
Algorithm Hash digest
SHA256 0a5c362c1208d1f24932c6b8d22a171e5ae8e3c84049d44fffb50bfc3dbdc7d6
MD5 f817f1de7370510918cf50f0bca57347
BLAKE2b-256 fb2f7eb8a9de0fa7a4066fbc998d5b9779a9722ec93e6cf1e5a4f1b3a62dc006

See more details on using hashes here.

File details

Details for the file pystencils-1.4-py3-none-any.whl.

File metadata

  • Download URL: pystencils-1.4-py3-none-any.whl
  • Upload date:
  • Size: 264.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pystencils-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 df02c82fd223fec816e3a5108527e76549588cd3ca6ce83eab8f7c1a90add959
MD5 3baed2da6e066c96c8ab07a7cf7a7f78
BLAKE2b-256 021d55a93db383913ed595ddd435217c1ad245b6b4330d04d735c611734883c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page