ó
\K]c           @   s)  d  Z  d d l m Z d d l m Z d d l Z d d l m Z m	 Z	 m
 Z
 d Z d Z d „  Z d Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d „  Z d „  Z d „  Z d e d d „ Z e d „  ƒ Z e j d d d „ Z d „  Z d d „ Z d e d „ Z d „  Z d „  Z d „  Z  d S(   sz   
The Device Array API is not implemented in the simulator. This module provides
stubs to allow tests to import correctly.
iÿÿÿÿ(   t   contextmanager(   t   warnN(   t   sixt   typest   numpy_supportc         C   s   t  |  d t ƒ S(   Nt   __cuda_ndarray__(   t   getattrt   False(   t   obj(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   is_cuda_ndarray   s    s™   Array contains non-contiguous buffer and cannot be transferred as a single memory region. Please ensure contiguous buffer with numpy .ascontiguousarray()t	   FakeShapec           B   s   e  Z d  Z d „  Z RS(   sº   
    The FakeShape class is used to provide a shape which does not allow negative
    indexing, similar to the shape in CUDA Python. (Numpy shape arrays allow
    negative indexing)
    c         C   sC   t  | t j ƒ r- | d k  r- t d ƒ ‚ n  t t |  ƒ j | ƒ S(   Ni    s   tuple index out of range(   t
   isinstanceR   t   integer_typest
   IndexErrort   superR
   t   __getitem__(   t   selft   k(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR       s    (   t   __name__t
   __module__t   __doc__R   (    (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR
      s   t   FakeCUDAArrayc           B   sþ   e  Z d  Z e Z d d „ Z e d „  ƒ Z e d „  ƒ Z d „  Z	 d d „ Z
 e d „  ƒ Z d d „ Z d	 „  Z d
 „  Z d d d „ Z d d „ Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z RS(   sj   
    Implements the interface of a DeviceArray/DeviceRecord, but mostly just
    wraps a NumPy array.
    i    c         C   s4   | j  d k r | j d ƒ n | |  _ | |  _ d  S(   Ni    i   (   t   ndimt   reshapet   _aryt   stream(   R   t   aryR   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   __init__/   s    'c         C   s
   |  j  j S(   N(   R   t   nbytes(   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt
   alloc_size3   s    c         C   s
   |  j  j S(   N(   R   R   (   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR   7   s    c         C   sN   y t  |  j | ƒ } | SWn- t k
 rI } t j t d | ƒ | ƒ n Xd  S(   Ns#   Wrapped array has no attribute '%s'(   R   R   t   AttributeErrorR   t
   raise_from(   R   t   attrnamet   attrt   e(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   __getattr__<   s    c         C   s   t  |  j | ƒ S(   N(   R   R   (   R   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   bindD   s    c         C   s
   |  j  ƒ  S(   N(   t	   transpose(   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   TG   s    c         C   s   t  t j |  j d | ƒƒ S(   Nt   axes(   R   t   npR%   R   (   R   R'   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR%   K   s    c         C   s;   |  j  j | ƒ } t | t j ƒ r7 t | d |  j ƒS| S(   NR   (   R   R   R   R(   t   ndarrayR   R   (   R   t   idxt   item(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR   N   s    c         C   s   |  j  j | | ƒ S(   N(   R   t   __setitem__(   R   R*   t   val(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR,   T   s    c         C   sE   | d  k r! t j |  j ƒ } n t |  | ƒ t j | |  j ƒ | S(   N(   t   NoneR(   t
   empty_likeR   t   check_array_compatibilityt   copyto(   R   R   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   copy_to_hostW   s
    c         C   s¤   t  |  ƒ t |  ƒ t | ƒ } } t | t ƒ rL t  | ƒ t | | ƒ nA t j | d | j d rk d n d d t d t	 ƒ} t | | ƒ t j
 | j | ƒ d S(   s  
        Copy from the provided array into this array.

        This may be less forgiving than the CUDA Python implementation, which
        will copy data up to the length of the smallest of the two arrays,
        whereas this expects the size of the arrays to be equal.
        t   ordert   C_CONTIGUOUSt   Ct   Ft   subokt   copyN(   t   sentry_contiguoust
   array_coreR   R   R0   R(   t   arrayt   flagst   TrueR   R1   R   (   R   R   R   t	   self_coret   ary_core(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   copy_to_device_   s    

	c         C   s   t  d ƒ t ‚ d  S(   Ns+   to_host() is deprecated and will be removed(   R   t   NotImplementedError(   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   to_hostu   s    
c         C   s   t  |  j j ƒ S(   N(   R
   R   t   shape(   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyRC   y   s    c         O   s   t  |  j j | | Ž  ƒ S(   N(   R   R   t   ravel(   R   t   argst   kwargs(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyRD   }   s    c         O   s   t  |  j j | | Ž  ƒ S(   N(   R   R   R   (   R   RE   RF   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR   €   s    c         C   s   |  j  j j S(   N(   R   R<   t   c_contiguous(   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   is_c_contiguousƒ   s    c         C   s   |  j  j j S(   N(   R   R<   t   f_contiguous(   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   is_f_contiguous†   s    c         C   s   t  |  j ƒ S(   N(   t   strR   (   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   __str__‰   s    c         C   s   t  |  j ƒ S(   N(   t   reprR   (   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   __repr__Œ   s    c         C   s   t  |  j ƒ S(   N(   t   lenR   (   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   __len__   s    c         C   s>   g  t  j |  j t | t |  ƒ | ƒ ƒ D] } t | ƒ ^ q( S(   N(   R(   t   splitR   t   rangeRO   R   (   R   t   sectionR   t   a(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyRQ   ’   s    N(   R   R   R   R=   R   R   t   propertyR   R   R#   R$   R&   R.   R%   R   R,   R2   R@   RB   RC   RD   R   RH   RJ   RL   RN   RP   RQ   (    (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR   &   s.   											c         C   sZ   |  j  s |  Sg  } x6 |  j  D]+ } | j | d k r; d n	 t d ƒ ƒ q W|  t | ƒ S(   sG  
    Extract the repeated core of a broadcast array.

    Broadcast arrays are by definition non-contiguous due to repeated
    dimensions, i.e., dimensions with stride 0. In order to ascertain memory
    contiguity and copy the underlying data from such arrays, we must create
    a view without the repeated dimensions.

    i    N(   t   stridest   appendt   sliceR.   t   tuple(   R   t
   core_indext   stride(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR:   ™   s    
	)c         C   s;   t  |  ƒ } | j d r7 | j d r7 t t ƒ ‚ n  d  S(   NR4   t   F_CONTIGUOUS(   R:   R<   t
   ValueErrort   errmsg_contiguous_buffer(   R   t   core(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR9   «   s    c         C   s°   |  j  ƒ  | j  ƒ  } } |  j | j k rJ t d |  j | j f ƒ ‚ n  | j | j k r{ t d |  j | j f ƒ ‚ n  | j | j k r¬ t d |  j | j f ƒ ‚ n  d  S(   Ns   incompatible dtype: %s vs. %ss   incompatible shape: %s vs. %ss   incompatible strides: %s vs. %s(   t   squeezet   dtypet	   TypeErrorRC   R]   RV   (   t   ary1t   ary2t   ary1sqt   ary2sq(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR0   ±   s    i    c      
   C   sš   t  |  ƒ | d  k rƒ |  j j d k r1 t j n |  j } t t j d t j t	 |  ƒ ƒ j
 | ƒ d |  j d |  j d |  j ƒ ƒ S| j |  d | ƒd  S(   Nt   Mmt   bufferRa   RC   RV   R   (   R9   R.   Ra   t   charR(   t   int64R   R)   R8   R:   t   viewRC   RV   R@   (   R   R   R8   t   tot   buffer_dtype(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt	   to_device¾   s    
$			c         c   s	   d  Vd  S(   N(    (   t   arg(    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   pinnedÎ   s    R5   c      	   C   s"   t  j d |  d | d | d | ƒ S(   NRC   RV   Ra   R3   (   R(   R)   (   RC   Ra   RV   R3   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   pinned_arrayÓ   s    c          O   s=   d | k r | j  d ƒ n d } t t j |  | Ž  d | ƒS(   NR   i    (   t   popR   R(   R)   (   RE   RF   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   device_array×   s    !c         C   s   t  t j |  ƒ ƒ S(   N(   R   R(   R/   (   R   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   device_array_likeÜ   s    c         C   s_   t  |  t ƒ r |  t f St  |  t j ƒ sI t j |  d t d t ƒ}  n  t |  | | ƒ t f S(   NR8   R7   (   R   R   R   R(   t   voidR;   R=   Rn   (   R   R   R8   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   auto_deviceà   s    
c         C   s   t  |  d t ƒ S(   s$   Check if an object is a CUDA ndarrayR   (   R   R   (   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR	   ì   s    c            sW   t  ˆ  ƒ ‡  f d †  } | d t ƒ | d t ƒ | d t j ƒ | d t j ƒ d S(   s,   Verify the CUDA 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   hasattrR   R   R   (   R!   t   typ(   R   (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   requires_attrõ   s    RC   RV   Ra   t   sizeN(   t   require_cuda_ndarrayRY   R(   Ra   R   R   (   R   Ry   (    (   R   sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   verify_cuda_ndarray_interfaceñ   s    
c         C   s   t  |  ƒ s t d ƒ ‚ n  d S(   s9   Raises ValueError is is_cuda_ndarray(obj) evaluates Falses   require an cuda ndarray objectN(   R	   R]   (   R   (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyR{     s    (!   R   t
   contextlibR    t   warningsR   t   numpyR(   t   numbaR   R   R   R.   t   DeviceRecordt   from_record_likeR	   R^   RY   R
   t   objectR   R:   R9   R0   R=   Rn   Rp   t   floatRq   Rs   Rt   Rv   R|   R{   (    (    (    sG   lib/python2.7/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyt   <module>   s,   	s						