o
    ˜û‚i—&  ã                   @   sÜ   d dl Z e  e¡Zd dlZd dlZd dlZd dlmZ d dl	m
Z
mZmZ ddlmZ er9d dl	mZmZmZmZ dd„ Zd	d
„ Zdd„ 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G dd„ dƒZdS )é    N)Úuse_vml)ÚMAX_THREADSÚ_get_num_threadsÚ_set_num_threadsé   )Úversion)Ú_get_vml_num_threadsÚ_get_vml_versionÚ_set_vml_accuracy_modeÚ_set_vml_num_threadsc                   C   ó   t rtƒ S dS )z*
    Get the VML/MKL library version.
    N)r   r	   © r   r   úP/home/ubuntu/maya3_transcribe/venv/lib/python3.10/site-packages/numexpr/utils.pyÚget_vml_version   s   r   c                 C   sT   t r(d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Ú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_threadsG   s   r    c                   C   r   )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   r   Úget_vml_num_threadsW   s   r!   c                 C   s   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_nthreadsr   r   r   Úset_num_threadsg   s   	r#   c                   C   s   t ƒ S )zE
    Gets the number of threads currently in use for operations.
    )r   r   r   r   r   Úget_num_threadss   s   r$   c                  C   s
  dt jv rt d¡ tdƒ dS d} tƒ }dtjv r!tjd dks-dtjv r2tjd dkr2d} t}n|tkr?t 	d	|tf ¡ |d
krLt 	d| ¡ d
}dtjv r`tjd dkr`t
tjd ƒ}ndtjv rttjd dkrtt
tjd ƒ}n|}| st 	d| ¡ 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.
    ÚsparczÃ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.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_threadsr   r   r   Ú_init_num_threadsy   s0   

r6   c                  C   s„   t tdƒr$dtjv rt d¡} t| tƒr| dkr| S n	tt g d¢¡ƒS zttj 	dd¡ƒ} | dkr5| W S W dS  t
yA   Y dS 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   )Úhasattrr/   Úsysconf_namesr7   Ú
isinstancer2   Ú
subprocessÚcheck_outputr0   r   r   )Úncpusr   r   r   r.   «   s"   


€ÿþþr.   c                  C   st   t  d¡ zttj dd¡ƒ} W n! ty1   zttj dd¡ƒ} W n ty.   tƒ } Y nw Y nw | tkr8t} | 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.r*   r'   r(   )	r,   r-   r2   r/   r0   r   r   r.   r   r   r   r   r   Údetect_number_of_threadsÂ   s   

ÿ€ýrA   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ÚsuperrB   Ú__init__)ÚselfrD   ©Ú	__class__r   r   rF   Û   s   zCacheDict.__init__c                    sX   t | ƒ| jkr!| jd }t|  ¡ ƒd |… D ]
}tt| ƒ |¡ qtt| ƒ ||¡ d S )Né
   )ÚlenrD   r   r   rE   rB   Ú__delitem__Ú__setitem__)rG   ÚkeyÚvalueÚentries_to_removeÚkrH   r   r   rM   ß   s
   
zCacheDict.__setitem__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rF   rM   Ú__classcell__r   r   rH   r   rB   Ö   s    rB   c                   @   sœ   e Zd ZdZdd„ Zd%dd„Zd&dd„Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ ZdS )'ÚContextDictz,
    A context aware version dictionary
    c                 C   s   t jdi d| _d S )NÚcontext_data)Údefault)ÚcontextvarsÚ
ContextVarÚ_context_data©rG   r   r   r   rF   í   s   zContextDict.__init__Nc                 K   sH   | j  ¡  ¡ }|d ur|||< | ¡ D ]\}}|||< q| j  |¡ d S rC   )r\   r   ÚcopyÚitemsÚset)rG   rN   rO   ÚkwargsÚdatarQ   Úvr   r   r   r`   ð   s   
zContextDict.setc                 C   s   | j  ¡ }| ||¡S rC   ©r\   r   )rG   rN   rY   rb   r   r   r   r   û   s   
zContextDict.getc                 C   s,   | j  ¡  ¡ }||v r||= | j  |¡ d S rC   )r\   r   r^   r`   )rG   rN   rb   r   r   r   Údeleteÿ   s   zContextDict.deletec                 C   s   | j  i ¡ d S rC   )r\   r`   r]   r   r   r   Úclear  ó   zContextDict.clearc                 C   s
   | j  ¡ S rC   rd   r]   r   r   r   Úall  ó   
zContextDict.allc                 O   s€   | j  ¡  ¡ }|r3t|ƒdkrtdt|ƒ› dƒ‚|d }t|tƒr(| |¡ n|D ]\}}|||< q*| |¡ | j  |¡ d S )Nr   z.update() takes at most 1 positional argument (z given)r   )	r\   r   r^   rK   Ú	TypeErrorr=   ÚdictÚupdater`   )rG   Úargsra   rb   ÚotherrQ   rc   r   r   r   rl     s   


zContextDict.updatec                 C   ó   | j  ¡  ¡ S rC   )r\   r   r   r]   r   r   r   r     ó   zContextDict.keysc                 C   ro   rC   )r\   r   Úvaluesr]   r   r   r   rq     rp   zContextDict.valuesc                 C   ro   rC   )r\   r   r_   r]   r   r   r   r_   !  rp   zContextDict.itemsc                 C   s
   |   |¡S rC   )r   ©rG   rN   r   r   r   Ú__getitem__$  ri   zContextDict.__getitem__c                 C   s   |   ||¡ d S rC   )r`   )rG   rN   rO   r   r   r   rM   '  rg   zContextDict.__setitem__c                 C   s   |   |¡ d S rC   )re   rr   r   r   r   rL   *  rp   zContextDict.__delitem__c                 C   s   || j  ¡ v S rC   rd   rr   r   r   r   Ú__contains__-  rp   zContextDict.__contains__c                 C   ó   t | j ¡ ƒS rC   )rK   r\   r   r]   r   r   r   Ú__len__0  rp   zContextDict.__len__c                 C   ru   rC   )Úiterr\   r   r]   r   r   r   Ú__iter__3  rp   zContextDict.__iter__c                 C   ru   rC   )Úreprr\   r   r]   r   r   r   Ú__repr__6  rp   zContextDict.__repr__)NNrC   )rR   rS   rT   rU   rF   r`   r   re   rf   rh   rl   r   rq   r_   rs   rM   rL   rt   rv   rx   rz   r   r   r   r   rW   é   s&    

rW   )ÚloggingÚ	getLoggerrR   r,   rZ   r/   r>   Únumexprr   Únumexpr.interpreterr   r   r   r'   r   r   r	   r
   r   r   r   r    r!   r#   r$   r6   r.   rA   rk   rB   rW   r   r   r   r   Ú<module>   s*   


2