B
     \<              +   @   sN  d dl mZmZ d dlZd dlZ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 d dlZd dlmZmZ d dlmZmZ d dlmZ ddlmZmZmZ eejjd	Zd
dddZej ej gej!ej!gg ej"ej"gej#ej#gej$gej%gej&gej'gg ej(gej)gg g g g g ej*ej*gej+gej,gg g g g ej-ej-gg g g g ej.ej.gej/ej/gg g g g g g ej0ej0gej1ej1gej2ej2gej3ej3gg d*Z4dddddddgZ5dd e46 D Z4dd e46 D Z7dd e46 D Z8d+ddZ9dd Z:d d! Z;eG d"d# d#eZ<e<=  d$d% Z>d&d' Z?eG d(d) d)eZ@eAd*krJeB  dS ),    )divisionprint_functionN)chaincombinations)prangeunittest_support)compile_isolatedFlags)exec_   )TestCasetagoverride_env_configz"SVML tests need SVML to be presentZnehalemZhaswellzskylake-avx512)         )*sincospowexplogacosacoshasinasinhZatan2atanatanhZcbrtZcdfnormZ
cdfnorminvZceilZcosdcosherferfcZerfcinvZerfinvZexp10Zexp2expm1ZfloorZfmodZhypotZinvsqrtlog10log1pZlog2ZlogbZ	nearbyintZrintroundZsindsinhsqrttantanhZtruncr%   r&   r!   r    r"   r'   r   c             C   s"   i | ]\}}t |d kr||qS )r   )len).0kv r,   4lib/python3.7/site-packages/numba/tests/test_svml.py
<dictcomp>Q   s    r.   c             C   s&   g | ]\}}d dd |D kr|qS )z<ufuncc             S   s   g | ]}t |d d qS ) r   )strsplit)r)   pr,   r,   r-   
<listcomp>T   s    z<listcomp>.<listcomp>r,   )r)   fr+   r,   r,   r-   r3   S   s    r3   c             C   s&   g | ]\}}d dd |D kr|qS )z	<built-inc             S   s   g | ]}t |d d qS )r/   r   )r0   r1   )r)   r2   r,   r,   r-   r3   V   s    z<listcomp>.<listcomp>r,   )r)   r4   r+   r,   r,   r-   r3   U   s            c             C   s  |dkr.d dd |D }d||| |f }	np|dkrhd||| d |f }	|	|dr`d	| nd
7 }	n6|dks||dks|td dd |D }djf t }	|dkp|dk}
|
r| d n| }|
r|d n|}t|ddrdnd}tjjrd| nd| }d|||f }|dkr"|g}d|g}n>|g}g }|dkr`|
sF|dkr`|dd||d |f g7 }| dkr|dkrdg}||g}n|dkrd g}||g}|	||fS )!z
    For a given function and its usage modes,
    returns python code and assembly patterns it should and should not generate
    scalar,c             S   s   g | ]}|d  qS )z[0]r,   )r)   ar,   r,   r-   r3   a   s    z!func_patterns.<locals>.<listcomp>z%s%s[0] += math.%s(%s)
numpyz%s%s += np.%s(%s)intz.astype(np.%s)

ranger   c             S   s   g | ]}|d  qS )z[i]r,   )r)   r8   r,   r,   r-   r3   h   s    zP{pad}for i in {mode}({res}.size):
{pad}{pad}{res}[i] += math.{func}({arg_list})
float32	complex64r4   r   fastmathF Z_haz$_$z__svml_%s%d%s,Z__svml_r   int32z%zmmr%   ZsqrtsZvsqrtp)	join
startswithAssertionErrorformatlocalsgetattrnumbaconfigIS_OSX)funcargsZresdtypemodevlenflagsZpadarg_listbodyZis_f32r4   r+   Z	prec_suffZscalar_funcZ	svml_funccontainsavoidsr,   r,   r-   func_patternsY   s>    





rV   c             C   s   dj f t S )z, Returns pretty name for given set of modes z%{dtype}_{mode}{vlen}_{flags.__name__})rF   rG   )rN   rO   rP   rQ   r,   r,   r-   usecase_name   s    rW   c          	   C   s   t | |||}djf t }t|dkr*tnt}| drD|t}t }t }xD|D ]<}	t	|	dgd| |||\}
}}|
| ||
7 }|
| qVW |d7 }i }t|t | ||| _|| ||fS )z? Combine multiple function calls under single umbrella usecase z\def {name}(n):
        x   = np.empty(n*8, dtype=np.{dtype})
        ret = np.empty_like(x)
r9   complexxretz        return ret)rW   rF   rG   setnumpy_funcsother_funcsrD   
differencecomplex_funcs_excluderV   updater
   globals__doc__)rN   rO   rP   rQ   namerS   ZfuncsrT   rU   r4   bcr8   Zldictr,   r,   r-   combo_svml_usecase   s"    




rf   c               @   sF   e Zd ZdZdZeddgee	 Z
edd Zedd Zd	S )
TestSVMLGenerationz; Tests all SVML-generating functions produce desired calls F|z\$[a-z_]\w+,c                sl   | dr|dkrd S | do&dk}||f t| d fdd}tdt   | d S )	NrX   r9   r:   r   zNot implementedc                s   t  \}}}tdt N tdd8 yt|tjfd}W n   td|j Y nX W d Q R X W d Q R X |j	   fdd|D } fdd|D }| 
| o| d	t|t|d
fdd d
D |jf  d S )NNUMBA_CPU_NAMENUMBA_CPU_FEATURESr@   )rQ   zraised while compiling c                s   g | ]}| kr|qS r,   r,   )r)   pattern)asmr,   r-   r3      s    zJTestSVMLGeneration._inject_test.<locals>.test_template.<locals>.<listcomp>c                s   g | ]}| kr|qS r,   r,   )r)   rk   )rl   r,   r-   r3      s    z?While expecting %s and no %s,
it contains:
%s
when compiling %sr;   c                s$   g | ]} j |rd |kr|qS )")
asm_filtersearch)r)   line)clsr,   r-   r3      s    )rf   r   vlen2cpur   rI   Zint64	Exceptionrb   libraryget_asm_strZ
assertTruer0   rC   r1   )selffnrT   rU   ZjitZmissedfound)rM   rq   rQ   rP   )rl   r-   test_template   s    (
z6TestSVMLGeneration._inject_test.<locals>.test_templateZtest_)rD   unittest
skipUnlesssetattrrW   )rq   rN   rO   rP   rQ   Zskippedry   r,   )rM   rq   rQ   rP   r-   _inject_test   s     zTestSVMLGeneration._inject_testc       	   
      s  dg t  fddtt d D g  g }xZ D ]R}t }|d |dd d|d	 |_x|D ]}|| qjW || q4W xFd
D ]>}x8tD ]0}x*|D ]"}xdD ]}| 	|||| qW qW qW qW t
jd dkr
x&dD ]}t| |tdt| | qW d S )Nr?   c                s   g | ]}t t |qS r,   )listr   )r)   rY   )
test_flagsr,   r-   r3      s    z3TestSVMLGeneration.autogenerate.<locals>.<listcomp>r   nrtZerror_modelr9   _)Zusecase)r>   float64r=   rB   )r6   r<   r   r9   r   r   )Ztest_int32_range4_usecase	important)sumr<   r(   r	   r[   rC   __name__appendrr   r}   sysversion_infor|   r   rH   )	rq   Z	flag_listZftrQ   r4   rN   rP   rO   nr,   )r   r-   autogenerate   s(    






 
zTestSVMLGeneration.autogenerateN)r   
__module____qualname__rb   _numba_parallel_test_recompilerC   r~   
svml_funcsrn   classmethodr}   r   r,   r,   r,   r-   rg      s
   !rg   c             C   s
   t | S )N)mathr   )rY   r,   r,   r-   math_sin_scalar   s    r   c             C   s:   t j| t jd}x$t| D ]}tt |||< qW |S )N)rN   )npemptyr   r<   r   r   )r   rZ   rY   r,   r,   r-   math_sin_loop   s    r   c                   sd   e Zd ZdZdZ fddZdd Zdd Zd	d
 Zdd Z	dd Z
eddd Zdd Z  ZS )TestSVMLz  Tests SVML behaves as expected Fc                sH   t  | _| jd t  | _| jd | jd tt| j|  d S )Nr   r?   )r	   rQ   r[   	fastflagssuperr   __init__)rv   rM   )	__class__r,   r-   r     s    zTestSVML.__init__c             O   sB   |rt tdd |D }t||| jd}t||| jd}||fS )Nc             S   s   g | ]}t |qS r,   )rI   Ztypeof)r)   rY   r,   r,   r-   r3     s    z$TestSVML.compile.<locals>.<listcomp>)rQ   )rE   tupler   rQ   r   )rv   rL   rM   kwargsZsigstdfastr,   r,   r-   r     s
    zTestSVML.compilec             G   s~   |s
t  S g }xf|D ]^}t|tjr6||d qt|tjrR||  qt|tjrj|| qt	dqW t |S )Nr*   z%Unsupported argument type encountered)
r   
isinstancer   Zndarrayr   copyZnumbernumbersNumber
ValueError)rv   rM   Znew_argsrY   r,   r,   r-   	copy_args  s    
zTestSVML.copy_argsc          
   O   s   | j |f| \}}|dd }|dd }|dd}|| j|  }	|j| j|  }
|j| j|  }tjj|
|	f| tjj||	f| td|P tdd: | j |f| \}}|r| || |r| || W d Q R X W d Q R X d S )Nstd_patternfast_patterncpu_namezskylake-avx512ri   rj   r@   )	r   popr   Zentry_pointr   ZtestingZassert_almost_equalr   check_svml_presence)rv   ZpyfuncrM   r   ZjitstdZjitfastr   r   r   Zpy_expectedZjitstd_resultZjitfast_resultr,   r,   r-   check*  s     zTestSVML.checkc             C   s   |j  }| || d S )N)rt   ru   ZassertIn)rv   rL   rk   rl   r,   r,   r-   r   I  s    
zTestSVML.check_svml_presencec             C   s4   t jjrdnd}| jtd|d | jtd|d d S )Nz$_sinz$sing      @)r   )r   )rI   rJ   rK   r   r   )rv   Zpatr,   r,   r-   test_scalar_contextM  s    zTestSVML.test_scalar_contextr   c             C   s   d}d}| j td||d d S )Nz__svml_sin8_ha,z__svml_sin8,
   )r   r   )r   r   )rv   r   r   r,   r,   r-   	test_svmlS  s    zTestSVML.test_svmlc             C   sP   d}t jtjd|gt jt jd}| \}}|jdkrLtd|j| f d S )Na  if 1:
            import os
            import numpy as np
            import math

            def math_sin_loop(n):
                ret = np.empty(n, dtype=np.float64)
                for x in range(n):
                    ret[x] = math.sin(np.float64(x))
                return ret

            def check_no_svml():
                try:
                    # ban the use of SVML
                    os.environ['NUMBA_DISABLE_INTEL_SVML'] = '1'

                    # delay numba imports to account for env change as
                    # numba.__init__ picks up SVML and it is too late by
                    # then to override using `numba.config`
                    import numba
                    from numba import config
                    from numba.tests.support import override_env_config
                    from numba.compiler import compile_isolated, Flags

                    # compile for overridden CPU, with and without fastmath
                    with override_env_config('NUMBA_CPU_NAME', 'skylake-avx512'),                          override_env_config('NUMBA_CPU_FEATURES', ''):
                        sig = (numba.int32,)
                        f = Flags()
                        f.set('nrt')
                        std = compile_isolated(math_sin_loop, sig, flags=f)
                        f.set('fastmath')
                        fast = compile_isolated(math_sin_loop, sig, flags=f)
                        fns = std, fast

                        # assert no SVML call is present in the asm
                        for fn in fns:
                            asm = fn.library.get_asm_str()
                            assert '__svml_sin' not in asm
                finally:
                    # not really needed as process is separate
                    os.environ['NUMBA_DISABLE_INTEL_SVML'] = '0'
                    config.reload_config()
            check_no_svml()
            z-c)stdoutstderrr   z/process failed with code %s: stderr follows
%s
)	
subprocessPopenr   
executablePIPEZcommunicate
returncoderE   decode)rv   codepopenouterrr,   r,   r-   test_svml_disabled[  s    -

zTestSVML.test_svml_disabled)r   r   r   rb   r   r   r   r   r   r   r   r   r   r   __classcell__r,   r,   )r   r-   r     s   
	r   __main__)r5   )CZ
__future__r   r   r   r9   r   r   r   	importlibr   r   	itertoolsr   r   rI   r   r   rz   Znumba.compilerr   r	   Z	numba.sixr
   Zsupportr   r   r   r{   rJ   Z
USING_SVMLZ
needs_svmlrr   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r$   r%   r&   r'   r   r_   itemsr\   r]   rV   rW   rf   rg   r   r   r   r   r   mainr,   r,   r,   r-   <module>   s   













4E 
