a
    –§»ažª  ã                   @   s¬  d dl Z d dlZd dlZd dl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mZmZmZmZmZmZ d dlmZ d dlmZmZmZmZmZ d dlmZ d dl m!Z!m"Z"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Zddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8 ddl9m:Z: ddl;m<Z< d dl&m=Z= ddl>m?Z? dd„ Z@G dd„ deƒZAe#ddd�G dd„ de!ƒƒZBe#ddd�G d d!„ d!e!ƒƒZCG d"d#„ d#eƒZDed@d$d%„ƒZEddddg ddfd&d'„ZFedAd(d)„ƒZGdBd*d+„ZHG d,d-„ d-ejIƒZJdCd.d/„ZKdDd0d1„ZLd2d3„ ZMG d4d5„ d5ejIƒZNG d6d7„ d7eOƒZPG d8d9„ d9eOƒZQG d:d;„ d;ejIƒZRG d<d=„ d=ƒZSG d>d?„ d?e	jTejIƒZTdS )Eé    N)Ú_dispatcher)ÚAbstractTemplateÚConcreteTemplate)ÚtypesÚtypingÚutilsÚfuncdescÚ	serializeÚconfigÚcompilerÚsigutils)Údefault_type_manager)ÚCompilerBaseÚDefaultPassBuilderÚcompile_resultÚFlagsÚOption)Úglobal_compiler_lock)ÚLoweringPassÚPassManagerÚregister_pass©Ú
OmittedArg)ÚNumbaDeprecationWarning)ÚIRLegalizationÚNativeLowering)ÚPurposeÚtypeof)Úwarné   )Úget_context)Úget_cudalib)Údriver)Úmissing_launch_config_msgÚnormalize_kernel_dimensions©Úget_current_device)Úwrap_arg)ÚNumbaPerformanceWarning©Úcuda_targetc                 C   s"   | d u rd S t | tƒsJ ‚| S d S ©N)Ú
isinstanceÚdict)Úx© r/   úM/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/cuda/compiler.pyÚ_nvvm_options_type$   s    r1   c                   @   s   e Zd Zeeddd�ZdS )Ú	CUDAFlagsNzNVVM options)ÚtypeÚdefaultÚdoc)Ú__name__Ú
__module__Ú__qualname__r   r1   Únvvm_optionsr/   r/   r/   r0   r2   -   s
   ýr2   TF)Zmutates_CFGZanalysis_onlyc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚCUDABackendZcuda_backendc                 C   s   t  | ¡ d S r+   ©r   Ú__init__©Úselfr/   r/   r0   r<   :   s    zCUDABackend.__init__c              
   C   sL   |d }t j|jg|j¢R Ž }t|j|j|jj|j	|j
|j||jd�|_dS )zH
        Back-end: Packages lowering output in a compile result
        Úcr)Útyping_contextÚtarget_contextZtyping_errorÚtype_annotationÚlibraryÚcall_helperÚ	signatureÚfndescT)r   rE   Úreturn_typeÚargsr   Ú	typingctxÚ	targetctxÚstatusZfail_reasonrB   rC   rD   rF   r?   )r>   ÚstateZloweredrE   r/   r/   r0   Úrun_pass=   s    ø
zCUDABackend.run_passN)r6   r7   r8   Ú_namer<   rM   r/   r/   r/   r0   r:   5   s   r:   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚCreateLibraryzå
    Create a CUDACodeLibrary for the NativeLowering pass to populate. The
    NativeLowering pass will create a code library if none exists, but we need
    to set it up with nvvm_options from the flags if they are present.
    Úcreate_libraryc                 C   s   t  | ¡ d S r+   r;   r=   r/   r/   r0   r<   [   s    zCreateLibrary.__init__c                 C   s8   |j  ¡ }|jj}|jj}|j||d�|_|j ¡  dS )N)r9   T)	rJ   ÚcodegenZfunc_idZfunc_qualnameÚflagsr9   rP   rC   Zenable_object_caching)r>   rL   rQ   Únamer9   r/   r/   r0   rM   ^   s    

zCreateLibrary.run_passN)r6   r7   r8   Ú__doc__rN   r<   rM   r/   r/   r/   r0   rO   Q   s   rO   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚCUDACompilerc                 C   sh   t }tdƒ}| | j¡}|j |j¡ | | j¡}|j |j¡ |  | j¡}|j |j¡ | ¡  |gS )NÚcuda)	r   r   Zdefine_untyped_pipelinerL   ZpassesÚextendZdefine_typed_pipelineÚdefine_cuda_lowering_pipelineÚfinalize)r>   ZdpbÚpmZuntyped_passesZtyped_passesZlowering_passesr/   r/   r0   Údefine_pipelinesj   s    zCUDACompiler.define_pipelinesc                 C   sD   t dƒ}| td¡ | td¡ | td¡ | td¡ | ¡  |S )NZcuda_loweringz$ensure IR is legal prior to loweringzcreate libraryznative loweringzcuda backend)r   Zadd_passr   rO   r   r:   rY   )r>   rL   rZ   r/   r/   r0   rX   z   s    ÿz*CUDACompiler.define_cuda_lowering_pipelineN)r6   r7   r8   r[   rX   r/   r/   r/   r0   rU   i   s   rU   c              
   C   sˆ   ddl m} |j}	|j}
tƒ }d|_d|_d|_|s8|r>d|_|rHd|_	|rRd|_
|r\||_tj|	|
| |||i td�}|j}| ¡  |S )Nr   r)   T)rI   rJ   ÚfuncrH   rG   rR   ÚlocalsZpipeline_class)Ú
descriptorr*   r@   rA   r2   Z
no_compileZno_cpython_wrapperZno_cfunc_wrapperZ	debuginfoZforceinlineÚfastmathr9   r   Zcompile_extrarU   rC   rY   )ÚpyfuncrG   rH   ÚdebugÚlineinfoÚinliner_   r9   r*   rI   rJ   rR   ÚcresrC   r/   r/   r0   Úcompile_cuda‰   s6    ù	re   c
           
      C   s   t | |||||||||	d�
