a
    –§»a“2  ã                   @   sÀ   d dl mZ d dl m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mZmZ d d	lZd d	lZd d	lZd d	lZd d	lZd
Zdd„ ZG dd„ dejeƒZG dd„ de
ƒZd	S )é    )Úbinding)Úir)Úwarn)ÚconfigÚ	serialize)ÚCodegenÚCodeLibrary)ÚNumbaInvalidConfigWarningé   )ÚdevicesÚdriverÚnvvmNznvptx64-nvidia-cudac              
   C   s  d }d }zàt  ¡ \}}t|dƒ�}| | ¡ W d   ƒ n1 s@0    Y  ztjd|gdtjtjd�}W n: ty¢ } z"|jdkrŽd}t	|ƒ‚W Y d }~n
d }~0 0 |j
 d¡W |d urÂt |¡ |d urÔt |¡ S |d urèt |¡ n*|d u� rþt |¡ |d u�rt |¡ 0 d S )NÚwbZnvdisasmT)ÚcheckÚstdoutÚstderrzšnvdisasm is required for SASS inspection, and has not been found.

You may need to install the CUDA toolkit and ensure that it is available on your PATH.
zutf-8)ÚtempfileÚmkstempÚopenÚwriteÚ
subprocessÚrunÚPIPEÚFileNotFoundErrorÚfilenameÚRuntimeErrorr   ÚdecodeÚosÚcloseÚunlink)ÚcubinÚfdÚfnameÚfÚcpÚeÚmsg© r'   úL/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/cuda/codegen.pyÚdisassemble_cubin   s:    (þ



û  ý


r)   c                       sÂ   e Zd ZdZd'‡ fdd„	Zdd„ Zd(dd„Zd)d	d
„Zdd„ Zd*dd„Z	dd„ Z
dd„ Zd+dd„Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZedd „ ƒZd!d"„ Zd#d$„ Ze‡ fd%d&„ƒZ‡  ZS ),ÚCUDACodeLibraryzÙ
    The CUDACodeLibrary generates PTX, SASS, cubins for multiple different
    compute capabilities. It also loads cubins to multiple devices (via
    get_cufunc), which may be of different compute capabilities.
    Nc                    s^   t ƒ  ||¡ d| _tƒ | _tƒ | _i | _i | _i | _i | _	|| _
|du rNi }|| _|| _dS )aŸ  
        codegen:
            Codegen object.
        name:
            Name of the function in the source.
        entry_name:
            Name of the kernel function in the binary, if this is a global
            kernel and not a device function.
        max_registers:
            The maximum register usage to aim for when linking.
        nvvm_options:
                Dict of options to pass to NVVM.
        N)ÚsuperÚ__init__Ú_moduleÚsetÚ_linking_librariesÚ_linking_filesÚ
_ptx_cacheÚ_cubin_cacheÚ_linkerinfo_cacheÚ_cufunc_cacheÚ_max_registersÚ_nvvm_optionsÚ_entry_name)ÚselfÚcodegenÚnameÚ
entry_nameÚmax_registersÚnvvm_options©Ú	__class__r'   r(   r,   8   s    zCUDACodeLibrary.__init__c                 C   s
   t | jƒS ©N)Ústrr-   ©r8   r'   r'   r(   Úget_llvm_strc   s    zCUDACodeLibrary.get_llvm_strc                 C   s   |   | j|d�¡S ©N©Úcc)Ú_join_ptxesÚ
