
    sKgc                     `   d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	m
Z
mZ ddlmZ e ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ  G d de      Z G d de      Z G d de      Z G d de      Z G d dee      Z G d deee      Z G d dee      Z  G d de!      Z"d Z#y)z,
Backends for embarrassingly parallel code.
    N)ABCMetaabstractmethod   )_TracebackCapturingWrapper*_retrieve_traceback_capturing_wrapped_call)mp)MemmappingPool)
ThreadPool)get_memmapping_executor)process_executor	cpu_count)ShutdownExecutorErrorc                        e Zd ZdZdZdZdZed        Zed        Z	dZ
d fd	Zg dZd	Zed
        Zedd       Zd ZddZd Zd Zd Zd Zd Zd ZddZd Zej8                  d        Zd Zed        Z  xZ!S )ParallelBackendBasezEHelper abc which defines all methods a ParallelBackend must implementFr   c                     | j                   S Nsupports_retrieve_callbackselfs    ]/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/joblib/_parallel_backends.pysupports_return_generatorz-ParallelBackendBase.supports_return_generator$       ...    c                     | j                   S r   r   r   s    r   supports_timeoutz$ParallelBackendBase.supports_timeout(   r   r   Nc                 @    t        |   di | || _        || _        y N )super__init__nesting_levelinner_max_num_threads)r   r"   r#   kwargs	__class__s       r   r!   zParallelBackendBase.__init__.   s#    "6"*%:"r   )OMP_NUM_THREADSOPENBLAS_NUM_THREADSMKL_NUM_THREADSBLIS_NUM_THREADSVECLIB_MAXIMUM_THREADSNUMBA_NUM_THREADSNUMEXPR_NUM_THREADS
ENABLE_IPCc                      y)a  Determine the number of jobs that can actually run in parallel

        n_jobs is the number of workers requested by the callers. Passing
        n_jobs=-1 means requesting all available workers for instance matching
        the number of CPU cores on the worker host(s).

        This method should return a guesstimate of the number of workers that
        can actually perform work concurrently. The primary use case is to make
        it possible for the caller to know in how many chunks to slice the
        work.

        In general working on larger data chunks is more efficient (less
        scheduling overhead and better use of CPU cache prefetching heuristics)
        as long as all the workers have enough work to do.
        Nr   r   n_jobss     r   effective_n_jobsz$ParallelBackendBase.effective_n_jobs<       r   c                      y)Schedule a func to be runNr   r   funccallbacks      r   apply_asynczParallelBackendBase.apply_asyncN   r2   r   c                      y)a  Called within the callback function passed in apply_async.

        The argument of this function is the argument given to a callback in
        the considered backend. It is supposed to return the outcome of a task
        if it succeeded or raise the exception if it failed.
        Nr   r   outs     r   retrieve_result_callbackz,ParallelBackendBase.retrieve_result_callbackR   r2   r   c                 2    || _         | j                  |      S )zReconfigure the backend and return the number of workers.

        This makes it possible to reuse an existing backend instance for
        successive independent calls to Parallel with different parameters.
        )parallelr1   )r   r0   r>   preferrequirebackend_argss         r   	configurezParallelBackendBase.configureZ   s     !$$V,,r   c                      y)z;Call-back method called at the beginning of a Parallel callNr   r   s    r   
start_callzParallelBackendBase.start_calld   r2   r   c                      y)z5Call-back method called at the end of a Parallel callNr   r   s    r   	stop_callzParallelBackendBase.stop_callg   r2   r   c                      y)z0Shutdown the workers and free the shared memory.Nr   r   s    r   	terminatezParallelBackendBase.terminatej   r2   r   c                      y) Determine the optimal batch sizer   r   r   s    r   compute_batch_sizez&ParallelBackendBase.compute_batch_sizem   s    r   c                      y)1Callback indicate how long it took to run a batchNr   )r   
batch_sizedurations      r   batch_completedz#ParallelBackendBase.batch_completedq   r2   r   c                     g S )z'List of exception types to be captured.r   r   s    r   get_exceptionsz"ParallelBackendBase.get_exceptionst   s    	r   c                      y)au  Abort any running tasks

        This is called when an exception has been raised when executing a task
        and all the remaining tasks will be ignored and can therefore be
        aborted to spare computation resources.

        If ensure_ready is True, the backend should be left in an operating
        state as future tasks might be re-submitted via that same backend
        instance.

        If ensure_ready is False, the implementer of this method can decide
        to leave the backend in a closed / terminated state as no new task
        are expected to be submitted to this backend.

        Setting ensure_ready to False is an optimization that can be leveraged
        when aborting tasks via killing processes from a local process pool
        managed by the backend it-self: if we expect no new tasks, there is no
        point in re-creating new workers.
        Nr   r   ensure_readys     r   abort_everythingz$ParallelBackendBase.abort_everythingx   s    , 	r   c                 d    t        | dd      dz   }|dkD  rt        |      dfS t        |      dfS )zBackend instance to be used by nested Parallel calls.

        By default a thread-based backend is used for the first level of
        nesting. Beyond, switch to sequential backend to avoid spawning too
        many threads on the host.
        r"   r   r   r"   N)getattrSequentialBackendThreadingBackend)r   r"   s     r   get_nested_backendz&ParallelBackendBase.get_nested_backend   s?      oq9A=1$=A4GG#-@$FFr   c              #      K   d yw)a  Context manager to manage an execution context.

        Calls to Parallel.retrieve will be made inside this context.

        By default, this does nothing. It may be useful for subclasses to
        handle nested parallelism. In particular, it may be required to avoid
        deadlocks if a backend manages a fixed number of workers, when those
        workers may be asked to do nested Parallel calls. Without
        'retrieval_context' this could lead to deadlock, as all the workers
        managed by the backend may be "busy" waiting for the nested parallel
        calls to finish, but the backend has no free workers to execute those
        tasks.
        Nr   r   s    r   retrieval_contextz%ParallelBackendBase.retrieval_context   s      	s   c                 .   | j                   }t        t               |z  d      }i }| j                  D ]5  }|!t        j
                  j                  ||      }n|}t        |      ||<   7 | j                  t        j
                  vrd|| j                  <   |S )af  Return environment variables limiting threadpools in external libs.

        This function return a dict containing environment variables to pass
        when creating a pool of process. These environment variables limit the
        number of threads to `n_threads` for OpenMP, MKL, Accelerated and
        OpenBLAS libraries in the child processes.
        r   1)	r#   maxr   MAX_NUM_THREADS_VARSosenvirongetstrTBB_ENABLE_IPC_VAR)r   r0   explicit_n_threadsdefault_n_threadsenvvar	var_values          r   _prepare_worker_envz'ParallelBackendBase._prepare_worker_env   s     "77	v 5q9
 ,,C!)JJNN30AB	.	9~CH - """**4 ,/C''(
r   c                  Z    t        t        j                         t        j                        S r   )
isinstance	threadingcurrent_thread_MainThreadr   r   r   in_main_threadz"ParallelBackendBase.in_main_thread   s    )224i6K6KLLr   )NNr   r   NNNT)"__name__
__module____qualname____doc__supports_inner_max_num_threadsr   default_n_jobspropertyr   r   r"   r!   rb   rg   r   r1   r8   r<   rB   rD   rF   rH   rK   rP   rR   rV   r\   
contextlibcontextmanagerr^   rm   staticmethodrs   __classcell__r%   s   @r   r   r      s    O%*"!&N/ / / / M; & " ( (-JD?@0G   : M Mr   r   )	metaclassc                   :    e Zd ZdZdZdZdZdZd Zd	dZ	d Z
d Zy)
rZ   zA ParallelBackend which will execute all batches sequentially.

    Does not use/create any threading objects, and hence has minimal
    overhead. Used when n_jobs == 1.
    TFc                 $    |dk(  rt        d      y?Determine the number of jobs which are going to run in parallelr   &n_jobs == 0 in Parallel has no meaningr   )
ValueErrorr/   s     r   r1   z"SequentialBackend.effective_n_jobs   s    Q;EFFr   Nc                     t        d      )r4   -Should never be called for SequentialBackend.RuntimeErrorr5   s      r   r8   zSequentialBackend.apply_async   s    JKKr   c                     t        d      )Nr   r   r:   s     r   r<   z*SequentialBackend.retrieve_result_callback   s    JKKr   c                     ddl m}  |       S )Nr   )get_active_backend)r>   r   )r   r   s     r   r\   z$SequentialBackend.get_nested_backend   s    0 "##r   r   )rv   rw   rx   ry   uses_threadsr   r   supports_sharedmemr1   r8   r<   r\   r   r   r   rZ   rZ      s6     L!&LL$r   rZ   c                   <    e Zd ZdZdZd Zd Zd Zd	dZd Z	d
dZ
y)PoolManagerMixinz,A helper class for managing pool of workers.Nc                 v    |dk(  rt        d      t        |y|dk  rt        t               dz   |z   d      }|S r   )r   r   ra   r   r/   s     r   r1   z!PoolManagerMixin.effective_n_jobs   sF    Q;EFFZ6> aZq6115Fr   c                     | j                   <| j                   j                          | j                   j                          d| _         yyz#Shutdown the process or thread poolN)_poolcloserH   r   s    r   rH   zPoolManagerMixin.terminate  s8    ::!JJJJ  "DJ "r   c                     | j                   S )z>Used by apply_async to make it possible to implement lazy init)r   r   s    r   	_get_poolzPoolManagerMixin._get_pool
  s    zzr   c                 Z    | j                         j                  t        |      d||      S )r4   r   )r7   error_callback)r   r8   r   r5   s      r   r8   zPoolManagerMixin.apply_async  s2    
 ~~++&t,bh , 
 	
r   c                     t        |      S )z=Mimic concurrent.futures results, raising an error if needed.)r   r:   s     r   r<   z)PoolManagerMixin.retrieve_result_callback  s    9#>>r   c                     | j                          |rH | j                  d| j                  j                  | j                  d| j                  j                   yy)z@Shutdown the pool and restart a new one with the same parametersr0   r>   Nr   )rH   rB   r>   r0   _backend_argsrT   s     r   rV   z!PoolManagerMixin.abort_everything  sK    DNN :$--"6"6 :!]]88: r   r   ru   )rv   rw   rx   ry   r   r1   rH   r   r8   r<   rV   r   r   r   r   r      s(    6E

?:r   r   c                   D     e Zd ZdZdZdZdZdZ fdZd Z	d Z
d	 Z xZS )
AutoBatchingMixinz/A helper class for automagically batching jobs.皙?   r   g        c                 h    t        |   di | | j                  | _        | j                  | _        y r   )r    r!   _DEFAULT_EFFECTIVE_BATCH_SIZE_effective_batch_size _DEFAULT_SMOOTHED_BATCH_DURATION_smoothed_batch_duration)r   r$   r%   s     r   r!   zAutoBatchingMixin.__init__5  s/    "6"%)%G%G"(,(M(M%r   c                    | j                   }| j                  }|dkD  r|| j                  k  r~t        || j                  z  |z        }|dz  }t	        d|z  |      }t        |d      }|| _         | j                  j                  dk\  r| j                  j                  d| d| d       n|| j                  kD  rr|dk\  rmt        || j                  z  |z        }t        d|z  d      }|| _         | j                  j                  dk\  r%| j                  j                  d| d| d       n|}||k7  r| j                  | _        |S )	rJ   r   r   r   
   zBatch computation too fast (zs.) Setting batch_size=.zBatch computation too slow ()r   r   MIN_IDEAL_BATCH_DURATIONintminra   r>   verbose_printMAX_IDEAL_BATCH_DURATIONr   )r   old_batch_sizebatch_durationideal_batch_sizerN   s        r   rK   z$AutoBatchingMixin.compute_batch_size:  s   3366Q!>!>>  #>#'#@#@$A#1$2  3 ! Q/1ABJZ+J)3D&}}$$*$$2>2B C**4Q8 t<<<!  #!>!>>O  Q!1115J)3D&}}$$*$$2>2B C**4Q8 (J' 55 ) r   c                     || j                   k(  r1| j                  }|| j                  k(  r|}nd|z  d|z  z   }|| _        yy)rM   g?r   N)r   r   r   )r   rN   rO   old_durationnew_durations        r   rP   z!AutoBatchingMixin.batch_completedx  sT    333  88LtDDD  (  #\1C(NB,8D) 4r   c                 H    | j                   | _        | j                  | _        y)zgReset batch statistics to default values.

        This avoids interferences with future jobs.
        N)r   r   r   r   r   s    r   reset_batch_statsz#AutoBatchingMixin.reset_batch_stats  s     
 &*%G%G"(,(M(M%r   )rv   rw   rx   ry   r   r   r   r   r!   rK   rP   r   r   r   s   @r   r   r   $  s;    9  "  ! %&!'*$N
<|9 Nr   r   c                   *    e Zd ZdZdZdZdZddZd Zy)r[   a  A ParallelBackend which will use a thread pool to execute batches in.

    This is a low-overhead backend but it suffers from the Python Global
    Interpreter Lock if the called function relies a lot on Python objects.
    Mostly useful when the execution bottleneck is a compiled extension that
    explicitly releases the GIL (for instance a Cython loop wrapped in a "with
    nogil" block or an expensive call to a library such as NumPy).

    The actual thread pool is lazily initialized: the actual thread pool
    construction is delayed to the first call to apply_async.

    ThreadingBackend is used as the default backend for nested calls.
    TNc                     | j                  |      }|dk(  rt        t        | j                              || _        || _        |S z?Build a process or thread pool and return the number of workersr   rX   )r1   FallbackToBackendrZ   r"   r>   _n_jobs)r   r0   r>   rA   s       r   rB   zThreadingBackend.configure  sI    &&v.Q;#!0B0BCE E r   c                 f    | j                   t        | j                        | _         | j                   S )zLazily initialize the thread pool

        The actual pool of worker threads is only initialized at the first
        call to apply_async.
        )r   r
   r   r   s    r   r   zThreadingBackend._get_pool  s(     ::#DLL1DJzzr   )r   N)	rv   rw   rx   ry   r   r   r   rB   r   r   r   r   r[   r[     s$     "&L	r   r[   c                   <     e Zd ZdZdZdZ fdZddZ fdZ xZ	S )MultiprocessingBackenda  A ParallelBackend which will use a multiprocessing.Pool.

    Will introduce some communication and memory overhead when exchanging
    input and output data with the with the worker Python processes.
    However, does not suffer from the Python Global Interpreter Lock.
    TFc                    t         yt        j                         j                  r,|dk7  r&t               rd}nd}t	        j
                  |d       yt        j                  dkD  r|dk7  rt	        j
                  dd       y| j                         s,| j                  dk(  s|dk7  rt	        j
                  dd       yt        t        | 3  |      S )	zDetermine the number of jobs which are going to run in parallel.

        This also checks if we are attempting to create a nested parallel
        loop.
        r   zInside a Dask worker with daemon=True, setting n_jobs=1.
Possible work-arounds:
- dask.config.set({'distributed.worker.daemon': False})- set the environment variable DASK_DISTRIBUTED__WORKER__DAEMON=False
before creating your Dask cluster.zHMultiprocessing-backed parallel loops cannot be nested, setting n_jobs=1   
stacklevelr   zTMultiprocessing-backed parallel loops cannot be nested, below loky, setting n_jobs=1zVMultiprocessing-backed parallel loops cannot be nested below threads, setting n_jobs=1)r   current_processdaemoninside_dask_workerwarningswarnr   _CURRENT_DEPTHrs   r"   r    r   r1   )r   r0   msgr%   s      r   r1   z'MultiprocessingBackend.effective_n_jobs  s     :&&{%'= =  ca0**Q.{4 " %%'4+=+=+B{7 " +TCFKKr   c                     | j                  |      }|dk(  rt        t        | j                              t	        j
                          t        |fi || _        || _        |S r   )	r1   r   rZ   r"   gccollectr	   r   r>   )r   r0   r>   r?   r@   memmappingpool_argss         r   rB   z MultiprocessingBackend.configure  sa     &&v.Q;#!0B0BCE E 	

#FB.AB
 r   c                 J    t         t        |           | j                          yr   )r    r   rH   r   )r   r%   s    r   rH   z MultiprocessingBackend.terminate  s    $d57 r   rt   )
rv   rw   rx   ry   r   r   r1   rB   rH   r   r   s   @r   r   r     s,     "& %0Ld! !r   r   c                   F    e Zd ZdZdZdZ	 	 d
dZd ZddZd Z	d Z
dd	Zy)LokyBackendz>Managing pool of workers with loky instead of multiprocessing.TNc                     | j                  |      }|dk(  rt        t        | j                              t	        |f|| j                  |      |j                  d|| _        || _        |S )z9Build a process executor and return the number of workersr   rX   )r0   )timeoutrj   
context_id)	r1   r   rZ   r"   r   rm   _id_workersr>   )r   r0   r>   r?   r@   idle_worker_timeoutmemmappingexecutor_argss          r   rB   zLokyBackend.configure  s     &&v.Q;#!0B0BCE E 0@/(((7||@ (?@ !r   c                    |dk(  rt        d      t        |yt        j                         j                  r,|dk7  r&t	               rd}nd}t        j                  |d       y| j                         s,| j                  dk(  s|dk7  rt        j                  dd       y|dk  rt        t               dz   |z   d      }|S )	r   r   r   r   zInside a Dask worker with daemon=True, setting n_jobs=1.
Possible work-arounds:
- dask.config.set({'distributed.worker.daemon': False})
- set the environment variable DASK_DISTRIBUTED__WORKER__DAEMON=False
before creating your Dask cluster.zRLoky-backed parallel loops cannot be called in a multiprocessing, setting n_jobs=1r   r   zKLoky-backed parallel loops cannot be nested below threads, setting n_jobs=1)r   r   r   r   r   r   r   rs   r"   ra   r   )r   r0   r   s      r   r1   zLokyBackend.effective_n_jobs!  s    Q;EFFZ6> !(({%'= =  ca0%%'4+=+=+B{0 " aZq6115Fr   c                 b    | j                   j                  |      }||j                  |       |S )r4   )r   submitadd_done_callback)r   r6   r7   futures       r   r8   zLokyBackend.apply_asyncJ  s/    %%d+$$X.r   c                 T    	 |j                         S # t        $ r t        d      w xY w)Na  The executor underlying Parallel has been shutdown. This is likely due to the garbage collection of a previous generator from a call to Parallel with return_as='generator'. Make sure the generator is not garbage collected when submitting a new job or that it is first properly exhausted.)resultr   r   r:   s     r   r<   z$LokyBackend.retrieve_result_callbackQ  s5    		::<$ 	O 	s    'c                     | j                   B| j                   j                  j                  | j                  j                  d       d | _         | j                          y )NF)r   force)r   _temp_folder_manager_clean_temporary_resourcesr>   r   r   r   s    r   rH   zLokyBackend.terminate]  sP    ==$ MM..II==,,E J  !DM r   c                     | j                   j                  d       d| _         |r2| j                  | j                  j                  | j                         yy)zLShutdown the workers and restart a new one with the same parameters
        T)kill_workersNr   )r   rH   rB   r>   r0   rT   s     r   rV   zLokyBackend.abort_everythingi  sF     	T2NN$--"6"6NO r   )r   NNNi,  r   ru   )rv   rw   rx   ry   r   rz   rB   r1   r8   r<   rH   rV   r   r   r   r   r     s7    H!%%)"FJ&)'R

!Pr   r   c                       e Zd ZdZd Zy)r   z<Raised when configuration should fallback to another backendc                     || _         y r   )backend)r   r   s     r   r!   zFallbackToBackend.__init__v  s	    r   N)rv   rw   rx   ry   r!   r   r   r   r   r   s  s
    Fr   r   c                  ^    	 ddl m}  	  |         y# t        $ r Y yw xY w# t        $ r Y yw xY w)zICheck whether the current function is executed inside a Dask worker.
    r   
get_workerFT)distributedr   ImportErrorr   r   s    r   r   r   z  s>    *    s      		,,)$ry   r   rc   r   rp   r}   abcr   r   _utilsr   r   _multiprocessing_helpersr   poolr	   multiprocessing.poolr
   executorr   externals.lokyr   r   externals.loky.process_executorr   r   rZ   objectr   r   r[   r   r   	Exceptionr   r   r   r   r   <module>r      s    
 	    '
 )>$/1 <FpMG pMf$+ $D/:v /:djN jNZ&')< &RO!-/@0O!ddP#%8 dPN	 r   