a
    —§»aŠ  ã                   @   s(  d dl Z d dlmZ d dl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mZmZmZmZmZ d
 edg¡ZG dd„ deƒZG dd„ deƒZG dd„ deƒZeeƒZG dd„ deƒZ eee< e eee < dd„ Z!e!eee < G dd„ deƒZ"G dd„ de j#ƒZ$dS )é    N)Úcontextmanager)Únjit)ÚerrorsÚcpuÚutilsÚtyping)ÚTargetDescriptor)ÚTargetConfig)ÚBasicRetarget)Úoverload)Údispatcher_registryÚCPUDispatcherÚCPUÚtarget_registryÚjit_registryÚ.Ú	CustomCPUc                   @   s   e Zd ZdZdS )r   zExtend from the CPU target
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úV/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/tests/test_retargeting.pyr      s   c                   @   s    e Zd ZdZdZedd„ ƒZdS )Ú_NestedContextNc              	   c   sB   | j | jf}z"|| _ || _d V  W |\| _ | _n|\| _ | _0 d S ©N)Ú_typing_contextÚ_target_context)ÚselfÚtyping_contextÚtarget_contextZ
old_nestedr   r   r   Únested#   s    z_NestedContext.nested)r   r   r   r   r   r   r    r   r   r   r   r      s   r   c                   @   sT   e Zd ZejZeƒ Zej	dd„ ƒZ
ej	dd„ ƒZedd„ ƒZedd„ ƒZd	d
„ ZdS )ÚCustomTargetDescrc                 C   s   t  | j| j¡S r   )r   Z
CPUContextr   Z_target_name©r   r   r   r   Ú_toplevel_target_context3   s    z*CustomTargetDescr._toplevel_target_contextc                 C   s   t  ¡ S r   )r   ÚContextr"   r   r   r   Ú_toplevel_typing_context8   s    z*CustomTargetDescr._toplevel_typing_contextc                 C   s   | j j}|dur|S | jS dS )z5
        The target context for DPU targets.
        N)Ú_nestedr   r#   ©r   r    r   r   r   r   =   s    z CustomTargetDescr.target_contextc                 C   s   | j j}|dur|S | jS dS )z5
        The typing context for CPU targets.
        N)r&   r   r%   r'   r   r   r   r   H   s    z CustomTargetDescr.typing_contextc                 C   s   | j  ||¡S )zˆ
        A context manager temporarily replacing the contexts with the
        given ones, for the current thread of execution.
        )r&   r    )r   r   r   r   r   r   Únested_contextS   s    z CustomTargetDescr.nested_contextN)r   r   r   r   ZCPUTargetOptionsÚoptionsr   r&   r   Úcached_propertyr#   r%   Úpropertyr   r   r(   r   r   r   r   r!   /   s   





