o
    iU                     @   s  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mZm	Z	m
Z
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 d dl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! d dl"m#Z# d dl$m%Z% d dl&m'Z'm(Z( d dl)m
Z* d dl+m,Z, d dl+m-Z- d dl.m/Z/ g dZ0G dd dej1Z2G dd de3Z4G dd dZ5G dd deZ6G dd deZ7G dd deej1Z8dS )     N)config	serializesigutilstypestypingutils)Cache	CacheImpl)global_compiler_lock)
Dispatcher)NumbaPerformanceWarning)Purposetypeof)get_current_device)wrap_arg)compile_cudaCUDACompiler)driver)get_context)cuda_target)missing_launch_config_msgnormalize_kernel_dimensions)r   cuda)_dispatcher)warn)ZhsinZhcosZhlogZhlog10Zhlog2ZhexpZhexp10Zhexp2ZhsqrtZhrsqrtZhfloorZhceilZhrcpZhrintZhtruncZhdivc                       s  e Zd ZdZe			d2 fdd	Zedd Zed	d
 Zdd Z	e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edd Zedd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd3d)d*Zd4d,d-Zd5d.d/Zd0d1 Z  ZS )6_Kernelz
    CUDA Kernel specialized for a given set of argument types. When called, this
    object launches the kernel on the device.
    NFTc                    s  |rt dt   d| _d | _|| _|| _|| _|| _|p g | _	||
r'dndd}t
 j}t| jtj| j| j|||||d	}|j}| jj}|j}|j}||j|j||||||	\ }|sag }d  v | _| jrnd _ fd	d
tD }|rtjtjt}tj|d}| | |D ]} !| q|j"| _#|j$| _$|j%| _& | _'|j(| _(|| _|j| _|j)| _)g | _*g | _+g | _,d S )Nz,Cannot compile a device function as a kernelF   r   )fastmathoptdebuglineinfoinliner   nvvm_optionsccZcudaCGGetIntrinsicHandleTc                    s"   g | ]}d |    v r|qS )Z__numba_wrapper_)get_asm_str).0fnlib d/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/numba/cuda/dispatcher.py
<listcomp>l   s    z$_Kernel.__init__.<locals>.<listcomp>zcpp_function_wrappers.cu)-RuntimeErrorsuper__init__Z
objectmodeentry_pointpy_funcargtypesr!   r"   
extensionsr   compute_capabilityr   r   voidtarget_context__code__co_filenameco_firstlinenoZprepare_cuda_kernellibraryfndescr&   cooperativeZneeds_cudadevrtcuda_fp16_math_funcsospathdirnameabspath__file__joinappendZadd_linking_filename
entry_name	signaturetype_annotation_type_annotation_codelibrarycall_helperenvironment_referenced_environmentsZliftedZreload_init)selfr2   r3   linkr!   r"   r#   r   r4   Zmax_registersr   devicer$   r%   cresZtgt_ctxcodefilenameZlinenumkernelresZbasedirZfunctions_cu_pathfilepath	__class__r)   r,   r0   .   sp   