_get_ptxes©r8   rF   r'   r'   r(   Úget_asm_strf   s    zCUDACodeLibrary.get_asm_strc           	         s"  |st  ¡ }|j}|j}| j |d ¡}|r.|S tj|Ž }| j 	¡ ‰ |ˆ d< t 
¡ jsŽ| jD ]*}|j d¡rZd|j› d�}tt|ƒƒ qZdˆ d< dd„ | jD ƒ}ˆ  dd¡r¾‡ fdd„|D ƒ}ntj|fi ˆ ¤Žg}d	d„ |D ƒ}tj�rtd
| j  dd¡ƒ t|  |¡ƒ tdƒ || j|< |S )NÚarchÚdebugzMdebuginfo is not generated for CUDA versions < 11.2 (debug=True on function: ú)Fc                 S   s   g | ]}t |ƒ‘qS r'   )rA   )Ú.0Úmodr'   r'   r(   Ú
<listcomp>‚   ó    z.CUDACodeLibrary._get_ptxes.<locals>.<listcomp>c                    s   g | ]}t j|fi ˆ ¤Ž‘qS r'   )r   Úllvm_to_ptx)rN   r   ©Úoptionsr'   r(   rP   ‰   rQ   c                 S   s   g | ]}|  ¡  d ¡ ¡ ‘qS )ú )r   Ústrip)rN   Úxr'   r'   r(   rP   ’   rQ   zASSEMBLY %séP   ú-zP================================================================================)r   Úget_contextÚdeviceÚcompute_capabilityr1   Úgetr   Zget_arch_optionr6   ÚcopyZNVVMZ	is_nvvm70Úlinking_librariesr:   r   r	   ÚmodulesrR   r   ZDUMP_ASSEMBLYÚprintÚ_nameÚcenterrG   )	r8   rF   Úctxr[   ÚptxesrK   Úlibr&   Zirsr'   rS   r(   rH   i   s:    



ÿ
zCUDACodeLibrary._get_ptxesc                 C   s
   d  |¡S )Nz

)Újoin)r8   re   r'   r'   r(   rG   �   s    zCUDACodeLibrary._join_ptxesc                 C   sÌ   |d u rt  ¡ }|j}|j}| j |d ¡}|r2|S tj| j|d�}| j	|d�}|D ]}| 
| ¡ ¡ qR| jD ]}| |¡ ql| ¡ \}	}
t |	t tj¡¡}ttjj||
fd�ƒ}|| j|< |j| j|< |S )N)r<   rF   rE   )Úshape)r   rZ   r[   r\   r2   r]   r   ZLinkerr5   rH   Zadd_ptxÚencoder0   Zadd_file_guess_extZcompleteÚctypesÚcastÚPOINTERÚc_charÚbytesÚnpZ	ctypeslibZas_arrayZinfo_logr3   )r8   rF   rd   r[   r    Zlinkerre   ZptxÚpathZ	cubin_bufÚsizeZ	cubin_ptrr'   r'   r(   Ú	get_cubin    s&    

zCUDACodeLibrary.get_cubinc                 C   sp   | j d u rd}t|ƒ‚t ¡ }|j}| j |jd ¡}|r<|S | j|j	d�}| 
|¡}| | j ¡}|| j|j< |S )NzLMissing entry_name - are you trying to get the cufunc for a device function?rE   )r7   r   r   rZ   r[   r4   r]   Úidrr   r\   Zcreate_module_imageÚget_function)r8   r&   rd   r[   Zcufuncr    Úmoduler'   r'   r(   Ú
get_cufunc¼   s    

zCUDACodeLibrary.get_cufuncc                 C   s2   z| j | W S  ty,   td|› �ƒ‚Y n0 d S )NzNo linkerinfo for CC )r3   ÚKeyErrorrI   r'   r'   r(   Úget_linkerinfoÔ   s    zCUDACodeLibrary.get_linkerinfoc                 C   s   t | j|d�ƒS rD   )r)   rr   rI   r'   r'   r(   Úget_sassÚ   s    zCUDACodeLibrary.get_sassc                 C   s$   |   ¡  | jd urtdƒ‚|| _d S )Nz(CUDACodeLibrary only supports one module)Ú_raise_if_finalizedr-   r   )r8   rO   r'   r'   r(   Úadd_ir_moduleÝ   s    
zCUDACodeLibrary.add_ir_modulec                 C   s    |  ¡  |  ¡  | j |¡ d S r@   )Z_ensure_finalizedrz   r/   Úadd)r8   Úlibraryr'   r'   r(   Úadd_linking_libraryã   s    z#CUDACodeLibrary.add_linking_libraryc                 C   s   | j  |¡ d S r@   )r0   r|   )r8   Úfilepathr'   r'   r(   Úadd_linking_fileí   s    z CUDACodeLibrary.add_linking_filec                 C   s4   | j jD ]}|j|kr|  S qtd|› d�ƒ‚d S )Nz	Function z
 not found)r-   Ú	functionsr:   rw   )r8   r:   Úfnr'   r'   r(   rt   ð   s    

zCUDACodeLibrary.get_functionc                 C   s   | j gdd„ | jD ƒ S )Nc                 S   s   g | ]}|j D ]}|‘qqS r'   )r`   )rN   rf   rO   r'   r'   r(   rP   ø   s   
ÿz+CUDACodeLibrary.modules.<locals>.<listcomp>)r-   r/   rB   r'   r'   r(   r`   ö   s    zCUDACodeLibrary.modulesc                 C   s*   g }| j D ]}| |j¡ | |¡ q
|S r@   )r/   Úextendr_   Úappend)r8   Zlibsrf   r'   r'   r(   r_   û   s
    
z!CUDACodeLibrary.linking_librariesc                 C   sX   |   ¡  | jD ]>}|jD ]2}|jD ]&}|js"| j dd¡rBd|_q"d|_q"qqd| _d S )NrL   FZweak_odrZlinkonce_odrT)	rz   r/   r`   r�   Zis_declarationr6   r]   ÚlinkageZ
_finalized)r8   r}   rO   r‚   r'   r'   r(   Úfinalize  s    


zCUDACodeLibrary.finalizec                 C   sB   | j rd}t|ƒ‚t| j| j| j| j| j| j| j	| j
| j| jd�
S )zÀ
        Reduce the instance for serialization. We retain the PTX and cubins,
        but loaded functions are discarded. They are recreated when needed
        after deserialization.
        zPcannot pickle CUDACodeLibrary function with additional libraries to link against)