S )N)ra   rb   rc   r_   Ú
extensionsÚmax_registersÚopt)Ú_Kernel)
r`   rH   Úlinkra   rb   rc   r_   rf   rg   rh   r/   r/   r0   Úcompile_kernel²   s    þrk   c              	   C   sž   ||||rdnddœ}t | d||||d�}	|	jj}
|r>|	j}nB|	jj}|	j}|	jj}t	|	jj
ƒ}| |	j||	jj||||¡\}}|pˆtj}|j|d�}||
fS )aë  Compile a Python function to PTX for a given set of argument types.

    :param pyfunc: The Python function to compile.
    :param args: A tuple of argument types to compile for.
    :param debug: Whether to include debug info in the generated PTX.
    :type debug: bool
    :param lineinfo: Whether to include a line mapping from the generated PTX
                     to the source code. Usually this is used with optimized
                     code (since debug mode would automatically include this),
                     so we want debug info in the LLVM but only the line
                     mapping in the final PTX.
    :type lineinfo: bool
    :param device: Whether to compile a device function. Defaults to ``False``,
                   to compile global kernel functions.
    :type device: bool
    :param fastmath: Whether to enable fast math flags (ftz=1, prec_sqrt=0,
                     prec_div=, and fma=1)
    :type fastmath: bool
    :param cc: Compute capability to compile for, as a tuple ``(MAJOR, MINOR)``.
               Defaults to ``(5, 2)``.
    :type cc: tuple
    :param opt: Enable optimizations. Defaults to ``True``.
    :type opt: bool
    :return: (ptx, resty): The PTX code and inferred return type
    :rtype: tuple
    é   r   ©ra   rb   r_   rh   N)ra   rb   r9   ©Úcc)re   rE   rG   rC   rF   Úllvm_func_namerA   rB   ÚfilenameÚintÚlinenumÚprepare_cuda_kernelrH   r
   ZCUDA_DEFAULT_PTX_CCÚget_asm_str)r`   rH   ra   rb   Údevicer_   ro   rh   r9   rd   ZrestyÚlibÚfnameZtgtrq   rs   ÚkernelÚptxr/   r/   r0   Úcompile_ptxº   s,    
üÿ
þ
r{   c              
   C   s    t ƒ j}t| ||||||dd�S )zÑCompile a Python function to PTX for a given set of argument types for
    the current device's compute capabilility. This calls :func:`compile_ptx`
    with an appropriate ``cc`` value for the current device.T)ra   rb   rv   r_   ro   rh   )r&   Úcompute_capabilityr{   )r`   rH   ra   rb   rv   r_   rh   ro   r/   r/   r0   Úcompile_ptx_for_current_deviceñ   s    
ÿr}   c                   @   sd   e Zd ZdZdd„ Zdd„ Zedd„ ƒZdd	„ Ze	d
d„ ƒZ
dd„ Zdd„ Zdd„ Zi fdd„ZdS )ÚDeviceDispatcherz#Unmaterialized device function
    c                 C   s@   || _ || _|| _|| _i | _t|ddƒ}|› d� |¡| _d S )Nr6   Úunknownz <CUDA device function>)Úpy_funcra   rc   rh   Ú	overloadsÚgetattrÚformatr6   )r>   r`   ra   rc   rh   rS   r/   r/   r0   r<   þ   s    zDeviceDispatcher.__init__c                 C   s   t | j| j| jd�S )N)r€   ra   rc   )r-   r€   ra   rc   r=   r/   r/   r0   Ú_reduce_states  s    zDeviceDispatcher._reduce_statesc                 C   s   t |||d�S )N)ra   rc   )Úcompile_device_dispatcher)Úclsr€   ra   rc   r/   r/   r0   Ú_rebuild
  s    zDeviceDispatcher._rebuildc                 C   sJ   |   t|ƒ¡ | jj}d |¡}tj||| jd�}t 	| j¡}||||fS )zÕ
        Get a typing.ConcreteTemplate for this dispatcher and the given
        *args* and *kws* types.  This allows to resolve the return type.

        A (template, pysig, args, kws) tuple is returned.
        zCallTemplate({0}))ÚkeyZ
signatures)
ÚcompileÚtupler€   r6   rƒ   r   Zmake_concrete_templateÚnopython_signaturesr   Úpysignature)r>   rH   ÚkwsÚ	func_namerS   Zcall_templateÚpysigr/   r/   r0   Úget_call_template  s    	
ÿz"DeviceDispatcher.get_call_templatec                 C   s   dd„ | j  ¡ D ƒS )Nc                 S   s   g | ]
}|j ‘qS r/   ©rE   )Ú.0Úinfor/   r/   r0   Ú
<listcomp>)  ó    z8DeviceDispatcher.nopython_signatures.<locals>.<listcomp>©r�   Úvaluesr=   r/   r/   r0   r‹   %  s    z$DeviceDispatcher.nopython_signaturesc                 C   s   | S r+   r/   )r>   Úsigr/   r/   r0   Úget_overload+  s    zDeviceDispatcher.get_overloadc                 C   sŽ   || j vr€| j| jrdnddœ}t| jd|| j| j|d�}| j  }|| j |< |jg}|rl|j | |j	|¡ qŠ|j 
| |j	|¡ n
| j | }|S )zÇCompile the function for the given argument types.

        Each signature is compiled once by caching the compiled function inside
        this object.

        Returns the `CompileResult`.
        rl   r   )ra   rh   N)ra   rc   r9   )r�   ra   rh   re   r€   rc   rC   rA   Úinsert_user_functionrF   Zadd_user_function)r>   rH   r9   rd   Zfirst_definitionZlibsr/   r/   r0   r‰   2  s"    
