U
    9%e%                     @   s   d dl mZmZmZmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZmZmZ d dlZd dlZd dlZd dlZeZeZG dd de	Ze ZG dd	 d	ZG d
d dZdd ZdS )    )byrefc_charc_char_pc_intc_size_tc_void_pPOINTER)IntEnum)config)
NvrtcErrorNvrtcCompilationErrorNvrtcSupportErrorNc                   @   s<   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdS )NvrtcResultr                           	   
      N)__name__
__module____qualname__NVRTC_SUCCESSZNVRTC_ERROR_OUT_OF_MEMORYZ$NVRTC_ERROR_PROGRAM_CREATION_FAILUREZNVRTC_ERROR_INVALID_INPUTZNVRTC_ERROR_INVALID_PROGRAMZNVRTC_ERROR_INVALID_OPTIONNVRTC_ERROR_COMPILATIONZ%NVRTC_ERROR_BUILTIN_OPERATION_FAILUREZ1NVRTC_ERROR_NO_NAME_EXPRESSIONS_AFTER_COMPILATIONZ/NVRTC_ERROR_NO_LOWERED_NAMES_BEFORE_COMPILATIONZ%NVRTC_ERROR_NAME_EXPRESSION_NOT_VALIDZNVRTC_ERROR_INTERNAL_ERROR r   r   W/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/numba/cuda/cudadrv/nvrtc.pyr      s   r   c                   @   s,   e Zd ZdZdd Zedd Zdd ZdS )	NvrtcProgramz
    A class for managing the lifetime of nvrtcProgram instances. Instances of
    the class own an nvrtcProgram; when an instance is deleted, the underlying
    nvrtcProgram is destroyed using the appropriate NVRTC API.
    c                 C   s   || _ || _d S N)_nvrtc_handle)selfnvrtchandler   r   r    __init__+   s    zNvrtcProgram.__init__c                 C   s   | j S r"   )r$   r%   r   r   r    r'   /   s    zNvrtcProgram.handlec                 C   s   | j r| j|  d S r"   )r$   r#   destroy_programr)   r   r   r    __del__3   s    zNvrtcProgram.__del__N)r   r   r   __doc__r(   propertyr'   r+   r   r   r   r    r!   %   s
   
r!   c                   @   s   e Zd ZdZeeeeefeeeeeeeeefeeefeeeeefeeee	feeefeeee	feeefeeee	feeef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S )NVRTCaB  
    Provides a Pythonic interface to the NVRTC APIs, abstracting away the C API
    calls.

    The sole instance of this class is a process-wide singleton, similar to the
    NVVM interface. Initialization is protected by a lock and uses the standard
    (for Numba) open_cudalib function to load the NVRTC library.
    )
nvrtcVersionnvrtcCreateProgramnvrtcDestroyProgramnvrtcCompileProgramnvrtcGetPTXSizenvrtcGetPTXZnvrtcGetCUBINSizeZnvrtcGetCUBINnvrtcGetProgramLogSizenvrtcGetProgramLogNc           	      C   s   t  | jd krddlm} t|  | _}z|d}W n2 tk
rj } zd | _td|W 5 d }~X Y nX |j	 D ]P\}}t
||}|d |_|dd  |_t|||ddd}t||| qvW 5 Q R X | jS )	Nr   )open_cudalibr&   zNVRTC cannot be loadedr   )funcnamec                 W   sp   | | }|t jkrt nR|t jkrlzt |j}W n  tk
rR   d| d}Y nX d| d| }t|d S )Nz"Unknown nvrtc_result (error code: )zFailed to call z: )r   r   r   r   r9   
ValueErrorr   )r8   r9   argserrorZ
error_namemsgr   r   r    checked_callx   s    

z#NVRTC.__new__.<locals>.checked_call)_nvrtc_lock_NVRTC__INSTANCEZnumba.cuda.cudadrv.libsr7   object__new__OSErrorr   _PROTOTYPESitemsgetattrrestypeZargtypes	functoolswrapssetattr)	clsr7   instliber9   protor8   r?   r   r   r    rC   g   s"    


zNVRTC.__new__c                 C   s,   t  }t  }| t|t| |j|jfS )zB
        Get the NVRTC version as a tuple (major, minor).
        )r   r/   r   value)r%   majorminorr   r   r    get_version   s    zNVRTC.get_versionc                 C   sL   t |tr| }t |tr$| }t }| t|||ddd t| |S )z@
        Create an NVRTC program with managed lifetime.
        r   N)
isinstancestrencodenvrtc_programr0   r   r!   )r%   srcr9   r'   r   r   r    create_program   s    

zNVRTC.create_programc                 C   sf   dd |D }dd |D }t t| }|| }z| |jt|| W dS  tk
r`   Y dS X dS )z
        Compile an NVRTC program. Compilation may fail due to a user error in
        the source; this function returns ``True`` if there is a compilation
        error and ``False`` on success.
        c                 S   s   g | ]}|  qS r   )rW   .0optr   r   r    
<listcomp>   s     z)NVRTC.compile_program.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )r   r[   r   r   r    r^      s     FTN)r   lenr2   r'   r   )r%   programoptionsZencoded_optionsZoption_pointersZc_options_typeZ	c_optionsr   r   r    compile_program   s    zNVRTC.compile_programc                 C   s   |  t|j dS )z+
        Destroy an NVRTC program.
        N)r1   r   r'   )r%   r`   r   r   r    r*      s    zNVRTC.destroy_programc                 C   s<   t  }| |jt| t|j  }| |j| |j S )z9
        Get the compile log as a Python string.
        )r   r5   r'   r   r   rQ   r6   decode)r%   r`   Zlog_sizelogr   r   r    get_compile_log   s
    zNVRTC.get_compile_logc                 C   s<   t  }| |jt| t|j  }| |j| |j S )z:
        Get the compiled PTX as a Python string.
        )r   r3   r'   r   r   rQ   r4   rc   )r%   r`   Zptx_sizeptxr   r   r    get_ptx   s
    zNVRTC.get_ptx)r   r   r   r,   nvrtc_resultr   r   rX   r   r   rE   rA   rC   rT   rZ   rb   r*   re   rg   r   r   r   r    r.   8   s8   
  
$#	r.   c                 C   s   t  }|| |}|\}}d| | }dtj }tjtjt}	tj|	}
d|
 }|||ddg}|	||}|
|}|rd| d| }t||rd| d| }t| ||}||fS )a~  
    Compile a CUDA C/C++ source to PTX for a given compute capability.

    :param src: The source code to compile
    :type src: str
    :param name: The filename of the source (for information only)
    :type name: str
    :param cc: A tuple ``(major, minor)`` of the compute capability
    :type cc: tuple
    :return: The compiled PTX and compilation log
    :rtype: tuple
    z--gpu-architecture=compute_z-Iz-rdctruez+NVRTC Compilation failure whilst compiling z:

z$NVRTC log messages whilst compiling )r.   rZ   r
   ZCUDA_INCLUDE_PATHospathdirnameabspath__file__rb   re   r   warningswarnrg   )rY   r9   ccr&   r`   rR   rS   archincludeZcudadrv_pathZnumba_cuda_pathZnumba_includera   Zcompile_errorrd   r>   rf   r   r   r    compile   s&    



rt   )ctypesr   r   r   r   r   r   r   enumr	   Z
numba.corer
   Znumba.cuda.cudadrv.errorr   r   r   rI   rj   	threadingro   rX   rh   r   Lockr@   r!   r.   rt   r   r   r   r    <module>   s   $ 