U
    |e7                     @   s  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 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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 d dl m!Z! d dl"m#Z#m$Z$ d dl%m&Z&m'Z' d dl(m)Z) eddddddZ*G dd de*eZ+G dd de
Z,e, Z-G dd dej.Z/e/eed < dd  Z0d!d" Z1d#d$ Z2ej3ej4ej5ej6d%Z7d&d' Z8ed(d) Z9G d*d+ d+e:Z;G d,d- d-e;Z<G d.d/ d/e;Z=d0d1 Z>dS )2    N)contextmanager)configtargetconfig)jit)TargetDescriptor)	is_jitted)NumbaDeprecationWarning)TargetOptionsinclude_default_options)
cpu_target)dispatcher_registrytarget_registry)utilstypes	serializecompilersigutils)as_dtype)	_internal)parse_signature)build_ufunc_wrapperbuild_gufunc_wrapper)FunctionCache	NullCache)global_compiler_locknopythonforceobjboundscheckfastmathtarget_backendwritable_argsc                   @   s   e Zd Zdd ZdS )UFuncTargetOptionsc                 C   sf   | dsd|_| ds d|_|jddd | ds@tj|_| dsR|j|_d|_|d d S )	Nenable_pyobjectTenable_loopliftnrt)default	debuginfor   r   )	is_setr"   r#   inherit_if_not_setr   DEBUGINFO_DEFAULTr&   r   enable_pyobject_looplift)selfflagsoptions r.   X/var/www/website-v5/atlas_env/lib/python3.8/site-packages/numba/np/ufunc/ufuncbuilder.pyfinalize$   s    



zUFuncTargetOptions.finalizeN)__name__
__module____qualname__r0   r.   r.   r.   r/   r!   "   s   r!   c                       s8   e Zd ZeZ fddZedd Zedd Z  Z	S )UFuncTargetc                    s   t  d d S )Nufunc)super__init__r+   	__class__r.   r/   r7   ;   s    zUFuncTarget.__init__c                 C   s   t jS N)r   typing_contextr8   r.   r.   r/   r<   >   s    zUFuncTarget.typing_contextc                 C   s   t jS r;   )r   target_contextr8   r.   r.   r/   r=   B   s    zUFuncTarget.target_context)
r1   r2   r3   r!   r-   r7   propertyr<   r=   __classcell__r.   r.   r9   r/   r4   8   s   
r4   c                   @   sR   e Zd ZdZeZi i fddZdd Zedd Z	dd	 Z
i fd
dZdd ZdS )UFuncDispatcherzK
    An object handling compilation of various signatures for a ufunc.
    c                 C   s(   || _ t | _|| _|| _t | _d S r;   )py_funcr   
UniqueDict	overloadstargetoptionslocalsr   cache)r+   rA   rE   rD   r.   r.   r/   r7   P   s
    
zUFuncDispatcher.__init__c                 C   s   t | j| j| jdS )4
        NOTE: part of ReduceMixin protocol
        )pyfuncrE   rD   )dictrA   rE   rD   r8   r.   r.   r/   _reduce_statesW   s
    zUFuncDispatcher._reduce_statesc                 C   s   | |||dS )rG   )rA   rE   rD   r.   )clsrH   rE   rD   r.   r.   r/   _rebuilda   s    zUFuncDispatcher._rebuildc                 C   s   t | j| _d S r;   )r   rA   rF   r8   r.   r.   r/   enable_cachingh   s    zUFuncDispatcher.enable_cachingc                 K   s`   | j  }|| | j }|| t }| jj|| d|_	d|_
d|_| |||S )NTnumpyF)rE   copyupdaterD   r   Flagstargetdescrr-   parse_as_flagsno_cpython_wrappererror_modelr#   _compile_core)r+   sigrE   rD   locstoptr,   r.   r.   r/   compilek   s    



zUFuncDispatcher.compilec           	         s   j j}j j}t fdd}t t |  |  j	
||  dk	r W  5 Q R  W  5 Q R  W  5 Q R  S t|\}}tj||j||||d j	|   W  5 Q R  W  5 Q R  W  5 Q R  S Q R X W 5 Q R X W 5 Q R X dS )z
        Trigger the compiler on the core function or load a previously
        compiled version from the cache.  Returns the CompileResult.
        c                  3   sH   z
d V  W n t k
r     Y n$X j j} | d krD j j< d S r;   )	ExceptionrC   get	signature)existscresr+   r.   r/   store_overloads_on_success   s    
zAUFuncDispatcher._compile_core.<locals>.store_overloads_on_successN)argsreturn_typer,   rE   )rR   r<   r=   r   r   r   ConfigStackenterrO   rF   load_overloadr   normalize_signaturer   compile_extrarA   save_overload)	r+   rW   r,   rE   	typingctx	targetctxra   rb   rc   r.   r_   r/   rV   ~   s(    (  zUFuncDispatcher._compile_coreN)r1   r2   r3   __doc__ufunc_targetrR   r7   rJ   classmethodrL   rM   rZ   rV   r.   r.   r.   r/   r@   J   s   

r@   npyufuncc                 C   s&   | j |f|}t|\}}|||fS r;   )rZ   r   rg   )nb_funcrD   rW   r`   rb   rc   r.   r.   r/   _compile_element_wise_function   s    rq   c                 C   s6   |dkr | j rtdn| jj}|tjks.t|| S )zGiven a compilation result, argument types, and a return type,
    build a valid Numba signature after validating that it doesn't
    violate the constraints for the compilation mode.
    Nz-return type must be specified for object mode)
