ó
 YO\c           @   sã   d  d l  Z  e  j e ƒ 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 e rŠ d  d l m Z m Z m Z n  d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   _set_num_threadst   MAX_THREADS(   t   use_vml(   t   _get_vml_versiont   _set_vml_accuracy_modet   _set_vml_num_threadsc           C   s   t  r t ƒ  Sd Sd S(   s    Get the VML/MKL library version.N(   R   R   t   None(    (    (    s,   lib/python2.7/site-packages/numexpr/utils.pyt   get_vml_version   s    c         C   s‘   t  r‰ i d d	 6d d 6d d 6d d 6} i d d 6d d 6d d 6} |  | j ƒ  k rd t d ƒ ‚ n  t | j |  d ƒ ƒ } | j | ƒ Sd	 Sd	 S(
   s>  
    Set the accuracy mode for VML operations.

    The `mode` parameter can take the values:
    - 'high': high accuracy mode (HA), <1 least significant bit
    - 'low': low accuracy mode (LA), typically 1-2 least significant bits
    - 'fast': enhanced performance mode (EP)
    - None: mode settings are ignored

    This call is equivalent to the `vmlSetMode()` in the VML library.
    See:

    http://www.intel.com/software/products/mkl/docs/webhelp/vml/vml_DataTypesAccuracyModes.html

    for more info on the accuracy modes.

    Returns old accuracy settings.
    i    i   t   lowi   t   highi   t   fasts9   mode argument must be one of: None, 'high', 'low', 'fast'N(   R   R   t   keyst
   ValueErrorR   t   get(   t   modet   acc_dictt   acc_reverse_dictt   retval(    (    s,   lib/python2.7/site-packages/numexpr/utils.pyt   set_vml_accuracy_mode"   s    "c         C   s   t  r t |  ƒ n  d S(   s[  
    Suggests a maximum number of threads to be used in VML operations.

    This function is equivalent to the call
    `mkl_domain_set_num_threads(nthreads, MKL_DOMAIN_VML)` in the MKL
    library.  See:

    http://www.intel.com/software/products/mkl/docs/webhelp/support/functn_mkl_domain_set_num_threads.html

    for more info about it.
    N(   R   R   (   t   nthreads(    (    s,   lib/python2.7/site-packages/numexpr/utils.pyt   set_vml_num_threadsA   s    c         C   s   t  |  ƒ } | S(   sV  
    Sets a number of threads to be used in operations.

    Returns the previous setting for the number of threads.

    During initialization time Numexpr sets this number to the number
    of detected cores in the system (see `detect_number_of_cores()`).

    If you are using Intel's VML, you may want to use
    `set_vml_num_threads(nthreads)` to perform the parallel job with
    VML instead.  However, you should get very similar performance
    with VML-optimized functions, and VML's parallelizer cannot deal
    with common expresions like `(x+1)*(x-2)`, while Numexpr's one
    can.
    (   R    (   R   t   old_nthreads(    (    s,   lib/python2.7/site-packages/numexpr/utils.pyt   set_num_threadsQ   s    c          C   s  d t  j ƒ  k r- t j d ƒ t d ƒ d St }  t ƒ  } d t j k rZ t	 }  t
 } nL | t
 k r€ t j d | t
 f ƒ n  | d k r¦ t j d | ƒ d } n  d t j k rË t t j d ƒ } nE d	 t j k rð t t j d	 ƒ } n  | } |  st j d
 | ƒ n  t | ƒ | S(   s	  
    Detects the environment variable 'NUMEXPR_MAX_THREADS' to set the threadpool 
    size, and if necessary the slightly redundant 'NUMEXPR_NUM_THREADS' or 
    'OMP_NUM_THREADS' env vars to set the initial number of threads used by 
    the virtual machine.
    t   sparcsÃ   The number of threads have been set to 1 because problems related to threading have been reported on some sparc machine. The number of threads can be changed using the "set_num_threads" function.i   t   NUMEXPR_MAX_THREADSss   Note: detected %d virtual cores but NumExpr set to maximum of %d, check "NUMEXPR_MAX_THREADS" environment variable.i   s`   Note: NumExpr detected %d cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.t   NUMEXPR_NUM_THREADSt   OMP_NUM_THREADSs!   NumExpr defaulting to %d threads.(   t   platformt   machinet   logt   warningR   t   Falset   detect_number_of_corest   ost   environt   TrueR   t   infot   int(   t   env_configuredt   n_corest   requested_threads(    (    s,   lib/python2.7/site-packages/numexpr/utils.pyt   _init_num_threadsd   s.    
			
c          C   sµ   t  t d ƒ rn d t j k rO t j d ƒ }  t |  t ƒ rk |  d k rk |  Sqn t t j d d d g ƒ ƒ Sn  y/ t t j j	 d d ƒ ƒ }  |  d k rœ |  SWn t
 k
 r° n Xd	 S(
   sC   
    Detects the number of cores on a system. Cribbed from pp.
    t   sysconft   SC_NPROCESSORS_ONLNi    t   sysctls   -ns   hw.ncput   NUMBER_OF_PROCESSORSt    i   (   t   hasattrR!   t   sysconf_namesR*   t
   isinstanceR%   t
   subprocesst   check_outputR"   R   R   (   t   ncpus(    (    s,   lib/python2.7/site-packages/numexpr/utils.pyR    ”   s    c          C   s•   t  j d ƒ y t t j j d d ƒ ƒ }  WnM t k
 r{ y t t j j d d ƒ ƒ }  Wq| t k
 rw t ƒ  }  q| Xn X|  t k r‘ t }  n  |  S(   s¥   
    DEPRECATED: use `_init_num_threads` instead.
    If this is modified, please update the note in: https://github.com/pydata/numexpr/wiki/Numexpr-Users-Guide
    s+   Deprecated, use `init_num_threads` instead.R   R.   R   (	   R   R   R%   R!   R"   R   R   R    R   (   R   (    (    s,   lib/python2.7/site-packages/numexpr/utils.pyt   detect_number_of_threads«   s    	t	   CacheDictc           B   s    e  Z d  Z d „  Z d „  Z RS(   sB   
    A dictionary that prevents itself from growing too much.
    c         C   s#   | |  _  t t |  ƒ j |  ƒ d  S(   N(   t
   maxentriest   superR6   t   __init__(   t   selfR7   (    (    s,   lib/python2.7/site-packages/numexpr/utils.pyR9   Ä   s    	c         C   ss   t  |  ƒ |  j k rV |  j d } x1 |  j ƒ  |  D] } t t |  ƒ j | ƒ q3 Wn  t t |  ƒ j | | ƒ d  S(   Ni
   (   t   lenR7   R   R8   R6   t   __delitem__t   __setitem__(   R:   t   keyt   valuet   entries_to_removet   k(    (    s,   lib/python2.7/site-packages/numexpr/utils.pyR=   È   s
    (   t   __name__t
   __module__t   __doc__R9   R=   (    (    (    s,   lib/python2.7/site-packages/numexpr/utils.pyR6   ¿   s   	(   t   loggingt	   getLoggerRB   R   R!   R2   R   t   numexpr.interpreterR    R   t   numexprR   R   R   R   R   R   R   R   R)   R    R5   t   dictR6   (    (    (    s,   lib/python2.7/site-packages/numexpr/utils.pyt   <module>   s    					0		