
    ܙdt4                     $   d 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.
    )contextmanagerNzArray 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     Hlib/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                     |t          | j        j                  v r-|                     t	          | j        j        |                    S |                     | j                            |                    S r    )dirr   _ary(_FakeWithinKernelCUDAArray__wrap_if_fakegetattrr   )r   attrnames     r   __getattr__z%FakeWithinKernelCUDAArray.__getattr__4   s_    s4:?++++&&wtz'I'IJJJ&&tz'='=h'G'GHHHr   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__=   s&    ""4:#9#9##>#>???r   c                 <    | j                             ||           d S r    r*   r   r0   r-   s      r   r+   z%FakeWithinKernelCUDAArray.__setitem__@   s     
sC(((((r   c                 *    t          | j                  S r    )lenr   r   s    r   __len__z!FakeWithinKernelCUDAArray.__len__C   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_fakesP   s#    #899 %inJr   outc              3   .   K   | ]} |          V  d S r     ).0or;   s     r   	<genexpr>z<FakeWithinKernelCUDAArray.__array_ufunc__.<locals>.<genexpr>X   s-      !@!@q--"2"2!@!@!@!@!@!@r   c              3   .   K   | ]} |          V  d S r    r>   )r?   ar;   s     r   rA   z<FakeWithinKernelCUDAArray.__array_ufunc__.<locals>.<genexpr>Y   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__F   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(   r.   r   r+   r6   rK   r>   r   r   r   r   !   s         & & &  I I 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'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   aryrN   s      r   r   zFakeCUDAArray.__init__e   s    	r   c                     | j         j        S r    r$   nbytesr5   s    r   
alloc_sizezFakeCUDAArray.alloc_sizei   s    yr   c                     | j         j        S r    rR   r5   s    r   rS   zFakeCUDAArray.nbytesm   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$   AttributeError)r   r'   attremsgs        r   r(   zFakeCUDAArray.__getattr__r   sV    	-49h//DK 	- 	- 	-7(BC %%1,	-s    
=8=c                 ,    t          | j        |          S r    r   r$   )r   rN   s     r   bindzFakeCUDAArray.bindz   s    TY///r   c                 *    |                                  S r    )	transposer5   s    r   TzFakeCUDAArray.T}   s    ~~r   Nc                 R    t          t          j        | j        |                    S )N)axes)r   npr_   r$   )r   rb   s     r   r_   zFakeCUDAArray.transpose   s!    R\$)$???@@@r   c                     | j                             |          }t          |          t          j        t          j        fvr|S t          || j                  S )NrN   )r$   r   typerc   ndarrayvoidr   rN   )r   r0   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+   r2   s      r   r+   zFakeCUDAArray.__setitem__   s    y$$S#...r   c                     |t          j        | j                  }nt          | |           t          j        || j                   |S r    )rc   
empty_liker$   check_array_compatibilitycopytorO   s      r   copy_to_hostzFakeCUDAArray.copy_to_host   sC    ;-	**CC%dC000
	#ty!!!
r   c                 l   t          |            t          |           t          |          }}t          |t                    r t          |           t	          ||           n7t          j        ||j        d         rdndd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FTF)ordersubokcopyN)
sentry_contiguous
array_corer   r   rm   rc   arrayflagsrn   r$   )r   rP   rN   	self_coreary_cores        r   copy_to_devicezFakeCUDAArray.copy_to_device   s     	$(..
38	c=)) 		;c"""%i::::x&_^<Ecc#	  H
 &i:::
	).(+++++r   c                 4    t          | j        j                  S r    )r   r$   shaper5   s    r   r   zFakeCUDAArray.shape   s    )))r   c                 @    t           | j        j        |i |          S r    )r   r$   ravelr   rH   rI   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$   rz   c_contiguousr5   s    r   is_c_contiguouszFakeCUDAArray.is_c_contiguous       y++r   c                 $    | j         j        j        S r    )r$   rz   f_contiguousr5   s    r   is_f_contiguouszFakeCUDAArray.is_f_contiguous   r   r   c                 *    t          | j                  S r    )strr$   r5   s    r   __str__zFakeCUDAArray.__str__       49~~r   c                 *    t          | j                  S r    )reprr$   r5   s    r   __repr__zFakeCUDAArray.__repr__   s    DIr   c                 *    t          | j                  S r    )r4   r$   r5   s    r   r6   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 r>   )r   )r?   rC   s     r   
<listcomp>z'FakeCUDAArray.split.<locals>.<listcomp>   s0     
 
 
 !
 
 
r   )rc   splitr$   ranger4   )r   sectionrN   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   propertyrT   rS   r(   r]   r`   r_   r   r+   ro   r}   r   r   r   r   r   r   r   r   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r>   r   r   r   r   ]   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slicerE   )rP   
core_indexstrides      r   rx   rx      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   )rP   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 )Nrq   F_CONTIGUOUS)rx   rz   
ValueErrorerrmsg_contiguous_buffer)rP   cores     r   rw   rw     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   rm   rm      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        | 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 )NFTrv   ru   Mm)bufferr   r   r   )rf   re   )rc   ry   rw   r   charint64r   rg   rv   rx   r   r   r   rf   r}   )rP   rN   rv   tobuffer_dtypes        r   	to_devicer   -  s    
(3U$
/
/
/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    r>   )args    r   pinnedr   >  s      	EEEEEr   c                  V    dD ]}||v r|                     |           t          | i |S )N)portablewc)popdevice_array)rH   rI   
unused_args      r   mapped_arrayr   C  sB    ( # #
JJz"""((((r   rr   c                 2    t          j        | |||          S N)r   r   r   rt   rc   rg   r   r   r   rt   s       r   pinned_arrayr   J      :E7%uMMMMr   c                 2    t          j        | |||          S r   r   r   s       r   managed_arrayr   N  r   r   c                  x    d|v r|                     d          nd}t          t          j        | i ||          S )NrN   r   re   )r   r   rc   rg   )rH   rI   rN   s      r   r   r   R  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.
    rq   r   r   Nc                     g | ]}|S r>   r>   )r?   xs     r   r   z2_contiguous_strides_like_array.<locals>.<listcomp>h  s    6661666r   c                     | d         S )Nr   r>   )r   s    r   <lambda>z0_contiguous_strides_like_array.<locals>.<lambda>i  s
    !A$ r   )keyr   )
