
    ܙd!C              	          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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mZ d dlmZmZmZ d dlZ ej        ej        d          Zdd	d
dZd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j0        e j0        gd-g d.g d/g g g g ej1        e j1        gej2        e j2        gej3        e j3        gg d0Z4g d1Z5d2 e46                                D             Z4d3 e46                                D             Z7d4 e46                                D             Z8d@d6Z9d7 Z:d8 Z;e G d9 d:e                      Z<e<=                                 d; Z>d< Z?e G d= d>e                      Z@eAd?k    r ejB                     dS dS )A    N)chaincombinations)configcpu)prangenjit)compile_isolatedFlags)TestCasetagoverride_env_configz"SVML tests need SVML to be presentnehalemhaswellskylake-avx512)          -prefer-256-bit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)r<   r3   r.   r4   r=   r   c                 @    i | ]\  }}t          |          d k    ||S )r   )len).0kvs      5lib/python3.11/site-packages/numba/tests/test_svml.py
<dictcomp>rE   T   s)    @@@tq!SVVaZZaZZZ    c                 4    g | ]\  }}d d |D             v |S )z<ufuncc                 ^    g | ]*}t          |                              d           d         +S  r   strsplitrA   ps     rD   
<listcomp>z<listcomp>.<listcomp>W   /    333A3q66<<$$Q'333rF    rA   frC   s      rD   rP   rP   V   sE     5 5 5TQ83333344 44q 44 44 44rF   c                 4    g | ]\  }}d d |D             v |S )z	<built-inc                 ^    g | ]*}t          |                              d           d         +S rI   rK   rN   s     rD   rP   z<listcomp>.<listcomp>Y   rQ   rF   rR   rS   s      rD   rP   rP   X   sE     5 5 5TQ;3333344 44q 44 44 44rF           c                 p   |dk    r-d                     d |D                       }||d| d|d}	n|dk    r@||d| dd                     |          d	}	|	|                    d
          rd|z  ndz  }	nF|dk    s|dk    sJ d                     d |D                       } dj        di t                      }	|dk    p|dk    }
|
r| dz   n| }|
r|dz  n|}|rdnd}t          j        rd|z   nd|z   }d|||fz  }|dk    r|g}d|g}n#|g}g }|dk    r|
s|dk    r|dd||dz  |fz  gz  }|	||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                     g | ]}|d z   S )z[0]rR   rA   as     rD   rP   z!func_patterns.<locals>.<listcomp>d       333QuW333rF   z[0] += math.(z)
numpyz += np.)intz.astype(np.%s)

ranger   c                     g | ]}|d z   S )z[i]rR   r\   s     rD   rP   z!func_patterns.<locals>.<listcomp>k   r^   rF   zP{pad}for i in {mode}({res}.size):
{pad}{pad}{res}[i] += math.{func}({arg_list})
float32	complex64rT   r   r   _haz$_$z__svml_%s%d%s,__svml_r   int32z%zmmrR   )join
startswithformatlocalsr   IS_OSX)funcargsresdtypemodevlenfastmathpadarg_listbodyis_f32rT   rC   	prec_suffscalar_func	svml_funccontainsavoidss                     rD   func_patternsr   \   s    x8833d33344-0S###tttXXXF	&)c333chhtnnnnEe.>.>u.E.EO"U**4Ow$("2"2"2"28833d33344 A" "" " i75K#7F$SA"QdA)EI"M4$q&&s1uK Aq)#44Ix=Y';199&9EW$4$4v/1ac92EEFFF6!!rF   c                     |  d| | d| S )z, Returns pretty name for given set of modes _rR   )rt   ru   rv   names       rD   usecase_namer      s%     ))d)D))4)))rF   c           
      D   t          | |||          } dj        di t                      }t          |dk    rt          nt
                    }|                     d          r|                    t                    }t                      }t                      }|D ]K}	t          |	dgd| |||          \  }
}}|
                    |           ||
z  }|
                    |           L|dz  }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)
