o
    in=                  	   @   s  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mZ d dlmZmZmZ d dlZeejdZdddd	Zd
d
dd	ZdddZi deje jgdeje jgdg deje jgdeje jgde jgde jgde jgde jgdg de j gde j!gdg dg dg dg dg i dej"e j"gde j#gd e j$gd!g d"g d#g d$g d%ej%e j%gd&g d'g d(g d)g d*ej&e j&gd+ej'e j'gd,g d-g d.g g g g ej(e j(gej)e j)gej*e j*gg d/Z+g d0Z,d1d2 e+- D Z+d3d4 e+- D Z.d5d4 e+- D Z/dFd7d8Z0d9d: Z1d;d< Z2eG d=d> d>eZ3e34  d?d@ Z5dAdB Z6eG dCdD dDeZ7e8dEkrWe9  dS dS )G    N)njitprange)config)TestCasetagoverride_env_configz"SVML tests need SVML to be presentnehalemhaswellzskylake-avx512)          z-prefer-256-bit)NUMBA_CPU_NAMENUMBA_CPU_FEATURESsincospowexplogacosacoshasinasinhatan2atanatanhcbrtcdfnorm
cdfnorminvceilcosdcosherferfcerfcinverfinvexp10exp2expm1floorfmodhypotinvsqrtlog10log1plog2logb	nearbyint)rintroundsindsinhtantanhtrunc)r6   r-   r(   r.   r7   r   c                 C   s"   i | ]\}}t |d kr||qS )r   )len).0kv r=   R/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/numba/tests/test_svml.py
<dictcomp>U   s   " r?   c                 C   &   g | ]\}}d dd |D v r|qS )z<ufuncc                 S      g | ]}t |d d qS  r   strsplitr:   pr=   r=   r>   
<listcomp>X       <listcomp>.<listcomp>r=   r:   fr<   r=   r=   r>   rI   W       rI   c                 C   r@   )z	<built-inc                 S   rA   rB   rD   rG   r=   r=   r>   rI   Z   rJ   rK   r=   rL   r=   r=   r>   rI   Y   rN           c                 C   s\  |dkrd dd |D }d||| |f }	n:|dkr4d||| d |f }	|	|dr0d	| nd
7 }	n|dks>|dks>J d dd |D }djdi t }	|dkpX|dk}
|
r_| d n| }|
rg|d n|}|rmdnd}tjrvd| nd| }d|||f }|dkr|g}d|g}n|g}g }|dkr|
s|dkr|dd||d |f g7 }|	||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      g | ]}|d  qS )z[0]r=   r:   ar=   r=   r>   rI   e       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   rR   )z[i]r=   rS   r=   r=   r>   rI   l   rU   zP{pad}for i in {mode}({res}.size):
{pad}{pad}{res}[i] += math.{func}({arg_list})
float32	complex64rM   r
   r   _haz$_$z__svml_%s%d%s,__svml_r   int32z%zmmNr=   )join
startswithformatlocalsr   IS_OSX)funcargsresdtypemodevlenfastmathpadarg_listbodyis_f32rM   r<   	prec_suffscalar_func	svml_funccontainsavoidsr=   r=   r>   func_patterns]   s6   

ru   c                 C   s   |  d| | d| S )z, Returns pretty name for given set of modes _r=   )rh   ri   rj   namer=   r=   r>   usecase_name   s   rx   c              	   C   s   t | |||}djdi t }t|dkrtnt}| dr$|t}t }t }|D ]}	t	|	dgd| |||\}
}}|
| ||
7 }|
| q,|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)
rV   complexxretz        return retNr=   )rx   rb   rc   setnumpy_funcsother_funcsra   
differencecomplex_funcs_excluderu   updateexecglobals__doc__)rh   ri   rj   rk   rw   rn   funcsrs   rt   rM   bcrT   ldictr=   r=   r>   combo_svml_usecase   s*   



