
\K]c           @  s  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 d  d l m Z m Z d  d l m Z d d	 l m Z m Z m Z e j e j j d
  Z  i d d 6d d 6d d 6Z! i* e j" e j" g d 6e j# e j# g d 6g  d 6e j$ e j$ g d 6e j% e j% g d 6e j& g d 6e j' g d 6e j( g d 6e j) g d 6g  d 6e j* g d 6e j+ g d 6g  d 6g  d 6g  d 6g  d  6g  d! 6e j, e j, g d" 6e j- g d# 6e j. g d$ 6g  d% 6g  d& 6g  d' 6g  d( 6e j/ e j/ g d) 6g  d* 6g  d+ 6g  d, 6g  d- 6e j0 e j0 g d. 6e j1 e j1 g d/ 6g  d0 6g  d1 6g  d2 6g  d3 6g  d4 6g  d5 6e j2 e j2 g d6 6e j3 e j3 g d7 6e j4 e j4 g d8 6e j5 e j5 g d9 6g  d: 6Z6 d7 d8 d. d) d/ d9 d g Z7 d;   e6 j8   D Z6 g  e6 j8   D]D \ Z9 Z: d< g  e: D] Z; e< e;  j= d=  d> ^ qgk rQe9 ^ qQZ> g  e6 j8   D]D \ Z9 Z: d? g  e: D] Z; e< e;  j= d=  d> ^ qk re9 ^ qZ? dJ d@  Z@ dA   ZA dB   ZB e  dC e f dD     Y ZC eC jD   dE   ZE dF   ZF e  dG e f dH     Y ZG eH dI k r}e jI   n  d S(K   i(   t   divisiont   print_functionN(   t   chaint   combinations(   t   pranget   unittest_support(   t   cpu(   t   compile_isolatedt   Flags(   t   exec_i   (   t   TestCaset   tagt   override_env_configs"   SVML tests need SVML to be presentt   nehalemi   t   haswelli   s   skylake-avx512i   t   sint   cost   powt   expt   logt   acost   acosht   asint   asinht   atan2t   atant   atanht   cbrtt   cdfnormt
   cdfnorminvt   ceilt   cosdt   cosht   erft   erfct   erfcinvt   erfinvt   exp10t   exp2t   expm1t   floort   fmodt   hypott   invsqrtt   log10t   log1pt   log2t   logbt	   nearbyintt   rintt   roundt   sindt   sinht   sqrtt   tant   tanht   truncc         C  s1   i  |  ]' \ } } t  |  d  k r | |  q S(   i    (   t   len(   t   .0t   kt   v(    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pys
   <dictcomp>R   s   	 s   <ufunct    i    s	   <built-inc         C  s`  | d k rK d j  g  | D] } | d ^ q  }	 d | | |  |	 f }
 n | d k r d | | |  d j  |  f }
 |
 | j d  r d | n d	 7}
 nV | d
 k s | d k s t  d j  g  | D] } | d ^ q  }	 d j t     }
 | d k p| d k } | r|  d n |  } | r0| d n | } t | d t  rNd n d } t j j	 rjd | n d | } d | | | f } | d k r| g } d | g } nQ | g } g  } | d k r| s| d k r| d d | | d | f g 7} n  |  d k rS| d k r,d g } | | g } qS| d k rSd g } | | g } qSn  |
 | | f S(   s   
    For a given function and its usage modes,
    returns python code and assembly patterns it should and should not generate
    t   scalart   ,s   [0]s   %s%s[0] += math.%s(%s)
t   numpys   %s%s += np.%s(%s)t   ints   .astype(np.%s)
s   
t   rangeR   s   [i]sP   {pad}for i in {mode}({res}.size):
{pad}{pad}{res}[i] += math.{func}({arg_list})
t   float32t	   complex64t   fi   t   fastmatht    t   _has   $_t   $s   __svml_%s%d%s,t   __svml_i   t   int32s   %zmmR5   t   sqrtst   vsqrtp(
   t   joint
   startswitht   AssertionErrort   formatt   localst   getattrt   Falset   numbat   configt   IS_OSX(   t   funct   argst   rest   dtypet   modet   vlent   flagst   padt   at   arg_listt   bodyt   is_f32RE   R<   t	   prec_sufft   scalar_funct	   svml_funct   containst   avoids(    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   func_patternsZ   s>    &&& 		$		c         C  s   d j  t     S(   s,    Returns pretty name for given set of modes s%   {dtype}_{mode}{vlen}_{flags.__name__}(   RQ   RR   (   R[   R\   R]   R^   (    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   usecase_name   s    c      	   C  s  t  |  | | |  } d j t     } t | d k r< t n t  } |  j d  rf | j t  } n  t   } t   } x\ | D]T }	 t	 |	 d g d |  | | |  \ }
 } } | j
 |  | |
 7} | j
 |  q W| d
 7} i  } t | t   |  | | | _ | | | | f S(   s?    Combine multiple function calls under single umbrella usecase s\   def {name}(n):
        x   = np.empty(n*8, dtype=np.{dtype})
        ret = np.empty_like(x)
R@   t   complext   xt   retR=   i   s
   return rets           s           return ret(   Rj   RQ   RR   t   sett   numpy_funcst   other_funcsRO   t
   differencet   complex_funcs_excludeRi   t   updateR	   t   globalst   __doc__(   R[   R\   R]   R^   t   nameRb   t   funcsRg   Rh   RE   t   bt   cR`   t   ldict(    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   combo_svml_usecase   s"    		*

t   TestSVMLGenerationc           B  sW   e  Z d  Z e Z e j d j d g e e	    Z
 e d    Z e d    Z RS(   s;    Tests all SVML-generating functions produce desired calls t   |s   \$[a-z_]\w+,c           s   | j  d  r | d k r d  S| j  d  o7  d k } | |   f   t j | d       f d    } t  d t     |  d  S(   NRk   R@   RA   i   s   Not implementedc   
        sX  t      \ } } } t d t   W t d d  A y t | t j f d  } Wn t d | j   n XWd  QXWd  QX| j j	   } g  | D] } | | k r | ^ q } g  | D] } | | k r | ^ q } |  j
 | o | d t |  t |  d j g  | j d  D]* }	  j j |	  rd |	 k r|	 ^ q | j f  d  S(	   Nt   NUMBA_CPU_NAMEt   NUMBA_CPU_FEATURESRG   R^   s   raised while compiling s?   While expecting %s and no %s,
it contains:
%s
when compiling %ss   
t   "(   R{   R   t   vlen2cpuR   RU   t   int64t	   ExceptionRu   t   libraryt   get_asm_strt
   assertTruet   strRN   t   splitt
   asm_filtert   search(
   t   selft   fnRg   Rh   t   jitt   asmt   patternt   missedt   foundt   line(   RY   t   clsR^   R]   (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   test_template   s     #%%*t   test_(   RO   t   unittestt
   skipUnlesst   setattrRj   (   R   R[   R\   R]   R^   t   skippedR   (    (   RY   R   R^   R]   s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   _inject_test   s    +c      	   C  s  d g } t  g  t t |  d  D] } t t | |   ^ q# g   } g  } x | D] } t   } | j d  | j d d  d j | d  | _ x: | D]2 } | j | i t	 j
 t  d 6j | t   q W| j |  qW WxW d D]O } xF t D]> } x5 | D]- } x$ d D] }	 |  j | |	 | |  qWqWq Wq Wt j d d k rx6 d D]+ }
 t |  |
 t d  t |  |
    q[Wn  d  S(   NRF   i   t   nrtt   error_modelR@   t   _t   usecaseRD   t   float64RC   RK   R>   RB   R   i    i   t   test_int32_range4_usecaset	   important(   R   (   RD   R   RC   RK   (   R>   RB   R   R@   (   R   (   t   sumRB   R9   t   listR   R   Rn   RN   t   __name__R   t   FastMathOptionst   Truet   gett   appendR   R   t   syst   version_infoR   R   RS   (   R   t
   test_flagsRl   t	   flag_listt   ftR^   RE   R[   R]   R\   t   n(    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   autogenerate   s,    		8	&(   R   t
   __module__Ru   RT   t   _numba_parallel_test_t   ret   compileRN   R   t
   svml_funcsR   t   classmethodR   R   (    (    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyR|      s
   %!c         C  s   t  j |   S(   N(   t   mathR   (   Rl   (    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   math_sin_scalar   s    c         C  sO   t  j |  d t  j } x0 t |   D]" } t j t  j |   | | <q% W| S(   NR[   (   t   npt   emptyR   RB   R   R   (   R   Rm   Rl   (    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   math_sin_loop   s     t   TestSVMLc           B  sh   e  Z d  Z e Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 e d  d    Z d	   Z RS(
   s     Tests SVML behaves as expected c         G  sn   t    |  _ |  j j d  t    |  _ |  j j d  |  j j d t j t   t t |   j	 |   d  S(   NR   RF   (
   R   R^   Rn   t	   fastflagsR   R   R   t   superR   t   __init__(   R   RY   (    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyR     s    c         O  so   | s t   t g  | D] } t j |  ^ q  } t | | d |  j } t | | d |  j } | | f S(   NR^   (   RP   t   tupleRU   t   typeofR   R^   R   (   R   RX   RY   t   kwargsRl   t   sigt   stdt   fast(    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyR     s
    (c         G  s   | s t    Sg  } x | D] } t | t j  rK | j | j d   q t | t j  rs | j | j    q t | t j  r | j |  q t	 d   q Wt  |  S(   NR;   s%   Unsupported argument type encountered(
   R   t
   isinstanceR   t   ndarrayR   t   copyt   numbert   numberst   Numbert
   ValueError(   R   RY   t   new_argsRl   (    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt	   copy_args  s    c         O  s9  |  j  | |  \ } } | j d d   } | j d d   } | j d d  } | |  j |     }	 | j |  j |     }
 | j |  j |     } t j j |
 |	 |  t j j | |	 |  t d |  e t d d  O |  j  | |  \ } } | r|  j	 | |  n  | r)|  j	 | |  n  Wd  QXWd  QXd  S(   Nt   std_patternt   fast_patternt   cpu_names   skylake-avx512R~   R   RG   (
   R   t   popt   NoneR   t   entry_pointR   t   testingt   assert_almost_equalR   t   check_svml_presence(   R   t   pyfuncRY   R   t   jitstdt   jitfastR   R   R   t   py_expectedt   jitstd_resultt   jitfast_result(    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   check-  s     c         C  s#   | j  j   } |  j | |  d  S(   N(   R   R   t   assertIn(   R   RX   R   R   (    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyR   L  s    c         C  sH   t  j j r d n d } |  j t d d | |  j t d d | d  S(   Ns   $_sins   $sing      @R   R   (   RU   RV   RW   R   R   (   R   t   pat(    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   test_scalar_contextP  s    R   c         C  s,   d } d } |  j  t d d | d | d  S(   Ns   __svml_sin8_ha,s   __svml_sin8,i
   R   R   (   R   R   (   R   R   R   (    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt	   test_svmlV  s    c         C  sz   d } t  j t j d | g d t  j d t  j } | j   \ } } | j d k rv t d | j | j   f   n  d  S(   Ns  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.targets import cpu
                    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', 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()
            s   -ct   stdoutt   stderri    s/   process failed with code %s: stderr follows
%s
(	   t
   subprocesst   PopenR   t
   executablet   PIPEt   communicatet
   returncodeRP   t   decode(   R   t   codet   popent   outt   err(    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   test_svml_disabled^  s    .(   R   R   Ru   RT   R   R   R   R   R   R   R   R   R   R   (    (    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyR     s   	
						t   __main__s           (J   t
   __future__R    R   R   R@   R   R   R   t	   importlibR   R   t	   itertoolsR   R   RU   R   R   R   t   numba.targetsR   t   numba.compilerR   R   t	   numba.sixR	   t   supportR
   R   R   R   RV   t
   USING_SVMLt
   needs_svmlR   R   R   R   R   R   R   R   R   R   R   R    R!   R"   R'   R,   R-   R4   R5   R6   R7   R   Rr   t   itemsRE   R<   t   pR   R   Ro   Rp   Ri   Rj   R{   R|   R   R   R   R   R   t   main(    (    (    s4   lib/python2.7/site-packages/numba/tests/test_svml.pyt   <module>   s   	
;;4		G
		