þÿ
ÿ
zDeviceDispatcher.compilec                 C   s"   |   |¡jj}d dd„ |D ƒ¡S )zÉReturns the LLVM-IR text compiled for *args*.

        Parameters
        ----------
        args: tuple[Type]
            Argument types.

        Returns
        -------
        llvmir : str
        z

c                 S   s   g | ]}t |ƒ‘qS r/   )Ústr)r’   Úmodr/   r/   r0   r”   _  r•   z1DeviceDispatcher.inspect_llvm.<locals>.<listcomp>)r‰   rC   ÚmodulesÚjoin)r>   rH   r�   r/   r/   r0   Úinspect_llvmR  s    zDeviceDispatcher.inspect_llvmc                 C   s8   d}t |td� |r$d}t |td� |  |¡j ¡  ¡ S )zÛReturns the PTX compiled for *args* for the currently active GPU

        Parameters
        ----------
        args: tuple[Type]
            Argument types.

        Returns
        -------
        ptx : bytes
        zHinspect_ptx for device functions is deprecated. Use compile_ptx instead.)ÚcategoryzJnvvm_options are ignored. Use compile_ptx if you want to set NVVM options.)r   r   r‰   rC   ru   Úencode)r>   rH   r9   Úmsgr/   r/   r0   Úinspect_ptxa  s    zDeviceDispatcher.inspect_ptxN)r6   r7   r8   rT   r<   r„   Úclassmethodr‡   r�   Úpropertyr‹   r™   r‰   rŸ   r£   r/   r/   r/   r0   r~   û   s   	

 r~   c                    sH   ddl m} tˆ|||d�‰ G ‡ ‡fdd„dtƒ}|j}| ˆ |¡ ˆ S )zJCreate a DeviceDispatcher and register it to the CUDA typing context.
    r   r)   )ra   rc   rh   c                       s(   e Zd Z” Z‡ fdd„Z‡fdd„ZdS )z;compile_device_dispatcher.<locals>.device_function_templatec                    s   |rJ ‚ˆ   |¡jS r+   )r‰   rE   )r>   rH   r�   )Ú
