pyRTX.core.parallel_utils

Functions

get_ncores(lst, **kwargs)

Returns the number of cores for parallel computations.

get_unwrapped(*args, **kwargs)

Returns the unwrapped version of a class method.

is_method(func, cls_inst)

Checks if the wrapped object is a method of a class.

parallel(func)

A decorator for parallel computations.

pyRTX.core.parallel_utils.get_ncores(lst, **kwargs)[source]

Returns the number of cores for parallel computations.

If not specified, the number of cores is set to the number available on your machine minus one.

Parameters:
  • lst (list or numpy.ndarray) – The list of items to be processed in parallel.

  • **kwargs (dict) – Additional keyword arguments. If ‘n_cores’ is present, it will be used as the number of cores.

Returns:

The number of cores to use for parallel computation.

Return type:

int

pyRTX.core.parallel_utils.is_method(func, cls_inst)[source]

Checks if the wrapped object is a method of a class.

Parameters:
  • func (function) – The function to check.

  • cls_inst (object) – The class instance.

Returns:

True if the function is a method of the class, False otherwise.

Return type:

bool

pyRTX.core.parallel_utils.get_unwrapped(*args, **kwargs)[source]

Returns the unwrapped version of a class method.

Parameters:
  • *args (tuple) – The arguments to pass to the method.

  • **kwargs (dict) – The keyword arguments to pass to the method. ‘method’ must be present and contain the name of the method to unwrap.

Returns:

The result of the unwrapped method call.

Return type:

object

pyRTX.core.parallel_utils.parallel(func)[source]

A decorator for parallel computations.

The decorated object can be a class method or a regular function. It applies the function to every item of an iterable, performing the computations in parallel. The output will be an iterator which contains the return value of every function call.

The decorated object can accept multiple arguments, but the last positional argument must be an item of the input iterable (e.g., an item of a list, numpy array, or range object).

If the keyword argument ‘n_cores’ is specified, the decorated object will run on n_cores processes. Otherwise, the number of cores is set to the number available on your machine.

Parameters:

func (function) – The function to be decorated.

Returns:

The decorated function.

Return type:

function

Examples

>>> iterable = [2, 5, 6, 7, 3, 4, 1]
>>> @parallel
... def target_func(some_input, item):
...     return some_input * item
>>> results = target_func(10, iterable, n_cores=5)