objectmode	TypeErrorr]   rc   r   pyobjectAssertionError)r`   rb   rc   r.   r.   r/   _finalize_ufunc_signature   s    
rv   c              	   C   st   | j }| j}| jj}t( t||||| j| }|j|j}W 5 Q R X dd |j	D }|
t|jj ||| jfS )zBuild a wrapper for the ufunc loop entry point given by the
    compilation result object, using the element-wise signature.
    c                 S   s   g | ]}t |jqS r.   )r   num.0ar.   r.   r/   
<listcomp>   s     z5_build_element_wise_ufunc_wrapper.<locals>.<listcomp>)r=   libraryfndescllvm_func_namer   r   rr   get_pointer_to_functionnamerb   appendr   rc   rw   environment)r`   r]   ctxr|   fnameinfoptr	dtypenumsr.   r.   r/   !_build_element_wise_ufunc_wrapper   s    
 r   )r      NZreorderablec                 C   s4   zt |  } W n" tk
r.   td| f Y nX | S )z]
    Parse an identity value and return the corresponding low-level value
    for Numpy.
    zInvalid identity value %r)_identitiesKeyError
ValueError)identityr.   r.   r/   parse_identity   s
    r   c                	   c   s.   t   t jdtdd dV  W 5 Q R X dS )zThis suppresses the NumbaDeprecationWarning that occurs through the use
    of `jit` without the `nopython` kwarg. This use of `jit` occurs in a few
    places in the `{g,}ufunc` mechanism in Numba, predominantly to wrap the
    "kernel" function.ignorez3.*The 'nopython' keyword argument was not supplied*)categorymessageN)warningscatch_warningsfilterwarningsr   r.   r.   r.   r/   3_suppress_deprecation_warning_nopython_not_supplied   s    
r   c                   @   s   e Zd ZdddZdd ZdS )_BaseUFuncBuilderNc                 C   sV   t | dr| j}n| jj}t| j||\}}}| |||}| j| || j|< |S )NrD   )hasattrrD   rp   rq   _finalize_signature_sigsr   _cres)r+   rW   rD   r`   rb   rc   r.   r.   r/   add   s    
  

z_BaseUFuncBuilder.addc                 C   s   dS )zI
        Disable the compilation of new signatures at call time.
        Nr.   r8   r.   r.   r/   disable_compile	  s    z!_BaseUFuncBuilder.disable_compile)N)r1   r2   r3   r   r   r.   r.   r.   r/   r      s   
r   c                   @   s4   e Zd Zddi fddZdd Zdd Zd	d
 ZdS )UFuncBuilderNFc              	   C   sZ   t |r|j}|| _t|| _t   tf d|d||| _W 5 Q R X g | _i | _d S )Nro   _targetrF   )	r   rA   r   r   r   r   rp   r   r   )r+   rA   r   rF   rD   r.   r.   r/   r7     s    
zUFuncBuilder.__init__c                 C   s   t |||S )z^Slated for deprecation, use ufuncbuilder._finalize_ufunc_signature()
        instead.
        )rv   r+   r`   rb   rc   r.   r.   r/   r     s    z UFuncBuilder._finalize_signaturec                 C   s   t  g }g }| jstdg }d }| jD ]H}| j| }| ||\}}}|| |t| ||j|f q*d gt	| }	|d krt