z_Kernel.__init__c                 C      | j S N)rK   rO   r+   r+   r,   r;         z_Kernel.libraryc                 C   rZ   r[   )rJ   r\   r+   r+   r,   rI      r]   z_Kernel.type_annotationc                 C   rZ   r[   )rN   r\   r+   r+   r,   _find_referenced_environments   s   z%_Kernel._find_referenced_environmentsc                 C   
   | j  S r[   )r7   codegenr\   r+   r+   r,   r`         
z_Kernel.codegenc                 C   s   t | jjS r[   )tuplerH   argsr\   r+   r+   r,   argument_types   s   z_Kernel.argument_typesc	           
         sX   |  | }	t| |	  d|	_||	_||	_||	_d|	_||	_||	_	||	_
||	_||	_|	S )&
        Rebuild an instance.
        N)__new__r/   r0   r1   r=   rG   rH   rJ   rK   r!   r"   rL   r4   )
clsr=   rF   rH   codelibraryr!   r"   rL   r4   instancerX   r+   r,   _rebuild   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=   rF   rH   rh   r!   r"   rL   r4   )	dictr=   rG   rH   rK   r!   r"   rL   r4   r\   r+   r+   r,   _reduce_states   s
   
z_Kernel._reduce_statesc                 C   s   | j   dS )z7
        Force binding to current CUDA context
        N)rK   
get_cufuncr\   r+   r+   r,   bind      z_Kernel.bindc                 C      | j  jjS )zN
        The number of registers used by each thread for this kernel.
        )rK   rm   attrsregsr\   r+   r+   r,   regs_per_thread      z_Kernel.regs_per_threadc                 C   rp   )zD
        The amount of constant memory used by this kernel.
        )rK   rm   rq   constr\   r+   r+   r,   const_mem_size   rt   z_Kernel.const_mem_sizec                 C   rp   )zM
        The amount of shared memory used per block for this kernel.
        )rK   rm   rq   Zsharedr\   r+   r+   r,   shared_mem_per_block   rt   z_Kernel.shared_mem_per_blockc                 C   rp   )z:
        The maximum allowable threads per block.
        )rK   rm   rq   Z
maxthreadsr\   r+   r+   r,   max_threads_per_block   rt   z_Kernel.max_threads_per_blockc                 C   rp   )zM
        The amount of local memory used per thread for this kernel.
        )rK   rm   rq   localr\   r+   r+   r,   local_mem_per_thread   rt   z_Kernel.local_mem_per_threadc                 C   r_   )z6
        Returns the LLVM IR for this kernel.
        )rK   get_llvm_strr\   r+   r+   r,   inspect_llvm   s   
z_Kernel.inspect_llvmc                 C   s   | j j|dS )z7
        Returns the PTX code for this kernel.
        r%   )rK   r&   )rO   r%   r+   r+   r,   inspect_asm   ro   z_Kernel.inspect_asmc                 C   r_   )zv
        Returns the CFG of the SASS for this kernel.

        Requires nvdisasm to be available on the PATH.
        )rK   Zget_sass_cfgr\   r+   r+   r,   inspect_sass_cfg      
z_Kernel.inspect_sass_cfgc                 C   r_   )zp
        Returns the SASS code for this kernel.

        Requires nvdisasm to be available on the PATH.
        )rK   Zget_sassr\   r+   r+   r,   inspect_sass   r   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================================================================================)rJ   
ValueErrorsysstdoutprintrG   rd   )rO   r   r+   r+   r,   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+   )xyr+   r+   r,   <lambda>&  s    z5_Kernel.max_cooperative_grid_blocks.<locals>.<lambda>)
r   rK   rm   
isinstancerb   	functoolsreduceZ$get_active_blocks_per_multiprocessorrQ   ZMULTIPROCESSOR_COUNT)rO   blockdimZdynsmemsizectxcufuncZactive_per_smZsm_countr+   r+   r,   max_cooperative_grid_blocks  s   

z#_Kernel.max_cooperative_grid_blocksc                    s  | j   | jr* jd } j|\}}|ttjksJ t }	|j	d|d g }
g }t
| j|D ]\}}| ||||
| q4tjrLtjd}nd }|rS|jpT|}tj jg|||||R d| ji | jrtt|	|| |	jdkr݇ fddfddd	D }fd
dd	D }|	j}| j|\}}}|d u rd}n|\}}}tj|}d|||f }d|||f }|rd||d f f|dd   }|| |f}|| |
D ]}|  qd S )NZ__errcode__r   )streamr=   c                    s<    j d j| f \}}t }tt||| |jS )Nz%s__%s__)	moduleget_global_symbolrF   ctypesc_intr   device_to_host	addressofvalue)rF   Zmemszval)r   r+   r,   load_symbolS  s   
z#_Kernel.launch.<locals>.load_symbolc                       g | ]} d | qS )tidr+   r'   ir   r+   r,   r-   [      z"_Kernel.launch.<locals>.<listcomp>Zzyxc                    r   )ctaidr+   r   r   r+   r,   r-   \  r    z"In function %r, file %s, line %s, z%stid=%s ctaid=%sz%s: %s   )rK   rm   r!   rF   r   r   r   Zsizeofr   Zmemsetziprd   _prepare_argsr   USE_NV_BINDINGZbindingZCUstreamhandleZlaunch_kernelr=   r   r   r   rL   Zget_exceptionr?   r@   rB   )rO   rc   griddimr   r   	sharedmemexcnameZexcmemZexcszZexcvalretr
kernelargstvZzero_streamZstream_handler   r   rS   ZexcclsZexc_argslocZlocinfosymrW   linenoprefixwbr+   )r   r   r,   launch-  sn   





z_Kernel.launchc                 C   sX  t | jD ]}|j||||d\}}qt|tjrt|||}tj	}t
d}	t
d}
||j}||jj}t|}tjrEt|}t
|}||	 ||
 || || || t|jD ]}|||j|  qht|jD ]}|||j|  qzdS t|tjrttd| |}|| dS |tjkrtt|tj}|| dS |tjkrt|}|| dS |tj krt!|}|| dS |tj"krt#t|}|| dS |tj$kr|t!|j% |t!|j& dS |tj'kr |t|j% |t|j& dS t|tj(tj)fr8|t*|tj+ dS t|tj,r\t|||}|j-}tjrUt
t|}|| dS t|tj.rt/|t/|ksnJ t0||D ]\}}| 1||||| qsdS t|tj2rz| 1|j|j3||| W dS  t4y   t4||w t4||)zF
        Convert arguments to ctypes and append to kernelargs
        )r   r   r   zc_%sN)5reversedr4   Zprepare_argsr   r   ZArrayr   Z	to_devicer   Z	c_ssize_tZc_void_psizeZdtypeitemsizer   Zdevice_pointerr   intrE   rangendimshapestridesIntegergetattrZfloat16Zc_uint16npviewZuint16Zfloat64Zc_doubleZfloat32Zc_floatbooleanZc_uint8Z	complex64realimagZ
complex128Z
NPDatetimeZNPTimedeltaZc_int64Zint64ZRecordZdevice_ctypes_pointerZ	BaseTuplelenr   r   Z
EnumMemberr   NotImplementedError)rO   tyr   r   r   r   	extensionZdevaryZc_intpZmeminfoparentZnitemsr   ZptrdataaxcvalZdevrecr   r   r+   r+   r,   r   u  s   


















z_Kernel._prepare_args)	NFFFFNNTFr[   )r   r   r   )__name__
__module____qualname____doc__r
   r0   propertyr;   rI   r^   r`   rd   classmethodrj   rl   rn   rs   rv   rw   rx   rz   r|   r~   r   r   r   r   r   r   __classcell__r+   r+   rX   r,   r   (   sL    \











Hr   c                   @   $   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)r   
dispatcherntasksthread_per_blockr   r   )rO   r   r   tpbr   r   r+   r+   r,   r0     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   r   specialized
specialize_compute_thread_per_blockr   r   )rO   rc   r   r   r   r+   r+   r,   __call__  s   