r   c                   @   sR   e Zd ZdZdZeddgee	 Z
edd 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                 C   sL   t | |}z| \}}W n ty   t }d}Y nw |||d d S )NF)statusmsg)getattr	Exception	traceback
format_excput)clstestnameoutqueuemethodokr   r=   r=   r>   	mp_runner   s   
zTestSVMLGeneration.mp_runnerc           	         s    drdkrd S  dodk}tjft fdd}td }d| t | t| d	fd
d}t d| | d S )Nry   rV   rW   r
   c            	   	      s,  t d d \} }}tdt 9 tdt " ztd d d| }W n
   td| j W d    n1 sCw   Y  W d    n1 sRw   Y  |  fdd	|D } fd
d	|D }| os| }dfdd	 	dD }d| d| d| d| j }||fS )Nrk   rw   r   r   error_modelrk   r   zraised while compiling c                    s   g | ]}| vr|qS r=   r=   r:   patternasmr=   r>   rI          zITestSVMLGeneration._inject_test.<locals>.run_template.<locals>.<listcomp>c                    s   g | ]}| v r|qS r=   r=   r   r   r=   r>   rI      r   rX   c                    s$   g | ]} j |rd |vr|qS )")
asm_filtersearch)r:   line)r   r=   r>   rI      s    
zWhile expecting z	 and not z,
it contains:
z
when compiling )
r   r   vlen2cpuvlen2cpu_featuresr   r   r   inspect_asmr`   rF   )	fnrs   rt   	jitted_fnmissedfoundr   detailr   )r   rh   flagsri   sigrj   r   r>   run_template   sB   

 
z5TestSVMLGeneration._inject_test.<locals>.run_templaterw   run_zNot implementedc           	         s   t d}| }|jt| j |gd}|  |jdd}|j}|d u r>|d u r0| 	d n|dk r>| 	d|  d | j
|dd	d
 | }|d }|d }| j||d
 d S )Nspawn)targetrf      )timeoutzProcess timed out.r   zProcess terminated with signal .zprocess ended unexpectedly)r   r   r   )mpget_contextQueueProcesstyper   startr`   exitcodefailassertEqualget
