ó
‡ˆ\c           @   s3  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 Z d d l Z d d l m	 Z	 m
 Z
 d d l m Z d d l m Z m Z d d l m Z d d l m Z m Z e d k	 rAd d	 l m Z d d
 l m Z d d l m Z d d l m Z d d l m Z d d l  m Z! d d l" m# Z# m$ Z$ n  d e e	 ƒ f d „  ƒ  YZ% d e% f d „  ƒ  YZ& d e' f d „  ƒ  YZ( d e' f d „  ƒ  YZ) d e( e% f d „  ƒ  YZ* d e( e) e% f d „  ƒ  YZ+ d e) e% f d „  ƒ  YZ, d e' f d „  ƒ  YZ- d e' f d  „  ƒ  YZ. d! e/ f d" „  ƒ  YZ0 d S(#   s,   
Backends for embarrassingly parallel code.
iÿÿÿÿN(   t   ABCMetat   abstractmethodi   (   t
   format_exc(   t   WorkerInterruptt   TransportableException(   t   mp(   t   with_metaclasst   PY27(   t   delete_folder(   t   MemmappingPool(   t
   ThreadPool(   t   get_memmapping_executor(   t   TimeoutError(   t   process_executort	   cpu_countt   ParallelBackendBasec           B   sà   e  Z d  Z e Z d Z d d „ Z d d d d d g Z e d „  ƒ Z	 e d d	 „ ƒ Z d
 d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z d „  Z e j d „  ƒ Z e d
 d „ ƒ Z RS(   sE   Helper abc which defines all methods a ParallelBackend must implementi    c         C   s   | |  _  d  S(   N(   t   nesting_level(   t   selfR   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   __init__$   s    t   OMP_NUM_THREADSt   OPENBLAS_NUM_THREADSt   MKL_NUM_THREADSt   VECLIB_MAXIMUM_THREADSt   NUMEXPR_NUM_THREADSc         C   s   d S(   så  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.
        N(    (   R   t   n_jobs(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   effective_n_jobs,   s    c         C   s   d S(   s   Schedule a func to be runN(    (   R   t   funct   callback(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   apply_async>   s    i   c         K   s   | |  _  |  j | ƒ S(   sØ   Reconfigure 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.
        (   t   parallelR   (   R   R   R   t   prefert   requiret   backend_args(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt	   configureB   s    	c         C   s   d S(   s;   Call-back method called at the beginning of a Parallel callN(    (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt
   start_callL   s    c         C   s   d S(   s5   Call-back method called at the end of a Parallel callN(    (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt	   stop_callO   s    c         C   s   d S(   s0   Shutdown the workers and free the shared memory.N(    (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt	   terminateR   s    c         C   s   d S(   s    Determine the optimal batch sizei   (    (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   compute_batch_sizeU   s    c         C   s   d S(   s1   Callback indicate how long it took to run a batchN(    (   R   t
   batch_sizet   duration(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   batch_completedY   s    c         C   s   g  S(   s'   List of exception types to be captured.(    (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   get_exceptions\   s    c         C   s   d S(   sv  Abort any running tasks

        This is called when an exception has been raised when executing a tasks
        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.
        N(    (   R   t   ensure_ready(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   abort_everything`   s    c         C   sL   t  |  d d ƒ d } | d k r5 t d | ƒ d f St d | ƒ d f Sd S(   sö   Backend 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   i    i   N(   t   getattrt   SequentialBackendt   Nonet   ThreadingBackend(   R   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   get_nested_backendx   s    c         c   s	   d Vd S(   sœ  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.
        N(    (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   retrieval_context…   s    c         C   sO   xH |  j  D]= } t j j | d ƒ } | d k r
 t | ƒ t j | <q
 q
 Wd S(   s  Initializer to limit the number of threads used by some C-libraries.

        This function set the number of threads to `n_threads` for OpenMP, MKL,
        Accelerated and OpenBLAS libraries, that can be used with scientific
        computing tools like numpy.
        N(   t   SUPPORTED_CLIB_VARSt   ost   environt   getR.   t   str(   t   clst	   n_threadst   vart	   var_value(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   limit_clib_threads–   s    N(   t   __name__t
   __module__t   __doc__t   Falset   supports_timeoutR   R   R2   R   R   R.   R   R!   R"   R#   R$   R%   R(   R)   t   TrueR+   R0   t
   contextlibt   contextmanagerR1   t   classmethodR;   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR      s*   	
							R-   c           B   s8   e  Z d  Z e Z e Z d „  Z d d „ Z d „  Z	 RS(   s®   A ParallelBackend which will execute all batches sequentially.

    Does not use/create any threading objects, and hence has minimal
    overhead. Used when n_jobs == 1.
    c         C   s   | d k r t  d ƒ ‚ n  d S(   s?   Determine the number of jobs which are going to run in paralleli    s&   n_jobs == 0 in Parallel has no meaningi   (   t
   ValueError(   R   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR   ®   s    c         C   s#   t  | ƒ } | r | | ƒ n  | S(   s   Schedule a func to be run(   t   ImmediateResult(   R   R   R   t   result(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR   ´   s    c         C   s   d d l  m } | ƒ  S(   Ni   (   t   get_active_backend(   R   RH   (   R   RH   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR0   »   s    N(
   R<   R=   R>   RA   t   uses_threadst   supports_sharedmemR   R.   R   R0   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR-   ¤   s   	t   PoolManagerMixinc           B   sG   e  Z d  Z d Z d „  Z d „  Z d „  Z d d „ Z e	 d „ Z
 RS(   s,   A helper class for managing pool of workers.c         C   sd   | d k r t  d ƒ ‚ nE t d k s3 | d k r7 d S| d k  r` t t ƒ  d | d ƒ } n  | S(   s?   Determine the number of jobs which are going to run in paralleli    s&   n_jobs == 0 in Parallel has no meaningi   N(   RE   R   R.   t   maxR   (   R   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR   É   s    c         C   s9   |  j  d k	 r5 |  j  j ƒ  |  j  j ƒ  d |  _  n  d S(   s#   Shutdown the process or thread poolN(   t   _poolR.   t   closeR$   (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR$   Õ   s    c         C   s   |  j  S(   s>   Used by apply_async to make it possible to implement lazy init(   RM   (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt	   _get_poolÜ   s    c         C   s   |  j  ƒ  j t | ƒ d | ƒS(   s   Schedule a func to be runR   (   RO   R   t   SafeFunction(   R   R   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR   à   s    c         C   s?   |  j  ƒ  | r; |  j d |  j j d |  j |  j j  n  d S(   s@   Shutdown the pool and restart a new one with the same parametersR   R   N(   R$   R!   R   R   t   _backend_args(   R   R*   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR+   å   s    
N(   R<   R=   R>   R.   RM   R   R$   RO   R   RA   R+   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyRK   Ä   s   			t   AutoBatchingMixinc           B   sJ   e  Z d  Z d Z d Z d Z d Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s/   A helper class for automagically batching jobs.gš™™™™™É?i   i   g        c         C   s   |  j  |  _ |  j |  _ d  S(   N(   t   _DEFAULT_EFFECTIVE_BATCH_SIZEt   _effective_batch_sizet    _DEFAULT_SMOOTHED_BATCH_DURATIONt   _smoothed_batch_duration(   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR   þ   s    c         C   s  |  j  } |  j } | d k r‘ | |  j k  r‘ t | |  j | ƒ } t d | d ƒ } | |  _  |  j j d k rö |  j j d | | f ƒ qö ne | |  j k rð | d k rð | d } | |  _  |  j j d k rö |  j j d | | f ƒ qö n | } | | k r|  j	 |  _ n  | S(   s    Determine the optimal batch sizei    i   i   i
   s:   Batch computation too fast (%.4fs.) Setting batch_size=%d.s:   Batch computation too slow (%.4fs.) Setting batch_size=%d.(
   RT   RV   t   MIN_IDEAL_BATCH_DURATIONt   intRL   R   t   verboset   _printt   MAX_IDEAL_BATCH_DURATIONRU   (   R   t   old_batch_sizet   batch_durationt   ideal_batch_sizeR&   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR%     s2    		

		
		c         C   sR   | |  j  k rN |  j } | |  j k r0 | } n d | d | } | |  _ n  d S(   s1   Callback indicate how long it took to run a batchgš™™™™™é?gš™™™™™É?N(   RT   RV   RU   (   R   R&   R'   t   old_durationt   new_duration(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR(   1  s    		c         C   s   |  j  |  _ |  j |  _ d S(   sg   Reset batch statistics to default values.

        This avoids interferences with future jobs.
        N(   RS   RT   RU   RV   (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   reset_batch_statsA  s    (   R<   R=   R>   RW   R[   RS   RU   R   R%   R(   Ra   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyRR   í   s   		/	R/   c           B   s8   e  Z d  Z e Z e Z e Z d d d „ Z d „  Z	 RS(   sŽ  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.
    i   c         K   sL   |  j  | ƒ } | d k r6 t t d |  j ƒ ƒ ‚ n  | |  _ | |  _ | S(   s?   Build a process or thread pool and return the number of workersi   R   (   R   t   FallbackToBackendR-   R   R   t   _n_jobs(   R   R   R   R    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR!   ]  s    		c         C   s+   |  j  d k r$ t |  j ƒ |  _  n  |  j  S(   s“   Lazily initialize the thread pool

        The actual pool of worker threads is only initialized at the first
        call to apply_async.
        N(   RM   R.   R
   Rc   (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyRO   h  s    N(
   R<   R=   R>   RA   R@   RI   RJ   R.   R!   RO   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR/   J  s   t   MultiprocessingBackendc           B   sA   e  Z d  Z d Z e Z d „  Z d d d d d „ Z d „  Z	 RS(   s  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.
    t   __JOBLIB_SPAWNED_PARALLEL__c         C   sÎ   t  d k r d St  j ƒ  j rE | d k rA t j d d d ƒn  d St j d k rz | d k rv t j d d d ƒn  d St t	 j
 ƒ  t	 j ƒ s¸ | d k r´ t j d d d ƒn  d St t |  ƒ j | ƒ S(	   s¢   Determine the number of jobs which are going to run in parallel.

        This also checks if we are attempting to create a nested parallel
        loop.
        i   sH   Multiprocessing-backed parallel loops cannot be nested, setting n_jobs=1t
   stackleveli   i    sT   Multiprocessing-backed parallel loops cannot be nested, below loky, setting n_jobs=1sV   Multiprocessing-backed parallel loops cannot be nested below threads, setting n_jobs=1N(   R   R.   t   current_processt   daemont   warningst   warnR   t   _CURRENT_DEPTHt
   isinstancet	   threadingt   current_threadt   _MainThreadt   superRd   R   (   R   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR     s*    


i   c         K   s«   |  j  | ƒ } | d k r6 t t d |  j ƒ ƒ ‚ n  t t j j |  j d ƒ ƒ } | ri t	 d ƒ ‚ n  d t j |  j <t
 j ƒ  t | d |  j | |  _ | |  _ | S(   s?   Build a process or thread pool and return the number of workersi   R   i    s%  [joblib] Attempting to do parallel computing without protecting your import on a system that does not support forking. To use parallel-computing in a script, you must protect your main loop using "if __name__ == '__main__'". Please see the joblib documentation on Parallel for more informationt   1t   initializer(   R   Rb   R-   R   RX   R3   R4   R5   t   JOBLIB_SPAWNED_PROCESSt   ImportErrort   gct   collectR	   R;   RM   R   (   R   R   R   R   R   t   memmappingpool_argst   already_forked(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR!   §  s    
	c         C   sC   t  t |  ƒ j ƒ  |  j t j k r5 t j |  j =n  |  j ƒ  d S(   s#   Shutdown the process or thread poolN(   Rp   Rd   R$   Rs   R3   R4   Ra   (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR$   Ã  s    N(
   R<   R=   R>   Rs   RA   R@   R   R.   R!   R$   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyRd   s  s   	&t   LokyBackendc           B   sh   e  Z d  Z e Z d d	 d	 d	 d d „ Z d „  Z d	 d „ Z e	 d	 d „ ƒ Z
 d „  Z e d „ Z RS(
   s>   Managing pool of workers with loky instead of multiprocessing.i   i,  c         K   sd   |  j  | ƒ } | d k r6 t t d |  j ƒ ƒ ‚ n  t | d | d |  j | |  _ | |  _ | S(   s9   Build a process executor and return the number of workersi   R   t   timeoutRr   (   R   Rb   R-   R   R   R;   t   _workersR   (   R   R   R   R   R   t   idle_worker_timeoutt   memmappingexecutor_args(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR!   Ñ  s    	c         C   s×   | d k r t  d ƒ ‚ n¸ t d k s3 | d k r7 d St j ƒ  j rl | d k rh t j d d d ƒn  d St t j	 ƒ  t j
 ƒ sª | d k r¦ t j d d d ƒn  d S| d k  rÓ t t ƒ  d | d ƒ } n  | S(	   s?   Determine the number of jobs which are going to run in paralleli    s&   n_jobs == 0 in Parallel has no meaningi   sR   Loky-backed parallel loops cannot be called in a multiprocessing, setting n_jobs=1Rf   i   sK   Loky-backed parallel loops cannot be nested below threads, setting n_jobs=1N(   RE   R   R.   Rg   Rh   Ri   Rj   Rl   Rm   Rn   Ro   RL   R   (   R   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR   à  s&    

c         C   sP   |  j  j t | ƒ ƒ } t j |  j | ƒ | _ | d k	 rL | j | ƒ n  | S(   s   Schedule a func to be runN(	   R{   t   submitRP   t	   functoolst   partialt   wrap_future_resultR5   R.   t   add_done_callback(   R   R   R   t   future(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR   ü  s
    c         C   s5   y |  j  d | ƒ SWn t k
 r0 t ƒ  ‚ n Xd S(   sk   Wrapper for Future.result to implement the same behaviour as
        AsyncResults.get from multiprocessing.Rz   N(   RG   t   LokyTimeoutErrorR   (   Rƒ   Rz   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR     s    c         C   s9   |  j  d  k	 r+ t |  j  j ƒ d  |  _  n  |  j ƒ  d  S(   N(   R{   R.   R   t   _temp_folderRa   (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR$     s    c         C   sX   |  j  j d t ƒ t |  j  j ƒ d |  _  | rT |  j d |  j j d |  j ƒ n  d S(   sL   Shutdown the workers and restart a new one with the same parameters
        t   kill_workersR   R   N(	   R{   t   shutdownRA   R   R…   R.   R!   R   R   (   R   R*   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR+     s
    	N(   R<   R=   R>   RA   R@   R.   R!   R   R   t   staticmethodR   R$   R+   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyRy   Ì  s   		
RF   c           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | ƒ  |  _  d  S(   N(   t   results(   R   t   batch(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR   "  s    c         C   s   |  j  S(   N(   R‰   (   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR5   '  s    (   R<   R=   R   R5   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyRF   !  s   	RP   c           B   s    e  Z d  Z d „  Z d „  Z RS(   s  Wrapper that handles the serialization of exception tracebacks.

    If an exception is triggered when calling the inner function, a copy of
    the full traceback is captured to make it possible to serialize
    it so that it can be rendered in a different Python process.
    c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR   2  s    c         O   s“   y |  j  | | Ž  SWnx t k
 r0 t ƒ  ‚ n_ t k
 rŽ t rˆ t j ƒ  \ } } } t | | | d d d d ƒ} t | | ƒ ‚ q ‚  n Xd  S(   Nt   contexti
   t	   tb_offseti   (	   R   t   KeyboardInterruptR   t   BaseExceptionR   t   syst   exc_infoR   R   (   R   t   argst   kwargst   e_typet   e_valuet   e_tbt   text(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   __call__5  s    	(   R<   R=   R>   R   R—   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyRP   +  s   	Rb   c           B   s   e  Z d  Z d „  Z RS(   s<   Raised when configuration should fallback to another backendc         C   s   | |  _  d  S(   N(   t   backend(   R   R˜   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyR   M  s    (   R<   R=   R>   R   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyRb   J  s   (1   R>   Ru   R3   R   Ri   Rm   R   RB   t   abcR    R   t   format_stackR   t   my_exceptionsR   R   t   _multiprocessing_helpersR   t   _compatR   R   R.   t   diskR   t   poolR	   t   multiprocessing.poolR
   t   executorR   t   multiprocessingR   t   externals.loky._baseR„   t   externals.lokyR   R   R   R-   t   objectRK   RR   R/   Rd   Ry   RF   RP   t	   ExceptionRb   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyt   <module>   s>   † )])	XU