zForAll.__call__c                 C   sZ   | j }|dkr	|S t }tt|j }t|j d| j	dd}|j
di |\}}|S )Nr   i   )funcZb2d_funcZmemsizeZblocksizelimitr+   )r   r   nextiter	overloadsvaluesrk   rK   rm   r   Zget_max_potential_block_size)rO   r   r   r   rU   kwargs_r+   r+   r,   r     s   z ForAll._compute_thread_per_blockN)r   r   r   r0   r   r   r+   r+   r+   r,   r     s    
r   c                   @   s   e Zd Zdd Zdd ZdS )_LaunchConfigurationc           	      C   sl   || _ || _|| _|| _|| _tjr2d}|d |d  |d  }||k r4d| d}tt| d S d S d S )N   r   r      z
Grid size zB will likely result in GPU under-utilization due to low occupancy.)	r   r   r   r   r   r   ZCUDA_LOW_OCCUPANCY_WARNINGSr   r   )	rO   r   r   r   r   r   Zmin_grid_sizeZ	grid_sizemsgr+   r+   r,   r0     s   	z_LaunchConfiguration.__init__c                 G   s   | j || j| j| j| jS r[   )r   callr   r   r   r   rO   rc   r+   r+   r,   r     s   z_LaunchConfiguration.__call__N)r   r   r   r0   r   r+   r+   r+   r,   r     s    r   c                   @   r   )CUDACacheImplc                 C   s   |  S r[   )rl   )rO   rU   r+   r+   r,   r      s   zCUDACacheImpl.reducec                 C   s   t jdi |S )Nr+   )r   rj   )rO   r7   payloadr+   r+   r,   rebuild#     zCUDACacheImpl.rebuildc                 C   s   dS )NTr+   )rO   rR   r+   r+   r,   check_cachable&  s   zCUDACacheImpl.check_cachableN)r   r   r   r   r   r   r+   r+   r+   r,   r     s    r   c                   @   s   e Zd ZdZeZdS )	CUDACachezS
    Implements a cache that saves and loads CUDA kernels and compile results.
    N)r   r   r   r   r   Z_impl_classr+   r+   r+   r,   r   1  s    r   c                       sB  e Zd ZdZdZeZef fdd	Ze	dd Z
