o
    ôjÕ›  ã                   @   sþ   d dl Z d dlZd dlmZ d dlmZmZm	Z	mZm
Z
mZmZ d dlmZmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlZd dlZd d	lmZ G d
d„ deƒZedd„ ƒZ dd„ Z!G dd„ deƒZ"ddd„Z#dd„ Z$ee#ƒdd„ ƒZ%dS )é    N)Úir)ÚtypesÚtypingÚutilsr   ÚconfigÚir_utilsÚregistry)ÚCallableTemplateÚ	signatureÚinfer_globalÚAbstractTemplate)Úlower_builtin)Úregister_jitable©ÚNumbaValueError)Úliteral_unroll)Únumpy_supportc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚStencilFuncLowererzMCallable class responsible for lowering calls to a specific StencilFunc.
    c                 C   s
   || _ d S ©N)ÚstencilFunc)ÚselfÚsf© r   úi/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/numba/stencils/stencil.pyÚ__init__   s   
zStencilFuncLowerer.__init__c                 C   s:   | j  |ji |jd ¡}| ||j||¡}| |jg¡ |S r   )r   Úcompile_for_argtysÚargsÚreturn_typeZcall_internalÚfndescÚadd_linking_libsÚlibrary)r   ÚcontextÚbuilderÚsigr   ÚcresÚresr   r   r   Ú__call__   s   ÿzStencilFuncLowerer.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r&   r   r   r   r   r      s    r   c                 G   s\   | j }t|ƒD ]$}| j|jkrtdƒ‚|j }tt|ƒƒD ]}|| || kr*tdƒ‚qqd S )Nz\Secondary stencil array does not have same number  of dimensions as the first stencil input.zaSecondary stencil array has some dimension smaller the same dimension in the first stencil input.)Úshaper   ÚndimÚ
ValueErrorÚrangeÚlen)Úar   ÚashapeÚargZargshapeÚir   r   r   Ú!raise_if_incompatible_array_sizes$   s   
ÿÿûr4   c                 C   s   t | j| | j| ƒS )z_ Called by stencil in Python mode to add the loop index to a
        user-specified slice.
    )ÚsliceÚstartÚstop)Ú	the_sliceZaddendr   r   r   Úslice_addition;   s   r9   c                   @   sd   e Zd Z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d„ Zdd„ Zdd„ ZdS )ÚStencilFuncz@
    A special type to hold stencil information for the IR.
    r   c                 C   sx   t | ƒj| _t | ƒ jd7  _|| _|| _|| _g | _tjj	| _
tjj| _|  | j
¡ | j d¡| _i | _t| ƒ| _d S )Né   Úneighborhood)ÚtypeÚ
id_counterÚidÚ	kernel_irÚmodeÚoptionsÚkwsr   Ú
cpu_targetÚtyping_contextÚ
_typingctxÚtarget_contextÚ
_targetctxÚ_install_typeÚgetr<   Ú_type_cacher   Ú	_lower_me)r   r@   rA   rB   r   r   r   r   H   s   

zStencilFunc.__init__c              
   C   s"  g }|  ¡ D ]ˆ\}}|j}|j}g }	|jD ]u}
t|
tjƒr…| |¡ t|ƒdkrEt 	|||¡}t 	||d |¡}|	 t 
|||
j|¡¡ qg }|D ]}t 	|||¡}||g7 }qI| d|¡}tj ||¡}|	 t |||¡¡ t 	|||¡}t 
|||
j|¡}|	 |¡ q|	 |
¡ q|	|_q|S )zß
        Find return statements in the IR and replace them with a SetItem
        call of the value "returned" by the kernel into the result array.
        Returns the block labels that contained return statements.
        r;   r   Ústencil_index)ÚitemsÚscopeÚlocÚbodyÚ
