
     !gY                         d dl Z  e j                  e      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y)    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                  $    t         r
t               S y)z*
    Get the VML/MKL library version.
    N)r   r	        -lib/python3.12/site-packages/numexpr/utils.pyget_vml_versionr      s     !!r   c                     t         r_ddddd}dddd	}| t        |j                               vrt        d
      t	        |j                  | d            }|j                  |      S y)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    %   sp    & Aq!<$F;tHMMO,,KM M'T1(=>##F++r   c                 &    t         rt        |        y)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$   D   s     X&r   c                  $    t         r
t               S y)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&   T   s     #%%r   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)   d   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+   p   s     r   c                  $   dt         j                  v r!t        j                  d       t	        d       yd} t               }dt        j                  v rt        j                  d   dk7  s(dt        j                  v rt        j                  d   dk7  r	d} t        }nF|t        kD  rt        j                  d	|t        fz         |d
kD  rt        j                  d|z         d
}dt        j                  v r3t        j                  d   dk7  rt        t        j                  d         }nadt        j                  v r3t        j                  d   dk7  rt        t        j                  d         }n|}| st        j                  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_THREADS OMP_NUM_THREADSTzsNote: detected %d virtual cores but NumExpr set to maximum of %d, check "NUMEXPR_MAX_THREADS" environment variable.   zaNote: NumExpr detected %d cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 16.NUMEXPR_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>   v   sv    '***  	 	N$&G+

;P0QUW0WRZZ'BJJ7H,IR,O [ HH  K  MT  Va  Lb  b  cR<HHx  zA  A  BG 

*rzz:O/PTV/V

+@ AB	bjj	(RZZ8I-Jb-P

+< =>#HH8@A %&r   c                  b   t        t        d      r^dt        j                  v r,t        j                  d      } t	        | t
              r'| dkD  r"| S t        t        j                  g d            S 	 t        t        j                  j                  dd            } | dkD  r| S 	 y# t        $ r Y yw xY w)zC
    Detects the number of cores on a system. Cribbed from pp.
    sysconfSC_NPROCESSORS_ONLNr   )sysctlz-nzhw.ncpuNUMBER_OF_PROCESSORSr/   r   )hasattrr7   sysconf_namesr@   
isinstancer:   
subprocesscheck_outputr8   r   r   )ncpuss    r   r6   r6      s    
 r9 B$4$44JJ45E%%%!)z../JKLLBJJNN#92>?19L    s   0/B" "	B.-B.c                  H   t         j                  d       	 t        t        j                  j                  dd            } | t        kD  rt        } | S # t        $ rG 	 t        t        j                  j                  dd            } n# t        $ r t               } Y nw xY wY `w xY w)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.r2   r/   r0   )	r4   r5   r:   r7   r8   r   r   r6   r   r"   s    r   detect_number_of_threadsrK      s    
 KK=>0rzz~~&;R@A +O  0	02::>>*;R@AH 	0-/H	00s5   )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                 :    || _         t        t        |   |        y )N)
maxentriessuperrM   __init__)selfrO   	__class__s     r   rQ   zCacheDict.__init__   s    $i'-r   c                     t        |       | j                  kD  rC| j                  dz  }t        | j                               d | D ]  }t        t
        |   |        t        t
        |   ||       y )N
   )lenrO   r   r   rP   rM   __delitem____setitem__)rR   keyvalueentries_to_removekrS   s        r   rX   zCacheDict.__setitem__   s`    t9t& $2 5$))+&'9(9:i215 ;i*36r   )__name__
__module____qualname____doc__rQ   rX   __classcell__)rS   s   @r   rM   rM      s    .7 7r   rM   )logging	getLoggerr]   r4   r7   rG   numexpr.interpreterr   r   r   numexprr   r/   r   r	   r
   r   r   r   r    r$   r&   r)   r+   r>   r6   rK   dictrM   r   r   r   <module>rg      su    g! 	  O O  
 
>	  
/d.(7 7r   