dispatcherr/   r0   Úgeneric‚  s    zCcompile_device_dispatcher.<locals>.device_function_template.genericc                    sx   t j t j tj¡¡}t ˆ ¡\}}t ˆ ¡}tt	 
ˆ ¡ƒ}dt| jddƒ|t	j||d�||t|ƒ d fˆ jdœ}|S )NÚoverloadr6   r   )Ústartr   )ÚkindrS   r˜   rq   ÚlinesÚ	docstring)ÚosÚpathÚdirnameÚnumbaÚ__file__ÚinspectÚgetsourcelinesÚgetsourcefiler›   r   rŒ   r‚   rˆ   Zsafe_relpathÚlenrT   )r†   ZbasepathÚcodeZfirstlinenor®   r˜   r“   )r`   r/   r0   Úget_template_info†  s    
úzMcompile_device_dispatcher.<locals>.device_function_template.get_template_infoN)r6   r7   r8   rˆ   r§   r·   r/   ©r¦   r`   r/   r0   Údevice_function_template  s   r¹   )r^   r*   r~   r   r@   rš   )r`   ra   rc   rh   r*   r¹   rI   r/   r¸   r0   r…   x  s    r…   c                 C   s   t | ||dddd�S )NTF)rc   ra   rb   )ÚDeviceFunction)r`   rG   rH   rc   ra   rb   r/   r/   r0   Úcompile_deviceš  s    ÿr»   c                    sv   ddl m} |j}|j}tj|g|¢R Ž ‰t| ˆƒ‰ G ‡ ‡fdd„dtƒ}tj	| ||d�}| 
ˆ |¡ | 
ˆ |¡ ˆ S )Nr   r)   c                       s   e Zd Z” Z”gZdS )z9declare_device_function.<locals>.device_function_templateN)r6   r7   r8   rˆ   Úcasesr/   ©Zextfnr˜   r/   r0   r¹   §  s   r¹   )rS   ÚrestypeÚargtypes)r^   r*   r@   rA   r   rE   ÚExternFunctionr   r   ZExternalFunctionDescriptorrš   )rS   r¾   r¿   r*   rI   rJ   r¹   rF   r/   r½   r0   Údeclare_device_function   s    
ÿrÁ   c                   @   s0   e Zd Zdd„ Zdd„ Zedd„ ƒZdd„ Zd	S )
rº   c                    sˆ   |ˆ_ |ˆ_|ˆ_dˆ_dˆ_dˆ_tˆj ˆjˆjˆjˆjˆjd�‰ ˆ ˆ_G ‡ ‡fdd„dtƒ}ˆ j	 
ˆ|¡ ˆ j 
ˆˆ jˆ jg¡ d S )NTF)ra   rc   rb   c                       s   e Zd Z”Z” jgZdS )z9DeviceFunction.__init__.<locals>.device_function_templateN)r6   r7   r8   rˆ   rE   r¼   r/   ©rd   r>   r/   r0   r¹   À  s   r¹   )r€   rG   rH   rc   ra   rb   re   rd   r   r@   rš   rA   rF   rC   )r>   r`   rG   rH   rc   ra   rb   r¹   r/   rÂ   r0   r<   ´  s$    þÿÿzDeviceFunction.__init__c                 C   s    t | j| j| j| j| j| jd�S )N)r€   rG   rH   rc   ra   rb   )r-   r€   rG   rH   rc   ra   rb   r=   r/   r/   r0   r„   É  s    
þzDeviceFunction._reduce_statesc                 C   s   | ||||||ƒS r+   r/   )r†   r€   rG   rH   rc   ra   rb   r/   r/   r0   r‡   Î  s    zDeviceFunction._rebuildc                 C   s   d}|  | j| jj¡S )Nz*<DeviceFunction py_func={0} signature={1}>)rƒ   r€   rd   rE   )r>   Úfmtr/   r/   r0   Ú__repr__Ò  s    zDeviceFunction.__repr__N)r6   r7   r8   r<   r„   r¤   r‡   rÄ   r/   r/   r/   r0   rº   ²  s
   
rº   c                   @   s   e Zd Zdd„ ZdS )rÀ   c                 C   s   || _ || _d S r+   )rS   r˜   )r>   rS   r˜   r/   r/   r0   r<   Ø  s    zExternFunction.__init__N)r6   r7   r8   r<   r/   r/   r/   r0   rÀ   ×  s   rÀ   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚForAllc                 C   s6   |dk rt d| ƒ‚|| _|| _|| _|| _|| _d S )Nr   z0Can't create ForAll with negative task count: %s)Ú
ValueErrorry   ÚntasksÚthread_per_blockÚstreamÚ	sharedmem)r>   ry   rÇ   ÚtpbrÉ   rÊ   r/   r/   r0   r<   Þ  s    ÿzForAll.__init__c                 G   s^   | j dkrd S | jjr| j}n| jj|Ž }|  |¡}| j | d | }|||| j| jf |Ž S )Nr   r   )rÇ   ry   ÚspecializedÚ
specializeÚ_compute_thread_per_blockrÉ   rÊ   )r>   rH   ry   ÚblockdimÚgriddimr/   r/   r0   Ú__call__è  s    

zForAll.__call__c                 C   s^   | j }|dkr|S tƒ }tt|j ¡ ƒƒ}t|j ¡ d| j	dd�}|j
f i |¤Ž\}}|S d S )Nr   i   )r\   Zb2d_funcZmemsizeZblocksizelimit)rÈ   r    ÚnextÚiterr�   r—   r-   Ú_codelibraryÚ
get_cufuncrÊ   Zget_max_potential_block_size)r>   ry   rË   ÚctxÚdefnÚkwargsÚ_r/   r/   r0   rÎ   õ  s    üz ForAll._compute_thread_per_blockN)r6   r7   r8   r<   rÑ   rÎ   r/   r/   r/   r0   rÅ   Ý  s   
rÅ   c                	       s´   e Zd ZdZed$‡ fdd„	ƒZedd„ ƒZe‡ fd	d
„ƒZ	dd„ Z
dd„ Zedd„ ƒZedd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ Zd%dd„Zd&dd„Zd'd d!„Zd"d#„ Z‡  ZS )(ri   z„
    CUDA Kernel specialized for a given set of argument types. When called, this
    object launches the kernel on the device.
    NFTc              
      s  t ƒ  ¡  || _|| _|| _|| _|p(g | _| j| j||
r>dnddœ}t| jtj	| j| j| j|||d�}|j
j}|jj}|j}| jj}|j}|j}| |j|||||||	¡\}}|s¸g }d| ¡ v | _| jrÞ| tddd�¡ |D ]}| |¡ qâ|j| _|j| _|j| _|| _|j| _d S )	Nrl   r   rm   )ra   rb   rc   r_   r9   ZcudaCGGetIntrinsicHandleZ	cudadevrtT)Zstatic)Úsuperr<   r€   r¿   ra   rb   rf   re   r   ÚvoidrF   rp   rE   rH   rA   Ú__code__Úco_filenameÚco_firstlinenort   rC   ru   ÚcooperativeÚappendr!   Zadd_linking_filerS   Ú
entry_namerB   Ú_type_annotationrÔ   rD   )r>   r€   r¿   rj   ra   rb   rc   r_   rf   rg   rh   r9   rd   rx   rH   Ztgt_ctxr¶   rq   rs   rw   ry   Úfilepath©Ú	__class__r/   r0   r<     sR    


üûýz_Kernel.__init__c                 C   s   t | jjƒS r+   )rŠ   rE   rH   r=   r/   r/   r0   Úargument_typesH  s    z_Kernel.argument_typesc
                    sV   |   | ¡}
t| |
ƒ ¡  ||
_||
_t|ƒ|
_d|
_||
_||
_	||
_
||
_|	|
_|
S )ú&
        Rebuild an instance.
        N)Ú__new__rÚ   r<   rß   rá   rŠ   ræ   râ   rÔ   ra   rb   rD   rf   )r†   rß   rS   r¿   Úcodelibraryrj   ra   rb   rD   rf   Úinstancerä   r/   r0   r‡   L  s    

z_Kernel._rebuildc              
   C   s(   t | j| j| j| j| j| j| j| jd�S )a  
        Reduce the instance for serialization.
        Compiled definitions are serialized in PTX form.
        Type annotation are discarded.
        Thread, block and shared memory configuration are serialized.
        Stream information is discarded.
        )rß   rS   r¿   ré   ra   rb   rD   rf   )	r-   rß   rá   r¿   ré   ra   rb   rD   rf   r=   r/   r/   r0   r„   a  s
    
ýz_Kernel._reduce_statesc                 C   s   | j  ¡  dS )z7
        Force binding to current CUDA context
        N)rÔ   rÕ   r=   r/   r/   r0   Úbindn  s    z_Kernel.bindc                 C   s
   | j  ¡ S )z+
        PTX code for this kernel.
        ©rÔ   ru   r=   r/   r/   r0   rz   t  s    z_Kernel.ptxc                 C   s   t ƒ S )z,
        Get current active context
        r%   r=   r/   r/   r0   rv   {  s    z_Kernel.devicec                 C   s   | j  ¡ jjS )zN
        The number of registers used by each thread for this kernel.
        )rÔ   rÕ   ÚattrsÚregsr=   r/   r/   r0   Úregs_per_thread‚  s    z_Kernel.regs_per_threadc                 C   s
   | j  ¡ S )z6
        Returns the LLVM IR for this kernel.
        )rÔ   Zget_llvm_strr=   r/   r/   r0   rŸ   ‰  s    z_Kernel.inspect_llvmc                 C   s   | j j|d�S )z7
        Returns the PTX code for this kernel.
        rn   rì   )r>   ro   r/   r/   r0   Úinspect_asm�  s    z_Kernel.inspect_asmc                 C   s
   | j  ¡ S )zp
        Returns the SASS code for this kernel.

        Requires nvdisasm to be available on the PATH.
        )rÔ   Zget_sassr=   r/   r/   r0   Úinspect_sass•  s    z_Kernel.inspect_sassc                 C   sb   | j du rtdƒ‚|du r tj}td| j| jf |d� td|d� t| j |d� td|d� dS )úÚ
        Produce a dump of the Python source of this function annotated with the
        corresponding Numba IR and type information. The dump is written to
        *file*, or *sys.stdout* if *file* is *None*.
        Nz Type annotation is not availablez%s %s©ÚfilezP--------------------------------------------------------------------------------zP================================================================================)râ   rÆ   ÚsysÚstdoutÚprintrá   ræ   )r>   rô   r/   r/   r0   Úinspect_types�  s    
z_Kernel.inspect_typesr   c                 C   sH   t ƒ }| j ¡ }t|tƒr*t dd„ |¡}| |||¡}|jj	}|| S )aÕ  
        Calculates the maximum number of blocks that can be launched for this
        kernel in a cooperative grid in the current context, for the given block
        and dynamic shared memory sizes.

        :param blockdim: Block dimensions, either as a scalar for a 1D block, or
                         a tuple for 2D or 3D blocks.
        :param dynsmemsize: Dynamic shared memory size in bytes.
        :return: The maximum number of blocks in the grid.
        c                 S   s   | | S r+   r/   )r.   Úyr/   r/   r0   Ú<lambda>½  r•   z5_Kernel.max_cooperative_grid_blocks.<locals>.<lambda>)
r    rÔ   rÕ   r,   rŠ   Ú	functoolsÚreduceZ$get_active_blocks_per_multiprocessorrv   ÚMULTIPROCESSOR_COUNT)r>   rÏ   ZdynsmemsizerÖ   ÚcufuncZactive_per_smZsm_countr/   r/   r0   Úmax_cooperative_grid_blocks®  s    

þz#_Kernel.max_cooperative_grid_blocksc                    sº  | j  ¡ ‰ | jrTˆ jd }ˆ j |¡\}}|t tj¡ks>J ‚t ¡ }	|j	d|d� g }
g }t
| j|ƒD ]\}}|  ||||
|¡ qh|rŽ|jp�d }tjˆ jg|¢|¢|‘|‘|‘R d| jiŽ | j�r¤t t |	¡||¡ |	jdk�r¤‡ fdd„‰‡fdd„d	D ƒ}‡fd
d„d	D ƒ}|	j}| j |¡\}}}|d u �r>d}n$|\}}}tj |¡}d|||f }d|||f }|�r–d||d f f|dd …  }n|f}||Ž ‚|
D ]}|ƒ  �q¨d S )NZ__errcode__r   )rÉ   rß   c                    s<   ˆ j  dˆ j| f ¡\}}t ¡ }t t |¡||¡ |jS )Nz%s__%s__)	ÚmoduleÚget_global_symbolrS   ÚctypesÚc_intr"   Údevice_to_hostÚ	addressofÚvalue)rS   ZmemÚszÚval)rþ   r/   r0   Úload_symbolå  s    ÿÿ
z#_Kernel.launch.<locals>.load_symbolc                    s   g | ]}ˆ d | ƒ‘qS )Útidr/   ©r’   Úi©r	  r/   r0   r”   í  r•   z"_Kernel.launch.<locals>.<listcomp>Zzyxc                    s   g | ]}ˆ d | ƒ‘qS )Úctaidr/   r  r  r/   r0   r”   î  r•   Ú z"In function %r, file %s, line %s, z%stid=%s ctaid=%sz%s: %sr   )rÔ   rÕ   ra   rS   r   r  r  Úsizeofr  ÚmemsetÚzipræ   Ú_prepare_argsÚhandler"   Zlaunch_kernelrß   r  r  r  rD   Zget_exceptionr­   r®   Úabspath)r>   rH   rÐ   rÏ   rÉ   rÊ   ÚexcnameZexcmemZexcszZexcvalÚretrÚ
kernelargsÚtÚvZstream_handler
  r  r¶   ZexcclsZexc_argsÚlocZlocinfoÚsymrã   ÚlinenoÚprefixÚwbr/   )rþ   r	  r0   ÚlaunchÄ  sd    


ÿþýüûú

þ
ÿz_Kernel.launchc                 C   s¸  t | jƒD ]}|j||||d�\}}q
t|tjƒ�rt|ƒ ||¡}tj	}t 
d¡}	t 
d¡}
||jƒ}||jjƒ}t 
t |¡¡}| |	¡ | |
¡ | |¡ | |¡ | |¡ t|jƒD ]}| ||j| ƒ¡ qÀt|jƒD ]}| ||j| ƒ¡ qä�n²t|tjƒ�r0ttd| ƒ|ƒ}| |¡ �n„|tjk�rTt |¡}| |¡ �n`|tjk�rxt |¡}| |¡ �n<|tjk�r t t|ƒ¡}| |¡ �n|tjk�rÒ| t |j¡¡ | t |j ¡¡ nâ|tj!k�r| t |j¡¡ | t |j ¡¡ n°t|tj"tj#fƒ�r2| t $| %t&j'¡¡¡ n‚t|tj(ƒ�r\t|ƒ ||¡}| |¡ nXt|tj)ƒ�rªt*|ƒt*|ƒk�s€J ‚t+||ƒD ]\}}|  ,|||||¡ �qŠn
t-||ƒ‚dS )zF
        Convert arguments to ctypes and append to kernelargs
        )rÉ   r  r   zc_%sN).Úreversedrf   Zprepare_argsr,   r   ÚArrayr'   Z	to_devicer  Ú	c_ssize_tÚc_void_pÚsizeZdtypeÚitemsizer"   Zdevice_pointerrà   ÚrangeÚndimÚshapeÚstridesZIntegerr‚   Úfloat64Úc_doubleÚfloat32Úc_floatÚbooleanÚc_uint8rr   Z	complex64ÚrealÚimagZ