isinstancer   ÚReturnÚappendr/   ÚVarÚSetItemÚvalueÚredefineÚExprÚbuild_tupleÚAssign)r   ÚblocksÚ
index_varsÚout_nameÚ
ret_blocksÚlabelÚblockrO   rP   Únew_bodyÚstmtZrvarZivarZvar_index_varsZone_varÚ	index_varÚs_index_varÚ
tuple_callÚsir   r   r   Úreplace_return_with_setitemX   s2   

z'StencilFunc.replace_return_with_setitemc           *      C   sÊ  i }g }	t jdkrtd||ƒ t |j¡ |du rd}
nd}
t|ƒ|kr.tdt|ƒ|f ƒ‚t |j¡}t	ƒ }|j 
¡ D �]�}|j}|j}g }|jD �]o}t|tjƒrqt|jtjƒrqt jdkritd|jj|jjƒ |jj||jj< t|tjƒrŒt|jtjƒrŒ|jjdv rŒ|jjj|jv s™t|tjƒr�|jj|jv r�td	ƒ‚t|tjƒ�rµt|jtjƒ�rµ|jjd
v �rµ|jjj|jv �rµ|jjj|v�rµ|jjdkrÏ|jj}n|jj}| |jjj¡ |
�rt|dƒsåJ ‚|j|v ró|	||j g7 }	n|j|v �r|	||j g7 }	ntdƒ‚|dk�r¬t ||d |¡}| d|¡}||j }t|tj j!ƒ�r„| d|¡}t" #t$¡}tj% &|¡}|||j< t 'd||¡}| (t |||¡¡ tj )|||gd|¡}| *| j+|tj,gi ¡||< | (t |||¡¡ | (t tj -|jj||¡|j|¡¡ qJtj .t/j|||¡}| (t |||¡¡ | (t tj -|jj||¡|j|¡¡ qJg }g }| d|¡}g }g } ||j }t0|ƒD ]Ì}!| d|¡}| (t t |!|¡||¡¡ ||g7 }t |||! |¡}||g7 }| d|¡}| |g7 } | d|¡}"tj -|||! |¡}#| (t |#|"|¡¡ t|tj1ƒ�r||! }$n|dd… }$t|$tj j!ƒ�rx| d|¡}t" #t$¡}tj% &|¡}|||j< t 'd||¡}| (t |||¡¡ tj )||"||! gd|¡}| *| j+|$tj,gi ¡||< | (t |||¡¡ �qÃtj .t/j|"||! |¡}| (t |||¡¡ �qÃtj 2| |¡}%| (t |%||¡¡ | (t tj -|jj||¡|j|¡¡ qJ| (|¡ qJ||_q<|
�radd„ t0|ƒD ƒ}t|	ƒdk�rÖtdƒ‚|	D ]ˆ}&t|&t3ƒ�sæt|&t4ƒ�r/t0t|&ƒƒD ]=}'|&|' }(t|(tjƒ�r|(j|v �r||(j }(t|(t5ƒ�r&t6||' d |(ƒ||' d< t7||' d |(ƒ||' d< �qìtdƒ‚t|&ƒ})n't|&t5ƒ�rRt6|d d |&ƒ|d d< t7|d d |&ƒ|d d< d})ntdƒ‚|)|k�r_tdƒ‚�qØ||fS )zß
        Transforms the stencil kernel as specified by the user into one
        that includes each dimension's index variable as part of the getitem
        calls.  So, in effect array[-1] becomes array[index0-1].
        r;   Úadd_indices_to_kernelNTFúD%d dimensional neighborhood specified for %d dimensional input arrayzremembering in const_dict)ÚsetitemÚstatic_setitemz?Assignments to arrays passed to stencil kernels is not allowed.)ÚgetitemÚstatic_getitemrm   ÚnamezDstencil kernel index is not constant, 'neighborhood' option requiredr   rM   r9   r   Úconst_indexZind_stencil_indexc                 S   s   g | ]}d d g‘qS )r   r   )Ú.0Ú_r   r   r   Ú