r`   complexxretz        return retrR   )r   rn   ro   setnumpy_funcsother_funcsrm   
differencecomplex_funcs_excluder   updateexecglobals__doc__)rt   ru   rv   rw   r   rz   funcsr   r   rT   bcr]   ldicts                 rD   combo_svml_usecaser      s;    tT400D+ $$*7 7-3XX7 7D twK@@E	"" 8  !677uuHUUF  C5%dHMM1aa	  DEwyy%   E$K;&((rF   c                       e Zd ZdZdZ ej        d                    dg ee	          z                       Z
ed             Zed             Zed             ZdS )	TestSVMLGenerationz; Tests all SVML-generating functions produce desired calls F|z\$[a-z_]\w+,c                     t          | |          }	  |            \  }}n%# t          $ r t          j                    }d}Y nw xY w|                    ||d           d S )NF)statusmsg)getattr	Exception	traceback
format_excput)clstestnameoutqueuemethodokr   s         rD   	mp_runnerzTestSVMLGeneration.mp_runner   s{    h''	fhhGB 	 	 	&((CBBB	 	3//00000s     AAc                    	
                      d          rdk    rd S                      d          odk    }t          j        f	t           	fd            }t	          d                   }d| 
t           
|           t          j        | d          
fd	            }t           d
| |           d S )Nr   r`   rb   r   c            	        	 t          d         d                   \  } }}t          dt                             5  t          dt                             5  	  t	          d         d                   |           }n#  t          d| j        z             xY w	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |                              		fd|D             }	fd	|D             }| o| }d
                    
fd		                    d
          D                       }d| d| d| d| j         }||fS )Nrw   r   NUMBA_CPU_NAMENUMBA_CPU_FEATURESerror_modelrw   r   zraised while compiling c                     g | ]}|v|	S rR   rR   rA   patternasms     rD   rP   zITestSVMLGeneration._inject_test.<locals>.run_template.<locals>.<listcomp>   s    LLL'W^^g^^^rF   c                     g | ]}|v |	S rR   rR   r   s     rD   rP   zITestSVMLGeneration._inject_test.<locals>.run_template.<locals>.<listcomp>   s    EEEgnnWnnnrF   rc   c                 P    g | ]"}j                             |          rd |v |#S )")
asm_filtersearch)rA   liner   s     rD   rP   zITestSVMLGeneration._inject_test.<locals>.run_template.<locals>.<listcomp>   sE     E E E$N))$//E8;t 8CrF   zWhile expecting z	 and not z,
it contains:
z
when compiling )
r   r   vlen2cpuvlen2cpu_featuresr   r   r   inspect_asmrl   rM   )fnr   r   	jitted_fnmissedfoundr   detailr   r   r   rt   flagsru   sigrv   s            @rD   run_templatez5TestSVMLGeneration._inject_test.<locals>.run_template   s}   #5eT46;J6G6;Fm$E $E B& %%5x~FF J J$%9;LT;RSSJ JJ!HS53D16}1E!H !H !HHJ!L !LIIJ#$=bj$HIII 	J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J '',,CLLLLXLLLFEEEEFEEEE)E	BYYE E E E#))D// E E EF FF/6 / /E / /!'/ /"$*/ / 
 s7NsN   CB0'BB0B  B0$C0B4	4C7B4	8CCCr   run_zNot implementedc                 *   t          j        d          }|                                }|                    t	          |           j        	|g          }|                                 |                    d          }|j        }|8|| 	                    d           n |dk     r| 	                    d|  d           | 
                    |dd	
           |                                }|d         }|d         }|                     ||
           d S )Nspawn)targetrr      )timeoutzProcess timed out.r   zProcess terminated with signal .zprocess ended unexpectedly)r   r   r   )mpget_contextQueueProcesstyper   startrl   exitcodefailassertEqualget
assertTrue)
selfctxqrO   term_or_timeoutr   outr   r   r   s
            rD   test_runnerz4TestSVMLGeneration._inject_test.<locals>.test_runner   s   .))C		A4::#7xmLLAGGIII  ffRf00OzH&#II23333\\IIL	LLLMMMXq.JKKK%%''C]Fe*COOFO,,,,,rF   test_)rm   numbaint64staticmethodr   setattrunittest
