freegsnke.parallel module
Custom parallel library for use in FreeGSNKE, based on Python threading for GIL releasing operations. Wraps expensive numpy and scipy functions with parallel implementations. Relies on numexpr for threadpool size control.
Copyright 2026 Tomas Rubio Cruz, STFC - Hartree Centre
This file is part of FreeGSNKE.
FreeGSNKE is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
FreeGSNKE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with FreeGSNKE. If not, see <http://www.gnu.org/licenses/>.
- class freegsnke.parallel.CustomThreadController(*, filepath=None, prefix=None, parent=None)[source]
Bases:
LibControllerCustom thread controller allowing this library to be controlled with threadpoolctl.
- filename_prefixes = ('interpreter',)
- get_num_threads() int[source]
Report this backend’s current thread count to threadpoolctl.
- Returns:
Current number of threads.
- Return type:
int
- get_version() str[source]
Report the installed FreeGSNKE distribution version.
- Returns:
Version string, or
"0+unknown"if the package is not installed.- Return type:
str
- internal_api = 'freegsnke.parallel'
- set_num_threads(num_threads: int) None[source]
Apply a threadpoolctl limit to this backend and numexpr.
- Parameters:
num_threads (int) – Number of threads to set.
- user_api = 'fg_threads'
- freegsnke.parallel.get_max_threads() int[source]
Utility function to inquire the maximum size allowed for a threadpool.
- Returns:
Number of threads
- Return type:
int
- freegsnke.parallel.get_num_threads() int[source]
Utility function to inquire the default number of threads used by functions in this parallel library.
- Returns:
Number of threads
- Return type:
int
- freegsnke.parallel.set_max_threads() int[source]
Utility function to programatically set the default number of threads used by functions in this parallel library to the estimated ideal maximum.
- Returns:
New size of the threadpool
- Return type:
int
- freegsnke.parallel.set_num_threads(num_threads: int) None[source]
Utility function to programatically set the default number of threads used by functions in this parallel library.
- Parameters:
num_threads (int) – Number of threads.
- freegsnke.parallel.threaded_clip(k2: ndarray, /, amin: Any, amax: Any, *, out: ndarray | None = None, single_thread: bool = False, **kwargs: Any) ndarray[source]
Parallel wrapper for numpy clip. Detailed behavior of the function can be consulted on numpy docs. This function only adds the argument single_thread (read below).
k2 is divided into slices and each thread clips its corresponding slice.
- Parameters:
k2 (ndarray) – Array containing the elements to clip
amin (array_like or None) – Minimum and maximum value. If
None, clipping is not performed on the corresponding edge. If bothaminandamaxareNone, the elements of the returned array stay the same. Both are broadcasted against k2.amax (array_like or None) – Minimum and maximum value. If
None, clipping is not performed on the corresponding edge. If bothaminandamaxareNone, the elements of the returned array stay the same. Both are broadcasted against k2.out (ndarray, optional) – The results will be placed in this array. It may be the input array for in-place clipping. out must be of the right shape to hold the output. Its type is preserved.
single_thread (bool, optional) – If True, the function is run in serial regardless of the pre-set number of threads.
**kwargs (Any) – As per numpy docs. Note that the ufunc argument where is not currently supported and is ignored.
- Returns:
An array with the elements of k2, but where values < amin are replaced with amin, and those > amax with amax.
- Return type:
ndarray
- Raises:
TypeError – If running in parallel and k2 or out is not a numpy ndarray.
ValueError – If running in parallel and k2 and out are not broadcastable.
- freegsnke.parallel.threaded_elliptics_ek(k2: ndarray, out: Any = None, single_thread: bool = False) tuple[ndarray, ndarray][source]
Parallel wrapper for both scipy.special.ellipe() and scipy.special.ellipk(). Behavior of these functions can be consulted on scipy docs. out parameter is not supported and is ignored.
On 2 threads, both integrals are simply calculated simultaneously. For larger threadpools, k2 is divided into slices and each thread calculates one of the integrals on its corresponding slice. If an odd number of threads was set, the next lower even number is used.
- Parameters:
k2 (ndarray) – The parameter of the elliptic integral
out (Any, optional) – Unused. Only for compatibility (matching signatures) with wrapped functions.
single_thread (bool, optional) – If True, the function is run in serial regardless of the pre-set number of threads.
- Returns:
Values of the complete elliptic integrals
(E(k2), K(k2))of the second and first kind, as returned byscipy.special.ellipeandscipy.special.ellipk.- Return type:
tuple[ndarray, ndarray]
- Raises:
TypeError – If running in parallel and k2 is not a numpy ndarray.
- freegsnke.parallel.threaded_take(a: ndarray, indices: ndarray, axis: int | None = None, out: ndarray | None = None, mode: str = 'raise') ndarray[source]
Parallel wrapper for numpy take. Detailed behavior of the function can be consulted on numpy docs. indices is divided into slices and each thread takes from its corresponding slice. Falls back to np.take if only one thread is set or there are fewer indices than threads.
- Parameters:
a (ndarray) – The source array
indices (ndarray) – The indices of the values to extract. Only numpy ndarrays are supported when running in parallel.
axis (int, optional) – The axis over which to select values. By default, the flattened input array is used.
out (ndarray, optional) – Buffer in which to place the result. Must have the shape of the output.
mode ({'raise', 'wrap', 'clip'}, optional) – Out-of-bounds index handling, as per numpy docs.
- Returns:
The extracted values, with the dtype of a.
- Return type:
ndarray
- Raises:
TypeError – If running in parallel and indices or out is not a numpy ndarray.
numpy.exceptions.AxisError – If running in parallel and axis is out of bounds for a.
ValueError – If running in parallel and out has an incorrect shape.