
    J/Ph5                     0   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!dS )#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                     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     h/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/cuda/simulator/cudadrv/devicearray.pyr   zFakeShape.__getitem__   sJ    a 	9!a%%7888Y%%11!444    )__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r   r      sB         5 5 5 5 5 5 5 5 5r   r   c                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )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                 H    t          |t                    sJ || j        d<   d S )N_item)r   FakeCUDAArray__dict__r   items     r   __init__z"FakeWithinKernelCUDAArray.__init__+   s*    $.....!%gr   c                 N    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2Kr   c                 h   	 |t          | j        j                  v r-|                     t	          | j        j        |                    S |                     | j                            |                    S # t          $ r0}t          |t                    st          |          |Y d }~d S d }~ww xY wr!   )	dirr   _ary(_FakeWithinKernelCUDAArray__wrap_if_fakegetattrr   	Exceptionr   AttributeError)r   attrnamees      r   __getattr__z%FakeWithinKernelCUDAArray.__getattr__5   s    	63tz////**74:?H+M+MNNN**4:+A+A(+K+KLLL 	6 	6 	6a00 6$X..A56 6 6 6 6 6	6s   AA7 
,A7 7
B1%B,,B1c                 <    | j                             ||           d S r!   r   __setitem__)r   nmvals      r   __setattr__z%FakeWithinKernelCUDAArray.__setattr__?   s     
r3'''''r   c                 \    |                      | j                            |                    S r!   )r&   r   r   )r   idxs     r   r   z%FakeWithinKernelCUDAArray.__getitem__B   s&    ""4:#9#9##>#>???r   c                 <    | j                             ||           d S r!   r.   r   r4   r1   s      r   r/   z%FakeWithinKernelCUDAArray.__setitem__E   s     
sC(((((r   c                 *    t          | j                  S r!   )lenr   r   s    r   __len__z!FakeWithinKernelCUDAArray.__len__H   s    4:r   c                     t          ||          }d |                    d          }|rt          fd|D                       |d<   t          fd|D                       } ||i |S )Nc                 H    t          | t                    r| j        j        } | S r!   )r   r   r   r%   objs    r   convert_fakesz@FakeWithinKernelCUDAArray.__array_ufunc__.<locals>.convert_fakesU   s#    #899 %inJr   outc              3   .   K   | ]} |          V  d S r!    ).0or?   s     r   	<genexpr>z<FakeWithinKernelCUDAArray.__array_ufunc__.<locals>.<genexpr>]   s-      !@!@q--"2"2!@!@!@!@!@!@r   c              3   .   K   | ]} |          V  d S r!   rB   )rC   ar?   s     r   rE   z<FakeWithinKernelCUDAArray.__array_ufunc__.<locals>.<genexpr>^   s-      44!]]1%%444444r   )r'   gettuple)r   ufuncmethodargskwargscallr@   r?   s          @r   __array_ufunc__z)FakeWithinKernelCUDAArray.__array_ufunc__K   s     uf%%	 	 	 jj 	A!!@!@!@!@C!@!@!@@@F5M4444t44444tT$V$$$r   N)r   r   r   r   r   r&   r,   r2   r   r/   r:   rO   rB   r   r   r   r   "   s         & & &  6 6 6( ( (@ @ @) ) )  % % % % %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'd
S )*r   zj
    Implements the interface of a DeviceArray/DeviceRecord, but mostly just
    wraps a NumPy array.
    Tr   c                 "    || _         || _        d S r!   )r%   streamr   aryrR   s      r   r   zFakeCUDAArray.__init__j   s    	r   c                     | j         j        S r!   r%   nbytesr9   s    r   
alloc_sizezFakeCUDAArray.alloc_sizen   s    yr   c                     | j         j        S r!   rV   r9   s    r   rW   zFakeCUDAArray.nbytesr   s     yr   c                     	 t          | j        |          }|S # t          $ r}d|z  }t          |          |d }~ww xY w)Nz#Wrapped array has no attribute '%s')r'   r%   r)   )r   r*   attrr+   msgs        r   r,   zFakeCUDAArray.__getattr__w   sV    	-49h//DK 	- 	- 	-7(BC %%1,	-s    
=8=c                 ,    t          | j        |          S r!   r   r%   )r   rR   s     r   bindzFakeCUDAArray.bind   s    TY///r   c                 *    |                                  S r!   )	transposer9   s    r   TzFakeCUDAArray.T   s    ~~r   Nc                 R    t          t          j        | j        |                    S )N)axes)r   npra   r%   )r   rd   s     r   ra   zFakeCUDAArray.transpose   s!    R\$)$???@@@r   c                     | j                             |          }t          |          t          j        t          j        fvr|S t          || j                  S )NrR   )r%   r   typere   ndarrayvoidr   rR   )r   r4   rets      r   r   zFakeCUDAArray.__getitem__   sK    i##C((99RZ111J T[9999r   c                 8    | j                             ||          S r!   )r%   r/   r6   s      r   r/   zFakeCUDAArray.__setitem__   s    y$$S#...r   c                     |t          j        | j                  }nt          | |           t          j        || j                   |S r!   )re   
empty_liker%   check_array_compatibilitycopytorS   s      r   copy_to_hostzFakeCUDAArray.copy_to_host   sC    ;-	**CC%dC000
	#ty!!!
r   c                    t          |            t          |           t          |          }}t          |t                    r t          |           t	          ||           nDt          j        ||j        d         rdnddt          dk     rdnd          }t	          ||           t          j	        |j
        |           dS )	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   rT   rR   	self_coreary_cores        r   copy_to_devicezFakeCUDAArray.copy_to_device   s     	$(..
38	c=)) 		;c"""%i::::x&_^<Ecc#+f44UU$	@ @ @H
 &i:::
	).(+++++r   c                 4    t          | j        j                  S r!   )r   r%   shaper9   s    r   r   zFakeCUDAArray.shape   s    )))r   c                 @    t           | j        j        |i |          S r!   )r   r%   ravelr   rL   rM   s      r   r   zFakeCUDAArray.ravel   s#    _TY_d=f==>>>r   c                 @    t           | j        j        |i |          S r!   )r   r%   reshaper   s      r   r   zFakeCUDAArray.reshape   s%    .TY.???@@@r   c                 @    t           | j        j        |i |          S r!   )r   r%   viewr   s      r   r   zFakeCUDAArray.view   s#    ^TY^T<V<<===r   c                 $    | j         j        j        S r!   )r%   r~   c_contiguousr9   s    r   is_c_contiguouszFakeCUDAArray.is_c_contiguous       y++r   c                 $    | j         j        j        S r!   )r%   r~   f_contiguousr9   s    r   is_f_contiguouszFakeCUDAArray.is_f_contiguous   r   r   c                 *    t          | j                  S r!   )strr%   r9   s    r   __str__zFakeCUDAArray.__str__       49~~r   c                 *    t          | j                  S r!   )reprr%   r9   s    r   __repr__zFakeCUDAArray.__repr__   s    DIr   c                 *    t          | j                  S r!   )r8   r%   r9   s    r   r:   zFakeCUDAArray.__len__   r   r   c                 2    t          | j        |k              S r!   r^   r   others     r   __eq__zFakeCUDAArray.__eq__       TY%/000r   c                 2    t          | j        |k              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__       TY.///r   c                 2    t          | j        |k              S r!   r^   r   s     r   __le__zFakeCUDAArray.__le__   r   r   c                 2    t          | j        |k              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                 0    t          | j        |z             S r!   r^   r   s     r   __add__zFakeCUDAArray.__add__       TY.///r   c                 0    t          | j        |z
            S r!   r^   r   s     r   __sub__zFakeCUDAArray.__sub__   r   r   c                 0    t          | j        |z            S r!   r^   r   s     r   __mul__zFakeCUDAArray.__mul__   r   r   c                 0    t          | j        |z            S r!   r^   r   s     r   __floordiv__zFakeCUDAArray.__floordiv__       TY%/000r   c                 0    t          | j        |z            S r!   r^   r   s     r   __truediv__zFakeCUDAArray.__truediv__   r   r   c                 0    t          | j        |z            S r!   r^   r   s     r   __mod__zFakeCUDAArray.__mod__   r   r   c                 0    t          | j        |z            S r!   r^   r   s     r   __pow__zFakeCUDAArray.__pow__   r   r   c           
          d t          j        | j        t          |t	          |           |                    D             S )Nc                 ,    g | ]}t          |          S rB   )r   )rC   rG   s     r   
<listcomp>z'FakeCUDAArray.split.<locals>.<listcomp>   s0     
 
 
 !
 
 
r   )re   splitr%   ranger8   )r   sectionrR   s      r   r   zFakeCUDAArray.split   sC    
 
XdiwD		7)K)KLL
 
 
 	
r   r   r!   )Nr   )(r   r   r   r   __cuda_ndarray__r   propertyrX   rW   r,   r_   rb   ra   r   r/   rq   r   r   r   r   r   r   r   r   r   r:   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rB   r   r   r   r   b   sy        
         X      X - - -0 0 0 0     X A A A A: : :/ / /   , , , ,, * * X*? ? ?A A A> > >, , ,, , ,      
1 1 11 1 10 0 01 1 10 0 01 1 10 0 00 0 00 0 01 1 10 0 00 0 01 1 1
 
 
 
 
 
r   r   c                     | j         r| j        s| S g }| j         D ],}|                    |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slicerI   )rT   
core_indexstrides      r   r|   r|      sn     ; ch 
J+ = =v{{!!d<<<<uZ  !!r   c                     | j         j        }t          t          | j                  t          | j                            D ]\  }}|dk    r|dk    r||k    r dS ||z  } dS )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   )rT   r   r   r   s       r   is_contiguousr     so     9DXci00(3;2G2GHH  v1991v~~uuEMD4r   c                     t          |           }|j        d         s!|j        d         st          t                    d S d S )Nrs   F_CONTIGUOUS)r|   r~   
ValueErrorerrmsg_contiguous_buffer)rT   cores     r   r{   r{     sM    c??D:n% 3dj.H 312223 3 3 3r   c                 p   |                                  |                                 }}| j        |j        k    rt          d| j        d|j                  |j        |j        k    rt	          d| j        d|j                  |j        |j        k    rt	          d| j        d|j                  d S )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zTZiTZZ1 2 2 	2|v|##j***djj2 3 3 	3~''j,,,6 7 7 	7 ('r   Tc           	         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                                      t          |                               S |                    | |           d S )	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   )rT   rR   rz   tobuffer_dtypes        r   	to_devicer   2  s    
(3!.!7!7T  C c	z#&9>T#9#9rxxsyJwz#//44\BBii	  
 dS		d""
 
 	
 	#f-----r   c              #      K   d V  d S r!   rB   )args    r   pinnedr   E  s      	EEEEEr   c                  V    dD ]}||v r|                     |           t          | i |S )N)portablewc)popdevice_array)rL   rM   
unused_args      r   mapped_arrayr   J  sB    ( # #
JJz"""((((r   rt   c                 2    t          j        | |||          S N)r   r   r   rx   re   ri   r   r   r   rx   s       r   pinned_arrayr   Q      :E7%uMMMMr   c                 2    t          j        | |||          S r   r   r   s       r   managed_arrayr   U  r   r   c                  x    d|v r|                     d          nd}t          t          j        | i ||          S )NrR   r   rg   )r   r   re   ri   )rL   rM   rR   s      r   r   r   Y  sD    %-%7%7VZZ!!!QFT4V44VDDDDr   c                 Z   | j         d         s| j         d         s| j        dk    rdS d t          | j                  D             }|                    d            dgt          | j                  z  }| j        j        }|D ]\  }}|||<   || j        |         z  }t          |          S )	z[
    Given an array, compute strides for a new contiguous array of the same
    shape.
    rs   r   r   Nc                     g | ]}|S rB   rB   )rC   xs     r   r   z2_contiguous_strides_like_array.<locals>.<listcomp>o  s    6661666r   c                     | d         S )Nr   rB   )r   s    r   <lambda>z0_contiguous_strides_like_array.<locals>.<lambda>p  s
    !A$ r   )keyr   )
r~   ndim	enumerater   sortr8   r   r   r   rI   )rT   
stridepermr   r   i_perm_s         r   _contiguous_strides_like_arrayr  ^  s     y  CIn$= Qt 76i44666JOOO''' cC$$$GYF $ $	 #)F##>>r   c                 >    | j         d         r| j         d         sdS dS )Nr   rs   ru   rt   )r~   )rT   s    r   _order_like_arrayr  {  s)    
y  >)B ssr   c                 x    t          |           }t          |           }t          | j        | j        ||          S Nr   )r  r  r   r   r   )rT   rR   r   rx   s       r   device_array_liker
    @    ,S11Gc""Ecisy'#% % % %r   c                 x    t          |           }t          |           }t          | j        | j        ||          S r	  )r  r  r   r   r   )rT   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   )rT   rR   rz   s      r   auto_devicer    sx    #}%% Ezc27## h'&0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22r   c                      t                       fd} |dt                      |dt                      |dt          j                    |dt                     dS )z,Verify the CUDA ndarray interface for an objc                     t          |           st          |           t          t          |           |          st          | d|          d S )Nz must be of type )hasattrr)   r   r'   )r[   typr>   s     r   requires_attrz4verify_cuda_ndarray_interface.<locals>.requires_attr  se    sD!! 	' &&&'#t,,c22 	H DDD##!FGGG	H 	Hr   r   r   r   r   N)require_cuda_ndarrayrI   re   r   r	   )r>   r  s   ` r   verify_cuda_ndarray_interfacer    s    H H H H H M'5!!!M)U###M'28$$$M&#r   c                 B    t          |           st          d          dS )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   rI   r   objectr   r   r|   r   r{   ro   r   r   r   float64r   r   r   r  r  r
  r  r  r  r  r  rB   r   r   <module>r!     s<    & % % % % % 0 0 0 0 0 0      3 
5 
5 
5 
5 
5 
5 
5 
5=% =% =% =% =% =% =% =%@X
 X
 X
 X
 X
F X
 X
 X
v" " "$   3 3 3
7 
7 
7. . . .& 
 
 
) ) ) !j$c N N N N  "z4s N N N NE E E
  :  % % % %% % %	. 	. 	. 	.3 3 3
   ; ; ; ; ;r   