U
    ?h                     @   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__ L/var/www/html/venv/lib/python3.8/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.ZcudartT)r   _loggerr   ZDISABLE_CUDAr   r   libr   )r   r   r   r   r   _initialize)   s    
zRuntime._initializec                 C   s~   zt | }W n tk
r(   t|Y nX |d }|dd  }| jsL|   | |}||_||_| ||}t	| || |S )Nr      )
r   KeyErrorAttributeErrorr   r   	_find_apirestypeargtypes_wrap_api_callsetattr)r   fnameprotor$   r%   libfnZ	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*   r1   r   r0   r   r&   L   s    zRuntime._wrap_api_callc                 C   s:   |t jkr6t|d}d||f }t| t||d S )NZcudaErrorUnknownzCall to %s results in %s)r	   ZCUDA_SUCCESSr   getr   errorr
   )r   r(   r/   Zerrnamer   r   r   r   r-   T   s
    

zRuntime._check_errorc                    s@   zt | j W S  tk
r"   Y nX  fdd}t|  | |S )Nc                     s   d}t |  d S )Nzruntime missing function: %s.)r   )r.   kwsr   r(   r   r   absent_functionc   s    z*Runtime._find_api.<locals>.absent_function)getattrr   r"   r'   )r   r(   r8   r   r7   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  
   )ctypesZc_intZcudaRuntimeGetVersionZbyrefvalue)r   Zrtvermajorminorr   r   r   get_versionj   s
    
zRuntime.get_versionc                 C   s   |   | jkS )zJ
        Returns True if the CUDA Runtime is a supported version.
        )r?   supported_versionsr   r   r   r   is_supported_versionu   s    zRuntime.is_supported_versionc                 C   s   t jdk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   )rE   r    )rE      )rE      )rE      )rE      )rE      )rE      )sysplatformr   ZMACHINE_BITSr   r   r   r   r@   |   s    zRuntime.supported_versionsN)r   r   r   r   r   r   r+   r&   r-   r#   r?   rA   propertyr@   r   r   r   r   r   !   s   r   c                   C   s   t  S )zA
    Return the runtime version as a tuple of (major, minor)
    )runtimer?   r   r   r   r   r?      s    r?   )r   r;   r2   rL   Z
numba.corer   Znumba.cuda.cudadrv.driverr   r   Znumba.cuda.cudadrv.errorr   r   Znumba.cuda.cudadrv.libsr   Znumba.cuda.cudadrv.rtapir   Znumba.cuda.cudadrvr	   r
   r   rO   r?   r   r   r   r   <module>   s   f