assertTrue)	selfctxqrH   term_or_timeoutr   outr   r   )r   r=   r>   test_runner   s    
z4TestSVMLGeneration._inject_test.<locals>.test_runnertest_)ra   numbaint64staticmethodrx   setattrunittest
skipUnless)	r   rh   ri   rj   r   skippedr   postfixr   r=   )r   rh   r   ri   r   r   rj   r>   _inject_test   s   
zTestSVMLGeneration._inject_testc                 C   s|   ddddddddg}dD ]}t D ]}|D ]}dD ]}| |||t| qqqqd	D ]}t| |td
t| | q,d S )NFrV   usecase)rk   r   rw   Tfastmath_usecase)r[   float64rZ   r_   )rP   rY   r   rV   )test_int32_range4_usecase	important)r   r   dictr   r   r   )r   	flag_listrh   rj   r   ri   nr=   r=   r>   autogenerate   s$   zTestSVMLGeneration.autogenerateN)__name__
__module____qualname__r   _numba_parallel_test_recompiler`   list
svml_funcsr   classmethodr   r   r   r=   r=   r=   r>   r      s    
	
@r   c                 C   s
   t | S N)mathr   )rz   r=   r=   r>   math_sin_scalar  s   
r   c                 C   6   t j| t jd}t| D ]}tt |||< q|S Nrh   npemptyr   rY   r   r   r   r{   rz   r=   r=   r>   math_sin_loop     r   c                   @   s   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZddddZ	dd Z
ejeddd Zdd Zejeddd Zdd Zejddieddd Zdd Zd S )!TestSVMLz  Tests SVML behaves as expected Fc                 O   sJ   |rJ t dd |D }t||}t|dd|}|j| |j| fS )Nc                 S   s   g | ]}t |qS r=   )r   typeof)r:   rz   r=   r=   r>   rI   %  s    z$TestSVML.compile.<locals>.<listcomp>Trk   )tupler   	overloads)r   re   rf   kwargsr   stdfastr=   r=   r>   r   #  s
   zTestSVML.compilec                 G   sx   |st  S g }|D ].}t|tjr||d q	t|tjr(||  q	t|tjr4|| q	t	dt |S )Nr;   z%Unsupported argument type encountered)
r   
isinstancer   ndarrayappendcopynumbernumbersNumber
ValueError)r   rf   new_argsrz   r=   r=   r>   	copy_args,  s   zTestSVML.copy_argsc           	      O   st   | j |g|R  \}}|| j|  }|j| j|  }|j| j|  }tjj||fi | tjj||fi | d S r   )r   r   entry_pointr   testingassert_almost_equal)	r   pyfuncrf   r   jitstdjitfastpy_expectedjitstd_resultjitfast_resultr=   r=   r>   check_result;  s   zTestSVML.check_resultc                 O   sV   | dd }| dd }| j|g|R  \}}|r| || |r)| || d S d S )Nstd_patternfast_pattern)popr   check_svml_presence)r   r  rf   r   r
  r  r  r  r=   r=   r>   	check_asmK  s   zTestSVML.check_asmbothwhatc                O   sd   |dv sJ |dks|dkr| j |g|R i | |dks"|dkr0| j|g|R i | d S d S )N)r  resultr   r  r  r   )r	  r  )r   r  r  rf   r   r=   r=   r>   checkW  s   zTestSVML.checkc                 C   s   |j  }| || d S r   )libraryget_asm_strassertIn)r   re   r   r   r=   r=   r>   r  ^  s   
zTestSVML.check_svml_presence)envvarsc                 C   s6   t jrdnd}| jtdd|d | jtdd|d d S )Nz$_sinz$sin      @r   )r  r
  )r  r  )r   rd   r  r   )r   patr=   r=   r>   test_scalar_context_asmb  s   z TestSVML.test_scalar_context_asmc                 C      | j tddd d S )Nr  r  r  )r  r   r   r=   r=   r>   test_scalar_context_resulti     z#TestSVML.test_scalar_context_resultc                 C   s    d}d}| j tdd||d d S )Nz__svml_sin8_ha,z__svml_sin8,
   r   )r  r
  r  r  r   )r   r   r   r=   r=   r>   test_svml_asmm  s
   
zTestSVML.test_svml_asmc                 C   r  )Nr  r  r  r   r  r=   r=   r>   test_svml_resultv  r  zTestSVML.test_svml_resultNUMBA_DISABLE_INTEL_SVML1c                 C   sd   dd }t jf}t||}t|dd|}|j| |j| f}|D ]}|j }| d| q"d S )Nc                 S   r   r   r   r   r=   r=   r>   r   ~  r   z2TestSVML.test_svml_disabled.<locals>.math_sin_loopTr   
__svml_sin)r   r_   r   r   r  r  assertNotIn)r   r   r   r   r   fnsr   r   r=   r=   r>   test_svml_disabledz  s   
zTestSVML.test_svml_disabledc                 C   s<   t dhdddd }|d | d||jd v  d S )	Nr   rV   r   c                 S   sL   t j| d t jd}t |}t|jD ]}||  t|| 7  < q|S )Nr   r   )r   r   r   
empty_likerY   sizer   r!   )r   rz   r{   ir=   r=   r>   impl  s
   
z@TestSVML.test_svml_working_in_non_isolated_context.<locals>.impl   intel_svmlccr   )r   r   inspect_llvm
signatures)r   r,  r=   r=   r>   )test_svml_working_in_non_isolated_context  s   
z2TestSVML.test_svml_working_in_non_isolated_contextN)r   r   r   r   r   r   r   r	  r  r  r  r   run_test_in_subprocess_skylake_axv512_envvarsr  r  r!  r"  r(  r1  r=   r=   r=   r>   r     s(    	





r   __main__)rO   ):r   rV   r   r   r   r   multiprocessingr   r   r   r   
numba.corer   numba.tests.supportr   r   r   r   r   
USING_SVML
needs_svmlr   r   r3  r   r   r   r   r   r   r   r   r   r   r!   r"   r#   r(   r-   r.   r5   r6   r7   r   r   itemsr}   r~   ru   rx   r   r   r   r   r   r   r   mainr=   r=   r=   r>   <module>   s    	
 !"#


,
+e
}