complex128Z
NPDatetimeZNPTimedeltaÚc_int64ÚviewÚnpÚint64ZRecordZ	BaseTuplerµ   r  r  ÚNotImplementedError)r>   Útyr  rÉ   r  r  Ú	extensionZdevaryZc_intpZmeminfoÚparentZnitemsr&  ÚdataÚaxÚcvalZdevrecr  r  r/   r/   r0   r    sh    ü









z_Kernel._prepare_args)NFFFFNNT)N)r   )r   r   )r6   r7   r8   rT   r   r<   r¥   ræ   r¤   r‡   r„   rë   rz   rv   rï   rŸ   rð   rñ   rø   rÿ   r   r  Ú__classcell__r/   r/   rä   r0   ri   
  s2      þ7






Cri   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú_KernelConfigurationc           
      C   s|   || _ || _|| _|| _|| _tjrxtƒ }|jj	}|d |d  |d  }|d| k rxd}	|	j
|d| d�}	tt|	ƒƒ d S )Nr   r   é   zjGrid size ({grid}) < 2 * SM count ({sm}) will likely result in GPU under utilization due to low occupancy.)ZgridÚsm)r¦   rÐ   rÏ   rÉ   rÊ   r
   ZCUDA_LOW_OCCUPANCY_WARNINGSr    rv   rý   rƒ   r   r(   )
