
    xKg95                     *   d Z ddlmZ ddlmZ ddlZdZdZdZ	 G d de
      Z G d d	e      Z G d
 de      Zd Zd Zd Zd ZddZed        Zd Zej,                  ddfdZej,                  ddfdZd Zd Zd Zd dZd Zd!dZd Zd Z d Z!y)"zz
The Device Array API is not implemented in the simulator. This module provides
stubs to allow tests to import correctly.
    )contextmanager)numpy_versionNzArray contains non-contiguous buffer and cannot be transferred as a single memory region. Please ensure contiguous buffer with numpy .ascontiguousarray()c                   "     e Zd ZdZ fdZ xZS )	FakeShapez
    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                 j    t        |t              r|dk  rt        d      t        t        |   |      S )Nr   ztuple index out of range)
isinstanceint
IndexErrorsuperr   __getitem__)selfk	__class__s     l/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyr   zFakeShape.__getitem__   s1    a!a%788Y1!44    )__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r   r      s    5 5r   r   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)FakeWithinKernelCUDAArrayaG  
    Created to emulate the behavior of arrays within kernels, where either
    array.item or array['item'] is valid (that is, give all structured
    arrays `numpy.recarray`-like semantics). This behaviour does not follow
    the semantics of Python and NumPy with non-jitted code, and will be
    deprecated and removed.
    c                 F    t        |t              sJ || j                  d<   y )N_item)r   FakeCUDAArray__dict__r   items     r   __init__z"FakeWithinKernelCUDAArray.__init__+   s     $...!%gr   c                 <    t        |t              rt        |      S |S N)r   r   r   r   s     r   __wrap_if_fakez(FakeWithinKernelCUDAArray.__wrap_if_fake/   s    dM*,T22Kr   c                     |t        | j                  j                        v r/| j                  t	        | j                  j                  |            S | j                  | j                  j                  |            S r!   )dirr   _ary(_FakeWithinKernelCUDAArray__wrap_if_fakegetattrr   )r   attrnames     r   __getattr__z%FakeWithinKernelCUDAArray.__getattr__5   sW    s4::??++&&wtzz'IJJ&&tzz'='=h'GHHr   c                 <    | j                   j                  ||       y r!   r   __setitem__)r   nmvals      r   __setattr__z%FakeWithinKernelCUDAArray.__setattr__;   s    

r3'r   c                 V    | j                  | j                  j                  |            S r!   )r&   r   r   )r   idxs     r   r   z%FakeWithinKernelCUDAArray.__getitem__>   s"    ""4::#9#9##>??r   c                 <    | j                   j                  ||       y r!   r+   r   r1   r.   s      r   r,   z%FakeWithinKernelCUDAArray.__setitem__A   s    

sC(r   c                 ,    t        | j                        S r!   )lenr   r   s    r   __len__z!FakeWithinKernelCUDAArray.__len__D   s    4::r   c                     t        ||      }d |j                  d      }|rt        fd|D              |d<   t        fd|D              } ||i |S )Nc                 R    t        | t              r| j                  j                  } | S r!   )r   r   r   r%   objs    r   convert_fakesz@FakeWithinKernelCUDAArray.__array_ufunc__.<locals>.convert_fakesQ   s    #89iinnJr   outc              3   .   K   | ]  } |        y wr!    ).0or<   s     r   	<genexpr>z<FakeWithinKernelCUDAArray.__array_ufunc__.<locals>.<genexpr>Y   s     !@Cq-"2C   c              3   .   K   | ]  } |        y wr!   r?   )r@   ar<   s     r   rB   z<FakeWithinKernelCUDAArray.__array_ufunc__.<locals>.<genexpr>Z   s     4t!]1%trC   )r'   gettuple)r   ufuncmethodargskwargscallr=   r<   s          @r   __array_ufunc__z)FakeWithinKernelCUDAArray.__array_ufunc__G   s]     uf%	 jj!!@C!@@F5M4t44T$V$$r   N)r   r   r   r   r   r&   r)   r/   r   r,   r7   rM   r?   r   r   r   r   "   s1    &I(@)%r   r   c                      e Zd ZdZd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Zd&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 d Z!d  Z"d! Z#d" Z$d# Z%d$ Z&d&d%Z'y	))r   zj
    Implements the interface of a DeviceArray/DeviceRecord, but mostly just
    wraps a NumPy array.
    Tc                      || _         || _        y r!   )r%   streamr   aryrP   s      r   r   zFakeCUDAArray.__init__f   s    	r   c                 .    | j                   j                  S r!   r%   nbytesr6   s    r   
