# Copyright (c) 2010-2025 The Regents of the University of Michigan# This file is from the freud project, released under the BSD 3-Clause License.r"""The :class:`freud.parallel` module controls the parallelization behavior offreud, determining how many threads the TBB-enabled parts of freud will use.freud uses all available threads for parallelization unless directed otherwise."""importfreud._parallel_num_threads=0
[docs]defget_num_threads():r"""Get the number of threads for parallel computation. Returns: (int): Number of threads. """return_num_threads
[docs]defset_num_threads(nthreads=None):r"""Set the number of threads for parallel computation. Args: nthreads (int, optional): Number of threads to use. If :code:`None`, use all threads available. (Default value = :code:`None`). """global_num_threads# noqa: PLW0603 - freud needs to maintain the stateifnthreadsisNoneornthreads<0:nthreads=0_num_threads=nthreadsfreud._parallel.setNumThreads(nthreads)
[docs]classNumThreads:r"""Context manager for managing the number of threads to use. Args: N (int, optional): Number of threads to use in this context. If :code:`None`, which will use all available threads. (Default value = :code:`None`). """def__init__(self,N=None):self.restore_N=_num_threadsself.N=Ndef__enter__(self):set_num_threads(self.N)returnselfdef__exit__(self,*args):set_num_threads(self.restore_N)