<listcomp>*  ó    z5StencilFunc.add_indices_to_kernel.<locals>.<listcomp>z=Stencil kernel with no accesses to relatively indexed arrays.zCstencil kernel index is not constant,'neighborhood' option requiredz/Non-tuple or non-integer used as stencil index.z2Stencil index does not match array dimensionality.)8r   ÚDEBUG_ARRAY_OPTÚprintr   Údump_blocksr\   r/   r   Zget_tuple_tableÚsetÚvaluesrO   rP   rQ   rR   r   r[   rW   ÚConstÚtargetro   rY   ÚopÚ	arg_namesrV   Úindexrd   ÚaddÚhasattrrU   rX   r   ÚmiscÚ	SliceTypeÚnumbaÚnjitr9   Ú	functionsÚ
DispatcherÚGlobalrT   ÚcallÚget_call_typerF   Úintprm   ÚbinopÚoperatorr.   Ú
ConstSizedrZ   ÚtupleÚlistÚintÚminÚmax)*r   ÚkernelZindex_namesr,   r<   Ústandard_indexedÚtypemapÚ	calltypesZ
const_dictZkernel_constsZneed_to_calc_kernelZtuple_tableÚrelatively_indexedra   rO   rP   rb   rc   Zstmt_index_varrd   ZtmpvarZstmt_index_var_typZsa_varZsa_funcZsa_func_typZg_saZslice_addition_callZacc_callr]   Zsum_resultsre   Zconst_index_varsZind_stencilsÚdimZ
getitemvarZgetitemcallZone_index_typrf   r~   r3   ÚteZ	index_lenr   r   r   ri   ‚   s6  

þÿ

ÿÿ
ÿÿ





þÿþ
ÿ


ÿ


ÿþ
ÿ
ÿ
ÿÿz!StencilFunc.add_indices_to_kernelc                 C   s¦   t jdkrtd|ƒ t | jj¡ t|d tj	j
ƒstdƒ‚ddlm} | | j| j| j|d i ¡\}}}}t|tj	j
ƒr@tdƒ‚tj	 
||d j|d j¡}|||fS )Nr;   Úget_return_typer   zGThe first argument to a stencil kernel must be the primary input array.)Útyped_passesz:Stencil kernel must return a scalar and not a numpy array.)r   ru   rv   r   rw   r@   r\   rR   r   ÚnpytypesÚArrayr   Ú
numba.corer›   Útype_inference_stagerF   rH   r,   Úlayout)r   Úargtysr›   r•   r   r–   rr   Úreal_retr   r   r   rš   K  s,   

úÿÿ
zStencilFunc.get_return_typec                 C   s2   t dt| jƒ tft| | jd�ƒ}| | |¡ dS )zmConstructs and installs a typing class for a StencilFunc object in
        the input typing context.
        ZStencilFuncTyping_)ÚkeyÚgenericN)r=   Ústrr?   r   ÚdictÚ_type_meÚinsert_user_function)r   Ú	typingctxZ_ty_clsr   r   r   rI   d  s   ÿýzStencilFunc._install_typec           
      C   s0   | j | \}}}}| j|||||g|¢R Ž }	|	S r   )rK   Ú_stencil_wrapper)
r   r¡   Úkwtysr   Úsigretrr   Úresultr•   r–   Únew_funcr   r   r   r   n  s   
ÿÿzStencilFunc.compile_for_argtysc                 C   s4  | j durt| j ƒ|d jkrtdt| j ƒ|d jf ƒ‚|}d}d}d|v r6||d f7 }|d7 }|d }d|v rE||d f7 }|d7 }|| jv rU| j| \}}}}|S |  |¡\}}	}
t|g|¢R Ž }d	 d
 | j	j
¡|¡}i }t||ƒ |d }|jt |¡d�}| j | j| |fg¡ |||	|
f| j|< |S )z¡
        Implement AbstractTemplate.generic() for the typing class
        built by StencilFunc._install_type().
        Return the call-site signature.
        Nr   rj   Ú Úoutz
, out=Noner<   z, neighborhood=Nonez*def __numba_dummy_stencil({}{}):
    pass