alloc_sizezFakeCUDAArray.alloc_sizej   s    yyr   c                 .    | j                   j                  S r!   rT   r6   s    r   rU   zFakeCUDAArray.nbytesn   s     yyr   c                 x    	 t        | j                  |      }|S # t        $ r}d|z  }t        |      |d }~ww xY w)Nz#Wrapped array has no attribute '%s')r'   r%   AttributeError)r   r(   attremsgs        r   r)   zFakeCUDAArray.__getattr__s   sC    	-499h/DK 	-7(BC %1,	-s    	949c                 .    t        | j                  |      S r!   r   r%   )r   rP   s     r   bindzFakeCUDAArray.bind{   s    TYY//r   c                 "    | j                         S r!   )	transposer6   s    r   TzFakeCUDAArray.T~   s    ~~r   Nc                 V    t        t        j                  | j                  |            S )N)axes)r   npra   r%   )r   rd   s     r   ra   zFakeCUDAArray.transpose   s    R\\$))$?@@r   c                     | j                   j                  |      }t        |      t        j                  t        j
                  fvr|S t        || j                        S )NrP   )r%   r   typere   ndarrayvoidr   rP   )r   r1   rets      r   r   zFakeCUDAArray.__getitem__   sE    ii##C(9RZZ11J T[[99r   c                 :    | j                   j                  ||      S r!   )r%   r,   r3   s      r   r,   zFakeCUDAArray.__setitem__   s    yy$$S#..r   c                     | t        j                  | j                        }nt        | |       t        j                  || j                         |S r!   )re   
empty_liker%   check_array_compatibilitycopytorQ   s      r   copy_to_hostzFakeCUDAArray.copy_to_host   s:    ;--		*C%dC0
		#tyy!
r   c                 X   t        |        t        |       t        |      }}t        |t              rt        |       t	        ||       nAt        j                  ||j                  d   rdnddt        dk  rdnd      }t	        ||       t        j                  |j                  |       y)	a  
        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.
        C_CONTIGUOUSCFT   r   FN)ordersubokcopy)sentry_contiguous
array_corer   r   ro   re   arrayflagsr   rp   r%   )r   rR   rP   	self_coreary_cores        r   copy_to_devicezFakeCUDAArray.copy_to_device   s     	$(.
38	c=)c"%i:xx&__^<c#+f4U$	@H
 &i:
		)..(+r   c                 @    t        | j                  j                        S r!   )r   r%   shaper6   s    r   r   zFakeCUDAArray.shape   s    ))r   c                 L    t         | j                  j                  |i |      S r!   )r   r%   ravelr   rJ   rK   s      r   r   zFakeCUDAArray.ravel   s!    _TYY__d=f=>>r   c                 L    t         | j                  j                  |i |      S r!   )r   r%   reshaper   s      r   r   zFakeCUDAArray.reshape   s$    .TYY..??@@r   c                 L    t         | j                  j                  |i |      S r!   )r   r%   viewr   s      r   r   zFakeCUDAArray.view   s!    ^TYY^^T<V<==r   c                 B    | j                   j                  j                  S r!   )r%   r~   c_contiguousr6   s    r   is_c_contiguouszFakeCUDAArray.is_c_contiguous       yy+++r   c                 B    | j                   j                  j                  S r!   )r%   r~   f_contiguousr6   s    r   is_f_contiguouszFakeCUDAArray.is_f_contiguous   r   r   c                 ,    t        | j                        S r!   )strr%   r6   s    r   __str__zFakeCUDAArray.__str__       499~r   c                 ,    t        | j                        S r!   )reprr%   r6   s    r   __repr__zFakeCUDAArray.__repr__   s    DIIr   c                 ,    t        | j                        S r!   )r5   r%   r6   s    r   r7   zFakeCUDAArray.__len__   r   r   c                 2    t        | j                  |k(        S r!   r^   r   others     r   __eq__zFakeCUDAArray.__eq__       TYY%/00r   c                 2    t        | j                  |k7        S r!   r^   r   s     r   __ne__zFakeCUDAArray.__ne__   r   r   c                 2    t        | j                  |k        S r!   r^   r   s     r   __lt__zFakeCUDAArray.__lt__       TYY.//r   c                 2    t        | j                  |k        S r!   r^   r   s     r   __le__zFakeCUDAArray.__le__   r   r   c                 2    t        | j                  |kD        S r!   r^   r   s     r   __gt__zFakeCUDAArray.__gt__   r   r   c                 2    t        | j                  |k\        S r!   r^   r   s     r   __ge__zFakeCUDAArray.__ge__   r   r   c                 2    t        | j                  |z         S r!   r^   r   s     r   __add__zFakeCUDAArray.__add__   r   r   c                 2    t        | j                  |z
        S r!   r^   r   s     r   __sub__zFakeCUDAArray.__sub__   r   r   c                 2    t        | j                  |z        S r!   r^   r   s     r   __mul__zFakeCUDAArray.__mul__   r   r   c                 2    t        | j                  |z        S r!   r^   r   s     r   __floordiv__zFakeCUDAArray.__floordiv__   r   r   c                 2    t        | j                  |z        S r!   r^   r   s     r   __truediv__zFakeCUDAArray.__truediv__   r   r   c                 2    t        | j                  |z        S r!   r^   r   s     r   __mod__zFakeCUDAArray.__mod__   r   r   c                 2    t        | j                  |z        S r!   r^   r   s     r   __pow__zFakeCUDAArray.__pow__   r   r   c           	          t        j                  | j                  t        |t	        |       |            D cg c]  }t        |       c}S c c}w r!   )re   splitr%   ranger5   r   )r   sectionrP   rE   s       r   r   zFakeCUDAArray.split   sJ     XXdiiwD	7)KL