r9   r:   r;   ru   r_   Ú	ptx_cacheÚcubin_cacheÚlinkerinfo_cacher<   r=   )r0   r   ÚdictZ_codegenr:   r7   r-   r/   r1   r2   r3   r5   r6   )r8   r&   r'   r'   r(   Ú_reduce_states'  s    özCUDACodeLibrary._reduce_statesc                    s^   |   | ¡}t| |ƒ ||¡ ||_||_||_tƒ |_||_||_	||_
i |_|	|_|
|_dS )z&
        Rebuild an instance.
        N)Ú__new__r+   r,   r7   r-   r/   r.   r0   r1   r2   r3   r4   r5   r6   )Úclsr9   r:   r;   ru   r_   r‡   rˆ   r‰   r<   r=   Úinstancer>   r'   r(   Ú_rebuild>  s    
zCUDACodeLibrary._rebuild)NNN)N)N)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   rC   rJ   rH   rG   rr   rv   rx   ry   r{   r~   r€   rt   Úpropertyr`   r_   r†   r‹   Úclassmethodr�   Ú__classcell__r'   r'   r>   r(   r*   1   s0     ÿ+

4





!r*   c                   @   s,   e Zd ZdZeZdd„ Zdd„ Zdd„ ZdS )	ÚJITCUDACodegenz™
    This codegen implementation for CUDA only generates optimized LLVM IR.
    Generation of PTX code is done separately (see numba.cuda.compiler).
    c                 C   s   t j| _t | j¡| _d S r@   )r   Zdefault_data_layoutÚ_data_layoutÚllZcreate_target_dataZ_target_data)r8   Úmodule_namer'   r'   r(   r,   ^  s    zJITCUDACodegen.__init__c                 C   s,   t  |¡}t|_| jr| j|_t |¡ |S r@   )r   ÚModuleÚCUDA_TRIPLEZtripler˜   Zdata_layoutr   Zadd_ir_version)r8   r:   Z	ir_moduler'   r'   r(   Ú_create_empty_moduleb  s    

z#JITCUDACodegen._create_empty_modulec                 C   s   d S r@   r'   )r8   ru   r'   r'   r(   Ú_add_modulej  s    zJITCUDACodegen._add_moduleN)	r�   r‘   r’   r“   r*   Z_library_classr,   r�   rž   r'   r'   r'   r(   r—   V  s
   r—   )Zllvmliter   r™   r   Úwarningsr   Z
numba.corer   r   Znumba.core.codegenr   r   Znumba.core.errorsr	   Zcudadrvr   r   r   rj   Únumpyro   r   r   r   rœ   r)   ZReduceMixinr*   r—   r'   r'   r'   r(   Ú<module>   s"     '