skipUnless)r   rt   ru   rv   r   skippedr   postfixr   r   r   s   `````    @@rD   _inject_testzTestSVMLGeneration._inject_test   s    I&& 	47??F""5))7dai{n		 	 	 	 	 	 	 	 	 
	6 udD%-@@#'##X|,,,		[*;	<	<	- 	- 	- 	- 
=	<	-* 	&W&&44444rF   c                    ddddddddg}dD ];}t           D ]1}|D ],}dD ]'}|                     |||t          |                     (-2<d	D ]7}t          | | t	          d
          t          | |                               8d S )NFr`   usecase)rw   r   r   Tfastmath_usecase)rg   float64rf   rk   )rY   rd   r   r`   )test_int32_range4_usecase	important)r   r   dictr   r   r   )r   	flag_listrt   rv   r   ru   ns          rD   autogeneratezTestSVMLGeneration.autogenerate   s    !&g%' '!%W.0 02	
 D 	I 	IE  I I& I IE D I I((dDKKHHHHIII
 	? 	?AC,C,,WS!__==>>>>	? 	?rF   N)__name__
__module____qualname__r   _numba_parallel_test_recompilerl   list
svml_funcsr   classmethodr   r   r   rR   rF   rD   r   r      s        EE "CHHn%5dd:6F6F%FGGHHJ1 1 [1 >5 >5 [>5@ ? ? [? ? ?rF   r   c                 *    t          j        |           S N)mathr   )r   s    rD   math_sin_scalarr     s    8A;;rF   c                     t          j        | t           j                  }t          |           D ]+}t	          j        t          j        |                    ||<   ,|S )Nrt   )npemptyr   rd   r   r   )r   r   r   s      rD   math_sin_loopr    sO    
(1BJ
'
'
'C1XX ) )"*Q--((AJrF   c                   V     e Zd ZdZdZ fdZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Z xZS )TestSVMLz  Tests SVML behaves as expected Fc                     t                      | _        d| j        _        t                      | _        d| j        _        t	          j        d          | j        _         t          t          |           j	        |  d S )NT)
r
   r   nrt	fastflagsr   FastMathOptionsrw   superr  __init__)r   rr   	__class__s     rD   r	  zTestSVML.__init__"  sc    WW

 !"%"5d";";&h&----rF   c                     |rJ t          d |D                       }t          ||| j                  }t          ||| j                  }||fS )Nc                 6    g | ]}t          j        |          S rR   )r   typeof)rA   r   s     rD   rP   z$TestSVML.compile.<locals>.<listcomp>.  s     333U\!__333rF   )r   )tupler	   r   r  )r   rq   rr   kwargsr   stdfasts          rD   r   zTestSVML.compile,  s`    33d33344tS
