
    J/PhD                     L   d Z ddlZddlZddlZddlmZmZmZ ddl	m
Z
 ddlmZ ej        Zej        Zej        Zed)d            Zd*d	Zd
 Zd Zed+d            Zeej        dddfd            Zeej        ddddfd            Zeej        ddfd            Zeej        dddddfd            Zej        ed,d                        Zd Zd Zd Zd-dZ d.dZ!d Z"ed             Z#ed             Z$ed             Z%ed             Z&ed             Z'eej        d                         Z(eej        d                          Z)d*d!Z*ej+        Z+d" Z,d# Z-d$ Z.d% Z/d/d&Z0d' Z1ej        d(             Z2 eej3                  Z3 eej4                  Z4 eej5                  Z5dS )0z%
API that are reported to numba.cuda
    N   )devicearraydevicesdriver)config)prepare_shape_strides_dtypeTc                    |                      d          }d|k    r&|                      d          }|t          d          | d         }|                      d          }t          j        | d                   }t	          |||d	
          \  }}}t          j        |||j                  }t          j        | d         d                   }	t          j	        t                      |	||          }
|                      dd          }|2t          |          }|r t          j        r|                                 nd}t          j        ||||
|          }|S )a  Create a DeviceNDArray from a cuda-array-interface description.
    The ``owner`` is the owner of the underlying memory.
    The resulting DeviceNDArray will acquire a reference from it.

    If ``sync`` is ``True``, then the imported stream (if present) will be
    synchronized.
    versionr   maskNzMasked arrays are not supportedshapestridestypestrC)orderdatar   )sizeownerstream)r   r   dtypegpu_datar   )getNotImplementedErrornpr   r   r   memory_size_from_infoitemsizeget_devptr_for_active_ctxMemoryPointercurrent_contextexternal_streamr   CUDA_ARRAY_INTERFACE_SYNCsynchronizer   DeviceNDArray)descr   syncr
   r   r   r   r   r   devptrr   
