glearn.device.restrict_to_single_processor#
- glearn.device.restrict_to_single_processor()#
Restricts the computations to only one CPU thread.
This function is primarily used to properly measure the process time of a computational task using
time.process_time
fromtime
module.Note
This function should be called at the very first line of the main script of your Python code before importing any other Python package.
Notes
Why using this function:
In Python, to measure the CPU processing time (and not the wall time) of a computational task, the function
time.process_time
fromtime
module can be used. For instance>>> import time >>> t_init = time.process_time() >>> # Perform some time-consuming task >>> t_final = time.process_time() >>> t_process = t_final - t_init
The process time differs from wall time in which it measures the total process time of all CPU threads and excludes the idle times when the process was not working.
Often, measuring the process time is affected by other factors and the result of the above approach is not reliable. One example of such is measuring the process time of the global optimization problem using
scipy.optimize.differential_evolution
function:>>> from scipy.optimize import differential_evolution >>> import time >>> t_init = time.process_time() >>> result = differential_evolution(worker=num_workers, ...) >>> t_final = time.process_time() >>> t_process = t_final - t_init
However, regardless of setting
worker=1
, orworker=-1
, the measured process time is identical, hence cannot be trusted.A solution to this problem is to restrict the computational task to use only one CPU thread.
Alternative Solution:
Instead of calling this function, export the following environment variables before executing your Python script:
export OMP_NUM_THREADS=1 export OPENBLAS_NUM_THREADS=1 export MKL_NUM_THREADS=1 export VECLIB_MAXIMUM_THREADS=1 export NUMEXPR_NUM_THREADS=1
Examples
>>> # Call this function before importing any other module >>> from glearn.device import restrict_to_single_processor >>> restrict_to_single_processor() >>> # Import packages >>> import scipy, numpy, glearn, time