
    J/Ph8                         d Z ddlZddlZddlmZ ddlmZmZ  G d de          Z G d d	e          Z	 G d
 de          Z
 e
            Zej        ZddZd Zd ZdS )a  
Expose each GPU devices directly.

This module implements a API that is like the "CUDA runtime" context manager
for managing CUDA context stack and clean up.  It relies on thread-local globals
to separate the context stack management of each thread. Contexts are also
shareable among threads.  Only the main thread can destroy Contexts.

Note:
- This module must be imported by the main-thread.

    N)contextmanager   )driverUSE_NV_BINDINGc                   L     e Zd Z fdZd Zd Zd Zd Zed             Z	 xZ
S )_DeviceListc                     |dk    r5t          j                    }d t          |          D             }|| _        |S t	          t
          |                               |          S )Nlstc                 P    g | ]#}t          t          j        |                    $S  )_DeviceContextManagerr   
get_device).0devids     Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/cuda/cudadrv/devices.py
<listcomp>z+_DeviceList.__getattr__.<locals>.<listcomp>   s;     0 0 0 *&*;E*B*BCC 0 0 0    )r   get_device_countranger
   superr   __getattr__)selfattrnumdevgpus	__class__s       r   r   z_DeviceList.__getattr__   sn    5== ,..F0 0!&v0 0 0D DHK [$''33D999r   c                     | j         |         S )zB
        Returns the context manager for device *devnum*.
        )r
   )r   devnums     r   __getitem__z_DeviceList.__getitem__$   s     xr   c                 J    d                     d | j        D                       S )Nz, c                 ,    g | ]}t          |          S r   )str)r   ds     r   r   z'_DeviceList.__str__.<locals>.<listcomp>+   s    333Q#a&&333r   )joinr
   r   s    r   __str__z_DeviceList.__str__*   s%    yy33$(333444r   c                 *    t          | j                  S N)iterr
   r%   s    r   __iter__z_DeviceList.__iter__-   s    DH~~r   c                 *    t          | j                  S r(   )lenr
   r%   s    r   __len__z_DeviceList.__len__0   s    48}}r   c                     t          j                    5 }|j        }|| |         cddd           S 	 ddd           dS # 1 swxY w Y   dS )zFReturns the active device or None if there's no active device
        N)r   get_active_contextr   )r   acr   s      r   currentz_DeviceList.current3   s     &(( 	$BYF!F|	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$!	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   A  AA)__name__
__module____qualname__r   r   r&   r*   r-   propertyr1   __classcell__)r   s   @r   r   r      s        : : : : :     5 5 5     $ $ X$ $ $ $ $r   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )r   aU  
    Provides a context manager for executing in the context of the chosen
    device. The normal use of instances of this type is from
    ``numba.cuda.gpus``. For example, to execute on device 2::

       with numba.cuda.gpus[2]:
           d_a = numba.cuda.to_device(a)

    to copy the array *a* onto device 2, referred to by *d_a*.
    c                     || _         d S r(   )_device)r   devices     r   __init__z_DeviceContextManager.__init__I   s    r   c                 ,    t          | j        |          S r(   )getattrr9   )r   items     r   r   z!_DeviceContextManager.__getattr__L   s    t|T***r   c                 N    t                               | j        j                   d S r(   )_runtimeget_or_create_contextr9   idr%   s    r   	__enter__z_DeviceContextManager.__enter__O   s     &&t|77777r   c                 \    | j                                                                          d S r(   )r9   get_primary_contextpop)r   exc_typeexc_valexc_tbs       r   __exit__z_DeviceContextManager.__exit__R   s(    ((**..00000r   c                 .    d                     |           S )Nz<Managed Device {self.id}>r%   )formatr%   s    r   r&   z_DeviceContextManager.__str__V   s    +222===r   N)	r2   r3   r4   __doc__r;   r   rC   rJ   r&   r   r   r   r   r   =   si        	 	  + + +8 8 81 1 1> > > > >r   r   c                   X    e Zd ZdZd Zed             Zd Zd Zd Z	d Z
d Zd	 Zd
 ZdS )_RuntimezEmulate the CUDA runtime context management.

    It owns all Devices and Contexts.
    Keeps at most one Context per Device
    c                     t                      | _        t          j                    | _        t          j                    | _        t          j                    | _        d S r(   )	r   r   	threadinglocal_tlscurrent_thread_mainthreadRLock_lockr%   s    r   r;   z_Runtime.__init__a   sF    MM	 O%%	 %355 _&&


