enable_smp_tools

enable_smp_tools#

enable_smp_tools(
backend: Literal['stdthread', 'tbb', 'openmp', 'sequential'] = 'stdthread',
n_threads: int | None = None,
) _SMPToolsContext[source]#

Enable a VTK SMP backend for filters that support shared-memory parallelism.

VTK’s Python wheels currently default to the sequential SMP backend. This helper switches to a parallel backend and optionally configures the maximum number of threads used by VTK filters that rely on vtkSMPTools.

The backend is applied immediately, so calling this function by itself enables the chosen backend for the rest of the process. The return value is also a context manager, so using it with a with statement will restore the previous backend and thread count on exit.

Parameters:
backendstr, default: ‘stdthread’

SMP backend to enable. Acceptable values are:

  • 'stdthread': Enable VTK’s std::thread backend. This is the default and is available in the current VTK wheels.

  • 'tbb': Enable Intel oneTBB when available in the current VTK build.

  • 'openmp': Enable OpenMP when available in the current VTK build.

  • 'sequential': Use VTK’s sequential backend.

n_threadsint, optional

Maximum number of threads to use. If not provided, VTK resets to its default maximum thread count and honors the VTK_SMP_MAX_THREADS environment variable when it is set.

Returns:
contextlib.AbstractContextManager

A context manager that restores the previous SMP backend and thread count when exited. The return value may be discarded when the change should apply for the remainder of the process.

Raises:
TypeError

If backend is not a string or if n_threads is not an integer.

ValueError

If backend is invalid or if n_threads is less than 1.

RuntimeError

If this VTK build does not support runtime SMP backend selection, or if the requested backend is unavailable.

Examples

Enable the wheel-supported stdthread backend for the rest of the process.

>>> import pyvista as pv
>>> pv.enable_smp_tools()

Configure the backend before running a contour filter.

>>> from pyvista import examples
>>> pv.enable_smp_tools(n_threads=8)
>>> grid = examples.download_fea_bracket()
>>> _ = grid.contour(5, scalars='Equivalent Stress')

Scope the backend change to a with block. The previous backend and thread count are restored on exit, even if an exception is raised.

>>> with pv.enable_smp_tools(n_threads=8):
...     _ = grid.contour(5, scalars='Equivalent Stress')