
    e                         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	m
Z
 d dlmZ ddlmZ erd dlmZmZmZmZ d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Z G d de          ZdS )    N)_set_num_threads_get_num_threadsMAX_THREADS)use_vml   )version)_get_vml_version_set_vml_accuracy_mode_set_vml_num_threads_get_vml_num_threadsc                  0    t           rt                      S dS )z*
    Get the VML/MKL library version.
    N)r   r	        -lib/python3.11/site-packages/numexpr/utils.pyget_vml_versionr      s      !!!tr   c                    t           rwddddd}dddd	}| t          |                                          vrt          d
          t	          |                    | 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   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)modeacc_dictacc_reverse_dictretvals       r   set_vml_accuracy_moder    &   s    &  	Aq!<<$F;;tHMMOO,,,,KM M M'T1(=(=>>##F+++tr   c                 2    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   nthreadss    r   set_vml_num_threadsr$   E   s      'X&&&Dr   c                  0    t           rt                      S dS )a  
    Gets the maximum number of threads to be used in VML operations.

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

    http://software.intel.com/en-us/node/522118

    for more info about it.
    N)r   r   r   r   r   get_vml_num_threadsr&   U   s      &#%%%4r   c                 $    t          |           }|S )a  
    Sets a number of threads to be used in operations.

    DEPRECATED: 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()`).
    )r   )r#   old_nthreadss     r   set_num_threadsr)   e   s     $H--Lr   c                      t                      S )zE
    Gets the number of threads currently in use for operations.
    )r   r   r   r   get_num_threadsr+   q   s     r   c                     dt           j        v r+t                              d           t	          d           dS d} t                      }dt          j        v r
d} t          }nT|t          k    r$t          	                    d|t          fz             |dk    rt          	                    d	|z             d}d
t          j        v r t          t          j        d
                   }nOdt          j        v r t          t          j        d                   }n!|}| st          	                    d|z             t	          |           |S )a	  
    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.
    sparczThe 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.r   FNUMEXPR_MAX_THREADSTzsNote: detected %d virtual cores but NumExpr set to maximum of %d, check "NUMEXPR_MAX_THREADS" environment variable.   z`Note: NumExpr detected %d cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.NUMEXPR_NUM_THREADSOMP_NUM_THREADSz!NumExpr defaulting to %d threads.)r   platform_machinelogwarningr)   detect_number_of_coresosenvironr   infoint)env_configuredn_coresrequested_threadss      r   _init_num_threadsr=   w   sn    '***  	 	 	 	qN$&&G
** [  HH  K  MT  Va  Lb  b  c  c  cQ;;HHwx  A  A  AG 
**
+@ ABB	bj	(	(
+< =>># 	BHH8@AAA %&&&r   c                     t          t          d          rcdt          j        v r2t          j        d          } t	          | t
                    r| dk    r| S n#t          t          j        g d                    S 	 t          t          j        	                    dd                    } | dk    r| S n# t          $ r Y nw xY wdS )zC
    Detects the number of cores on a system. Cribbed from pp.
    sysconfSC_NPROCESSORS_ONLNr   )sysctlz-nzhw.ncpuNUMBER_OF_PROCESSORS r   )hasattrr6   sysconf_namesr?   
isinstancer9   
subprocesscheck_outputr7   r   r   )ncpuss    r   r5   r5      s    
 r9 M B$444J455E%%% %!))z./J/J/JKKLLLBJNN#92>>??199L    1s   :4B0 0
B=<B=c                  v   t                               d           	 t          t          j                            dd                    } n]# t          $ rP 	 t          t          j                            dd                    } n# t          $ r t                      } Y nw xY wY nw xY w| t          k    rt          } | S )z
    DEPRECATED: use `_init_num_threads` instead.
    If this is modified, please update the note in: https://github.com/pydata/numexpr/wiki/Numexpr-Users-Guide
    z+Deprecated, use `init_num_threads` instead.r0   rC   r1   )	r3   r4   r9   r6   r7   r   r   r5   r   r"   s    r   detect_number_of_threadsrK      s    
 KK=>>>0rz~~&;R@@AA 0 0 0	02:>>*;R@@AAHH 	0 	0 	0-//HHH	00 +Os5   -A
 

B$-BB$BB$BB$#B$c                   ,     e Zd ZdZ fdZ fdZ xZS )	CacheDictzB
    A dictionary that prevents itself from growing too much.
    c                 f    || _         t          t          |                               |            d S )N)
maxentriessuperrM   __init__)selfrO   	__class__s     r   rQ   zCacheDict.__init__   s.    $i''-----r   c                 F   t          |           | j        k    r^| j        dz  }t          |                                           d |         D ]*}t	          t
          |                               |           +t	          t
          |                               ||           d S )N
   )lenrO   r   r   rP   rM   __delitem____setitem__)rR   keyvalueentries_to_removekrS   s        r   rX   zCacheDict.__setitem__   s    t99t&& $2 5$))++&&'9(9'9: 6 6i&&2215555i**366666r   )__name__
__module____qualname____doc__rQ   rX   __classcell__)rS   s   @r   rM   rM      s[         . . . . .7 7 7 7 7 7 7 7 7r   rM   )logging	getLoggerr]   r3   r6   rG   platformnumexpr.interpreterr   r   r   numexprr   rC   r   r	   r
   r   r   r   r    r$   r&   r)   r+   r=   r5   rK   dictrM   r   r   r   <module>rh      s   g!! 				      O O O O O O O O O O            
            
    >	 	 	    
 
 
  - - -`  .  (7 7 7 7 7 7 7 7 7 7r   