o
    i%                     @   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_SUCCESSNVRTC_ERROR_OUT_OF_MEMORY$NVRTC_ERROR_PROGRAM_CREATION_FAILURENVRTC_ERROR_INVALID_INPUTNVRTC_ERROR_INVALID_PROGRAMNVRTC_ERROR_INVALID_OPTIONNVRTC_ERROR_COMPILATION%NVRTC_ERROR_BUILTIN_OPERATION_FAILURE1NVRTC_ERROR_NO_NAME_EXPRESSIONS_AFTER_COMPILATION/NVRTC_ERROR_NO_LOWERED_NAMES_BEFORE_COMPILATION%NVRTC_ERROR_NAME_EXPRESSION_NOT_VALIDNVRTC_ERROR_INTERNAL_ERROR r)   r)   V/home/ubuntu/transcripts/venv/lib/python3.10/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*   r1   /   s   zNvrtcProgram.handlec                 C   s   | j r| j|  d S d S r,   )r.   r-   destroy_programr3   r)   r)   r*   __del__3   s   zNvrtcProgram.__del__N)r   r   r   __doc__r2   propertyr1   r5   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nvrtcGetPTXnvrtcGetCUBINSizenvrtcGetCUBINnvrtcGetProgramLogSizenvrtcGetProgramLogNc           	      C   s   t o | jd u r_ddlm} t|  | _}z|d}W n ty0 } zd | _td|d }~ww |j	 D ]1\}}t
||}|d |_|dd  |_t|||ddd}t||| q6W d    | jS W d    | jS 1 stw   Y  | jS )	Nr   )open_cudalibr0   zNVRTC cannot be loadedr   )funcnamec                 W   sl   | | }|t jkrt |t jkr4zt |j}W n ty'   d| d}Y nw d| d| }t|d S )Nz"Unknown nvrtc_result (error code: )zFailed to call z: )r   r#   r   r   rE   
ValueErrorr   )rD   rE   argserror
error_namemsgr)   r)   r*   checked_callx   s   

z#NVRTC.__new__.<locals>.checked_call)_nvrtc_lock_NVRTC__INSTANCEnumba.cuda.cudadrv.libsrC   object__new__OSErrorr   _PROTOTYPESitemsgetattrrestypeargtypes	functoolswrapssetattr)	clsrC   instliberE   protorD   rL   r)   r)   r*   rQ   g   s4   




 
  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   r9   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_programr:   r   r+   )r/   srcrE   r1   r)   r)   r*   create_program   s   


zNVRTC.create_programc                 C   s`   dd |D }dd |D }t t| }|| }z| |jt|| W dS  ty/   Y dS w )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)   )rf   .0optr)   r)   r*   
<listcomp>       z)NVRTC.compile_program.<locals>.<listcomp>c                 S   s   g | ]}t |qS r)   )r   rj   r)   r)   r*   rm      rn   FT)r   lenr<   r1   r   )r/   programoptionsencoded_optionsoption_pointersc_options_type	c_optionsr)   r)   r*   compile_program   s   zNVRTC.compile_programc                 C   s   |  t|j dS )z+
        Destroy an NVRTC program.
        N)r;   r   r1   )r/   rp   r)   r)   r*   r4      s   zNVRTC.destroy_programc                 C   <   t  }| |jt| t|j  }| |j| |j S )z9
        Get the compile log as a Python string.
        )r   rA   r1   r   r   r`   rB   decode)r/   rp   log_sizelogr)   r)   r*   get_compile_log   
   
zNVRTC.get_compile_logc                 C   rw   )z:
        Get the compiled PTX as a Python string.
        )r   r=   r1   r   r   r`   r>   rx   )r/   rp   ptx_sizeptxr)   r)   r*   get_ptx   r|   zNVRTC.get_ptx)r   r   r   r6   nvrtc_resultr   r   rg   r   r   rS   rN   rQ   rc   ri   rv   r4   r{   r   r)   r)   r)   r*   r8   8   s6    

$#	r8   c                 C   s   t  }|| |}|\}}d| | }dtj }tjtjt}	tj|	}
d|
 }|||ddg}|	||}|
|}|rOd| d| }t||r^d| 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 )r8   ri   r
   CUDA_INCLUDE_PATHospathdirnameabspath__file__rv   r{   r   warningswarnr   )rh   rE   ccr0   rp   ra   rb   archincludecudadrv_pathnumba_cuda_pathnumba_includerq   compile_errorrz   rK   r~   r)   r)   r*   compile   s&   



r   )ctypesr   r   r   r   r   r   r   enumr	   
numba.corer
   numba.cuda.cudadrv.errorr   r   r   rX   r   	threadingr   rg   r   r   LockrM   r+   r8   r   r)   r)   r)   r*   <module>   s    $  