U
    hâËd   ã                   @   sÖ   d dl mZ d dlmZmZ d dlmZ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mZ d dlmZ d dlZd dlmZmZmZ d dlZeG d	d
„ d
ƒƒZedƒG dd„ deƒƒZe dkrÒe !¡  dS )é    )ÚList)Ú	dataclassÚfield)ÚcudaÚfloat32)Úcompile_ptx_for_current_deviceÚcompile_ptx)	ÚcosÚsinÚtanÚexpÚlogÚlog10Úlog2ÚpowÚtanh)ÚtruedivN)ÚCUDATestCaseÚskip_on_cudasimÚskip_unless_cc_75c                   @   sx   e Zd ZU eed�Zee ed< eed�Z	ee ed< eed�Z
ee ed< eed�Zee ed< eeedœdd„Zd	S )
ÚFastMathCriterion)Údefault_factoryÚfast_expectedÚfast_unexpectedÚprec_expectedÚprec_unexpected)ÚtestÚfastÚprecc                    s|   |  t‡ fdd„| jD ƒƒ¡ |  t‡ fdd„| jD ƒƒ¡ |  t‡fdd„| jD ƒƒ¡ |  t‡fdd„| jD ƒƒ¡ d S )Nc                 3   s   | ]}|ˆ kV  qd S ©N© ©Ú.0Úi©r   r    ú^/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numba/cuda/tests/cudapy/test_fastmath.pyÚ	<genexpr>   s     z*FastMathCriterion.check.<locals>.<genexpr>c                 3   s   | ]}|ˆ kV  qd S r   r    r!   r$   r    r%   r&      s     c                 3   s   | ]}|ˆ kV  qd S r   r    r!   ©r   r    r%   r&      s     c                 3   s   | ]}|ˆ kV  qd S r   r    r!   r'   r    r%   r&      s     )Ú
assertTrueÚallr   r   r   r   )Úselfr   r   r   r    )r   r   r%   Úcheck   s    zFastMathCriterion.checkN)Ú__name__Ú
__module__Ú__qualname__r   Úlistr   r   ÚstrÚ__annotations__r   r   r   r   r+   r    r    r    r%   r      s
   
r   z4Fastmath and PTX inspection not available on cudasimc                   @   s¢   e Zd Zdd„ Zedœdd„Zedœdd„Zd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d„ Zejd d!„ ƒZd"S )#ÚTestFastMathOptionc           
      C   sx   t j||dd�|ƒ}t j||d�|ƒ}| | | |¡| |¡¡ t|||dd�\}}t|||d�\}	}| | ||	¡ d S )NT)ÚdeviceÚfastmath©r3   )r   Újitr+   Úinspect_asmr   )
r*   ZpyfuncÚsigr3   Ú	criterionÚfastverÚprecverÚfastptxÚ_Úprecptxr    r    r%   Ú_test_fast_math_common   s&      ÿ   ÿ
  ÿ