stream_ptrr   das                 N/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/cuda/api.pyfrom_cuda_array_interfacer)      sc    hhy!!GG||xx%&GHHHMEhhy!!GHT)_%%E7wS* * *E7E'wGGD-d6l1o>>F6E; ; ;D(D))J ,, 	!F4 	!   		").*0
2 
2 
2B I    c                 l    t          |           st          d          t          | j        | |          S )aq  Create a DeviceNDArray from any object that implements
    the :ref:`cuda array interface <cuda-array-interface>`.

    A view of the underlying GPU buffer is created.  No copying of the data
    is done.  The resulting DeviceNDArray will acquire a reference from `obj`.

    If ``sync`` is ``True``, then the imported stream (if present) will be
    synchronized.
    z1*obj* doesn't implement the cuda array interface.)r   r$   )is_cuda_array	TypeErrorr)   __cuda_array_interface__)objr$   s     r(   as_cuda_arrayr0   ?   sD      ?KLLL()E/2? ? ? 	?r*   c                 "    t          | d          S )zTest if the object has defined the `__cuda_array_interface__` attribute.

    Does not verify the validity of the interface.
    r.   )hasattr)r/   s    r(   r,   r,   P   s    
 32333r*   c                      dS )zwWhether 16-bit floats are supported.

    float16 is always supported in current versions of Numba - returns True.
    T r4   r*   r(   is_float16_supportedr5   X   s	    
 4r*   c                 v    |t          j        | ||d          \  }}|S |r|                    | |           |S )a  to_device(obj, stream=0, copy=True, to=None)

    Allocate and transfer a numpy ndarray or structured scalar to the device.

    To copy host->device a numpy array::

        ary = np.arange(10)
        d_ary = cuda.to_device(ary)

    To enqueue the transfer to a stream::

        stream = cuda.stream()
        d_ary = cuda.to_device(ary, stream=stream)

    The resulting ``d_ary`` is a ``DeviceNDArray``.

    To copy device->host::

        hary = d_ary.copy_to_host()

    To copy device->host to an existing array::

        ary = np.empty(shape=d_ary.shape, dtype=d_ary.dtype)
        d_ary.copy_to_host(ary)

    To enqueue the transfer to a stream::

        hary = d_ary.copy_to_host(stream=stream)
    NT)r   copyuser_explicitr   )r   auto_devicecopy_to_device)r/   r   r7   tonews        r(   	to_devicer>   `   sX    > 
z)#f48<> > >C	 .
#f---Ir*   r   c                 ^    t          | |||          \  } }}t          j        | |||          S )zdevice_array(shape, dtype=np.float64, strides=None, order='C', stream=0)

    Allocate an empty device ndarray. Similar to :meth:`numpy.empty`.
    )r   r   r   r   )r   r   r"   r   r   r   r   r   s        r(   device_arrayrA      sF     8w8=? ?E7E$5',24 4 4 4r*   c                 d   t          | |||          \  } }}t          j        | ||j                  }t	                                          ||          }t          j        | ||||          }t          j                            |t          j
                  }	|	                    ||           |	S )a  managed_array(shape, dtype=np.float64, strides=None, order='C', stream=0,
                     attach_global=True)

    Allocate a np.ndarray with a buffer that is managed.
    Similar to np.empty().

    Managed memory is supported on Linux / x86 and PowerPC, and is considered
    experimental on Windows and Linux / AArch64.

    :param attach_global: A flag indicating whether to attach globally. Global
                          attachment implies that the memory is accessible from
                          any stream on any device. If ``False``, attachment is
                          *host*, and memory is only accessible by devices
                          with Compute Capability 6.0 and later.
    )attach_globalr   r   r   r   buffertyper9   )r   r   r   r   r   memallocmanagedr   ndarrayviewr   ManagedNDArraydevice_setup)
r   r   r   r   r   rC   bytesizerE   nparymanagedviews
             r(   managed_arrayrP      s    $ 8w8=? ?E7E+E7ENKKH..x=J / L LFJUG5$& & &E*//%k.H/IIKVF333r*   c                     t          | |||          \  } }}t          j        | ||j                  }t	                                          |          }t          j        | ||||          S )zpinned_array(shape, dtype=np.float64, strides=None, order='C')

    Allocate an :class:`ndarray <numpy.ndarray>` with a buffer that is pinned
    (pagelocked).  Similar to :func:`np.empty() <numpy.empty>`.
    rD   )r   r   r   r   r   memhostallocr   rI   )r   r   r   r   rM   rE   s         r(   pinned_arrayrS      s{     8w8=? ?E7E+E7,1N< <H++H55F:E7%u#% % % %r*   Fc                 d   t          | |||          \  } }}t          j        | ||j                  }t	                                          |d          }t          j        | ||||          }	t          j                            |	t          j
                  }
|
                    ||           |
S )a<  mapped_array(shape, dtype=np.float64, strides=None, order='C', stream=0,
                    portable=False, wc=False)

    Allocate a mapped ndarray with a buffer that is pinned and mapped on
    to the device. Similar to np.empty()

    :param portable: a boolean flag to allow the allocated device memory to be
              usable in multiple devices.
    :param wc: a boolean flag to enable writecombined allocation which is faster
        to write by the host and to read by the device, but slower to
        write by the host and slower to write by the device.
    TmappedrD   rF   r9   )r   r   r   r   r   rR   r   rI   rJ   r   MappedNDArrayrL   )r   r   r   r   r   portablewcrM   rE   rN   
mappedviews              r(   mapped_arrayr[      s     8w8=? ?E7E+E7ENKKH++HT+BBFJUG5$& & &E[-FGGJF6222r*   c              #     K   t          j        |          }t          j        |          |j        z  }t          j        r&t          j                                        }| |_        nt	          j	        j
        |  }t	          j        d|||          }|                    t                      |||          V  |                                 dS )a  
    A context manager that opens a IPC *handle* (*CUipcMemHandle*) that is
    represented as a sequence of bytes (e.g. *bytes*, tuple of int)
    and represent it as an array of the given *shape*, *strides* and *dtype*.
    The *strides* can be omitted.  In that case, it is assumed to be a 1D
    C contiguous array.

    Yields a device array.

    The IPC handle is closed automatically when context manager exits.
    N)offset)r   r   r   )r   r   prodr   r   USE_NV_BINDINGbindingCUipcMemHandlereserveddrvapicu_ipc_mem_handle	IpcHandle
open_arrayr   close)handler   r   r   r]   r   driver_handle	ipchandles           r(   open_ipc_arrayrk      s       HUOOE75>>EN*D A5577!'7@ }d6JJJI


00'.e  = = = = =OOr*   c                  B    t                                                      S )z Synchronize the current context.)r   r!   r4   r*   r(   r!   r!      s    ((***r*   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.
    C_CONTIGUOUSF_CONTIGUOUSr   Nc                     g | ]}|S r4   r4   ).0xs     r(   
<listcomp>z2_contiguous_strides_like_array.<locals>.<listcomp>  s    6661666r*   c                     | d         S )Nr   r4   )rr   s    r(   <lambda>z0_contiguous_strides_like_array.<locals>.<lambda>  s
    !A$ r*   )keyr   )
flagsndim	enumerater   sortlenr   r   r   tuple)ary
stridepermr   stride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 )Nro   rn   Fr   )rw   )r}   s    r(   _order_like_arrayr     s)    
y  >)B ssr*   c                 z    t          |           }t          |           }t          | j        | j        |||          S )zd
    Call :func:`device_array() <numba.cuda.device_array>` with information from
    the array.
    r@   )r   r   rA   r   r   )r}   r   r   r   s       r(   device_array_liker   #  sD    
 -S11Gc""Ecisy'#F4 4 4 4r*   c           	      ~    t          |           }t          |           }t          | j        | j        |||||          S )zh
    Call :func:`mapped_array() <numba.cuda.mapped_array>` with the information
    from the array.
    )r   r   r   r   r   rX   rY   )r   r   r[   r   r   )r}   r   rX   rY   r   r   s         r(   mapped_array_liker   .  sL    
 -S11Gc""Ecisy'#FX"N N N Nr*   c                 x    t          |           }t          |           }t          | j        | j        ||          S )zh
    Call :func:`pinned_array() <numba.cuda.pinned_array>` with the information
    from the array.
    )r   r   r   r   )r   r   rS   r   r   )r}   r   r   s      r(   pinned_array_liker   9  sB    
 -S11Gc""Ecisy'#% % % %r*   c                  B    t                                                      S )zN
    Create a CUDA stream that represents a command queue for the device.
    )r   create_streamr4   r*   r(   r   r   E  s    
 **,,,r*   c                  B    t                                                      S )a~  
    Get the default CUDA stream. CUDA semantics in general are that the default
    stream is either the legacy default stream or the per-thread default stream
    depending on which CUDA APIs are in use. In Numba, the APIs for the legacy
    default stream are always the ones in use, but an option to use APIs for
    the per-thread default stream may be provided in future.
    )r   get_default_streamr4   r*   r(   default_streamr   M  s     //111r*   c                  B    t                                                      S )z-
    Get the legacy default CUDA stream.
    )r   get_legacy_default_streamr4   r*   r(   legacy_default_streamr   Y  s    
 66888r*   c                  B    t                                                      S )z1
    Get the per-thread default CUDA stream.
    )r   get_per_thread_default_streamr4   r*   r(   per_thread_default_streamr   a  s    
 ::<<<r*   c                 D    t                                          |           S )zCreate a Numba stream object for a stream allocated outside Numba.

    :param ptr: Pointer to the external stream to wrap in a Numba Stream
    :type ptr: int
    )r   create_external_stream)ptrs    r(   r   r   i  s     33C888r*   c               '      K   g }| D ]`}t                                          |t          j        |          t          j        |          d          }|                    |           adV  dS )zIA context manager for temporary pinning a sequence of host ndarrays.
    FrU   N)r   mempinr   host_pointerhost_memory_sizeappend)arylistpmlistr}   pms       r(   pinnedr   t  s      
 F  %%c6+>s+C+C&,&=c&B&B-2 & 4 4 	b	EEEEEr*   c               /   0  K   |rd|v s
J d            |                     dd          }g }g }| D ]}t                                          |t          j        |          t          j        |          d          }|                    |           t          j        |||          }|                    |           	 t          |          dk    r|d         V  n|V  |D ]}|
                                 dS # |D ]}|
                                 w xY w)	zKA context manager for temporarily mapping a sequence of host ndarrays.
    r   z Only accept 'stream' as keyword.r   TrU   )r   r   r   N)r   r   r   r   r   r   r   r   from_array_liker{   free)r   kwsr   r   
