U
    ,‰d¥  ã                   @   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__© ú>/tmp/pip-unpacked-wheel-eu7e0c37/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è  é
   )ÚctypesÚc_intZcudaRuntimeGetVersionÚ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.
        )rA   Ú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:   r   )r:   r    )r:   rH   )é   r   )rI   r    )rI   rH   )ÚsysÚplatformr   ZMACHINE_BITSr   r   r   r   rB   |   s    zRuntime.supported_versionsN)r   r   r   r   r   r   r+   r&   r-   r#   rA   rC   ÚpropertyrB   r   r   r   r   r   !   s   r   c                   C   s   t  ¡ S )zA
    Return the runtime version as a tuple of (major, minor)
    )ÚruntimerA   r   r   r   r   rA   ‹   s    rA   )r   r;   r2   rJ   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   rM   rA   r   r   r   r   Ú<module>   s   g