B
    ;9\                 @   s   d dl Z d dlZd dlmZmZ d dlmZ erDd dlmZmZm	Z	 dd Z
dd Zd	d
 Zdd Zdd Zdd ZG dd deZdS )    N)_set_num_threadsMAX_THREADS)use_vml)_get_vml_version_set_vml_accuracy_mode_set_vml_num_threadsc               C   s   t r
t S dS dS )z Get the VML/MKL library version.N)r   r    r   r   ,lib/python3.7/site-packages/numexpr/utils.pyget_vml_version   s    r
   c             C   sX   t rPddddd}dddd	}| t| kr6td
t|| d}||S dS dS )a>  
    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.
    r            )Nlowhighfastr   r   r   )r   r   r   z9mode argument must be one of: None, 'high', 'low', 'fast'N)r   listkeys
ValueErrorr   get)modeZacc_dictZacc_reverse_dictZretvalr   r   r	   set_vml_accuracy_mode   s    
r   c             C   s   t rt|  dS )a[  
    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   )nthreadsr   r   r	   set_vml_num_threads=   s    r   c             C   s   t | }|S )aV  
    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   Zold_nthreadsr   r   r	   set_num_threadsM   s    r   c              C   s   t tdrJdtjkr6td} t| trJ| dkrJ| S nttdddgS y"ttj	dd} | dkrj| S W n t
k
r   Y nX d	S )
zC
    Detects the number of cores on a system. Cribbed from pp.
    sysconfSC_NPROCESSORS_ONLNr   Zsysctlz-nzhw.ncpuZNUMBER_OF_PROCESSORS r   )hasattrossysconf_namesr   
isinstanceint
subprocessZcheck_outputenvironr   r   )Zncpusr   r   r	   detect_number_of_coresa   s    


r$   c              C   sn   yt tjdd} W nF tk
r\   yt tjdd} W n tk
rV   t } Y nX Y nX | tkrjt} | S )zt
    If this is modified, please update the note in: https://github.com/pydata/numexpr/wiki/Numexpr-Users-Guide
    ZNUMEXPR_NUM_THREADSr   ZOMP_NUM_THREADS)r!   r   r#   r   r   r$   r   )r   r   r   r	   detect_number_of_threadsx   s    r%   c                   s,   e Zd ZdZ fddZ fddZ  ZS )	CacheDictzB
    A dictionary that prevents itself from growing too much.
    c                s   || _ tt| |  d S )N)
maxentriessuperr&   __init__)selfr'   )	__class__r   r	   r)      s    zCacheDict.__init__c                s\   t | | jkrF| jd }x,t|  d | D ]}tt| | q.W tt| || d S )N
   )lenr'   r   r   r(   r&   __delitem____setitem__)r*   keyvalueZentries_to_removek)r+   r   r	   r/      s
    
zCacheDict.__setitem__)__name__
__module____qualname____doc__r)   r/   __classcell__r   r   )r+   r	   r&      s   r&   )r   r"   Znumexpr.interpreterr   r   Znumexprr   r   r   r   r
   r   r   r   r$   r%   dictr&   r   r   r   r	   <module>   s   