dd Zejd	d
dAddZdd ZdBd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dCd!d"ZdCd#d$ZdCd%d&ZdCd'd(ZdCd)d*Zd+d, ZdCd-d.Zd/d0 Zd1d2 Z dCd3d4Z!dCd5d6Z"dCd7d8Z#dCd9d:Z$dCd;d<Z%e&d=d> Z'd?d@ Z(  Z)S )DCUDADispatchera  
    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                    s"   t  j|||d d| _i | _d S )N)targetoptionspipeline_classF)r/   r0   _specializedspecializations)rO   r2   r   r   rX   r+   r,   r0   J  s
   
	
zCUDADispatcher.__init__c                 C   s
   t | S r[   )
cuda_typesr   r\   r+   r+   r,   _numba_type_Z  ra   zCUDADispatcher._numba_type_c                 C   s   t | j| _d S r[   )r   r2   _cacher\   r+   r+   r,   enable_caching^  r   zCUDADispatcher.enable_cachingr   )maxsizer   c                 C   s   t ||\}}t| ||||S r[   )r   r   )rO   r   r   r   r   r+   r+   r,   	configurea  s   zCUDADispatcher.configurec                 C   s   t |dvr
td| j| S )N)r   r      z.must specify at least the griddim and blockdim)r   r   r   r   r+   r+   r,   __getitem__f  s   
zCUDADispatcher.__getitem__c                 C   s   t | ||||dS )a3  Returns a 1D-configured dispatcher 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 dispatcher will be
                       launched.
        :param sharedmem: The number of bytes of dynamic shared memory required
                          by the kernel.
        :return: A configured dispatcher, ready to launch on a set of
                 arguments.)r   r   r   )r   )rO   r   r   r   r   r+   r+   r,   forallk  s   zCUDADispatcher.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.
        r4   )r   getr\   r+   r+   r,   r4     s   zCUDADispatcher.extensionsc                 O   s   t tr[   )r   r   )rO   rc   r   r+   r+   r,   r     s   zCUDADispatcher.__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   r   Z
_cuda_callr   )rO   rc   r   r   r   r   rU   r+   r+   r,   r     s   zCUDADispatcher.callc                    s(   |rJ  fdd|D }  t|S )Nc                    s   g | ]}  |qS r+   )typeof_pyvalr'   ar\   r+   r,   r-     s    z4CUDADispatcher._compile_for_args.<locals>.<listcomp>)compilerb   )rO   rc   kwsr3   r+   r\   r,   _compile_for_args  s   z CUDADispatcher._compile_for_argsc                 C   sD   zt |tjW S  ty!   t|r t tj|ddtj Y S  w )NF)sync)r   r   argumentr   r   Zis_cuda_arrayZas_cuda_array)rO   r   r+   r+   r,   r    s   
zCUDADispatcher.typeof_pyvalc                    s   t  j}t fdd|D } jrtd j||f}|r"|S  j}t j	|d}|
| |  d|_| j||f< |S )zd
        Create a new instance of this dispatcher specialized for the given
        *args*.
        c                    s   g | ]} j |qS r+   )Z	typingctxZresolve_argument_typer  r\   r+   r,   r-     r   z-CUDADispatcher.specialize.<locals>.<listcomp>zDispatcher already specialized)r   T)r   r5   rb   r   r.   r   r  r   r   r2   r  Zdisable_compiler   )rO   rc   r%   r3   Zspecializationr   r+   r\   r,   r     s$   
zCUDADispatcher.specializec                 C   rZ   )z>
        True if the Dispatcher has been specialized.
        )r   r\   r+   r+   r,   r     s   zCUDADispatcher.specializedNc                 C   D   |dur| j |j jS | jrtt| j  jS dd | j  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      i | ]\}}||j qS r+   )rs   r'   sigoverloadr+   r+   r,   