r>   r¦   rÐ   rÏ   rÉ   rÊ   rÖ   ZsmcountZ	grid_sizer¢   r/   r/   r0   r<   Q  s    z_KernelConfiguration.__init__c                 G   s   | j  || j| j| j| j¡S r+   )r¦   ÚcallrÐ   rÏ   rÉ   rÊ   ©r>   rH   r/   r/   r0   rÑ   c  s    ÿz_KernelConfiguration.__call__N)r6   r7   r8   r<   rÑ   r/   r/   r/   r0   r?  P  s   r?  c                   @   sü   e Zd ZdZdZeZdd„ Zd6dd„Zdd	„ Z	d7d
d„Z
edd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdd„ Zd8dd„Zedd „ ƒZd9d"d#„Zd$d%„ Zd:d&d'„Zd;d(d)„Zd<d*d+„Zd=d,d-„Zed.d/„ ƒZd0d1„ Zed2d3„ ƒZd4d5„ Z d!S )>Ú
Dispatchera–  
    CUDA Dispatcher object. When configured and called, the dispatcher will
    specialize itself for the given arguments (if no suitable specialized
    version already exists) & compute capability, and launch on the device
    associated with the current context.

    Dispatcher objects are not to be constructed by the user, but instead are
    created using the :func:`numba.cuda.jit` decorator.
    Fc                 C   s4  || _ g | _| dd¡| _d| _i | _t ¡ | _|| _	t
| j	 dg ¡ƒ| j	d< | jj| _t| _t |¡}t|jƒ}t|jƒ}| j jp†d}tdd„ |D ƒƒ}d}	zt
|j ¡ ƒd }
W n tyÊ   d}Y n0 |
j|
jk}d}tj | | j ¡ || j|||	||¡	 |�r0t|ƒd	k�rt d
ƒ‚|  !|d ¡ d| _d S )Nrj   r/   Trf   c                 s   s   | ]}t |ƒV  qd S r+   r   )r’   r  r/   r/   r0   Ú	<genexpr>”  r•   z&Dispatcher.__init__.<locals>.<genexpr>Féÿÿÿÿr   z'Only one signature supported at presentr   )"r€   ÚsigsÚpoprj   Ú_can_compileÚspecializationsÚcollectionsÚOrderedDictr�   ÚtargetoptionsÚlistÚgetÚtargetdescrr@   rI   r   Z_tmr   rŒ   rµ   Ú
parametersrŠ   Ú__defaults__r—   Ú
IndexErrorrª   ÚVAR_POSITIONALr   rD  r<   Zget_pointerÚ
_fold_argsÚ	TypeErrorr‰   )r>   r€   rG  rM  r�   Ú	arg_countÚargnamesZdefault_valuesZdefargsZcan_fallbackZlastargZhas_starargZexact_match_requiredr/   r/   r0   r<   z  sB    
ÿ




