enable_smp_tools#
- enable_smp_tools(
- backend: Literal['stdthread', 'tbb', 'openmp', 'sequential'] = 'stdthread',
- n_threads: int | None = None,
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
withstatement will restore the previous backend and thread count on exit.- Parameters:
- backend
str, default: ‘stdthread’ SMP backend to enable. Acceptable values are:
'stdthread': Enable VTK’sstd::threadbackend. 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_threads
int,optional Maximum number of threads to use. If not provided, VTK resets to its default maximum thread count and honors the
VTK_SMP_MAX_THREADSenvironment variable when it is set.
- backend
- Returns:
contextlib.AbstractContextManagerA 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:
TypeErrorIf
backendis not a string or ifn_threadsis not an integer.ValueErrorIf
backendis invalid or ifn_threadsis less than1.RuntimeErrorIf this VTK build does not support runtime SMP backend selection, or if the requested backend is unavailable.
Examples
Enable the wheel-supported
stdthreadbackend 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
withblock. 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')