<dictcomp>      z6CUDADispatcher.get_regs_per_thread.<locals>.<dictcomp>)r   rc   rs   r   r   r   r   itemsrO   rH   r+   r+   r,   get_regs_per_thread     z"CUDADispatcher.get_regs_per_threadc                 C   r  )a  
        Returns the size in bytes of constant memory used by this kernel for
        the device in the current context.

        :param signature: The signature of the compiled kernel to get constant
                          memory usage for. This may be omitted for a
                          specialized kernel.
        :return: The size in bytes of constant memory allocated by the
                 compiled variant of the kernel for the given signature and
                 current device.
        Nc                 S   r  r+   )rv   r  r+   r+   r,   r    r  z5CUDADispatcher.get_const_mem_size.<locals>.<dictcomp>)r   rc   rv   r   r   r   r   r  r  r+   r+   r,   get_const_mem_size  s   z!CUDADispatcher.get_const_mem_sizec                 C   r  )a  
        Returns the size in bytes of statically allocated shared memory
        for this kernel.

        :param signature: The signature of the compiled kernel to get shared
                          memory usage for. This may be omitted for a
                          specialized kernel.
        :return: The amount of shared memory allocated by the compiled variant
                 of the kernel for the given signature and current device.
        Nc                 S   r  r+   )rw   r  r+   r+   r,   r    r  z;CUDADispatcher.get_shared_mem_per_block.<locals>.<dictcomp>)r   rc   rw   r   r   r   r   r  r  r+   r+   r,   get_shared_mem_per_block  r  z'CUDADispatcher.get_shared_mem_per_blockc                 C   r  )a(  
        Returns the maximum allowable number of threads per block
        for this kernel. Exceeding this threshold will result in
        the kernel failing to launch.

        :param signature: The signature of the compiled kernel to get the max
                          threads per block for. This may be omitted for a
                          specialized kernel.
        :return: The maximum allowable threads per block for the compiled
                 variant of the kernel for the given signature and current
                 device.
        Nc                 S   r  r+   )rx   r  r+   r+   r,   r  $  r  z<CUDADispatcher.get_max_threads_per_block.<locals>.<dictcomp>)r   rc   rx   r   r   r   r   r  r  r+   r+   r,   get_max_threads_per_block  s   z(CUDADispatcher.get_max_threads_per_blockc                 C   r  )a  
        Returns the size in bytes of local memory per thread
        for this kernel.

        :param signature: The signature of the compiled kernel to get local
                          memory usage for. This may be omitted for a
                          specialized kernel.
        :return: The amount of local memory allocated by the compiled variant
                 of the kernel for the given signature and current device.
        Nc                 S   r  r+   )rz   r  r+   r+   r,   r  7  r  z;CUDADispatcher.get_local_mem_per_thread.<locals>.<dictcomp>)r   rc   rz   r   r   r   r   r  r  r+   r+   r,   get_local_mem_per_thread'  r  z'CUDADispatcher.get_local_mem_per_threadc                 C   sP   | j r
| 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 resolution of the return type.

        A (template, pysig, args, kws) tuple is returned.
        zCallTemplate({0}))keyZ
signatures)_can_compilecompile_devicerb   r2   r   formatr   Zmake_concrete_templateZnopython_signaturesr   Zpysignature)rO   rc   r  	func_namerF   Zcall_templateZpysigr+   r+   r,   get_call_template:  s   
z CUDADispatcher.get_call_templatec           
      C   s   || j vrc| jQ | jd}| jd}| jd}| jd}| jdr)dnd|d}t j}t| j||||||||d		}	|	| j |< |	j	|	j
|	j|	jg W d
   |	S 1 s\w   Y  |	S | j | }	|	S )zCompile the device function for the given argument types.

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

        Returns the `CompileResult`.
        r!   r"   r#   r   r   r   r   )r   r   r    N)r   Z_compiling_counterr   r  r   r5   r   r2   r7   Zinsert_user_functionr1   r<   r;   )
rO   rc   return_typer!   r"   r#   r   r$   r%   rR   r+   r+   r,   r  U  s:   