r!   c                   @   s   e Zd ZeZdS )ÚCustomCPUDispatcherN)r   r   r   Úcustom_targetZtargetdescrr   r   r   r   r,   ^   s   r,   c                  O   s*   d|vsJ ‚d|vsJ ‚t | dti|¤ŽS )NÚtargetÚ_target)r   ÚCUSTOM_TARGET)ÚargsÚkwargsr   r   r   Ú
custom_jitf   s    r3   c                   @   s    e Zd Zedd„ ƒZdd„ ZdS )ÚCustomCPURetargetc                 C   s   t S r   )r0   r"   r   r   r   Úoutput_targetr   s    zCustomCPURetarget.output_targetc                 C   s   t td�|jƒ}|S )N©r/   )r   r0   Zpy_func)r   Zcpu_dispÚkernelr   r   r   Úcompile_retargetv   s    z"CustomCPURetarget.compile_retargetN)r   r   r   r+   r5   r8   r   r   r   r   r4   q   s   
r4   c                   @   s`   e Zd Zdd„ Zdd„ Ze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 )ÚTestRetargetingc                    sB   t dd�dd„ ƒ‰ t ‡ fdd„ƒ}t dd„ ƒ}tƒ | _tƒ | _d S )	Nr   r6   c                 S   s   | d S )zo
            This has a fixed target to "cpu".
            Cannot be used in CUSTOM_TARGET target.
            é
   r   ©Úxr   r   r   Úfixed_target   s    z+TestRetargeting.setUp.<locals>.fixed_targetc                    s   ˆ | ƒd S )z‹
            This has a flexible target, but uses a fixed target function.
            Cannot be used in CUSTOM_TARGET target.
            éd   r   r;   ©r=   r   r   Úflex_call_fixed‡   s    z.TestRetargeting.setUp.<locals>.flex_call_fixedc                 S   s   | d S )zf
            This has a flexible target.
            Can be used in CUSTOM_TARGET target.
            iè  r   r;   r   r   r   Úflex_target�   s    z*TestRetargeting.setUp.<locals>.flex_target)r   ÚlocalsÚ	functionsr4   Úretarget)r   r@   rA   r   r?   r   ÚsetUp|   s    

zTestRetargeting.setUpc                 C   s   t  | j¡S r   )r	   Úswitch_targetrD   r"   r   r   r   rF   œ   s    zTestRetargeting.switch_targetc                 c   sN   |   tj¡�}d V  W d   ƒ n1 s(0    Y  |  t› d�t|jƒ¡ d S )Nz != cpu)ÚassertRaisesr   Z
NumbaErrorZassertInr0   ÚstrÚ	exception)r   Zraisesr   r   r   Úcheck_retarget_errorŸ   s    $z$TestRetargeting.check_retarget_errorc                 C   s(   | j j ¡ }|  |d |d  d¡ d S )NÚhitÚmissr   )rD   ÚcacheÚstatsZassertGreater)r   rN   r   r   r   Úcheck_non_empty_cache¥   s    z%TestRetargeting.check_non_empty_cachec                    s^   | j d ‰ | j d ‰t‡ ‡fdd„ƒ}|dƒ}|  |d¡ | jj ¡ }|  |tddd�¡ d S )	Nr=   rA   c                    s   ˆ | ƒ} ˆ| ƒ} | S r   r   r;   ©r=   rA   r   r   Úfoo°   s    z'TestRetargeting.test_case0.<locals>.fooé{   im  r   )rK   rL   )rC   r   ÚassertEqualrD   rM   rN   Údict)r   rQ   ÚrrN   r   rP   r   Ú
test_case0¬   s    

zTestRetargeting.test_case0c                    sb   | j d ‰ t‡ fdd„ƒ}|  ¡ � |dƒ}W d   ƒ n1 s@0    Y  |  |d¡ |  ¡  d S )NrA   c                    s   ˆ | ƒ} | S r   r   r;   ©rA   r   r   rQ   ¿   s    z'TestRetargeting.test_case1.<locals>.foorR   ic  )rC   r   rF   rS   rO   )r   rQ   rU   r   rW   r   Ú
test_case1¼   s    

&zTestRetargeting.test_case1c              	      s‚   | j d ‰ | j d ‰t‡ ‡fdd„ƒ}|  ¡ �@ |  ¡ � |dƒ W d  ƒ n1 sV0    Y  W d  ƒ n1 st0    Y  dS )zK
        The non-nested call into fixed_target should raise error.
        r=   rA   c                    s   ˆ | ƒ} ˆ| ƒ} | S r   r   r;   rP   r   r   rQ   Ð   s    z'TestRetargeting.test_case2.<locals>.foorR   N©rC   r   rJ   rF   ©r   rQ   r   rP   r   Ú
test_case2É   s    



zTestRetargeting.test_case2c              	      s‚   | j d ‰| j d ‰ t‡ ‡fdd„ƒ}|  ¡ �@ |  ¡ � |dƒ W d  ƒ n1 sV0    Y  W d  ƒ n1 st0    Y  dS )zF
        The nested call into fixed_target should raise error
        rA   r@   c                    s   ˆ | ƒ} ˆ| ƒ} | S r   r   r;   ©r@   rA   r   r   rQ   á   s    z'TestRetargeting.test_case3.<locals>.foorR   NrY   rZ   r   r\   r   Ú
test_case3Ú   s    



zTestRetargeting.test_case3c              	      s–   | j d ‰| j d ‰ ˆ dƒ}|  |d¡ t‡ ‡fdd„ƒ}|  ¡ �@ |  ¡ � |dƒ W d  ƒ n1 sj0    Y  W d  ƒ n1 sˆ0    Y  dS )z‚
        Same as case2 but flex_call_fixed() is invoked outside of CUSTOM_TARGET
        target before the switch_target.
        rA   r@   rR   éé   c                    s   ˆ | ƒ} ˆ| ƒ} | S r   r   r;   r\   r   r   rQ   ö   s    z'TestRetargeting.test_case4.<locals>.fooN)rC   rS   r   rJ   rF   )r   rU   rQ   r   r\   r   Ú
test_case4ë   s    



zTestRetargeting.test_case4c                    s¨   dd„ ‰ t ˆ td�dd„ ƒ}t ˆ dd�dd„ ƒ}td	d
„ ƒ}t‡ fdd„ƒ}|dƒ}|  |d¡ |  ¡ �$ |dƒ}|  |d¡ W d  ƒ n1 s’0    Y  |  ¡  dS )zA
        Tests overload resolution with target switching
        c                 S   s   d S r   r   r;   r   r   r   Úoverloaded_func  s    z3TestRetargeting.test_case5.<locals>.overloaded_func)r.   c                 S   s   dd„ }|S )Nc                 S   s   dS )Ninõ  r   r;   r   r   r   Úimpl
  s    zRTestRetargeting.test_case5.<locals>.ol_overloaded_func_custom_target.<locals>.implr   ©r<   ra   r   r   r   Ú ol_overloaded_func_custom_target  s    zDTestRetargeting.test_case5.<locals>.ol_overloaded_func_custom_targetr   c                 S   s   dd„ }|S )Nc                 S   s   dS )Ni·z  r   r;   r   r   r   ra     s    zHTestRetargeting.test_case5.<locals>.ol_overloaded_func_cpu.<locals>.implr   rb   r   r   r   Úol_overloaded_func_cpu  s    z:TestRetargeting.test_case5.<locals>.ol_overloaded_func_cpuc                 S   s   d S r   r   r;   r   r   r   Úflex_resolve_overload  s    z9TestRetargeting.test_case5.<locals>.flex_resolve_overloadc                    s   | ˆ | ƒ S r   r   r;   ©r`   r   r   rQ     s    z'TestRetargeting.test_case5.<locals>.foorR   i2{  iéõ  N)r   r0   r   rS   rF   rO   )r   rc   rd   re   rQ   rU   r   rf   r   Ú
test_case5   s    





*zTestRetargeting.test_case5N)r   r   r   rE   rF   r   rJ   rO   rV   rX   r[   r]   r_   rg   r   r   r   r   r9   {   s    
r9   )%ZunittestÚ
contextlibr   Znumbar   Z
numba.corer   r   r   r   Znumba.core.descriptorsr   Znumba.core.dispatcherr	   Znumba.core.retargetr
   Znumba.core.extendingr   Znumba.core.target_extensionr   r   r   r   r   Újoinr   r0   r   Úobjectr   r!   r-   r,   r3   r4   ZTestCaser9   r   r   r   r   Ú<module>   s(   ,