z)TestFastMathOption._test_fast_math_common)r9   c                    sP   ‡ fdd„}‡ fdd„}| j |td d d… tfd|d� | j |tfd|d� d S )	Nc                    s   ˆ |ƒ| d< d S ©Nr   r    ©ÚrÚx©Úopr    r%   Úkernel2   s    z8TestFastMathOption._test_fast_math_unary.<locals>.kernelc                    s   ˆ | ƒS r   r    )rC   rD   r    r%   Údevice_function5   s    zATestFastMathOption._test_fast_math_unary.<locals>.device_functioné   F©r3   r9   T©r?   r   )r*   rE   r9   rF   rG   r    rD   r%   Ú_test_fast_math_unary1   s       ÿ   ÿz(TestFastMathOption._test_fast_math_unaryc                    sT   ‡ fdd„}‡ fdd„}| j |td d d… ttfd|d� | j |ttfd|d� d S )	Nc                    s   ˆ ||ƒ| d< d S r@   r    ©rB   rC   ÚyrD   r    r%   rF   @   s    z9TestFastMathOption._test_fast_math_binary.<locals>.kernelc                    s
   ˆ | |ƒS r   r    )rC   rM   rD   r    r%   r3   C   s    z9TestFastMathOption._test_fast_math_binary.<locals>.devicerH   FrI   TrJ   )r*   rE   r9   rF   r3   r    rD   r%   Ú_test_fast_math_binary?   s      þ   ÿz)TestFastMathOption._test_fast_math_binaryc                 C   s   |   ttdgdgd�¡ d S )Núcos.approx.ftz.f32 ©r   r   )rK   r	   r   ©r*   r    r    r%   Ú	test_cosfN   s    þþzTestFastMathOption.test_cosfc                 C   s   |   ttdgdgd�¡ d S )Núsin.approx.ftz.f32 rP   )rK   r
   r   rQ   r    r    r%   Ú	test_sinfW   s    þþzTestFastMathOption.test_sinfc                 C   s    |   ttdddgdgd�¡ d S )NrS   rO   údiv.approx.ftz.f32 rP   )rK   r   r   rQ   r    r    r%   Ú	test_tanf`   s    ýüþzTestFastMathOption.test_tanfc                 C   s   |   ttdgdgd�¡ d S )Nútanh.approx.f32 rP   )rK   r   r   rQ   r    r    r%   Ú
test_tanhfj   s    þþzTestFastMathOption.test_tanhfc                    sL   dd„ ‰‡ ‡fdd„}|dt dgdgd�d� |d	t d
dgdgd�d� d S )Nc                 S   s   t |ƒ| d< d S r@   )r   rA   r    r    r%   Útanh_kernelv   s    z>TestFastMathOption.test_tanhf_compile_ptx.<locals>.tanh_kernelc                    sT   t ˆtd d d… tfd| d�\}}t ˆtd d d… tf| d�\}}| ˆ ||¡ d S )NrH   T)r4   Úcc)rZ   )r   r   r+   )rZ   r9   r<   r=   r>   ©r*   rY   r    r%   Útanh_common_testy   s     ÿ
ÿ
zCTestFastMathOption.test_tanhf_compile_ptx.<locals>.tanh_common_test)é   é   rW   rP   )rZ   r9   )r]   r   zex2.approx.ftz.f32 zrcp.approx.ftz.f32 )r   )r*   r\   r    r[   r%   Útest_tanhf_compile_ptxu   s    þ
ÿýÿz)TestFastMathOption.test_tanhf_compile_ptxc                 C   s   |   ttdgdgd�¡ d S )Nzfma.rn.f32 )r   r   )rK   r   r   rQ   r    r    r%   Ú	test_expf‹   s    þþzTestFastMathOption.test_expfc                 C   s   |   ttddgdgd�¡ d S )Núlg2.approx.ftz.f32 Z
0f3F317218rP   )rK   r   r   rQ   r    r    r%   Ú	test_logf”   s     þÿzTestFastMathOption.test_logfc                 C   s   |   ttddgdgd�¡ d S )Nra   Z
0f3E9A209BrP   )rK   r   r   rQ   r    r    r%   Útest_log10f�   s     þÿzTestFastMathOption.test_log10fc                 C   s   |   ttdgdgd�¡ d S ©Nra   rP   )rK   r   r   rQ   r    r    r%   Ú
test_log2f¦   s     þÿzTestFastMathOption.test_log2fc                 C   s   |   ttdgdgd�¡ d S rd   )rN   r   r   rQ   r    r    r%   Ú	test_powf®   s     þÿzTestFastMathOption.test_powfc              	   C   s$   |   ttdgdgdgdgd�¡ d S )NrU   ú
div.rn.f32)r   r   r   r   )rN   r   r   rQ   r    r    r%   Ú	test_divf¶   s     üÿzTestFastMathOption.test_divfc              	   C   s¼   dd„ }t d d d… t t f}tj|ddd�|ƒ}tj|dd�|ƒ}d}tj|tj d�}|  t¡� |d|f |d	d
ƒ W 5 Q R X z|d|f |d	d
ƒ W n tk
r¶   |  d¡ Y nX d S )Nc                 S   s   || | d< d S r@   r    rL   r    r    r%   Úf10Á   s    z3TestFastMathOption.test_divf_exception.<locals>.f10rH   T)r4   Údebug)rj   é
   )Zdtypeg      $@g        z5Divide in fastmath should not throw ZeroDivisionError)r   r   r6   ÚnpÚemptyÚassertRaisesÚZeroDivisionErrorZfail)r*   ri   r8   r:   r;   ZnelemZaryr    r    r%   Útest_divf_exceptionÀ   s    z&TestFastMathOption.test_divf_exceptionc                    s    t jddd�dd„ ƒ‰ ‡ fdd„}td d d… tf}t j|dd	�|ƒ}t  |¡|ƒ}|  d
| |¡¡ |  d| |¡¡ |  d
| |¡¡ |  d| |¡¡ d S )Nzfloat32(float32, float32)Tr5   c                 S   s   | | S r   r    )ÚaÚbr    r    r%   ÚfooÖ   s    z@TestFastMathOption.test_device_fastmath_propagation.<locals>.fooc                    s&   t  d¡}|| jk r"ˆ ||ƒ| |< d S )NrH   )r   ÚgridÚsize)ZarrÚvalr#   ©rs   r    r%   ÚbarÚ   s    

z@TestFastMathOption.test_device_fastmath_propagation.<locals>.barrH   )r4   zdiv.approx.f32rg   zdiv.full.f32)r   r6   r   ZassertInr7   ZassertNotIn)r*   rx   r8   r:   r;   r    rw   r%   Ú test_device_fastmath_propagationÑ   s    
z3TestFastMathOption.test_device_fastmath_propagationN)r,   r-   r.   r?   r   rK   rN   rR   rT   rV   r   rX   r_   r`   rb   rc   re   rf   rh   rp   ÚunittestZexpectedFailurery   r    r    r    r%   r2      s$   		


			
r2   Ú__main__)"Útypingr   Údataclassesr   r   Znumbar   r   Znumba.cuda.compilerr   r   Úmathr	   r
   r   r   r   r   r   r   r   Úoperatorr   Únumpyrl   Znumba.cuda.testingr   r   r   rz   r   r2   r,   Úmainr    r    r    r%   Ú<module>   s   , T