ó
\K]c           @@ s¾  d  d l  m Z m Z d  d l Z d  d l m Z d  d l m Z d d l	 m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z 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! y e" Wn e# k
 re$ Z" n Xd e% f d „  ƒ  YZ& d „  Z' d „  Z( e j) d d d „ Z+ d „  Z, d d e- d d „ Z. d „  Z/ d „  Z0 d „  Z1 d „  Z2 d „  Z3 e j) d d d „ Z4 e j) d d d „ Z5 d S(   i    (   t   absolute_importt   print_functionN(   t   mviewbuf(   t   get_contexti   (   t   get_global_idt   get_global_sizet   get_local_idt   get_local_sizet   get_group_idt   get_work_dimt   get_num_groupst   barriert	   mem_fencet   sharedt   wavebarriert   activelanepermute_wavewidtht
   ds_permutet   ds_bpermute(   t   jit(   t   CLK_LOCAL_MEM_FENCEt   CLK_GLOBAL_MEM_FENCE(   t   hsa(   t   devicearrayt   _AutoDeregisterc           B@ s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C@ s   | |  _  d  S(   N(   t   args(   t   selfR   (    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt   __init__*   s    c         C@ s   d  S(   N(    (   R   (    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt	   __enter__-   s    c         C@ s   t  |  j Œ  d  S(   N(   t
   deregisterR   (   R   t   exc_typet   exc_valt   exc_tb(    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt   __exit__0   s    (   t   __name__t
   __module__R   R   R    (    (    (    s,   lib/python2.7/site-packages/numba/roc/api.pyR   )   s   		c          G@ s[   xN |  D]F } t  | t j ƒ r; t j | j j | j ƒ q t t	 | ƒ ƒ ‚ q Wt
 |  ƒ S(   sÐ   Register data into the HSA system

    Returns a contextmanager for use in with-context for auto deregistration.

    Use in context:

        with hsa.register(array):
            do_work_on_HSA(array)

    (   t
   isinstancet   npt   ndarrayt   _hsadrvt   hsa_memory_registert   ctypest   datat   nbytest	   TypeErrort   typeR   (   R   R)   (    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt   register4   s
    c          G@ sU   xN |  D]F } t  | t j ƒ r; t j | j j | j ƒ q t t	 | ƒ ƒ ‚ q Wd S(   s(   Deregister data from the HSA system
    N(
   R#   R$   R%   R&   t   hsa_memory_deregisterR(   R)   R*   R+   R,   (   R   R)   (    (    s,   lib/python2.7/site-packages/numba/roc/api.pyR   G   s    t   Cc         C@ s:   t  |  | | | ƒ \ }  } } t j d |  d | d | ƒ S(   sˆ   device_array(shape, dtype=np.float, strides=None, order='C')

    Allocate an empty device ndarray. Similar to :meth:`numpy.empty`.
    t   shapet   stridest   dtype(   t   _prepare_shape_strides_dtypeR   t   DeviceNDArray(   R0   R2   R1   t   order(    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt   device_arrayP   s    c         C@ s"   t  d |  j d |  j d |  j ƒ S(   s<   Call roc.devicearray() with information from the array.
    R0   R2   R1   (   R6   R0   R2   R1   (   t   ary(    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt   device_array_likeZ   s    c         C@ sS   | p t  ƒ  } | d k r- t j |  ƒ } n  | rO | j |  d | d | ƒn  | S(   sõ  to_device(obj, context, copy=True, to=None)

    Allocate and transfer a numpy ndarray or structured scalar to the device.

    To copy host->device a numpy array::

        ary = numpy.arange(10)
        d_ary = roc.to_device(ary)

    The resulting ``d_ary`` is a ``DeviceNDArray``.

    To copy device->host::

        hary = d_ary.copy_to_host()

    To copy device->host to an existing array::

        ary = numpy.empty(shape=d_ary.shape, dtype=d_ary.dtype)
        d_ary.copy_to_host(ary)

    t   streamt   contextN(   R   t   NoneR   t   from_array_liket   copy_to_device(   t   objR9   R:   t   copyt   to(    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt	   to_device`   s    c           C@ s
   t  j ƒ  S(   N(   R&   t   create_stream(    (    (    s,   lib/python2.7/site-packages/numba/roc/api.pyR9   €   s    c         C@ sÞ   t  |  ƒ } d g | } | d k rt | j | d <xŸ t t | d ƒ ƒ D]$ } | | d |  | d | | <qI Wn` | d k rÈ | j | d <xD t d | ƒ D]$ } | | d |  | d | | <q Wn t d ƒ ‚ t | ƒ S(   Ni    R/   iÿÿÿÿi   t   Fs   must be either C/F order(   t   lent   itemsizet   reversedt   ranget
   ValueErrort   tuple(   R0   R2   R5   t   ndR1   t   d(    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt   _fill_stride_by_orderƒ   s    %%c         C@ s‹   t  j | ƒ } t |  t t f ƒ r0 |  f }  n  t | t t f ƒ rQ | f } n- |  d k rf d }  n  | p{ t |  | | ƒ } |  | | f S(   Ni   (    (   i   (   R$   R2   R#   t   intt   longRL   (   R0   R1   R2   R5   (    (    s,   lib/python2.7/site-packages/numba/roc/api.pyR3   “   s    	c         C@ sV   t  |  ƒ t  | ƒ k s$ t d ƒ ‚ t  |  ƒ } t j |  | | | ƒ \ } } | | S(   s_   Get the byte size of a contiguous memory buffer given the shape, strides
    and itemsize.
    s   # dim mismatch(   RD   t   AssertionErrorR   t   memoryview_get_extents_info(   R0   R1   RE   t   ndimt   st   e(    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt   _memory_size_from_infoŸ   s    $c         C@ sÃ   d d l  m } t | | | | ƒ \ } } } t | | | j ƒ } g  | j ƒ  D] } | j ^ qP } | j ƒ  j | d |  d | ƒ}	 t	 j
 d | d | d | d | d	 |	 ƒ }
 |
 j d
 t j ƒ S(   Ni   (   t   devicest	   finegraint   allow_access_toR0   R1   R2   R5   t   bufferR,   (   t   hsadrvRU   R3   RT   RE   t   get_all_contextst   _agentt   get_cpu_contextt   memhostallocR$   R%   t   viewR   t	   HostArray(   RV   R0   R2   R1   R5   RU   t   bytesizet   ct   agentst   buft   arr(    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt   _host_array¨   s    "	!	c         C@ s%   t  d t d |  d | d | d | ƒ S(   sa   coarsegrain_array(shape, dtype=np.float, strides=None, order='C')
    Similar to np.empty().
    RV   R0   R2   R1   R5   (   Re   t   False(   R0   R2   R1   R5   (    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt   coarsegrain_array¶   s    c         C@ s%   t  d t d |  d | d | d | ƒ S(   s`   finegrain_array(shape, dtype=np.float, strides=None, order='C')

    Similar to np.empty().
    RV   R0   R2   R1   R5   (   Re   Rf   (   R0   R2   R1   R5   (    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt   finegrain_array¾   s    (6   t
   __future__R    R   t   numpyR$   t   numbaR   t   numba.roc.hsadrv.devicesR   t   stubsR   R   R   R   R   R	   R
   R   R   R   R   R   R   R   t
   decoratorsR   t   enumsR   R   t   hsadrv.driverR   R&   RY   R   RN   t	   NameErrorRM   t   objectR   R-   R   t   floatR;   R6   R8   t   TrueRA   R9   RL   R3   RT   Re   Rg   Rh   (    (    (    s,   lib/python2.7/site-packages/numba/roc/api.pyt   <module>   s2   ^
			
	 						