ýzDispatcher.__init__r   c                 C   s   t ||ƒ\}}t| ||||ƒS r+   )r$   r?  )r>   rÐ   rÏ   rÉ   rÊ   r/   r/   r0   Ú	configure«  s    zDispatcher.configurec                 C   s   t |ƒdvrtdƒ‚| j|Ž S )N)r@  rl   é   z.must specify at least the griddim and blockdim)rµ   rÆ   rY  rC  r/   r/   r0   Ú__getitem__¯  s    zDispatcher.__getitem__c                 C   s   t | ||||d�S )a  Returns a 1D-configured kernel for a given number of tasks.

        This assumes that:

        - the kernel maps the Global Thread ID ``cuda.grid(1)`` to tasks on a
          1-1 basis.
        - the kernel checks that the Global Thread ID is upper-bounded by
          ``ntasks``, and does nothing if it is not.

        :param ntasks: The number of tasks.
        :param tpb: The size of a block. An appropriate value is chosen if this
                    parameter is not supplied.
        :param stream: The stream on which the configured kernel will be
                       launched.
        :param sharedmem: The number of bytes of dynamic shared memory required
                          by the kernel.
        :return: A configured kernel, ready to launch on a set of arguments.)rË   rÉ   rÊ   )rÅ   )r>   rÇ   rË   rÉ   rÊ   r/   r/   r0   Úforall´  s    zDispatcher.forallc                 C   s
   | j d S )aS  
        A list of objects that must have a `prepare_args` function. When a
        specialized kernel is called, each argument will be passed through
        to the `prepare_args` (from the last object in this list to the
        first). The arguments to `prepare_args` are:

        - `ty` the numba type of the argument
        - `val` the argument value itself
        - `stream` the CUDA stream used for the current call to the kernel
        - `retr` a list of zero-arg functions that you may want to append
          post-call cleanup work to.

        The `prepare_args` function must return a tuple `(ty, val)`, which
        will be passed in turn to the next right-most `extension`. After all
        the extensions have been called, the resulting `(ty, val)` will be
        passed into Numba's default argument marshalling logic.
        rf   )rM  r=   r/   r/   r0   rf   É  s    zDispatcher.extensionsc                 O   s   t tƒ‚d S r+   )rÆ   r#   )r>   rH   rØ   r/   r/   r0   rÑ   Þ  s    zDispatcher.__call__c                 C   sD   | j rtt| j ¡ ƒƒ}ntjj| g|¢R Ž }| |||||¡ dS )zJ
        Compile if necessary and invoke this kernel with *args*.
        N)	rÌ   rÒ   rÓ   r�   r—   r   rD  Z
_cuda_callr   )r>   rH   rÐ   rÏ   rÉ   rÊ   ry   r/   r/   r0   rB  â  s    zDispatcher.callc                    s(   |rJ ‚‡ fdd„|D ƒ}ˆ   t|ƒ¡S )Nc                    s   g | ]}ˆ   |¡‘qS r/   ©Útypeof_pyval©r’   Úar=   r/   r0   r”   ð  r•   z0Dispatcher._compile_for_args.<locals>.<listcomp>)r‰   rŠ   )r>   rH   r�   r¿   r/   r=   r0   Ú_compile_for_argsí  s    zDispatcher._compile_for_argsc                    sF   |rJ ‚‡ fdd„|D ƒ}d}ˆ j D ]}ˆ j ||j¡}|r$d}q$|S )Nc                    s   g | ]}ˆ   |¡‘qS r/   r]  r_  r=   r/   r0   r”   ö  r•   z6Dispatcher._search_new_conversions.<locals>.<listcomp>FT)r‹   rI   Zinstall_possible_conversionsrH   )r>   rH   r�   Úfoundr˜   Úconvr/   r=   r0   Ú_search_new_conversionsó  s    
z"Dispatcher._search_new_conversionsc                 C   sP   zt |tjƒW S  tyJ   tj |¡rDt tjj|dd�tjƒ Y S ‚ Y n0 d S )NF)Úsync)r   r   ÚargumentrÆ   r°   rV   Zis_cuda_arrayZas_cuda_array©r>   r  r/   r/   r0   r^  þ  s    ÿzDispatcher.typeof_pyvalc                 C   s   dd„ | j  ¡ D ƒS )Nc                 S   s   g | ]
}|j ‘qS r/   r‘   )r’   ry   r/   r/   r0   r”     r•   z2Dispatcher.nopython_signatures.<locals>.<listcomp>r–   r=   r/   r/   r0   r‹     s    zDispatcher.nopython_signaturesc                    s|   t ƒ j}t‡ fdd„|D ƒƒ}ˆ jr,tdƒ‚ˆ j ||f¡}|rD|S ˆ j}ˆ j|d< t	ˆ j
tj|Ž g|ƒ}|ˆ j||f< |S )zd
        Create a new instance of this dispatcher specialized for the given
        *args*.
        c                    s   g | ]}ˆ j  |¡‘qS r/   )rI   Zresolve_argument_typer_  r=   r/   r0   r”     r•   z)Dispatcher.specialize.<locals>.<listcomp>zDispatcher already specializedrj   )r&   r|   rŠ   rÌ   ÚRuntimeErrorrJ  rO  rM  rj   rD  r€   r   rÛ   )r>   rH   ro   r¿   ZspecializationrM  r/   r=   r0   rÍ     s     ÿ
ÿzDispatcher.specializeTc                 C   s   | | _ d S r+   )rI  rg  r/   r/   r0   Údisable_compile'  s    zDispatcher.disable_compilec                 C   s   t | jƒdko| j S )z>
        True if the Dispatcher has been specialized.
        r   )rµ   rG  rI  r=   r/   r/   r0   rÌ   *  s    zDispatcher.specializedNc                 C   sH   |dur| j |j jS | jr0tt| j  ¡ ƒƒjS dd„ | j  ¡ D ƒS dS )aÑ  
        Returns the number of registers used by each thread in this kernel for
        the device in the current context.

        :param signature: The signature of the compiled kernel to get register
                          usage for. This may be omitted for a specialized
                          kernel.
        :return: The number of registers used by the compiled variant of the
                 kernel for the given signature and current device.
        Nc                 S   s   i | ]\}}||j “qS r/   )rï   ©r’   r˜   r¨   r/   r/   r0   Ú
