ó
î–+[c           @   s  d  Z  d d l Z d d l Z d d l Z d d l m Z d d l Z d d l Z d d l Z d d l	 Z	 y d d l
 Z Wn d d l Z n Xe e j j d d ƒ ƒ p® d Z e rä y d d l Z Wqä e k
 rà d Z qä Xn  e r6y e j ƒ  Z [ Wq6e e f k
 r2Z d Z e j d e f ƒ q6Xn  d d l m Z m Z d d l m Z m Z d d	 l m Z m Z d
 „  Z  d „  Z! d e" f d „  ƒ  YZ# d e$ f d „  ƒ  YZ% d „  Z& d e$ f d „  ƒ  YZ' d e$ f d „  ƒ  YZ( d e f d „  ƒ  YZ) d S(   s+   
Helpers for embarrassingly parallel code.
iÿÿÿÿN(   t   sqrtt   JOBLIB_MULTIPROCESSINGi   s'   %s.  joblib will operate in serial mode(   t
   format_exct   format_outer_frames(   t   Loggert   short_format_time(   t   TransportableExceptiont   _mk_exceptionc           C   s   t  d k r d St  j ƒ  S(   s     Return the number of CPUs.
    i   N(   t   multiprocessingt   Nonet	   cpu_count(    (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR
   0   s    c         C   sv   | s
 t  S| d k r t S|  d k r* t Sd d | d } t |  | ƒ } t |  d | ƒ } t | ƒ t | ƒ k S(   s¤    Returns False for indices increasingly appart, the distance
        depending on the value of verbose.

        We use a lag increasing as the square of index
    i
   i    g      à?i   i   i   (   t   Truet   FalseR    t   int(   t   indext   verboset   scalet
   next_scale(    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyt   _verbosity_filter;   s    t   WorkerInterruptc           B   s   e  Z d  Z RS(   sa    An exception that is not KeyboardInterrupt to allow subprocesses
        to be interrupted.
    (   t   __name__t
   __module__t   __doc__(    (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR   N   s   t   SafeFunctionc           B   s    e  Z d  Z d „  Z d „  Z RS(   sÍ    Wraps a function to make it exception with full traceback in
        their representation.
        Useful for parallel computing with multiprocessing, for which
        exceptions cannot be captured.
    c         C   s   | |  _  d  S(   N(   t   func(   t   selfR   (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyt   __init__\   s    c         O   s}   y |  j  | | Ž  SWnb t k
 r0 t ƒ  ‚ nI t j ƒ  \ } } } t | | | d d d d ƒ} t | | ƒ ‚ n Xd  S(   Nt   contexti
   t	   tb_offseti   (   R   t   KeyboardInterruptR   t   syst   exc_infoR   R   (   R   t   argst   kwargst   e_typet   e_valuet   e_tbt   text(    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyt   __call___   s    	(   R   R   R   R   R&   (    (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR   V   s   	c            sM   t  j ˆ  ƒ ‡  f d †  } y t j ˆ  ƒ | ƒ } Wn t k
 rH n X| S(   s<    Decorator used to capture the arguments of a function.
    c             s   ˆ  |  | f S(   N(    (   R    R!   (   t   function(    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyt   delayed_functionv   s    (   t   picklet   dumpst	   functoolst   wrapst   AttributeError(   R'   R(   (    (   R'   sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyt   delayedo   s    t   ImmediateApplyc           B   s    e  Z d  Z d „  Z d „  Z RS(   s#    A non-delayed apply function.
    c         C   s   | | | Ž  |  _  d  S(   N(   t   results(   R   R   R    R!   (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR   ƒ   s    c         C   s   |  j  S(   N(   R0   (   R   (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyt   getˆ   s    (   R   R   R   R   R1   (    (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR/   €   s   	t   CallBackc           B   s    e  Z d  Z d „  Z d „  Z RS(   sk    Callback used by parallel: it is used for progress reporting, and
        to add data to be processed
    c         C   s   | |  _  | |  _ d  S(   N(   t   parallelR   (   R   R   R3   (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR   ‘   s    	c         C   s3   |  j  j |  j ƒ |  j  j r/ |  j  j ƒ  n  d  S(   N(   R3   t   print_progressR   t	   _iterablet   dispatch_next(   R   t   out(    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR&   •   s    (   R   R   R   R   R&   (    (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR2      s   	t   Parallelc           B   s_   e  Z d  Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z	 d „  Z
 RS(   sÖ   Helper class for readable parallel mapping.

        Parameters
        -----------
        n_jobs: int
            The number of jobs to use for the computation. If -1 all CPUs
            are used. If 1 is given, no parallel computing code is used
            at all, which is useful for debuging. For n_jobs below -1,
            (n_cpus + 1 - n_jobs) are used. Thus for n_jobs = -2, all
            CPUs but one are used.
        verbose: int, optional
            The verbosity level: if non zero, progress messages are
            printed. Above 50, the output is sent to stdout.
            The frequency of the messages increases with the verbosity level.
            If it more than 10, all iterations are reported.
        pre_dispatch: {'all', integer, or expression, as in '3*n_jobs'}
            The amount of jobs to be pre-dispatched. Default is 'all',
            but it may be memory consuming, for instance if each job
            involves a lot of a data.

        Notes
        -----

        This object uses the multiprocessing module to compute in
        parallel the application of a function to many different
        arguments. The main functionality it brings in addition to
        using the raw multiprocessing API are (see examples for details):

            * More readable code, in particular since it avoids
              constructing list of arguments.

            * Easier debuging:
                - informative tracebacks even when the error happens on
                  the client side
                - using 'n_jobs=1' enables to turn off parallel computing
                  for debuging without changing the codepath
                - early capture of pickling errors

            * An optional progress meter.

            * Interruption of multiprocesses jobs with 'Ctrl-C'

        Examples
        --------

        A simple example:

        >>> from math import sqrt
        >>> from joblib import Parallel, delayed
        >>> Parallel(n_jobs=1)(delayed(sqrt)(i**2) for i in range(10))
        [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

        Reshaping the output when the function has several return
        values:

        >>> from math import modf
        >>> from joblib import Parallel, delayed
        >>> r = Parallel(n_jobs=1)(delayed(modf)(i/2.) for i in range(10))
        >>> res, i = zip(*r)
        >>> res
        (0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5)
        >>> i
        (0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0)

        The progress meter: the higher the value of `verbose`, the more
        messages::

            >>> from time import sleep
            >>> from joblib import Parallel, delayed
            >>> r = Parallel(n_jobs=2, verbose=5)(delayed(sleep)(.1) for _ in range(10)) #doctest: +SKIP
            [Parallel(n_jobs=2)]: Done   1 out of  10 | elapsed:    0.1s remaining:    0.9s
            [Parallel(n_jobs=2)]: Done   3 out of  10 | elapsed:    0.2s remaining:    0.5s
            [Parallel(n_jobs=2)]: Done   6 out of  10 | elapsed:    0.3s remaining:    0.2s
            [Parallel(n_jobs=2)]: Done   9 out of  10 | elapsed:    0.5s remaining:    0.1s
            [Parallel(n_jobs=2)]: Done  10 out of  10 | elapsed:    0.5s finished

        Traceback example, note how the line of the error is indicated
        as well as the values of the parameter passed to the function that
        triggered the exception, even though the traceback happens in the
        child process::

         >>> from string import atoi
         >>> from joblib import Parallel, delayed
         >>> Parallel(n_jobs=2)(delayed(atoi)(n) for n in ('1', '300', 30)) #doctest: +SKIP
         #...
         ---------------------------------------------------------------------------
         Sub-process traceback:
         ---------------------------------------------------------------------------
         TypeError                                          Fri Jul  2 20:32:05 2010
         PID: 4151                                     Python 2.6.5: /usr/bin/python
         ...........................................................................
         /usr/lib/python2.6/string.pyc in atoi(s=30, base=10)
             398     is chosen from the leading characters of s, 0 for octal, 0x or
             399     0X for hexadecimal.  If base is 16, a preceding 0x or 0X is
             400     accepted.
             401
             402     """
         --> 403     return _int(s, base)
             404
             405
             406 # Convert string to long integer
             407 def atol(s, base=10):

         TypeError: int() can't convert non-string with explicit base
         ___________________________________________________________________________

        Using pre_dispatch in a producer/consumer situation, where the
        data is generated on the fly. Note how the producer is first
        called a 3 times before the parallel loop is initiated, and then
        called to generate new data on the fly. In this case the total
        number of iterations cannot be reported in the progress messages::

         >>> from math import sqrt
         >>> from joblib import Parallel, delayed

         >>> def producer():
         ...     for i in range(6):
         ...         print 'Produced %s' % i
         ...         yield i

         >>> out = Parallel(n_jobs=2, verbose=100, pre_dispatch='1.5*n_jobs')(
         ...                         delayed(sqrt)(i) for i in producer()) #doctest: +SKIP
         Produced 0
         Produced 1
         Produced 2
         [Parallel(n_jobs=2)]: Done   1 jobs       | elapsed:    0.0s
         Produced 3
         [Parallel(n_jobs=2)]: Done   2 jobs       | elapsed:    0.0s
         Produced 4
         [Parallel(n_jobs=2)]: Done   3 jobs       | elapsed:    0.0s
         Produced 5
         [Parallel(n_jobs=2)]: Done   4 jobs       | elapsed:    0.0s
         [Parallel(n_jobs=2)]: Done   5 out of   6 | elapsed:    0.0s remaining:    0.0s
         [Parallel(n_jobs=2)]: Done   6 out of   6 | elapsed:    0.0s finished
    i   i    t   allc         C   s=   | |  _  | |  _ | |  _ d  |  _ d  |  _ t ƒ  |  _ d  S(   N(   R   t   n_jobst   pre_dispatchR	   t   _poolt   _outputt   listt   _jobs(   R   R:   R   R;   (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR   $  s    					c         C   s&  |  j  d k r” t | | | ƒ } t |  j ƒ } t | |  j ƒ sr |  j d | d t t	 j	 ƒ  |  j
 ƒ f ƒ n  |  j j | ƒ |  j d 7_ nŽ |  j j ƒ  zp yS |  j  j t | ƒ | | d t |  j |  ƒ ƒ} |  j j | ƒ |  j d 7_ Wn t k
 rd GHn XWd |  j j ƒ  Xd S(   sK    Queue the function for computing, with or without multiprocessing
        s!   Done %3i jobs       | elapsed: %si   t   callbacks   [Parallel] Pool seems closedN(   R<   R	   R/   t   lenR?   R   R   t   _printR   t   timet   _start_timet   appendt   n_dispatchedt   _lockt   acquiret   apply_asyncR   R2   t   AssertionErrort   release(   R   R   R    R!   t   jobR   (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyt   dispatch.  s&    	  c         C   s   |  j  d 7_  xy |  j  rŠ y> |  j j ƒ  \ } } } |  j | | | ƒ |  j  d 8_  Wq t k
 rl q t k
 r† d |  _ d SXq Wd S(   s4    Dispatch more data for parallel processing
        i   N(   t   _dispatch_amountR5   t   nextRM   t
   ValueErrort   StopIterationR	   (   R   R   R    R!   (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR6   H  s    	c         C   sY   |  j  s d S|  j  d k  r+ t j j } n t j j } | | } | d |  | f ƒ d S(   sG    Display the message on stout or stderr depending on verbosity
        Ni2   s	   [%s]: %s
(   R   R   t   stderrt   writet   stdout(   R   t   msgt   msg_argst   writer(    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyRB   [  s    	
c         C   s  |  j  s d St j ƒ  |  j } |  j rb t | |  j  ƒ r? d S|  j d | d t | ƒ f ƒ n« |  j } | d k sÄ | | d |  j } | |  j  d } | d | k } | s½ | | rÄ d Sn  | | d |  j | d } |  j d | d | t | ƒ t | ƒ f ƒ d S(   su   Display the process of the parallel execution only a fraction
           of time, controled by self.verbose.
        Ns!   Done %3i jobs       | elapsed: %si   i    g      ð?s/   Done %3i out of %3i | elapsed: %s remaining: %s(	   R   RC   RD   R5   R   RB   R   RF   t   _pre_dispatch_amount(   R   R   t   elapsed_timet   queue_lengtht   cursort	   frequencyt   is_last_itemt   remaining_time(    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR4   i  s.    						c         C   sR  t  ƒ  |  _ x?|  j rMt |  d ƒ r7 |  j j ƒ  n  |  j j d ƒ } t |  d ƒ rh |  j j ƒ  n  y |  j j | j	 ƒ  ƒ Wq t
 |  j ƒ k
 rI} t | t t f ƒ rç t |  d ƒ rÞ |  j j ƒ  |  j j ƒ  n  | ‚ nY t | t ƒ r@t d d d d ƒ } d | | j f } t | j ƒ d } | | ƒ ‚ n  | ‚ q Xq Wd  S(	   NRG   i    R<   R   i
   t   stack_starti   sÏ   Multiprocessing exception:
%s
---------------------------------------------------------------------------
Sub-process traceback:
---------------------------------------------------------------------------
%s(   R>   R=   R?   t   hasattrRG   RH   t   popRK   RE   R1   t   tuplet
   exceptionst
   isinstanceR   R   R<   t   closet	   terminateR   R   t   messageR   t   etype(   R   RL   t	   exceptiont   this_reportt   reportt   exception_type(    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyt   retrieve  s2    		c   	   
   C   sz  |  j  r t d ƒ ‚ n  |  j } | d k  rY t d  k	 rY t t j ƒ  d | d ƒ } n  t g |  _ | d  k s‰ t d  k s‰ | d k r› d } d  |  _	 nk t j
 ƒ  j rÏ d } d  |  _	 t j d d d ƒn7 t j | ƒ |  _	 t j ƒ  |  _ |  j j t t g ƒ |  j d k s!| d k r6d  |  _ d |  _ n^ | |  _ d |  _ |  j } t | d ƒ rot | ƒ } n  t | ƒ |  _ } t j | | ƒ } t j ƒ  |  _ d |  _  z x* | D]" \ } } } |  j! | | | ƒ q¶W|  j" ƒ  t j ƒ  |  j } |  j# d	 t$ |  j% ƒ t$ |  j% ƒ t& | ƒ f ƒ Wd  | d k rW|  j	 j' ƒ  |  j	 j( ƒ  n  t) ƒ  |  _  X|  j% } d  |  _% | S(
   Ns)   This Parallel instance is already runningi    i   s1   Parallel loops cannot be nested, setting n_jobs=1t
   stackleveli   R9   t   endswiths*   Done %3i out of %3i | elapsed: %s finished(*   R?   RP   R:   R   R	   t   maxR
   R   Rc   R<   t   current_processt	   _daemonict   warningst   warnt   Poolt	   threadingt   LockRG   t   extendR   R   R;   R5   RX   RN   R`   t   evalR   t	   itertoolst   isliceRC   RD   RF   RM   Rm   RB   RA   R=   R   Re   t   joinR>   (	   R   t   iterableR:   R;   R'   R    R!   RY   t   output(    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR&   ¸  s\    		 $	
					
			c         C   s   d |  j  j |  j f S(   Ns   %s(n_jobs=%s)(   t	   __class__R   R:   (   R   (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyt   __repr__÷  s    (   R   R   R   R   RM   R6   RB   R4   Rm   R&   R€   (    (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyR8   œ   s   ‡
				'	(	?(*   R   t   osR   Rs   t   mathR    R+   RC   Rv   Rz   t   cPickleR)   R   t   environR1   R	   R   t   ImportErrort	   Semaphoret   _semt   OSErrort   eRt   t   format_stackR   R   t   loggerR   R   t   my_exceptionsR   R   R
   R   t	   ExceptionR   t   objectR   R.   R/   R2   R8   (    (    (    sW   /home/psgendb/BIRCHDEV/install/quast-5.2.0/quast_libs/site_packages/joblib2/parallel.pyt   <module>   sH   !			