| j}
t	|
j}nt	|jj}d}t| jj| jj|||||	|| j	}|W  5 Q R  S Q R X d S )NNo definitionr   )r   rp   rs   r   r   buildr   intr|   leninspectgetfullargspecrA   rb   r]   r   fromfuncr1   rl   r   )r+   	dtypelistZptrlist	keepaliver`   rW   r   r   envZdatlistargspecZinctZoutctr5   r.   r.   r/   build_ufunc$  s>    


      zUFuncBuilder.build_ufuncc                 C   s
   t ||S )z^Slated for deprecation, use
        ufuncbuilder._build_element_wise_ufunc_wrapper().
        )r   )r+   r`   r]   r.   r.   r/   r   K  s    zUFuncBuilder.build)r1   r2   r3   r7   r   r   r   r.   r.   r.   r/   r     s   'r   c                   @   s:   e Zd Zddi dfddZdd Zedd	 Zd
d ZdS )GUFuncBuilderNFr.   c              	      s   || _ t|| _t  td|d|| _W 5 Q R X || _t|\| _| _	|| _
|| _g | _i | _t| t fdd|D | _d S )Nro   r   c                    s   g | ]} |qS r.   r.   rx   transform_argr.   r/   r{   c  s     z*GUFuncBuilder.__init__.<locals>.<listcomp>)rA   r   r   r   r   rp   r]   r   sinsoutrD   rF   r   r   _get_transform_argtupler    )r+   rA   r]   r   rF   rD   r    r.   r   r/   r7   U  s    
zGUFuncBuilder.__init__c                 C   s2   |j s|jjtjkrtd|d kr*tj}|| S )Nz(gufunc kernel must have void return type)rr   r]   rc   r   voidrs   r   r.   r.   r/   r   e  s
    z!GUFuncBuilder._finalize_signaturec                 C   s   g }g }| j stdg }| jD ]F}| j| }| |\}}}|| |t| ||j|f q d gt| }	t| j	}
t| j
}t| jj| jj|||
||	|| j| j| j}|S )Nr   )rp   rs   r   r   r   r   r   r|   r   r   r   r   r   rA   r1   rl   r   r]   r    )r+   Z	type_listZ	func_listr   rW   r`   r   r   r   Zdatalistninnoutr5   r.   r.   r/   r   n  s8    




        zGUFuncBuilder.build_ufuncc           	      C   sx   |j }t| j|| j| j| jdd}|j}|j|j	}g }|j
D ],}t|tjrX|j}n|}|t|j q@|||fS )zJ
        Returns (dtype numbers, function ptr, EnvironmentObject)
        F)rF   
is_parfors)r]   r   rA   r   r   rF   r   r|   r   r   rb   
isinstancer   Arraydtyper   r   rw   )	r+   r`   r]   r   r   r   r   rz   tyr.   r.   r/   r     s$        
zGUFuncBuilder.build)r1   r2   r3   r7   r   r   r   r   r.   r.   r.   r/   r   R  s    
	
r   c                    s2   t j dd t D  fdd}|S )z-Return function that transform arg into indexc                 S   s   i | ]\}}||qS r.   r.   )ry   iargr.   r.   r/   
<dictcomp>  s      z&_get_transform_arg.<locals>.<dictcomp>c              	      sR   t | tr| S z
|  W S  tk
rL   d|  d  dj }t|Y nX d S )NzSpecified writable arg z not found in arg list z for function )r   r   r   r3   RuntimeError)r   msgrb   Z
pos_by_argrA   r.   r/   r     s    

z)_get_transform_arg.<locals>.transform_arg)r   r   rb   	enumerate)rA   r   r.   r   r/   r     s    r   )?r   r   
contextlibr   
numba.corer   r   numba.core.decoratorsr   numba.core.descriptorsr   numba.core.extendingr   numba.core.errorsr   numba.core.optionsr	   r
   numba.core.registryr   numba.core.target_extensionr   r   r   r   r   r   r   numba.np.numpy_supportr   numba.np.ufuncr   Znumba.np.ufunc.sigparser   numba.np.ufunc.wrappersr   r   numba.core.cachingr   r   numba.core.compiler_lockr   _options_mixinr!   r4   rm   ReduceMixinr@   rq   rv   r   PyUFunc_ZeroPyUFunc_OnePyUFunc_NonePyUFunc_ReorderableNoner   r   r   objectr   r   r   r   r.   r.   r.   r/   <module>   sZ   
^
BQ