<dictcomp>A  s   ÿz2Dispatcher.get_regs_per_thread.<locals>.<dictcomp>)r�   rH   rï   rÌ   rÒ   rÓ   r—   Úitems©r>   rE   r/   r/   r0   Úget_regs_per_thread1  s    ÿzDispatcher.get_regs_per_threadc                 C   sº   t  |¡\}}|du s$|tjks$J ‚| jr<tt| j ¡ ƒƒS | j 	|¡}|du r¶| j
s^tdƒ‚t| j|fd| ji| j¤Ž}dd„ |D ƒ}| j||dd� || j|< | ¡  | j |¡ |S )z
        Compile and bind to the current context a version of this kernel
        specialized for the given signature.
        NzCompilation disabledrj   c                 S   s   g | ]
}|j ‘qS r/   )Ú_coder_  r/   r/   r0   r”   W  r•   z&Dispatcher.compile.<locals>.<listcomp>T)rV   )r   Znormalize_signaturer   ÚnonerÌ   rÒ   rÓ   r�   r—   rO  rI  rh  ri   r€   rj   rM  Z_insertrë   rG  rà   )r>   r˜   r¿   rG   ry   Zc_sigr/   r/   r0   r‰   D  s"    ÿ
zDispatcher.compilec                 C   s.   |dur| j |  ¡ S dd„ | j  ¡ D ƒS dS )zó
        Return the LLVM IR for this kernel.

        :param signature: A tuple of argument types.
        :return: The LLVM IR for the given signature, or a dict of LLVM IR
                 for all previously-encountered signatures.

        Nc                 S   s   i | ]\}}||  ¡ “qS r/   )rŸ   rj  r/   r/   r0   rk  k  s   ÿz+Dispatcher.inspect_llvm.<locals>.<dictcomp>)r�   rŸ   rl  rm  r/   r/   r0   rŸ   _  s
    	ÿzDispatcher.inspect_llvmc                    s<   t ƒ j‰ |dur | j|  ˆ ¡S ‡ fdd„| j ¡ D ƒS dS )a+  
        Return this kernel's PTX assembly code for for the device in the
        current context.

        :param signature: A tuple of argument types.
        :return: The PTX code for the given signature, or a dict of PTX codes
                 for all previously-encountered signatures.
        Nc                    s   i | ]\}}||  ˆ ¡“qS r/   )rð   rj  rn   r/   r0   rk  {  s   ÿz*Dispatcher.inspect_asm.<locals>.<dictcomp>)r&   r|   r�   rð   rl  rm  r/   rn   r0   rð   n  s    	
ÿzDispatcher.inspect_asmc                 C   s.   |dur| j |  ¡ S dd„ | j  ¡ D ƒS dS )a§  
        Return this kernel's SASS assembly code for for the device in the
        current context.

        :param signature: A tuple of argument types.
        :return: The SASS code for the given signature, or a dict of SASS codes
                 for all previously-encountered signatures.

        SASS for the device in the current context is returned.

        Requires nvdisasm to be available on the PATH.
        Nc                 S   s   i | ]\}}||  ¡ “qS r/   )rñ   )r’   r˜   r×   r/   r/   r0   rk  Ž  s   ÿz+Dispatcher.inspect_sass.<locals>.<dictcomp>)r�   rñ   rl  rm  r/   r/   r0   rñ   ~  s
    ÿzDispatcher.inspect_sassc                 C   s2   |du rt j}| j ¡ D ]\}}|j|d� qdS )rò   Nró   )rõ   rö   r�   rl  rø   )r>   rô   rÙ   r×   r/   r/   r0   rø   ‘  s    zDispatcher.inspect_typesc                 C   s   dd„ | j  ¡ D ƒS )Nc                 S   s   i | ]\}}||j “qS r/   )rz   rj  r/   r/   r0   rk  Ÿ  r•   z"Dispatcher.ptx.<locals>.<dictcomp>)r�   rl  r=   r/   r/   r0   rz   �  s    zDispatcher.ptxc                 C   s   | j  ¡ D ]}| ¡  q
d S r+   )r�   r—   rë   )r>   r×   r/   r/   r0   rë   ¡  s    zDispatcher.bindc                 C   s   | |||ƒ}|S )rç   r/   )r†   r€   rG  rM  rê   r/   r/   r0   r‡   ¥  s    zDispatcher._rebuildc                 C   s   t | j| j| jd�S )zd
        Reduce the instance for serialization.
        Compiled definitions are discarded.
        )r€   rG  rM  )r-   r€   rG  rM  r=   r/   r/   r0   r„   ­  s    
ÿzDispatcher._reduce_states)r   r   )r   r   r   )T)N)N)N)N)N)!r6   r7   r8   rT   rU  r*   rP  r<   rY  r[  r\  r¥   rf   rÑ   rB  ra  rd  r^  r‹   rÍ   ri  rÌ   rn  r‰   rŸ   rð   rñ   rø   rz   rë   r¤   r‡   r„   r/   r/   r/   r0   rD  h  s@   1












rD  )FFFFN)FFFFNT)FFFFT)FFT)TFF)UrK  r  rû   r²   r­   rõ   Únumpyr5  r°   r   Znumba.core.typing.templatesr   r   Z
numba.corer   r   r   r   r	   r
   r   r   Znumba.core.typeconv.rulesr   Znumba.core.compilerr   r   r   r   r   Znumba.core.compiler_lockr   Znumba.core.compiler_machineryr   r   r   Znumba.core.dispatcherr   Znumba.core.errorsr   Znumba.core.typed_passesr   r   Znumba.core.typing.typeofr   r   Úwarningsr   Zcudadrv.devicesr    Zcudadrv.libsr!   Zcudadrvr"   Úerrorsr#   r$   Úapir&   rH   r'   r(   r^   r*   r1   r2   r:   rO   rU   re   rk   r{   r}   ZReduceMixinr~   r…   r»   rÁ   rº   ÚobjectrÀ   rÅ   ri   r?  rD  r/   r/   r/   r0   Ú<module>   s~   (	

   ÿ(þ
  ÿ6  ÿ

}
"  ÿ
%-  H