ú,Z__numba_dummy_stencil)Úpysig)r<   r/   r,   r   rK   rš   r
   ÚformatÚjoinr@   r}   ÚexecÚreplacer   ÚpysignaturerH   Zinsert_func_defnrL   )r   r¡   r«   Zargtys_extraÚ	sig_extrar­   Z_sigrr   r¢   r•   r–   r#   Z
dummy_textÚdctZ
dummy_funcr   r   r   r§   u  s<   
þ
ÿ
zStencilFunc._type_mec           
      C   sŠ   i }|  ¡ }i |_|j ¡ D ]2\}}t  |j| ¡}g |_|j| jD ]}t  |¡}	|j |	¡ ||v r:|| ||	< q#||j|< q||fS )a  
        Create a copy of a given IR along with its calltype information.
        We need a copy of the calltypes because copy propagation applied
        to the copied IR will change the calltypes and make subsequent
        uses of the original IR invalid.
        )Úcopyr\   rN   ÚdeepcopyrQ   rT   )
r   r   r–   Úcopy_calltypesÚkernel_copyZblock_labelra   Ú	new_blockrc   Zscopyr   r   r   Úcopy_ir_with_calltypesž  s   
€z"StencilFunc.copy_ir_with_calltypesc           D   
      s  |   | j|¡\}}t |j¡ |jd }	t |j|¡\}
}t |j¡}t |j|
|||¡ d|v r5t	dƒ‚t 
d|¡}tjdkrFtd||ƒ |d }tjdkratd||jt|jƒ|ƒ t |j¡ dtt|ƒƒ d	d
¡| jf }g }t|jƒD ]}t 
dt|ƒ |¡}||g7 }qwt 
d|¡}t 
d|¡}d}|d ur¢|d |¡7 }dt| jƒv r°|d |¡7 }| j dg ¡}|	|v r¿t	dƒ‚tt|ƒt|jƒ ƒdkrÐt	dƒ‚|  |||j| j|||¡\}}| jd u rç|| _tjdkrötdƒ t |j¡ |   |j||¡}tjdk�rtd|ƒ t |j¡ d |d !|j¡|¡}g }t|jƒD ]-}t"|| d t#ƒ�r:|| d }|| d }nd ||¡}d ||¡}| $||f¡ �q!t|ƒdk�rq|d|	 7 }|D ]}||	k�rk|d| 7 }�q^|d7 }t 
d|¡}|d ||	¡7 }dd„ } |d u �rt% &|j¡jj'}!d |||!¡}"d| jv �r¸| jd }#t(j) )|#¡}$| j* +|$|j¡�s·d }%t	|%ƒ‚nd}#|d!|" 7 }t|jƒD ]G}&d"g|j }'d"g|j }(d# | j|& d ¡|'|&< d$ | j|& d ¡|(|&< |d!d% |d !|'¡| |#ƒ¡ 7 }|d!d% |d !|(¡| |#ƒ¡ 7 }�qÅn.d| jv �r<| jd }#t(j) )|#¡}$| j* +|$|j¡�s.d }%t	|%ƒ‚d& || |#ƒ¡}"|d!|" 7 }d})t|jƒD ](}t|)ƒD ]}*|d!7 }�qI|d' || || d |||| d ¡7 }|)d7 })�qCt|)ƒD ]}*|d!7 }�qp|d( |¡7 }|d) |¡7 }tjdk�r”td*ƒ t|ƒ i }+|+ ,t-ƒ ¡ t.||+ƒ |+| },|d u�r²t/ 0|,¡}-|-|_1dd+l2m3}. |. 4|,¡}/t 5|/j¡ t |/j¡}0i }1||||g|j | }2|0 6¡ D ]!\}3}4|3|2v�rút"|4t7j8ƒ�sìJ ‚|4j9 :|4j;|4j<¡}5|5j;|1|3< �qÚt =|/j|1¡ t>|/j ?¡ ƒd ‰ t @|jˆ ¡|_t>|j ?¡ ƒd }6‡ fd,d-„|D ƒ}tjdk�rFtd.|ˆ ƒ td/ƒ t |/j¡ td0ƒ t |j¡ |/j 6¡ D ]{\}7}8tA|8jBƒD ]o\}}9t"|9t7jCƒ�rÂ|9jDj;|k�rÂ|9j<}:|8j9};t7 E|;|:¡}<|8jBd |… |<_B|8jB|d d … |8_BtF|j ?¡ ƒ}=|< $t7 G|=|:¡¡ |j 6¡ D ]
\}>}?|?|/j|>< �q™|8|/j|6< |<|/j|7< |D ]}@|/j|@  $t7 G|6|:¡¡ �q° n�qT�qK t H|/j¡|/_t 5|/j¡ t"|tIjJƒ�sÝJ ‚|}AtK|Aƒ}Btjdk�rôtd1|Bƒ t |/j¡ t L|/j¡ |. M| j*| jN|/|Bd |.jOi ¡}C|CS )2Nr   r°   z6Cannot use the reserved word 'out' in stencil kernels.Z__sentinel__r;   Úname_var_tablerª   z__numba_stencil_%s_%sú-rr   r~   r<   r¯   z	, {}=NoneÚstandard_indexingzYThe first argument to a stencil kernel must use relative indexing, not standard indexing.z[Standard indexing requested for an array name not present in the stencil kernel definition.zAfter add_indices_to_kernelz!After replace_return_with_setitemzdef {}({}{}):
r±   z	{}[{}][0]z	{}[{}][1]z&    raise_if_incompatible_array_sizes(z)
Z
full_shapez    {} = {}.shape
c                 S   s>   t  | ¡st  | ¡rdS t  | ¡r| dk rdS dS d S t| ƒS )Nznp.nanr   z-np.infznp.inf)ÚnpÚisfiniteÚisnanÚisinfr¥   )Úcvalr   r   r   Úcval_as_strD  s   


