
\K]c           @   s  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l m	 Z	 d e
 f d     YZ d e
 f d     YZ d	 e
 f d
     YZ d e
 f d     YZ e j   Z e j   Z e j   Z e j   Z d e
 f d     YZ d e
 f d     YZ e d    Z d S(   sf   
Implements the cuda module as called from within an executing kernel
(@cuda.jit-decorated function).
i(   t   contextmanagerN(   t   numpy_supportt   Dim3c           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s;   
    Used to implement thread/block indices/dimensions
    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   xt   yt   z(   t   selfR   R   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   __init__   s    		c         C   s   d |  j  |  j |  j f S(   Ns   (%s, %s, %s)(   R   R   R   (   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   __str__   s    c         C   s   d |  j  |  j |  j f S(   Ns   Dim3(%s, %s, %s)(   R   R   R   (   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   __repr__   s    c         c   s   |  j  V|  j V|  j Vd  S(   N(   R   R   R   (   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   __iter__   s    (   t   __name__t
   __module__t   __doc__R   R   R	   R
   (    (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyR      s
   			t   FakeCUDALocalc           B   s   e  Z d  Z d   Z RS(   s   
    CUDA Local arrays
    c         C   s   t  j |  } t j | |  S(   N(   R   t   as_dtypet   npt   empty(   R   t   shapet   dtype(    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   array)   s    (   R   R   R   R   (    (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyR   %   s   t   FakeCUDAConstc           B   s   e  Z d  Z d   Z RS(   s   
    CUDA Const arrays
    c         C   s   | S(   N(    (   R   t   ary(    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt
   array_like2   s    (   R   R   R   R   (    (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyR   .   s   t   FakeCUDASharedc           B   s    e  Z d  Z d   Z d   Z RS(   s  
    CUDA Shared arrays.

    Limitations: assumes that only one call to cuda.shared.array is on a line,
    and that that line is only executed once per thread. i.e.::

        a = cuda.shared.array(...); b = cuda.shared.array(...)

    will erroneously alias a and b, and::

        for i in range(10):
            sharedarrs[i] = cuda.shared.array(...)

    will alias all arrays created at that point (though it is not certain that
    this would be supported by Numba anyway).
    c         C   s1   i  |  _  | |  _ t j | d t j |  _ d  S(   NR   (   t   _allocationst   _dynshared_sizeR   t   zerost   bytet
   _dynshared(   R   t   dynshared_size(    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyR   H   s    		c         C   s   t  j |  } | d k rJ |  j | j } t j |  j j d | d | St j	 t
 j    } | d d d !} |  j j |  } | d  k r t j | |  } | |  j | <n  | S(   Ni    R   t   countii   (   R   R   R   t   itemsizeR   t
   frombufferR   t   datat	   tracebackt   extract_stackt   syst	   _getframeR   t   gett   NoneR   (   R   R   R   R   t   stackt   callert   res(    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyR   M   s    (   R   R   R   R   R   (    (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyR   6   s   	t   FakeCUDAAtomicc           B   s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C   s!   t   | | c | 7<Wd  QXd  S(   N(   t   addlock(   R   R   t   indext   val(    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   addj   s    c         C   s[   t  O t j | |  r' | | | <n t j |  r: d  St | | |  | | <Wd  QXd  S(   N(   t   maxlockR   t   isnant   max(   R   R   R.   R/   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyR3   n   s    c         C   s[   t  O t j | |  r' | | | <n t j |  r: d  St | | |  | | <Wd  QXd  S(   N(   t   minlockR   R2   t   min(   R   R   R.   R/   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyR5   y   s    c         C   sE   t  9 d | j } | | } | | k r7 | | | <n  | SWd  QXd  S(   Ni    (   i    (   t   caslockt   ndim(   R   R   t   oldR/   R.   t   loaded(    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   compare_and_swap   s    
(   R   R   R0   R3   R5   R:   (    (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyR,   i   s   			t   FakeCUDAModulec           B   s  e  Z d  Z d   Z e d    Z e d    Z e d    Z e d    Z e d    Z	 e d    Z
 e d    Z e d	    Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s7  
    An instance of this class will be injected into the __globals__ for an
    executing function in order to implement calls to cuda.*. This will fail to
    work correctly if the user code does::

        from numba import cuda as something_else

    In other words, the CUDA module must be called cuda.
    c         C   sU   t  |   |  _ t  |   |  _ t   |  _ t |  |  _ t   |  _ t	   |  _
 d  S(   N(   R   t   gridDimt   blockDimR   t   _localR   t   _sharedR   t   _constR,   t   _atomic(   R   t   grid_dimt	   block_dimR   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyR      s    c         C   s   |  j  S(   N(   R>   (   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   local   s    c         C   s   |  j  S(   N(   R?   (   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   shared   s    c         C   s   |  j  S(   N(   R@   (   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   const   s    c         C   s   |  j  S(   N(   RA   (   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   atomic   s    c         C   s   t  j   j S(   N(   t	   threadingt   current_threadt	   threadIdx(   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyRJ      s    c         C   s   t  j   j S(   N(   RH   RI   t   blockIdx(   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyRK      s    c         C   s   d S(   Ni    (    (   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   warpsize   s    c         C   s   t  j   j d S(   Ni    (   RH   RI   t	   thread_id(   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   laneid   s    c         C   s   t  j   j   d  S(   N(   RH   RI   t   syncthreads(   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyRO      s    c         C   s   d  S(   N(    (   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   threadfence   s    c         C   s   d  S(   N(    (   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   threadfence_block   s    c         C   s   d  S(   N(    (   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   threadfence_system   s    c         C   s   t  j   j |  S(   N(   RH   RI   t   syncthreads_count(   R   R/   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyRS      s    c         C   s   t  j   j |  S(   N(   RH   RI   t   syncthreads_and(   R   R/   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyRT      s    c         C   s   t  j   j |  S(   N(   RH   RI   t   syncthreads_or(   R   R/   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyRU      s    c         C   s   t  |  j d  S(   Nt   1(   t   binR   (   R   R/   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   popc   s    c         C   s   | | | S(   N(    (   R   t   at   bt   c(    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   fma   s    c         C   s#   t  d j |  d  d  d  d  S(   Ns   {:032b}ii   (   t   intt   format(   R   R/   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   brev   s    c         C   s,   d j  |  } t |  t | j d   S(   Ns   {:032b}t   0(   R^   t   lent   lstrip(   R   R/   t   s(    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   clz   s    c         C   s,   d j  |  } t |  t | j d   S(   Ns   {:032b}R`   (   R^   Ra   t   rstrip(   R   R/   Rc   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   ffs   s    c         C   s   | r
 | S| S(   N(    (   R   RY   RZ   R[   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   selp   s    c         C   s   |  j  } |  j } |  j } | j | j | j } | d k rB | S| j | j | j } | d k ro | | f S| j | j | j } | d k r | | | f St d |   d  S(   Ni   i   i   s*   Global ID has 1-3 dimensions. %d requested(   R=   RK   RJ   R   R   R   t   RuntimeError(   R   t   nt   bdimt   bidt   tidR   R   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   grid   s    			
c         C   s   |  j  } |  j } | j | j } | d k r2 | S| j | j } | d k rX | | f S| j | j } | d k r | | | f St d |   d  S(   Ni   i   i   s,   Global grid has 1-3 dimensions. %d requested(   R=   R<   R   R   R   Rh   (   R   Ri   Rj   t   gdimR   R   R   (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   gridsize   s    		
(   R   R   R   R   t   propertyRD   RE   RF   RG   RJ   RK   RL   RN   RO   RP   RQ   RR   RS   RT   RU   RX   R\   R_   Rd   Rf   Rg   Rm   Ro   (    (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyR;      s2   																c         #   s   d d l  m   |  j } t   f d   | j   D  } t  f d   | j   D  } | j |  z	 d  VWd  | j |  Xd  S(   Ni(   t   cudac         3   s-   |  ]# \ } } |   k r | | f Vq d  S(   N(    (   t   .0t   kt   v(   Rq   (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pys	   <genexpr>  s    c         3   s!   |  ] \ } } |   f Vq d  S(   N(    (   Rr   Rs   Rt   (   t   fake_cuda_module(    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pys	   <genexpr>  s    (   t   numbaRq   t   __globals__t   dictt   itemst   update(   t   fnRu   t   fn_globst   origt   repl(    (   Rq   Ru   s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   swapped_cuda_module  s    	""	(   R   t
   contextlibR    R%   RH   R#   t   numpyR   Rv   R   t   objectR   R   R   R   t   LockR-   R1   R4   R6   R,   R;   R   (    (    (    s=   lib/python2.7/site-packages/numba/cuda/simulator/kernelapi.pyt   <module>   s"   	-$