;;;c@@@DyrF   c                    |st                      S g }|D ]}t          |t          j                  r)|                    |                    d                     Et          |t          j                  r(|                    |                                           t          |t          j                  r|                    |           t          d          t          |          S )NrB   z%Unsupported argument type encountered)
r  
isinstancer   ndarrayappendcopynumbernumbersNumber
ValueError)r   rr   new_argsr   s       rD   	copy_argszTestSVML.copy_args5  s     	77N 	J 	JA!RZ(( Js,,,,Ary)) J))))Aw~.. J"""" !HIIIXrF   c                     | j         |g|R  \  }}|                    dd           }|                    dd           }|                    dd          }|                    dd          }	 | | j        |  }
 |j         | j        |  } |j         | j        |  }t	          j        j        ||
fi | t	          j        j        ||
fi | t          d|          5  t          d|	          5   | j         |g|R  \  }}|r|                     ||           |r|                     ||           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )	Nstd_patternfast_patterncpu_namer   cpu_featuresr   r   r   )	r   popr  entry_pointr   testingassert_almost_equalr   check_svml_presence)r   pyfuncrr   r  jitstdjitfastr  r  r   r!  py_expectedjitstd_resultjitfast_results                rD   checkzTestSVML.checkD  sT   &$,v5555jj55zz.$77::j*:;; zz.2CDD fndnd34 +*NDND,AB -,ndnd.CD 	
&}kLLVLLL

&~{MMfMMM !!18<< 	@ 	@ !5|DD	@ 	@ +dl69D999OFG >((=== @((,???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s7   E,AD:.E:D>	>ED>	EEEc                 d    |j                                         }|                     ||           d S r   )libraryget_asm_strassertIn)r   rq   r   r   s       rD   r&  zTestSVML.check_svml_presencef  s/    l&&((gs#####rF   c                     t           j        rdnd}|                     t          d|           |                     t          d|           d S )Nz$_sinz$sing      @)r  )r  )r   rp   r-  r   )r   pats     rD   test_scalar_contextzTestSVML.test_scalar_contextj  sH    2ggF

?BC
888

?BS
99999rF   c                 J    d}d}|                      t          d||           d S )Nz__svml_sin8_ha,z__svml_sin8,
   )r  r  )r-  r  )r   r  r  s      rD   	test_svmlzTestSVML.test_svmlp  s.      

="#D
IIIIIrF   c                    d}t          j        t          j        d|gt           j        t           j                  }|                                \  }}|j        dk    r-t          d|j        d|                                d          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.core import cpu
                    from numba.tests.support import override_env_config
                    from numba.core.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.nrt = True
                        std = compile_isolated(math_sin_loop, sig, flags=f)
                        f.fastmath = cpu.FastMathOptions(True)
                        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 z: stderr follows
rc   )	
subprocessPopensys
executablePIPEcommunicate
returncodeAssertionErrordecode)r   codepopenr   errs        rD   test_svml_disabledzTestSVML.test_svml_disabledw  s    -\  ^T4(?:?< < < $$&&Sq   .!!!3::<<<<12 2 2 ! rF   c                     t          dhd          d             } |d           |                     d|                    |j        d                   v            d S )Nr  r`   r   c                     t          j        | dz  t           j                  }t          j        |          }t	          |j                  D ]*}||xx         t          j        ||                   z  cc<   +|S )Nr   r   )r   r   r   
empty_likerd   sizer   r'   )r   r   r   is       rD   implz@TestSVML.test_svml_working_in_non_isolated_context.<locals>.impl  sk    (1q5
333A-""C38__ . .FFFdi!oo-FFFFJrF      intel_svmlccr   )r   r   inspect_llvm
signatures)r   rM  s     rD   )test_svml_working_in_non_isolated_contextz2TestSVML.test_svml_working_in_non_isolated_context  sl    	xW	5	5	5	 	 
6	5	 	Q$*;*;DOA<N*O*OOPPPPPrF   )r   r   r   r   r   r	  r   r  r-  r&  r4  r7  rG  rR  __classcell__)r
  s   @rD   r  r    s        ** ". . . . .     @  @  @D$ $ $: : :J J J62 62 62p	Q 	Q 	Q 	Q 	Q 	Q 	QrF   r  __main__)rW   )Cr   r`   r   r;  r  	importlibr=  r   r   multiprocessingr   	itertoolsr   r   r   
numba.corer   r   r   r   numba.core.compilerr	   r
   numba.tests.supportr   r   r   r   r   
USING_SVML
needs_svmlr   r   r   r   r   r   r   r   r   r   r    r!   r'   r(   r)   r.   r3   r4   r;   r<   r=   r   r   itemsr   r   r   r   r   r   r   r   r  r  r   mainrR   rF   rD   <module>r_     s#                 



 				         ) ) ) ) ) ) ) )  " " " " " " " "         7 7 7 7 7 7 7 7 B B B B B B B B B B  X !2!EG G
 Y+;<< r&788 *	!*	!* 
"* 
!	*
 
!* 	{* 
|* 	{* 
|* "* 	{* 
|* "* "* "*  "!*" "#* *$ #%*& 
z'*( 	{)** "+*, "-*. "/*0 "1*2 $*%3*4 "5*6 "7*8 "9*: ";*< $*%=*> $*%?*@ "A*B "C*D "E* *F #!#S* * *
X JII  A@z//11@@@
5 5Z--// 5 5 55 5Z--// 5 5 5(" (" (" ("V* * *) ) )4 b? b? b? b? b? b? b? b?J    ! ! !     \Q \Q \Q \Q \Qx \Q \Q \Q~ zHMOOOOO rF   