ó
\K]c           @@ sj  d  Z  d d l m Z m Z d d l 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 m Z d d l Z d d l j j Z d d l j Z d d l m Z d d l m Z d d l m Z m Z m Z d d	 l m  Z  d
 „  Z! e! ƒ  Z" d „  Z# d e j$ f d „  ƒ  YZ% d „  Z& d e j' f d „  ƒ  YZ( d „  Z) e
 ƒ  Z* e j j+ d ƒ Z, e d „  ƒ Z- yR e j. r¿d e j/ ƒ  k sže, r³e j0 ƒ  j	 ƒ  Z1 qËe- ƒ  Z1 n e j	 ƒ  Z1 Wn9 e2 k
 rZ3 d Z4 e j5 e4 e6 e3 ƒ ƒ e- ƒ  Z1 n Xe7 a8 d a: d „  Z; d „  Z< d e j= k Z> e? e j= j@ d d ƒ ƒ ZA e> rfeA rfe< ƒ  n  d S(   sò  
This file implements the code-generator for parallel-vectorize.

ParallelUFunc is the platform independent base class for generating
the thread dispatcher.  This thread dispatcher launches threads
that execute the generated function of UFuncCore.
UFuncCore is subclassed to specialize for the input/output types.
The actual workload is invoked inside the function generated by UFuncCore.
UFuncCore also defines a work-stealing mechanism that allows idle threads
to steal works from other threads.
i    (   t   print_functiont   absolute_importN(   t   RLock(   t   contextmanager(   t   ufuncbuilder(   t   as_dtype(   t   typest   configt   utils(   t   _wrapper_infoc          C@ s(   t  j }  |  d k  r$ t d ƒ ‚ n  |  S(   s*   
    Gets the available thread count.
    i   s(   Number of threads specified must be > 0.(   R   t   NUMBA_NUM_THREADSt
   ValueError(   t   t(    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyt   get_thread_count!   s    	c         @ sþ  t  | t ƒ s t ‚ t j j d ƒ } t j j | ƒ ‰ t j j ˆ ƒ } | j t j	 ƒ } t j j | ƒ } t j j
 t j j ƒ  t j j ˆ ƒ t j j | ƒ t j j | ƒ ˆ g ƒ }	 | j ƒ  j d ƒ }
 |
 j d ƒ } d j t | j ƒ | j ƒ } | j |	 d | ƒ} | j d ƒ } t j | ƒ ‰  | j \ } } } } | j ˆ  ƒ } | j ƒ  } | j ƒ  } ‡  ‡ f d †  } t | j ƒ d } t j j
 t j j ƒ  ˆ g d	 | g d
 ƒ } | j | d d ƒ} t j j
 t j j ƒ  | | | ˆ g ƒ } | j | d | j ƒ} |
 j | j ƒ ˆ  j | ˆ ƒ } g  | | | | g D] } | | ƒ ^ qO} ˆ  j | | g | g  | | f D] } | | ƒ ^ q‡ƒ | j  | ƒ | j! | ƒ ˆ  j" ƒ  |
 j# | ƒ |
 j |  ƒ t$ d |
 d | j d | j ƒ S(   s   Wrap the original CPU ufunc/gufunc with a parallel dispatcher.
    This function will wrap gufuncs and ufuncs something like.

    Args
    ----
    ctx
        numba's codegen context

    info: (library, env, name)
        inner function info

    sig
        type signature of the gufunc

    inner_ndim
        inner dimension of the gufunc (this is len(sig.args) in the case of a
        ufunc)

    Returns
    -------
    wrapper_info : (library, env, name)
        The info for the gufunc wrapper.

    Details
    -------

    The kernel signature looks like this:

    void kernel(char **args, npy_intp *dimensions, npy_intp* steps, void* data)

    args - the input arrays + output arrays
    dimensions - the dimensions of the arrays
    steps - the step size for the array (this is like sizeof(type))
    data - any additional data

    The parallel backend then stages multiple calls to this kernel concurrently
    across a number of threads. Practically, for each item of work, the backend
    duplicates `dimensions` and adjusts the first entry to reflect the size of
    the item of work, it also forms up an array of pointers into the args for
    offsets to read/write from/to with respect to its position in the items of
    work. This allows the same kernel to be used for each item of work, with
    simply adjusted reads/writes/domain sizes and is safe by virtue of the
    domain partitioning.

    NOTE: The execution backend is passed the requested thread count, but it can
    choose to ignore it (TBB)!
    i   t   parallelgufuncwrappers   parallel.gufunc.wrappers   .kernel.{}_{}t   namet    c         @ s   ˆ  j  |  ˆ ƒ S(   N(   t   bitcast(   t   arg(   t   buildert
   byte_ptr_t(    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyt   as_void_ptr~   s    i   i   i   t   numba_parallel_fort   libraryt   env(%   t
   isinstancet   tuplet   AssertionErrort   lct   Typet   intt   pointert   get_value_typeR   t   intpt   functiont   voidt   codegent   create_libraryt   create_ir_modulet   formatt   idR   R   t   add_functiont   append_basic_blockt   Buildert   argst   get_python_apit
   gil_ensuret   save_threadt   lent   get_or_insert_functiont   add_linking_libraryR   R   t   callt   restore_threadt   gil_releaset   ret_voidt   add_ir_moduleR	   (   R   t   ctxt   infot   sigt
   inner_ndimt   byte_tt   byte_ptr_ptr_tt   intp_tt
   intp_ptr_tt   fntyt
   wrapperlibt   modt   kernel_namet   lfunct   bb_entryR,   t
   dimensionst   stepst   datat   pyapit	   gil_statet   thread_stateR   t   array_countt   parallel_for_tyt   parallel_fort   innerfunc_fntyt   tmp_voidptrt   fnptrt   xt	   innerargs(    (   R   R   s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyt   build_gufunc_kernel.   sV    0$		('
t   ParallelUFuncBuilderc           B@ s   e  Z d  „  Z RS(   c         C@ s¸   t  ƒ  | j } | j } | j } | j j } t | | | | | ƒ } | j j | j ƒ } g  | j	 D] }	 t
 j |	 j ƒ j ^ qe }
 |
 j t
 j | j j ƒ j ƒ d } |
 | | f S(   N(    (   t   _launch_threadst   target_contextt	   signatureR   t   fndesct   llvm_func_namet   build_ufunc_wrappert   get_pointer_to_functionR   R,   t   npt   dtypet   numt   appendt   return_type(   t   selft   cresR:   R8   RX   R   t   fnameR9   t   ptrt   at	   dtypenumst	   keepalive(    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyt   build¨   s    			+(   t   __name__t
   __module__Ri   (    (    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyRU   §   s   c      	   C@ sI   t  j |  | | | d t d | ƒ} t |  | | | t | j ƒ ƒ } | S(   Nt   objmodeRc   (   R   R[   t   FalseRT   R0   R,   (   R   R8   Rd   RX   Rc   t	   innerfuncR9   (    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyR[   º   s    	t   ParallelGUFuncBuilderc           B@ s#   e  Z d e i  d  „ Z d „  Z RS(   c         C@ sK   | j  t d t ƒ ƒ t t |  ƒ j d | d | d | d | d | ƒ d  S(   Nt   nopythont   py_funcRX   t   identityt   cachet   targetoptions(   t   updatet   dictt   Truet   superRo   t   __init__(   Rb   Rq   RX   Rr   Rs   Rt   (    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyRy   Æ   s    c      	   C@ s¶   t  ƒ  t |  j | |  j |  j d |  j d t ƒ} | j j | j	 ƒ } | j
 } g  } xN | j j D]@ } t | t j ƒ r‰ | j } n | } | j t | ƒ j ƒ qe W| | | f S(   sJ   
        Returns (dtype numbers, function ptr, EnvironmentObject)
        Rs   t
   is_parfors(   RV   t   build_gufunc_wrapperRq   t   sint   soutRs   Rm   R   R\   R   R   RX   R,   R   R   t   ArrayR^   R`   R   R_   (   Rb   Rc   R9   Re   R   Rg   Rf   t   ty(    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyRi   Ó   s    !		N(   Rj   Rk   t   NoneRm   Ry   Ri   (    (    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyRo   Å   s   c      	   C@ s—   | j  } | j } | j } t j |  | | | d | d | ƒ}	 t d „  | Dƒ ƒ }
 t d „  | Dƒ ƒ } t |
 | Bƒ } t | | |	 | | ƒ } | S(   sÑ   Build gufunc wrapper for the given arguments.
    The *is_parfors* is a boolean indicating whether the gufunc is being
    built for use as a ParFors kernel. This changes codegen and caching
    behavior.
    Rs   Rz   c         s@ s"   |  ] } | D] } | Vq q d  S(   N(    (   t   .0t   termt   sym(    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pys	   <genexpr>ü   s    c         s@ s"   |  ] } | D] } | Vq q d  S(   N(    (   R   R‚   Rƒ   (    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pys	   <genexpr>ý   s    (   R   RW   RX   R   R{   t   setR0   RT   (   Rq   Rc   R|   R}   Rs   Rz   R   R8   RX   t	   innerinfot   sym_int   sym_outR;   R9   (    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyR{   ð   s    			t   win32c           c@ s	   d  Vd  S(   N(    (    (    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyt   _nop  s    t   forks@  Could not obtain multiprocessing lock due to OS level error: %s
A likely cause of this problem is '/dev/shm' is missing orread-only such that necessary semaphores cannot be written.
*** The responsibility of ensuring multiprocessing safe access to this initialization sequence/module import is deferred to the user! ***
c           C@ s#   t  d k r t d ƒ ‚ n t  Sd S(   sM   
    Get the name of the threading layer in use for parallel CPU targets
    s#   Threading layer is not initialized.N(   t   _threading_layerR€   R   (    (    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyt   threading_layer2  s    c          @ sñ  t  åt Øt r d  Sd d l m }  m } d „  ‰ ‡ f d †  } t t j ƒ j	 ƒ  } d d d g } d  } t j ƒ  d k } t j ƒ  d	 k } t ƒ  ‰  d
 ˆ  d <d ˆ  d <g  } ‡  f d †  }	 | | k r.ˆ | ƒ } | s%| d k r | j d ƒ q%| d k r%| r%| j d ƒ q%n  | }
 n9| d k rd g } | j d ƒ | d k r_n | d k r‘| r| j d ƒ n  | j d ƒ n^ | d k rÙ| s³| j d ƒ n  | rÉ| j d ƒ n  | j d ƒ n d } t | | ƒ ‚ | | ƒ \ } }
 nc | d k rQ| | ƒ \ } }
 | sg| j d ƒ | rN| j d ƒ qNqgn d } t | | ƒ ‚ | sz|	 | ƒ n  t j d | j ƒ t j d | j ƒ t j d | j ƒ |  d  | ƒ | j ƒ } | t ƒ |
 a t a Wd  QXWd  QXd  S(   Ni    (   t	   CFUNCTYPEt   c_intc         S@ s¶   d	 } |  j d ƒ r@ y d d l m } Wq² t k
 r< q² Xnr |  j d ƒ rz y d d l m } Wq² t k
 rv q² Xn8 |  j d ƒ rœ d d l m } n d } t | |  ƒ ‚ | S(
   sZ   
                Loads a specific threading layer backend based on string
                t   tbbi   (   t   tbbpoolt   omp(   t   omppoolt	   workqueue(   R“   s/   Unknown value specified for threading layer: %sN(   R€   t
   startswithR   R   t   ImportErrorR’   R“   R   (   t   backendt   libt   msg(    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyt   select_known_backendE  s     c         @ sC   d } x0 |  D]" } ˆ  | ƒ } | d k	 r Pq q Wd } | | f S(   s_   
                Selects from presented backends and returns the first working
                R   N(   R€   (   t   backendsR—   R–   (   R™   (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyt   select_from_backends]  s    R   R‘   R“   t   Darwint   Linuxs3   Intel TBB is required, try:
$ conda/pip install tbbt   TBBs?   Intel OpenMP is required, try:
$ conda/pip install intel-openmpt   OSX_OMPc         @ s«   d } d } t  |  ƒ d k r' d } n  t  |  ƒ d k rN | ˆ  |  d } n  t  |  ƒ d k r— d j g  |  D] } ˆ  | ^ qm ƒ } | d | } n  t | | ƒ ‚ d  S(   Ns&   No threading layer could be loaded.
%ss   HINT:
%si    R   i   s   
OR
s
   One of:
%s(   R0   t   joinR   (   t   requiredt   errmsgt   hintmsgt   hintRR   t   options(   t   err_helpers(    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyt   raise_with_hintw  s    	&t
   threadsafet   forksafet   safes+   No threading layer available for purpose %st   defaults7   The threading layer requested '%s' is unknown to Numba.R   t   do_scheduling_signedt   do_scheduling_unsigned(   R¨   R©   Rª   (   t   _backend_init_process_lockt   _backend_init_thread_lockt   _is_initializedt   ctypesR   RŽ   t   strR   t   THREADING_LAYERt   lowerR€   t   platformt   systemRv   R`   R   t   llt
   add_symbolRN   R¬   R­   t   launch_threadst   NUM_THREADSR‹   Rw   (   R   RŽ   R›   R   t   namedbackendsR—   t   _IS_OSXt	   _IS_LINUXt   requirementsR§   t   libnamet	   availableR˜   R¹   (    (   R¦   R™   s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyRV   <  sx    		

		
t   NUMBA_DYLD_WORKAROUND(B   t   __doc__t
   __future__R    R   t   osRµ   t   syst   warningst	   threadingR   t   threadRLockt   multiprocessingt
   contextlibR   t   numpyR]   t   llvmlite.llvmpy.coret   llvmpyt   coreR   t   llvmlite.bindingt   bindingR·   t   numba.npyufuncR   t   numba.numpy_supportR   t   numbaR   R   R   t   numba.npyufunc.wrappersR	   R   Rº   RT   t   UFuncBuilderRU   R[   t   GUFuncBuilderRo   R{   R¯   R”   t   _windowsR‰   t   PY3t   get_start_methodt   get_contextR®   t   OSErrort   eR˜   t   warnR²   Rm   R°   R€   R‹   RŒ   RV   t   environt   _DYLD_WORKAROUND_SETR   t   gett   _DYLD_WORKAROUND_VAL(    (    (    s6   lib/python2.7/site-packages/numba/npyufunc/parallel.pyt   <module>   sV   	
		y	+				
	‹