devarylistr}   r   devarys           r(   rV   rV     sT     
 Ih#ooo'IoooWWXq!!FFJ " "%%c6+>s+C+C&,&=c&B&B-1 & 3 3 	b,S2fMMM&!!!!
z??aQ-
  	 	BGGIIII	 	& 	 	BGGIIII	s   ="C: :Dc                 J    t                                          |           }|S )zs
    Create a CUDA event. Timing data is only recorded by the event if it is
    created with ``timing=True``.
    )timing)r   create_event)r   evts     r(   eventr     s%    
 


(
(
(
7
7CJr*   c                 8    t          j        |           }|j        S )z
    Make the context associated with device *device_id* the current context.

    Returns a Device instance.

    Raises exception on error.
    )r   get_contextdevice)	device_idcontexts     r(   select_devicer     s     !),,G>r*   c                  (    t                      j        S )z5Get current device associated with the current thread)r   r   r4   r*   r(   get_current_devicer     s    ##r*   c                      t           j        S )z%Return a list of all detected devices)r   gpusr4   r*   r(   list_devicesr     s
    <r*   c                  ,    t          j                     dS )z
    Explicitly clears all contexts in the current thread, and destroys all
    contexts if the current thread is the main thread.
    N)r   resetr4   r*   r(   rg   rg     s    
 MOOOOOr*   c                 0    t          j        | ||          S )N)r   r7   )r   r:   )r}   r   r7   s      r(   _auto_devicer     s    "3vDAAAAr*   c                     t                      } t          dt          |           z             d}| D ]}g }|j        }|j        }|j        }|j        }|dd|z  fgz  }|d|j        fgz  }|d|j        fgz  }|d|j	        fgz  }|d|rd	nd
fgz  }t          j        dk    r|d|rdndfgz  }|d|fgz  }|dk     rd}n|dk     rd}|dz  }nd}|dz  }t          d|j        |j        |fz             |D ]\  }	}
t          |	dd|
           t          d           t          d|t          |           fz             |dk    S )z
    Detect supported CUDA hardware and print a summary of the detected hardware.

    Returns a boolean indicating whether any supported devices were detected.
    zFound %d CUDA devicesr   zCompute Capabilityz%d.%dzPCI Device IDz