L !L
 	
 
s   Ar   r!   )Nr   )(r   r   r   r   __cuda_ndarray__r   propertyrV   rU   r)   r_   rb   ra   r   r,   rq   r   r   r   r   r   r   r   r   r   r7   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r?   r   r   r   r   ^   s    
         -0    A:/,, * *?A>,,
1101010001001
r   r   c                     | j                   r| j                  s| S g }| j                   D ]#  }|j                  |dk(  rdn
t        d             % | t	        |         S )aG  
    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.

    r   N)stridessizeappendslicerG   )rR   
core_indexstrides      r   r|   r|      sS     ;;chh
J++v{!d< uZ !!r   c                     | j                   j                  }t        t        | j                        t        | j
                              D ]  \  }}|dkD  s|dk7  s||k7  r y||z  } y)z
    Returns True iff `ary` is C-style contiguous while ignoring
    broadcasted and 1-sized dimensions.
    As opposed to array_core(), it does not call require_context(),
    which can be quite expensive.
       r   FT)dtypeitemsizezipreversedr   r   )rR   r   r   r   s       r   is_contiguousr     s`     99DXcii0(3;;2GHv191v~EMD	 I
 r   c                 v    t        |       }|j                  d   s|j                  d   st        t              y y )Nrs   F_CONTIGUOUS)r|   r~   
ValueErrorerrmsg_contiguous_buffer)rR   cores     r   r{   r{     s7    c?D::n%djj.H122 /I%r   c                    | j                         |j                         }}| j                  |j                  k7  r%t        d| j                  d|j                        |j                  |j                  k7  r%t	        d| j                  d|j                        |j
                  |j
                  k7  r%t	        d| j
                  d|j
                        y )Nzincompatible dtype: z vs. zincompatible shape: zincompatible strides: )squeezer   	TypeErrorr   r   r   )ary1ary2ary1sqary2sqs       r   ro   ro   !  s    \\^T\\^FFzzTZZTZZ1 2 	2||v||#**djj2 3 	3~~',,6 7 	7 (r   c           	         t        j                  | t        dk  rdnd d      } t        |        || j                  j
                  dv rt         j                  n| j                  }t        t        j                  t        j                  t        |             j                  |      | j                  | j                  | j                        j                  t        |                   S |j                  | |       y )	Nrv   FTrz   ry   Mm)bufferr   r   r   )rh   rg   )re   r}   r   r{   r   charint64r   ri   rz   r|   r   r   r   rh   r   )rR   rP   rz   tobuffer_dtypes        r   	to_devicer   .  s    
((3!.!7TC c	z#&99>>T#9rxxsyyJJwwz#/44\Biiii	
 dS	d"
 	
 	#f-r   c              #      K   d  y wr!   r?   )args    r   pinnedr   A  s	     	s   c                  R    dD ]  }||v s|j                  |        t        | i |S )N)portablewc)popdevice_array)rJ   rK   
unused_args      r   mapped_arrayr   F  s2    (
JJz" ) (((r   rt   c                 4    t        j                  | |||      S N)r   r   r   rx   re   ri   r   r   r   rx   s       r   pinned_arrayr   M      ::E7%uMMr   c                 4    t        j                  | |||      S r   r   r   s       r   managed_arrayr   Q  r   r   c                  p    d|v r|j                  d      nd}t        t        j                  | i ||      S )NrP   r   rg   )r   r   re   ri   )rJ   rK   rP   s      r   r   r   U  s4    %-%7VZZ!QFT4V4VDDr   c                    | j                   d   s| j                   d   s| j                  dk  ryt        | j                        D cg c]  }| }}|j	                  d        dgt        | j                        z  }| j                  j                  }|D ]  \  }}|||<   || j                  |   z  } t        |      S c c}w )z[
    Given an array, compute strides for a new contiguous array of the same
    shape.
    rs   r   r   Nc                     | d   S )Nr   r?   )xs    r   <lambda>z0_contiguous_strides_like_array.<locals>.<lambda>l  s    !A$r   )keyr   )
r~   ndim	enumerater   sortr5   r   r   r   rG   )rR   r   
stridepermr   r   i_perm_s          r   _contiguous_strides_like_arrayr   Z  s     yy CIIn$=Q (46414J6OOO' cC$$GYYF	 #))F##   > 7s   	B?c                 B    | j                   d   r| j                   d   syy)Nr   rs   ru   rt   )r~   )rR   s    r   _order_like_arrayr  w  s    
yy >)Br   c                 t    t        |       }t        |       }t        | j                  | j                  ||      S Nr   )r   r  r   r   r   )rR   rP   r   rx   s       r   device_array_liker  ~  4    ,S1Gc"Eciisyy'#% %r   c                 t    t        |       }t        |       }t        | j                  | j                  ||      S r  )r   r  r   r   r   )rR   r   rx   s      r   pinned_array_liker    r  r   c                     t        | t              r| dfS t        | t        j                        s#t        j                  | t
        dk  rdnd d      } t        | ||      dfS )NFrv   Tr   )r   r   re   rj   r}   r   r   )rR   rP   rz   s      r   auto_devicer
    sZ    #}%Ezc277#hh'&0d S&$'--r   c                     t        | dd      S )z$Check if an object is a CUDA ndarrayr   F)r'   r:   s    r   is_cuda_ndarrayr    s    3*E22r   c                      t                 fd} |dt                |dt                |dt        j                          |dt               y)z,Verify the CUDA ndarray interface for an objc                     t        |       st        |       t        t        |       |      st        | d|      y )Nz must be of type )hasattrrY   r   r'   )rZ   typr;   s     r   requires_attrz4verify_cuda_ndarray_interface.<locals>.requires_attr  s>    sD! &&'#t,c2 D#!FGG 3r   r   r   r   r   N)require_cuda_ndarrayrG   re   r   r	   )r;   r  s   ` r   verify_cuda_ndarray_interfacer    s?    H '5!)U#'288$&#r   c                 0    t        |       st        d      y)z9Raises ValueError is is_cuda_ndarray(obj) evaluates Falsezrequire an cuda ndarray objectN)r  r   r:   s    r   r  r    s    39::  r   )r   TNr   )r   T)"r   
contextlibr   numba.np.numpy_supportr   numpyre   DeviceRecordfrom_record_liker   rG   r   objectr   r   r|   r   r{   ro   r   r   r   float64r   r   r   r   r  r  r  r
  r  r  r  r?   r   r   <module>r     s    & 0   3 
5 
59% 9%xX
F X
v"$ 3
7.& 
 
) !jj$c N  "zz4s NE
:%%	.3
 ;r   