r   c              #   J  K   t          j                    5  |                                 }|                     d          }|                     |           	 dV  |                     |           n# |                     |           w xY w	 ddd           dS # 1 swxY w Y   dS )a  Ensure a CUDA context is available inside the context.

        On entrance, queries the CUDA driver for an active CUDA context and
        attaches it in TLS for subsequent calls so they do not need to query
        the CUDA driver again.  On exit, detach the CUDA context from the TLS.

        This will allow us to pickup thirdparty activated CUDA context in
        any top-level Numba CUDA API.
        N)r   r/   _get_attached_contextrA   _set_attached_context)r   oldctxnewctxs      r   ensure_contextz_Runtime.ensure_contextn   s       &(( 	3 	3//11F//55F&&v...3**62222**622222	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3s)   ?BA0B0BBBBc                     |-|                                  }||                     |          S |S t          rt          |          }|                     |          S )zReturns the primary context and push+create it if needed
        for *devnum*.  If *devnum* is None, use the active CUDA context (must
        be primary) or create a new one with ``devnum=0``.
        )rY   _get_or_create_context_uncachedr   int_activate_context_for)r   r   attached_ctxs      r   rA   z_Runtime.get_or_create_context   sa    
 >5577L#;;FCCC## %V--f555r   c                 l   | j         5  t          j                    5 }|s-|                     d          cddd           cddd           S | j        |j                                                 }t          r)t          |j	                  }t          |j
                  }n|j	        j        }|j
        j        }||k    r$d}t          |                    |                    |                                 |cddd           cddd           S # 1 swxY w Y   	 ddd           dS # 1 swxY w Y   dS )zbSee also ``get_or_create_context(devnum)``.
        This version does not read the cache.
        r   Nz5Numba cannot operate on non-primary CUDA context {:x})rW   r   r/   ra   r   r   rE   r   r`   handlecontext_handlevalueRuntimeErrorrL   prepare_for_use)r   r   r0   ctx
ctx_handleac_ctx_handlemsgs          r   r_   z(_Runtime._get_or_create_context_uncached   s    Z 	 	 *,,  *55a88      	 	 	 	 	 	 	 	 )BI.BBDDC% @%(__
(+B,=(>(>%(Z%5
(*(9(?!]22 4*3::m+D+DEEE'')))'      	 	 	 	 	 	 	 	        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s@   D)DD)B+D7D)D	D)D	D))D-0D-c                    | j         5  | j        |         }|                                }|                                 }|||urt	          d          |                                 |cd d d            S # 1 swxY w Y   d S )NzCannot switch CUDA-context.)rW   r   rE   rY   rg   push)r   r   gpur\   
cached_ctxs        r   ra   z_Runtime._activate_context_for   s    Z 	 	)F#C,,..F3355J%*F*B*B"#@AAAKKMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A A55A9<A9c                 .    t          | j        dd           S )Nattached_context)r=   rS   r%   s    r   rY   z_Runtime._get_attached_context   s    ty"4d;;;r   c                     || j         _        d S r(   )rS   rr   )r   ri   s     r   rZ   z_Runtime._set_attached_context   s    %(	"""r   c                     t          j                    	 t          j                    t          j                    | j        k    r|                                  dS dS )zqClear all contexts in the thread.  Destroy the context if and only
        if we are in the main thread.
        N)r   pop_active_contextrQ   rT   rU   _destroy_all_contextsr%   s    r   resetz_Runtime.reset   s`    
 '))5 '))5 #%%)999&&((((( :9r   c                 B    | j         D ]}|                                 d S r(   )r   rw   )r   ro   s     r   rv   z_Runtime._destroy_all_contexts   s,    9 	 	CIIKKKK	 	r   N)r2   r3   r4   rM   r;   r   r]   rA   r_   ra   rY   rZ   rw   rv   r   r   r   rO   rO   Z   s         ' ' ' 3 3 ^3&6 6 6   8	 	 	< < <) ) )
) 
) 
)    r   rO   c                 6    t                               |           S )z^Get the current device or use a device by device number, and
    return the CUDA context.
    )r@   rA   )r   s    r   get_contextrz      s     ))&111r   c                 F     t          j                    fd            }|S )z
    A decorator that ensures a CUDA context is available when *fn* is executed.

    Note: The function *fn* cannot switch CUDA-context.
    c                  z    t                                           5   | i |cd d d            S # 1 swxY w Y   d S r(   )r@   r]   )argskwsfns     r   _require_cuda_contextz.require_context.<locals>._require_cuda_context   s    $$&& 	$ 	$2t#s##	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   044)	functoolswraps)r   r   s   ` r   require_contextr      s;     _R$ $ $ $ $ ! r   c                  8    t                                            dS )zReset the CUDA subsystem for the current thread.

    In the main thread:
    This removes all CUDA contexts.  Only use this at shutdown or for
    cleaning up between tests.

    In non-main threads:
    This clear the CUDA context stack only.

    N)r@   rw   r   r   r   rw   rw      s     NNr   r(   )rM   r   rQ   
contextlibr   r   r   objectr   r   rO   r@   r   rz   r   rw   r   r   r   <module>r      s-            % % % % % % * * * * * * * *&$ &$ &$ &$ &$& &$ &$ &$R> > > > >F > > >:t t t t tv t t tn 8:: }2 2 2 2! ! !    r   