PCI Bus IDUUIDWatchdogEnabledDisabledntzCompute ModeTCCWDDMzFP32/FP64 Performance Ratio)      z[NOT SUPPORTED: CC < 3.5])r   r   z[SUPPORTED (DEPRECATED)]r   z[SUPPORTED]zid %d    %20s %40sz>40z: zSummary:z	%d/%d devices are supported)r   printr{   compute_capabilityKERNEL_EXEC_TIMEOUT
TCC_DRIVER%SINGLE_TO_DOUBLE_PRECISION_PERF_RATIOPCI_DEVICE_ID
PCI_BUS_IDuuidosnameid)devlistsupported_countdevattrscckernel_timeouttccfp32_to_fp64_ratiosupportrv   vals              r(   detectr     s    nnG	
!CLL
0111O + +#0n F'2677?C$5677<011638$%%:NJyy
KLL7d??~'?uu@AAE02DEFF;;1GG&[[0Gq OO#Gq O"cfch%@@AAA 	+ 	+HCSS)****	+ 
*	
)_c'll,K
KLLLQr*   c               #      K   t                                                      5  dV  ddd           dS # 1 swxY w Y   dS )aV  
    Temporarily disable memory deallocation.
    Use this to prevent resource deallocation breaking asynchronous execution.

    For example::

        with defer_cleanup():
            # all cleanup is deferred in here
            do_speed_critical_code()
        # cleanup can occur here

    Note: this context manager can be nested.
    N)r   defer_cleanupr4   r*   r(   r   r     s       
			(	(	*	*                   s   599)NT)T)r   TN)Nr   )r   )r   FF)r   T)6__doc__
contextlibr   numpyr   cudadrvr   r   r   
numba.corer   numba.cuda.api_utilr   require_contextr   r   r   r)   r0   r,   r5   r>   float64rA   rP   rS   r[   contextmanagerrk   r!   r   r   r   r   r   r   r   r   r   r   r   rV   r   event_elapsed_timer   r   r   rg   r   r   r   	profilingprofile_startprofile_stopr4   r*   r(   <module>r      s    
     				     1 1 1 1 1 1 1 1 1 1       ; ; ; ; ; ; )%| % % % %P? ? ? ?"4 4 4   $ $ $ $N  j$c! 4 4 4 4 !z4s1 $   :  j$c % % % %  j$c!E   2     8+ + +
  :  4 4 4 4N N N N% % % - - - 2 2 2 9 9 9 = = = 9 9 9 	
 	
  	
    6    . 
	 	 	$ $ $
  
  B B B B& & &R   $ OF,--	 455v233r*   