üz1StencilFunc._stencil_wrapper.<locals>.cval_as_strz{} = np.empty({}, dtype=np.{})
rÇ   z-cval type does not match stencil return type.z    ú:z:-{}z-{}:z{}[{}] = {}
z{}[:] = {}
z.for {} in range(-min(0,{}),{}[{}]-max(0,{})):
z{} = 0
z    return {}
znew stencil func text©Úcompilerc                    s   g | ]}|ˆ  ‘qS r   r   ©rq   Úx©Zstencil_stub_last_labelr   r   rs   ¸  rt   z0StencilFunc._stencil_wrapper.<locals>.<listcomp>zret_blocks w/ offsetsz"before replace sentinel stencil_irz#before replace sentinel kernel_copyÚnew_stencil_param_types)Pr¿   r@   r   Zremove_argsr\   r}   Zcopy_propagateZget_name_var_tableZapply_copy_propagater   Zget_unused_var_namer   ru   rv   Údtyper=   rw   Úhexr?   r¶   r.   r,   r¥   r³   r¦   rC   rB   rJ   r/   rx   ri   r<   rh   r´   rR   r�   rT   r   Úas_dtyper'   r   ÚtypeofrF   Úcan_convertÚupdateÚglobalsrµ   r   r·   r²   rž   rË   Úrun_frontendZremove_delsrN   r   rU   rO   rX   ro   rP   Zreplace_var_namesr’   ÚkeysZadd_offset_to_labelsÚ	enumeraterQ   r[   r{   ÚBlockr‘   ÚJumpZrename_labelsr   ÚTyper�   Zfixup_var_define_in_scopeZ
compile_irrH   ÚDEFAULT_FLAGS)Dr   r­   r¬   r   r•   r–   r   r½   r¼   Ú	first_argZin_cpsZout_cpsrÀ   Zsentinel_nameZ	the_arrayZstencil_func_namer]   r3   Zindex_var_namer^   Zneighborhood_namer¸   r”   Úkernel_sizer—   r_   Z	func_textZrangesÚloÚhiZother_arrayZ
shape_namerÈ   Zreturn_type_nameZout_initrÇ   Zcval_tyÚmsgr˜   Zstart_itemsZ	end_itemsÚoffsetÚjr¹   Zstencil_funcr²   rË   Z
stencil_irZ	var_tableZnew_var_dictÚreserved_namesro   ÚvarZnew_varZ	new_labelr`   ra   ÚinstrP   rO   Z
prev_blockZbody_first_labelÚlÚbZ	ret_blockÚarray_typesrÏ   r®   r   rÎ   r   rª   ¹  s¸  ÿ
û



ÿþÿÿ
þ

ÿ
ÿ
€
ÿÿÿ
þ $ú



ûÿ



ÿÿÿ

€ÿ



ÿ€
ùzStencilFunc._stencil_wrapperc                 O   s&  | j  ¡  | jd ur"t| jƒ|d jkr"td t| jƒ|d j¡ƒ‚d|v rT|d }|j}t 	|¡}t
j ||jt |¡¡}tdd„ |D ƒƒ}tdd„ |D ƒ|g ƒ}nd }tdd„ |D ƒƒ}|}tjdkrmtd	|||ƒ |  |¡\}	}
}| j|d |	|
|g|¢R Ž }|d u r‹|j|Ž S |j||f Ž S )
Nr   zD{} dimensional neighborhood specified for {} dimensional input arrayr°   c                 S   ó   g | ]}t j |¡‘qS r   ©r   rÓ   rÌ   r   r   r   rs     ó    z(StencilFunc.__call__.<locals>.<listcomp>c                 S   rë   r   rì   rÌ   r   r   r   rs     rí   c                 S   rë   r   rì   rÌ   r   r   r   rs     rí   r;   r&   )rF   Úrefreshr<   r/   r,   r   r³   rÐ   r   Ú
from_dtyper   rœ   r�   Ú
map_layoutrŽ   r   ru   rv   rš   rª   Úentry_point)r   r   Úkwargsr­   ÚrdtypeZrttypeÚresult_typerê   Zarray_types_fullr¢   r•   r–   r®   r   r   r   r&     sB   

ÿÿ
ÿÿ
ÿÿ
zStencilFunc.__call__N)r'   r(   r)   r*   r>   r   rh   ri   rš   rI   r   r§   r¿   rª   r&   r   r   r   r   r:   A   s     * J
)  Kr:   Úconstantc                 K   sX   t | tƒs
d}| }n| }d }|D ]}|dvrtd| ƒ‚qt||ƒ}|d ur*||ƒS |S )Nrõ   )rÇ   rÂ   r<   zUnknown stencil option )rR   r¥   r   Ú_stencil)Zfunc_or_moderB   rA   ÚfuncÚoptionÚwrapperr   r   r   Ústencil$  s   
ÿ
rú   c                    s&   ˆ dkr
t dˆ  ƒ‚‡ ‡fdd„}|S )Nrõ   zUnsupported mode style c                    s"   ddl m} | | ¡}t|ˆ ˆƒS )Nr   rÊ   )rž   rË   r×   r:   )r÷   rË   r@   ©rA   rB   r   r   Ú	decorated:  s   
z_stencil.<locals>.decoratedr   )rA   rB   rü   r   rû   r   rö   6  s   rö   c                 C   s   t  t  tjj¡d¡S )z lowering for dummy stencil callsr   )ÚlirÚConstantÚIntTyper   rŠ   Úbitwidth)r!   r"   r#   r   r   r   r   Ústencil_dummy_lowerA  s   r  )rõ   )&rº   ÚnumpyrÃ   Úllvmliter   rý   rž   r   r   r   r   r   r   Únumba.core.typing.templatesr	   r
   r   r   Znumba.core.imputilsr   Znumba.core.extendingr   Únumba.core.errorsr   Únumba.misc.specialr   rƒ   rŒ   Únumba.npr   Úobjectr   r4   r9   r:   rú   rö   r  r   r   r   r   Ú<module>   s4   $
     
h