rz   ndim	enumerater   sortr4   r   r   r   rE   )rP   
stridepermr   r   i_perm_s         r   _contiguous_strides_like_arrayr  W  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   rq   rs   rr   )rz   )rP   s    r   _order_like_arrayr  t  s)    
y  >)B ssr   c                 x    t          |           }t          |           }t          | j        | j        ||          S Nr   )r  r  r   r   r   )rP   rN   r   rt   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   )rP   r   rt   s      r   pinned_array_liker	    r  r   c                     t          | t                    r| dfS t          | t          j                  st          j        | dd          } t          | ||          dfS )NFTr   )r   r   rc   rh   ry   r   )rP   rN   rv   s      r   auto_devicer    sj    #}%% Ezc27## h   S&$''--r   c                 $    t          | dd          S )z$Check if an object is a CUDA ndarrayr   F)r&   r9   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 )hasattrrW   r   r&   )rX   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_ndarrayrE   rc   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   r9   s    r   r  r    s+    3 ;9:::; ;r   )r   TNr   )r   T) r   
contextlibr   numpyrc   DeviceRecordfrom_record_liker   rE   r   objectr   r   rx   r   rw   rm   r   r   r   float_r   r   r   r  r  r  r	  r  r  r  r  r>   r   r   <module>r     s*    & % % % % %      3 
5 
5 
5 
5 
5 
5 
5 
59% 9% 9% 9% 9% 9% 9% 9%xX
 X
 X
 X
 X
F X
 X
 X
v" " "$   3 3 3
7 
7 
7. . . ." 
 
 
) ) ) !iS N N N N  "y$c N N N NE E E
  :  % % % %% % %	. 	. 	. 	.3 3 3
   ; ; ; ; ;r   