o
    i                     @   s   d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
mZ ddlmZ ddlmZ ddlmZ G d	d
 d
eZG dd dZe Zdd ZdS )z
CUDA Runtime wrapper.

This provides a very minimal set of bindings, since the Runtime API is not
really used in Numba except for querying the Runtime version.
    N)config)	ERROR_MAPmake_logger)CudaSupportErrorCudaRuntimeError)open_cudalib)API_PROTOTYPES)enumsc                       s(   e Zd ZdZ fddZdd Z  ZS )CudaRuntimeAPIErrorzP
    Raised when there is an error accessing a C API from the CUDA Runtime.
    c                    s   || _ || _t || d S )N)codemsgsuper__init__)selfr   r   	__class__ W/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/numba/cuda/cudadrv/runtime.pyr      s   zCudaRuntimeAPIError.__init__c                 C   s   d| j | jf S )Nz[%s] %s)r   r   r   r   r   r   __str__   s   zCudaRuntimeAPIError.__str__)__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r
      s    r
   c                   @   s\   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zedd ZdS )RuntimezA
    Runtime object that lazily binds runtime API functions.
    c                 C   s
   d| _ d S )NF)is_initializedr   r   r   r   r   &   s   
zRuntime.__init__c                 C   s,   t  atjrd}t|td| _d| _d S )NzzCUDA is disabled due to setting NUMBA_DISABLE_CUDA=1 in the environment, or because CUDA is unsupported on 32-bit systems.cudartT)r   _loggerr   DISABLE_CUDAr   r   libr   )r   r   r   r   r   _initialize)   s   

zRuntime._initializec                 C   sx   zt | }W n ty   t|w |d }|dd  }| js#|   | |}||_||_| ||}t	| || |S )Nr      )
r   KeyErrorAttributeErrorr   r!   	_find_apirestypeargtypes_wrap_api_callsetattr)r   fnameprotor&   r'   libfn	safe_callr   r   r   __getattr__7   s   
zRuntime.__getattr__c                    s   t  fdd}|S )Nc                     s&   t dj |  } | d S )Nzcall runtime api: %s)r   debugr   _check_error)argsretcoder*   r,   r   r   r   safe_cuda_api_callM   s   z2Runtime._wrap_api_call.<locals>.safe_cuda_api_call)	functoolswraps)r   r*   r,   r4   r   r3   r   r(   L   s   zRuntime._wrap_api_callc                 C   s:   |t jkrt|d}d||f }t| t||d S )NcudaErrorUnknownzCall to %s results in %s)r	   CUDA_SUCCESSr   getr   errorr
   )r   r*   r2   errnamer   r   r   r   r0   T   s   


zRuntime._check_errorc                    s>   zt | j W S  ty   Y nw  fdd}t|  | |S )Nc                     s   d}t |  )Nzruntime missing function: %s.)r   )r1   kwsr   r*   r   r   absent_functionc   s   z*Runtime._find_api.<locals>.absent_function)getattrr    r$   r)   )r   r*   r>   r   r=   r   r%   [   s   zRuntime._find_apic                 C   s<   t  }| t | |jd }|j|d  d }||fS )zM
        Returns the CUDA Runtime version as a tuple (major, minor).
        i  
   )ctypesc_intcudaRuntimeGetVersionbyrefvalue)r   rtvermajorminorr   r   r   get_versionj   s
   
zRuntime.get_versionc                 C   s   |   | jv S )zJ
        Returns True if the CUDA Runtime is a supported version.
        )rI   supported_versionsr   r   r   r   is_supported_versionu   s   zRuntime.is_supported_versionc                 C   s   t jdvs
tjdkrdS dS )zzA tuple of all supported CUDA toolkit versions. Versions are given in
        the form ``(major_version, minor_version)``.)linuxwin32@   r   ))   r   )rO   r"   )rO      )rO      )rO      )rO      )rO      )rO      )sysplatformr   MACHINE_BITSr   r   r   r   rJ   |   s   zRuntime.supported_versionsN)r   r   r   r   r   r!   r.   r(   r0   r%   rI   rK   propertyrJ   r   r   r   r   r   !   s    r   c                   C   s   t  S )zA
    Return the runtime version as a tuple of (major, minor)
    )runtimerI   r   r   r   r   rI      s   rI   )r   rA   r5   rV   
numba.corer   numba.cuda.cudadrv.driverr   r   numba.cuda.cudadrv.errorr   r   numba.cuda.cudadrv.libsr   numba.cuda.cudadrv.rtapir   numba.cuda.cudadrvr	   r
   r   rZ   rI   r   r   r   r   <module>   s    f