σ
\K]c           @` ss  d  Z  d d l m Z 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 m Z d d l m Z m Z m Z d d	 l m Z y e Wn e k
 rΫ e Z n Xd
   Z d   Z d   Z d e f d     YZ d e f d     YZ  d e j! f d     YZ" d d  Z$ d Z% d   Z& d   Z' d e( d  Z) d S(   sΫ   
A HSA dGPU backed ND Array is recognized by checking the __hsa_memory__
attribute on the object.  If it exists and evaluate to True, it must define
shape, strides, dtype and size attributes similar to a NumPy ndarray.
i    (   t   print_functiont   absolute_importt   divisionN(   t   c_void_p(   t   driveri   (   t   devices(   t
   dummyarrayt   typest   numpy_support(   t   HsaContextMismatchErrorc         C` s   t  |  d t  S(   s#   Check if an object is a HSA ndarrayt   __hsa_ndarray__(   t   getattrt   False(   t   obj(    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   is_hsa_ndarray   s    c         ` sZ   t       f d   } | d t  | d t  | d t j  | d t t f  d S(   s+   Verify the HSA ndarray interface for an objc         ` sS   t    |   s t |    n  t t   |   |  sO t d |  | f   n  d  S(   Ns   %s must be of type %s(   t   hasattrt   AttributeErrort
   isinstanceR   (   t   attrt   typ(   R   (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   requires_attr!   s    t   shapet   stridest   dtypet   sizeN(   t   require_hsa_ndarrayt   tuplet   npR   t   intt   long(   R   R   (    (   R   s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   verify_hsa_ndarray_interface   s    
c         C` s   t  |   s t d   n  d S(   s8   Raises ValueError if is_hsa_ndarray(obj) evaluates Falses   require an hsa ndarray objectN(   R   t
   ValueError(   R   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyR   -   s    t   DeviceNDArrayBasec           B` sz   e  Z d  Z e Z e Z d d  Z e d    Z	 e d    Z
 e d    Z d d d  Z d d d  Z d   Z RS(	   sG   Base class for an on dGPU NDArray representation cf. numpy.ndarray
    c         C` s―  t  | t t f  r! | f } n  t  | t t f  rB | f } n  t |  |  _ t |  |  j k ru t d   n  t j j d | | | j	  |  _
 t |  |  _ t |  |  _ t j |  |  _ t t j |  j   |  _ |  j d k r| d k rHd d l m } | |  j |  j |  j j	  |  _ t j   j |  j  } q’t | d d  } | d k rut d   n  | d k st  | |  _ n d } d |  _ | |  _ d S(   s   
        Args
        ----

        shape
            array shape.
        strides
            array strides.
        dtype
            data type as numpy.dtype.
        dgpu_data
            user provided device memory for the ndarray data buffer
        s   strides not match ndimi    (   t   _memory_size_from_infot   _hsa_memsize_s'   dgpu_data as no _hsa_memsize_ attributeN(   R   R   R   t   lent   ndimR   R   t   Arrayt	   from_desct   itemsizet   _dummyR   R   R   R   R   t   prodR   t   Nonet   numba.roc.apiR!   t
   alloc_sizeR   t   get_contextt   mempoolallocR   t   AssertionErrort	   dgpu_data(   t   selfR   R   R   R0   R!   t   sz(    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   __init__9   s6    		c         C` s
   |  j  j S(   N(   R0   t   context(   R1   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   _contexth   s    c         C` s(   t  j |  j  } t j | |  j d  S(   sn   
        Magic attribute expected by Numba to get the numba type that
        represents this object.
        t   A(   R   t
   from_dtypeR   R   R%   R$   (   R1   R   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   _numba_type_l   s    c         C` s'   |  j  d k r t d  S|  j  j Sd S(   s:   Returns the ctypes pointer to the GPU data buffer
        i    N(   R0   R*   R   t   device_ctypes_pointer(   R1   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyR9   u   s    
c         C` sa  | j  d k r d S| d k	 rn |  j d k	 rw |  j | } } | j | j k rk t d | d |   qk qw n	 |  j } |  j } | d k rΰ t j j	   t
 | t  rΔ t j |  j |  | |  q]t j |  j |  | |  n} t
 | t  r&t j d |  j d | j d |  d | d	 | d
 |  n7 t j d |  j d t j   d |  d | d	 | d
 |  d S(   sή   Copy `ary` to `self`.

        If `ary` is a HSA memory, perform a device-to-device transfer.
        Otherwise, perform a a host-to-device transfer.

        If `stream` is a stream object, an async copy to used.
        i    Nt   expectt   gott   dst_ctxt   src_ctxt   dstt   srcR   t   stream(   R   R*   R0   R5   t   unproxyR	   R,   t   _drivert   hsat   implicit_syncR   t   DeviceNDArrayt   dGPU_to_dGPUt   host_to_dGPUt   async_dGPU_to_dGPUt   async_host_to_dGPUR   t   get_cpu_context(   R1   t   aryR@   R4   R:   R;   R2   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   copy_to_device~   s.    				
c         C` s=  | d k r- t j d |  j d t j  } nμ | j |  j k rN t d   n  | j |  j k r¬ d d f } | j | k o |  j | k s¬ t d |  j | j f   q¬ n  | j |  j k rd |  j j	 f f } | j | k oξ |  j | k st d |  j | j f   qn  | } |  j d k s4t
 d   |  j } |  j d k rΑ|  j } | d k rt j j   t j | | |  |  qΑt j d	 t j   d
 |  j d | d |  d | d |  n  | d k r3|  j d k rt j d |  j d |  j d |  } q9t j d |  j d |  j d |  j d |  } n | } | S(   sή  Copy ``self`` to ``ary`` or create a new Numpy ndarray
        if ``ary`` is ``None``.

        The transfer is synchronous: the function returns after the copy
        is finished.

        Always returns the host array.

        Example::

            import numpy as np
            from numba import hsa

            arr = np.arange(1000)
            d_arr = hsa.to_device(arr)

            my_kernel[100, 100](d_arr)

            result_array = d_arr.copy_to_host()
        R   R   s   incompatible dtypei   s&   incompatible shape; device %s; host %ss(   incompatible strides; device %s; host %si    s   Negative memory sizeR<   R=   R>   R?   R   R@   t   bufferR   N(    (   i   (    (   R*   R   t   emptyR,   t   byteR   t	   TypeErrorR   R   R'   R/   R5   RB   RC   RD   t   dGPU_to_hostt   async_dGPU_to_hostR   RJ   R   t   ndarray(   R1   RK   R@   t   hostaryt
   scalshapest   scalstridesR4   R2   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   copy_to_host―   sF    !			c         C` s   |  j  S(   sE   Returns a device memory object that is used as the argument.
        (   R0   (   R1   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt
   as_hsa_argφ   s    N(   t   __name__t
   __module__t   __doc__t   Truet   __hsa_memory__R
   R*   R3   t   propertyR5   R8   R9   RL   RW   RX   (    (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyR    3   s   /		1GRE   c           B` s5   e  Z d  Z d   Z d   Z d   Z d d  Z RS(   s   
    An on-dGPU array type
    c         C` s
   |  j  j S(   sA   
        Return true if the array is Fortran-contiguous.
        (   R(   t   is_f_contig(   R1   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   is_f_contiguous   s    c         C` s
   |  j  j S(   s;   
        Return true if the array is C-contiguous.
        (   R(   t   is_c_contig(   R1   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   is_c_contiguous  s    c      	   O` sι   t  |  d k r8 t | d t t f  r8 | d } n  t |   } | |  j k r~ | d |  j d |  j d |  j d |  j  S|  j	 j
 | |   \ } } | |  j	 j g k rΩ | d | j d | j d |  j d |  j  St d   d S(	   sΆ   
        Reshape the array without changing its contents, similarly to
        :meth:`numpy.ndarray.reshape`. Example::

            d_arr = d_arr.reshape(20, 50, order='F')
        i   i    R   R   R   R0   s   operation requires copyingN(   R#   R   R   t   listt   typeR   R   R   R0   R(   t   reshapet   extentt   NotImplementedError(   R1   t   newshapet   kwst   clst   newarrt   extents(    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyRe     s    +t   Cc      	   C` sw   t  |   } |  j j d |  \ } } | |  j j g k rg | d | j d | j d |  j d |  j  St d   d S(   sr   
        Flatten the array without changing its contents, similar to
        :meth:`numpy.ndarray.ravel`.
        t   orderR   R   R   R0   s   operation requires copyingN(	   Rd   R(   t   ravelRf   R   R   R   R0   Rg   (   R1   Rn   Rj   Rk   Rl   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyRo   $  s    (   RY   RZ   R[   R`   Rb   Re   Ro   (    (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyRE   ό   s
   			t	   HostArrayc           B` s   e  Z e Z e d     Z RS(   c         C` s   |  j  j t  S(   N(   t   ctypest   data_asR   (   R1   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyR9   7  s    (   RY   RZ   R\   R]   R^   R9   (    (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyRp   4  s   c         C` s@   |  j  d k r! |  j d  }  n  t |  j |  j |  j d | S(   s/   Create a DeviceNDArray object that is like ary.i    i   R0   (   R$   Re   RE   R   R   R   (   RK   R0   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   from_array_like<  s    s   Array contains non-contiguous buffer and cannot be transferred as a single memory region. Please ensure contiguous buffer with numpy .ascontiguousarray()c         C` s7   t  j |  j  } |  j | |  j | } | |  j k S(   N(   R   t   argmaxR   R   t   nbytes(   RK   t   iR   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   _single_bufferK  s    c         C` s`   |  j  d r\ |  j  d r\ |  j d d k r= t |  d  St |   rM t St t   n  d  S(   Nt   C_CONTIGUOUSt   F_CONTIGUOUSi    (   t   flagsR   t   sentry_contiguousRw   R\   R   t   errmsg_contiguous_buffer(   RK   (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyR{   Q  s    c         C` s_   t  j |   r |  t f St |   t |   } | rQ | j |  d | d | n  | t f Sd S(   sͺ   
    Create a DeviceArray like obj and optionally copy data from
    host to device. If obj already represents device memory, it is returned and
    no copy is made.
    R@   R4   N(   RB   t   is_device_memoryR   R{   Rs   RL   R\   (   R   R4   R@   t   copyt   devobj(    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   auto_device^  s    

(*   R[   t
   __future__R    R   R   t   warningst   mathR~   t   weakrefRq   R   t   numpyR   t   numba.roc.hsadrvR   RB   t    R   t   numbaR   R   R   t   errorR	   R   t	   NameErrorR   R   R   R   t   objectR    RE   RS   Rp   R*   Rs   R|   Rw   R{   R\   R   (    (    (    s;   lib/python2.7/site-packages/numba/roc/hsadrv/devicearray.pyt   <module>   s4   
			Ι8			