zCUDADispatcher.compile_devicec                 C   s,   dd |D }| j ||dd || j|< d S )Nc                 S   s   g | ]}|j qS r+   )_coder  r+   r+   r,   r-   }  s    z/CUDADispatcher.add_overload.<locals>.<listcomp>Tr   )Z_insertr   )rO   rU   r3   Zc_sigr+   r+   r,   add_overload|  s   zCUDADispatcher.add_overloadc                 C   s   t |\}}|du s|tjksJ | jrtt| j S | j	|}|dur*|S | j
|| j}|dur@| j|  d7  < n&| j|  d7  < | jsPtdt| j|fi | j}|  | j
|| | || |S )z
        Compile and bind to the current context a version of this kernel
        specialized for the given signature.
        Nr   zCompilation disabled)r   Znormalize_signaturer   noner   r   r   r   r   r  r   Zload_overloadZ	targetctxZ_cache_hitsZ_cache_missesr  r.   r   r2   r   rn   Zsave_overloadr#  )rO   r  r3   r!  rU   r+   r+   r,   r    s$   zCUDADispatcher.compilec                 C   sb   | j d}|dur|r| j| j S | j|  S |r'dd | j D S dd | j 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.

        rQ   Nc                 S   s   i | ]
\}}||j  qS r+   )r;   r{   r  r+   r+   r,   r        z/CUDADispatcher.inspect_llvm.<locals>.<dictcomp>c                 S      i | ]	\}}||  qS r+   )r|   r  r+   r+   r,   r        )r   r  r   r;   r{   r|   r  rO   rH   rQ   r+   r+   r,   r|     s   	zCUDADispatcher.inspect_llvmc                    sv   t  j | jd}|dur!|r| j| j S | j|  S |r/ fdd| j D S  fdd| j 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.
        rQ   Nc                    s   i | ]\}}||j  qS r+   )r;   r&   r  r}   r+   r,   r    s    z.CUDADispatcher.inspect_asm.<locals>.<dictcomp>c                    s   i | ]
\}}||  qS r+   )r~   r  r}   r+   r,   r    r%  )	r   r5   r   r  r   r;   r&   r~   r  r(  r+   r}   r,   r~     s   	

zCUDADispatcher.inspect_asmc                 C   >   | j dr
td|dur| j|  S dd | j D S )a  
        Return this kernel's CFG for the device in the current context.

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

        The CFG for the device in the current context is returned.

        Requires nvdisasm to be available on the PATH.
        rQ   z'Cannot get the CFG of a device functionNc                 S   r&  r+   )r   r'   r  defnr+   r+   r,   r    r'  z3CUDADispatcher.inspect_sass_cfg.<locals>.<dictcomp>)r   r  r.   r   r   r  r  r+   r+   r,   r     s   zCUDADispatcher.inspect_sass_cfgc                 C   r)  )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.
        rQ   z(Cannot inspect SASS of a device functionNc                 S   r&  r+   )r   r*  r+   r+   r,   r    r'  z/CUDADispatcher.inspect_sass.<locals>.<dictcomp>)r   r  r.   r   r   r  r  r+   r+   r,   r     s   zCUDADispatcher.inspect_sassc                 C   s2   |du rt j}| j D ]
\}}|j|d qdS )r   Nr   )r   r   r   r  r   )rO   r   r   r+  r+   r+   r,   r     s
   zCUDADispatcher.inspect_typesc                 C   s   | ||}|S )re   r+   )rg   r2   r   ri   r+   r+   r,   rj     s   
zCUDADispatcher._rebuildc                 C   s   t | j| jdS )zd
        Reduce the instance for serialization.
        Compiled definitions are discarded.
        )r2   r   )rk   r2   r   r\   r+   r+   r,   rl     s   zCUDADispatcher._reduce_statesr   )r   r   r   r[   )*r   r   r   r   Z
_fold_argsr   Ztargetdescrr   r0   r   r   r   r   	lru_cacher   r  r  r4   r   r   r	  r  r   r   r  r  r  r  r  r   r  r#  r  r|   r~   r   r   r   r   rj   rl   r   r+   r+   rX   r,   r   8  sL    










'
$




r   )9numpyr   r?   r   r   r   Z
numba.corer   r   r   r   r   r   Znumba.core.cachingr   r	   Znumba.core.compiler_lockr
   Znumba.core.dispatcherr   Znumba.core.errorsr   Znumba.core.typing.typeofr   r   Znumba.cuda.apir   Znumba.cuda.argsr   Znumba.cuda.compilerr   r   Znumba.cuda.cudadrvr   Znumba.cuda.cudadrv.devicesr   Znumba.cuda.descriptorr   Znumba.cuda.errorsr   r   Z
numba.cudar   Znumbar   r   warningsr   r>   ZReduceMixinr   objectr   r   r   r   r   r+   r+   r+   r,   <module>   s@        /.