o
    Ggg                     @   sv  d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	Z	ddl
mZ ddlmZmZ ddlmZ ddlmZ dZg d	Zejd
d ejdkrSejnejZejdkr^ejnejZG dd dejZzejZ W n e!y{   ej"Z Y nw G dd deZ#G dd de#Z$G dd de#Z%G dd de#Z&G dd de#Z'G dd de#Z(e$e%e'e(e&gZ)e*e+dd e)D Z,dd e)D Z-e*e+dd e)D Z.d d e)D Z/e(j0Z1d!d" Z2d#d$ Z3ed%d&d'd( Z4e3e*e,e-d)d*d+ Z5G d,d- d-Z6G d.d/ d/e6eZ7e3d08d1d e,D d08e/d08e1d2G d3d4 d4e6Z9G d5d6 d6Z:d7d8 Z;e<d9kr9e;  dS dS ):zthreadpoolctl

This module provides utilities to introspect native libraries that relies on
thread pools (notably BLAS and OpenMP implementations) and dynamically set the
maximal number of threads they can use.
    N)final)find_library)ABCabstractmethod)	lru_cache)ContextDecoratorz3.5.0)threadpool_limitsthreadpool_infoThreadpoolControllerLibControllerregisterKMP_DUPLICATE_LIB_OKTruel        c                   @   s,   e Zd ZdefdejfdejfdefgZdS )_dl_phdr_info	dlpi_addr	dlpi_name	dlpi_phdr
dlpi_phnumN)	__name__
__module____qualname___SYSTEM_UINTctypesc_char_pc_void_p_SYSTEM_UINT_HALF_fields_ r   r   P/var/www/html/AyurvedaChatbot/venv/lib/python3.10/site-packages/threadpoolctl.pyr   8   s    r   c                   @   sv   e Zd ZdZeddddddZdd Zdd	 Zed
d Z	e
dd Ze
dd Ze
dd Zdd Zdd ZdS )r   a  Abstract base class for the individual library controllers

    A library controller must expose the following class attributes:
        - user_api : str
            Usually the name of the library or generic specification the library
            implements, e.g. "blas" is a specification with different implementations.
        - internal_api : str
            Usually the name of the library or concrete implementation of some
            specification, e.g. "openblas" is an implementation of the "blas"
            specification.
        - filename_prefixes : tuple
            Possible prefixes of the shared library's filename that allow to
            identify the library. e.g. "libopenblas" for libopenblas.so.

    and implement the following methods: `get_num_threads`, `set_num_threads` and
    `get_version`.

    Threadpoolctl loops through all the loaded shared libraries and tries to match
    the filename of each library with the `filename_prefixes`. If a match is found, a
    controller is instantiated and a handler to the library is stored in the `dynlib`
    attribute as a `ctypes.CDLL` object. It can be used to access the necessary symbols
    of the shared library to implement the above methods.

    The following information will be exposed in the info dictionary:
      - user_api : standardized API, if any, or a copy of internal_api.
      - internal_api : implementation-specific API.
      - num_threads : the current thread limit.
      - prefix : prefix of the shared library's filename.
      - filepath : path to the loaded shared library.
      - version : version of the library (if available).

    In addition, each library controller may expose internal API specific entries. They
    must be set as attributes in the `set_additional_attributes` method.
    Nfilepathprefixparentc                C   sH   || _ || _|| _tj|td| _|  \| _| _	| 
 | _|   dS )z0This is not meant to be overriden by subclasses.modeN)r"   r!   r    r   CDLL_RTLD_NOLOADdynlib_find_affixes_symbol_prefix_symbol_suffixget_versionversionset_additional_attributes)selfr    r!   r"   r   r   r   __init__l   s   
zLibController.__init__c                    s0   d | j | j| jd fddt|  D S )&Return relevant info wrapped in a dict)r'   r"   r)   r*   )user_apiinternal_apinum_threadsc                    s   i | ]\}}| vr||qS r   r   ).0kvhidden_attrsr   r   
<dictcomp>~   s    z&LibController.info.<locals>.<dictcomp>)r1   r2   r3   varsitemsr.   r   r7   r   infow   s   zLibController.infoc                 C      dS )z>Set additional attributes meant to be exposed in the info dictNr   r<   r   r   r   r-          z'LibController.set_additional_attributesc                 C      |   S )zExposes the current thread limit as a dynamic property

        This is not meant to be used or overriden by subclasses.
        )get_num_threadsr<   r   r   r   r3      s   zLibController.num_threadsc                 C   r>   )z5Return the maximum number of threads available to useNr   r<   r   r   r   rA      r?   zLibController.get_num_threadsc                 C   r>   )z(Set the maximum number of threads to useNr   )r.   r3   r   r   r   set_num_threads   r?   zLibController.set_num_threadsc                 C   r>   )z(Return the version of the shared libraryNr   r<   r   r   r   r+      r?   zLibController.get_versionc                 C   r>   )z8Return the affixes for the symbols of the shared library) rC   r   r<   r   r   r   r(         zLibController._find_affixesc                 C   s   t | j| j | | j dS )zBReturn the symbol of the shared library accounding for the affixesN)getattrr'   r)   r*   )r.   namer   r   r   _get_symbol   s   zLibController._get_symbol)r   r   r   __doc__r   r/   r=   r-   propertyr3   r   rA   rB   r+   r(   rG   r   r   r   r   r   H   s     #





r   c                   @   sv   e Zd ZdZdZdZdZdZdZe	dd e
ee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 )OpenBLASControllerzController class for OpenBLASblasopenblas)libopenblaslibblaslibscipy_openblas)rC   scipy_)rC   64__64c                 c   s"    | ]\}}| d | V  qdS )openblas_get_num_threadsNr   )r4   r!   suffixr   r   r   	<genexpr>   s
    
zOpenBLASController.<genexpr>c                 C   s@   t | j| jD ]\}}t| j| d| r||f  S qd S NrS   )	itertoolsproduct_symbol_prefixes_symbol_suffixeshasattrr'   )r.   r!   rT   r   r   r   r(      s   z OpenBLASController._find_affixesc                 C      |   | _|  | _d S N_get_threading_layerthreading_layer_get_architecturearchitecturer<   r   r   r   r-         
z,OpenBLASController.set_additional_attributesc                 C   s   |  d}|d ur| S d S rV   rG   )r.   get_num_threads_funcr   r   r   rA      s   
z"OpenBLASController.get_num_threadsc                 C   s   |  d}|d ur||S d S )Nopenblas_set_num_threadsrd   )r.   r3   set_num_threads_funcr   r   r   rB      s   
z"OpenBLASController.set_num_threadsc                 C   sF   |  d}|d ur!tj|_|  }|d dkr|d dS d S d S )Nopenblas_get_configr   s   OpenBLAS   utf-8)rG   r   r   restypesplitdecode)r.   get_version_funcconfigr   r   r   r+      s   

zOpenBLASController.get_versionc                 C   s8   |  d}|dur| }|dkrdS |dkrdS dS dS )	z&Return the threading layer of OpenBLASopenblas_get_parallelN   openmpri   pthreadsdisabledunknownrd   )r.   get_threading_layer_funcr`   r   r   r   r_      s   
z'OpenBLASController._get_threading_layerc                 C   s*   |  d}|durtj|_| dS dS )z,Return the architecture detected by OpenBLASopenblas_get_corenameNrj   )rG   r   r   rk   rm   )r.   get_architecture_funcr   r   r   ra      s
   
z$OpenBLASController._get_architectureN)r   r   r   rH   r1   r2   filename_prefixesrY   rZ   tuplerW   rX   check_symbolsr(   r-   rA   rB   r+   r_   ra   r   r   r   r   rJ      s"    
rJ   c                   @   sP   e Zd ZdZdZdZdZdZdd Zdd	 Z	d
d Z
dd Zdd Zdd ZdS )BLISControllerzController class for BLISrK   blis)libblisrN   )bli_thread_get_num_threadsbli_thread_set_num_threadsbli_info_get_version_strbli_info_get_enable_openmpbli_info_get_enable_pthreadsbli_arch_query_idbli_arch_stringc                 C   r\   r]   r^   r<   r   r   r   r-      rc   z(BLISController.set_additional_attributesc                 C   (   t | jddd }| }|dkrdS |S )Nr   c                   S      d S r]   r   r   r   r   r   <lambda>   r?   z0BLISController.get_num_threads.<locals>.<lambda>ri   rE   r'   r.   get_funcr3   r   r   r   rA         zBLISController.get_num_threadsc                 C      t | jddd }||S )Nr   c                 S   r   r]   r   r3   r   r   r   r     r?   z0BLISController.set_num_threads.<locals>.<lambda>r   r.   r3   set_funcr   r   r   rB        zBLISController.set_num_threadsc                 C   s.   t | jdd }|d u rd S tj|_| dS )Nr   rj   )rE   r'   r   r   rk   rm   )r.   get_version_r   r   r   r+     s
   zBLISController.get_versionc                 C   s4   t | jddd  rdS t | jddd  rdS dS )	z"Return the threading layer of BLISr   c                   S   r>   NFr   r   r   r   r   r     r?   z5BLISController._get_threading_layer.<locals>.<lambda>rr   r   c                   S   r>   r   r   r   r   r   r   r     r?   rs   rt   r   r<   r   r   r   r_     s
   z#BLISController._get_threading_layerc                 C   sP   t | jdd}t | jdd}|du s|du rdS tj|_tj|_|| dS )z(Return the architecture detected by BLISr   Nr   rj   )rE   r'   r   c_intrk   r   rm   )r.   r   r   r   r   r   ra     s   z BLISController._get_architectureN)r   r   r   rH   r1   r2   ry   r{   r-   rA   rB   r+   r_   ra   r   r   r   r   r|      s    
r|   c                       s   e Zd ZdZdZdZdZdZedd Z	edd	 Z
 fd
dZdd Zdd Zdd Zdd ZdddZdd Zdd Z  ZS )FlexiBLASControllerzController class for FlexiBLASrK   	flexiblas)libflexiblas)flexiblas_get_num_threadsflexiblas_set_num_threadsflexiblas_get_versionflexiblas_listflexiblas_list_loadedflexiblas_current_backendc                 C   s   | j ddS )NTloaded)_get_backend_listr<   r   r   r   loaded_backends9  s   z#FlexiBLASController.loaded_backendsc                 C   r@   r]   )_get_current_backendr<   r   r   r   current_backend=  s   z#FlexiBLASController.current_backendc                    s"   t   }| j|d< | j|d< |S )r0   r   r   )superr=   r   r   )r.   exposed_attrs	__class__r   r   r=   A  s   


zFlexiBLASController.infoc                 C   s   | j dd| _d S )NFr   )r   available_backendsr<   r   r   r   r-   K  s   z-FlexiBLASController.set_additional_attributesc                 C   r   )Nr   c                   S   r   r]   r   r   r   r   r   r   O  r?   z5FlexiBLASController.get_num_threads.<locals>.<lambda>r   ri   r   r   r   r   r   rA   N  r   z#FlexiBLASController.get_num_threadsc                 C   r   )Nr   c                 S   r   r]   r   r   r   r   r   r   W  r?   z5FlexiBLASController.set_num_threads.<locals>.<lambda>r   r   r   r   r   rB   U  r   z#FlexiBLASController.set_num_threadsc                 C   sj   t | jdd }|d u rd S t }t }t }|t|t|t| |j d|j d|j S )Nr   .)rE   r'   r   r   byrefvalue)r.   r   majorminorpatchr   r   r   r+   [  s   zFlexiBLASController.get_versionFc                 C   s   d|rdnd }t | j|d}|du rdS |ddd}g }t|D ]}td}||d| |jddkr@||jd q"|S )	zReturn the list of available backends for FlexiBLAS.

        If loaded is False, return the list of available backends from the FlexiBLAS
        configuration. If loaded is True, return the list of actually loaded backends.
        r   _loadedrC   Nr      rj   __FALLBACK__)rE   r'   ranger   create_string_bufferr   rm   append)r.   r   	func_nameget_backend_list_
n_backendsbackendsibackend_namer   r   r   r   f  s   
z%FlexiBLASController._get_backend_listc                 C   s@   t | jdd}|du rdS td}||t| |jdS )zReturn the backend of FlexiBLASr   Nr   rj   )rE   r'   r   r   sizeofr   rm   )r.   get_backend_backendr   r   r   r   }  s   
z(FlexiBLASController._get_current_backendc                 C   s   || j vr;|| jv rt| jddd }n	t| jddd }|t|d}|dkr6td|d	| j d
| j  t| jddd }| j 	|}||}|dkrZtd|ddS )a  Switch the backend of FlexiBLAS

        Parameters
        ----------
        backend : str
            The name or the path to the shared library of the backend to switch to. If
            the backend is not already loaded, it will be loaded first.
        flexiblas_load_backendc                 S   r>   Nr   r   _r   r   r   r     r?   z4FlexiBLASController.switch_backend.<locals>.<lambda>flexiblas_load_backend_libraryc                 S   r>   r   r   r   r   r   r   r     r?   rj   r   zFailed to load backend zS. It must either be the name of a backend available in the FlexiBLAS configuration z' or the path to a valid shared library.flexiblas_switchc                 S   r>   r   r   r   r   r   r   r     r?   zFailed to switch to backend r   N)
r   r   rE   r'   strencodeRuntimeErrorr"   _load_librariesindex)r.   r   	load_funcresswitch_funcidxr   r   r   switch_backend  s(   
	

z"FlexiBLASController.switch_backend)F)r   r   r   rH   r1   r2   ry   r{   rI   r   r   r=   r-   rA   rB   r+   r   r   r   __classcell__r   r   r   r   r   *  s$    	




r   c                   @   sH   e Zd ZdZdZdZdZdZdd Zdd	 Z	d
d Z
dd Zdd ZdS )MKLControllerzController class for MKLrK   mkl)	libmkl_rtmkl_rtrN   )MKL_Get_Max_ThreadsMKL_Set_Num_ThreadsMKL_Get_Version_StringMKL_Set_Threading_Layerc                 C   s   |   | _d S r]   )r_   r`   r<   r   r   r   r-     s   z'MKLController.set_additional_attributesc                 C      t | jddd }| S )Nr   c                   S   r   r]   r   r   r   r   r   r     r?   z/MKLController.get_num_threads.<locals>.<lambda>r   r.   r   r   r   r   rA        zMKLController.get_num_threadsc                 C   r   )Nr   c                 S   r   r]   r   r   r   r   r   r     r?   z/MKLController.set_num_threads.<locals>.<lambda>r   r   r   r   r   rB        zMKLController.set_num_threadsc                 C   s\   t | jdsd S td}| j|d |jd}td|}|d ur*|	 d }|
 S )Nr      rj   zVersion ([^ ]+) r   )r[   r'   r   r   r   r   rm   researchgroupsstrip)r.   r   r,   groupr   r   r   r+     s   
zMKLController.get_versionc                 C   s0   t | jddd }dddddd	d
}||d S )z!Return the threading layer of MKLr   c                 S   r>   r   r   )layerr   r   r   r     r?   z4MKLController._get_threading_layer.<locals>.<lambda>intel
sequentialpgignutbbznot specified)r   ri   rq         r   r   r   )r.   set_threading_layer	layer_mapr   r   r   r_     s   z"MKLController._get_threading_layerN)r   r   r   rH   r1   r2   ry   r{   r-   rA   rB   r+   r_   r   r   r   r   r     s    r   c                   @   s8   e Zd ZdZdZdZdZdZdd Zdd Z	d	d
 Z
dS )OpenMPControllerzController class for OpenMPrr   )libiomplibgomplibompvcomp)omp_get_max_threadsomp_get_num_threadsc                 C   r   )Nr   c                   S   r   r]   r   r   r   r   r   r     r?   z2OpenMPController.get_num_threads.<locals>.<lambda>r   r   r   r   r   rA     r   z OpenMPController.get_num_threadsc                 C   r   )Nomp_set_num_threadsc                 S   r   r]   r   r   r   r   r   r     r?   z2OpenMPController.set_num_threads.<locals>.<lambda>r   r   r   r   r   rB     r   z OpenMPController.set_num_threadsc                 C   r   r]   r   r<   r   r   r   r+     rD   zOpenMPController.get_versionN)r   r   r   rH   r1   r2   ry   r{   rA   rB   r+   r   r   r   r   r     s    r   c                 c       | ]}|j V  qd S r]   r1   r4   libr   r   r   rU         rU   c                 C      g | ]}|j qS r   )r2   r   r   r   r   
<listcomp>      r   c                 c   s     | ]}|j D ]}|V  qqd S r]   )ry   )r4   r   r!   r   r   r   rU     s    c                 C   s   g | ]
}|j d kr|jqS )rK   )r1   r2   r   r   r   r   r   
  s    c                 C   s2   t |  t| j t| j t| j dS )zRegister a new controllerN)	_ALL_CONTROLLERSr   _ALL_USER_APISr1   _ALL_INTERNAL_APISr2   _ALL_PREFIXESextendry   )
controllerr   r   r   r     s   
r   c                     s    fdd}|S )Nc                    s"   | j d ur| j j i | _ | S r]   )rH   format)oargskwargsr   r   	decorator  s   
z$_format_docstring.<locals>.decoratorr   )r  r  r  r   r  r   _format_docstring  s   r  i'  )maxsizec                 C   s   t j| S )zCSmall caching wrapper around os.path.realpath to limit system calls)ospathrealpathr    r   r   r   	_realpath!  s   r  )	USER_APISINTERNAL_APISc                   C   s
   t   S )a  Return the maximal number of threads for each detected library.

    Return a list with all the supported libraries that have been found. Each
    library is represented by a dict with the following information:

      - "user_api" : user API. Possible values are {USER_APIS}.
      - "internal_api": internal API. Possible values are {INTERNAL_APIS}.
      - "prefix" : filename prefix of the specific implementation.
      - "filepath": path to the loaded library.
      - "version": version of the library (if available).
      - "num_threads": the current thread limit.

    In addition, each library may contain internal_api specific entries.
    )r
   r=   r   r   r   r   r	   '  s   
r	   c                   @   sh   e Zd ZdZdddddZdd Zdd	 Zedddd
dZdd Z	e	Z
dd Zdd Zdd ZdS )_ThreadpoolLimitera  The guts of ThreadpoolController.limit

    Refer to the docstring of ThreadpoolController.limit for more details.

    It will only act on the library controllers held by the provided `controller`.
    Using the default constructor sets the limits right away such that it can be used as
    a callable. Setting the limits can be delayed by using the `wrap` class method such
    that it can be used as a decorator.
    Nlimitsr1   c                C   s6   || _ | ||\| _| _| _| j  | _|   d S r]   )_controller_check_params_limits	_user_api	_prefixesr=   _original_info_set_threadpool_limitsr.   r  r  r1   r   r   r   r/   E  s   z_ThreadpoolLimiter.__init__c                 C   s   | S r]   r   r<   r   r   r   	__enter__M  s   z_ThreadpoolLimiter.__enter__c                 C   s   |    d S r]   )restore_original_limits)r.   typer   	tracebackr   r   r   __exit__P  s   z_ThreadpoolLimiter.__exit__c                C   s   t |||dS )z@Return an instance of this class that can be used as a decorator)r  r  r1   )_ThreadpoolLimiterDecorator)clsr  r  r1   r   r   r   wrapS  s   z_ThreadpoolLimiter.wrapc                 C   s,   t | jj| jD ]\}}||d  qdS )z,Set the limits back to their original valuesr3   N)zipr  lib_controllersr  rB   )r.   lib_controlleroriginal_infor   r   r   r  Z  s
   
z*_ThreadpoolLimiter.restore_original_limitsc                    s   i }g }| j D ]1  fdd| jD }t|}t|}|dkr$| }n|dkr+d}n	t|}|  || < q|rGtdd	| d  |S )	zuOriginal num_threads from before calling threadpool_limits

        Return a dict `{user_api: num_threads}`.
        c                    s    g | ]}|d   kr|d qS )r1   r3   r   r4   lib_infor   r   r   r   m  s
    z?_ThreadpoolLimiter.get_original_num_threads.<locals>.<listcomp>ri   r   Nz1Multiple value possible for following user apis: , z. Returning the minimum.)
r  r  setlenpopminr   warningswarnjoin)r.   r3   warning_apisr  n_limitslimitr   r   r   get_original_num_threadsd  s0   




z+_ThreadpoolLimiter.get_original_num_threadsc                    s  t  tr dkr| j  \ } du st  trE|du r"t}n|tv r*|g}ntdt d| d durB fdd|D  g }n7t  trRdd  D  nt  t	r_d	d  j
D  t  tsntd
t  ddd  D }dd  D } ||fS )zCSuitable values for the _limits, _user_api and _prefixes attributessequential_blas_under_openmpNzuser_api must be either in z or None. Got z	 instead.c                    s   i | ]}| qS r   r   r4   apir  r   r   r9     r   z4_ThreadpoolLimiter._check_params.<locals>.<dictcomp>c                 S   s   i | ]	}|d  |d qS r!   r3   r   r(  r   r   r   r9     s    c                 S   s   i | ]}|j |jqS r   r:  r4   r&  r   r   r   r9     s    zUlimits must either be an int, a list, a dict, or 'sequential_blas_under_openmp'. Got z insteadc                 S      g | ]}|t v r|qS r   )r   )r4   r!   r   r   r   r         z4_ThreadpoolLimiter._check_params.<locals>.<listcomp>c                 S   r<  r   )r   r7  r   r   r   r     r=  )
isinstancer   r  ,_get_params_for_sequential_blas_under_openmpvaluesintr   
ValueErrorlistr
   r%  dict	TypeErrorr  )r.   r  r1   prefixesr   r9  r   r    sF   



z _ThreadpoolLimiter._check_paramsc                 C   sh   | j du rdS | jjD ]&}|j| j v r| j |j }n|j| j v r'| j |j }nq|dur1|| qdS )zChange the maximal number of threads in selected thread pools.

        Return a list with all the supported libraries that have been found
        matching `self._prefixes` and `self._user_api`.
        N)r  r  r%  r!   r1   rB   )r.   r&  r3   r   r   r   r    s   

z)_ThreadpoolLimiter._set_threadpool_limits)r   r   r   rH   r/   r  r   classmethodr#  r  
unregisterr5  r  r  r   r   r   r   r  :  s    
$3r  c                   @   s(   e Zd ZdZdddddZdd ZdS )r!  z8Same as _ThreadpoolLimiter but to be used as a decoratorNr  c                C   s"   |  ||\| _| _| _|| _d S r]   )r  r  r  r  r  r  r   r   r   r/     s   
z$_ThreadpoolLimiterDecorator.__init__c                 C   s   | j  | _|   | S r]   )r  r=   r  r  r<   r   r   r   r    s   z%_ThreadpoolLimiterDecorator.__enter__)r   r   r   rH   r/   r  r   r   r   r   r!    s    r!  r*  c                 c   s    | ]	}d | d V  qdS )"Nr   r7  r   r   r   rU     s    r  	BLAS_LIBSOPENMP_LIBSc                       s4   e Zd ZdZd fdd	Zed fdd	Z  ZS )r   ag  Change the maximal number of threads that can be used in thread pools.

    This object can be used either as a callable (the construction of this object
    limits the number of threads), as a context manager in a `with` block to
    automatically restore the original state of the controlled libraries when exiting
    the block, or as a decorator through its `wrap` method.

    Set the maximal number of threads that can be used in thread pools used in
    the supported libraries to `limit`. This function works for libraries that
    are already loaded in the interpreter and can be changed dynamically.

    This effect is global and impacts the whole Python process. There is no thread level
    isolation as these libraries do not offer thread-local APIs to configure the number
    of threads to use in nested parallel calls.

    Parameters
    ----------
    limits : int, dict, 'sequential_blas_under_openmp' or None (default=None)
        The maximal number of threads that can be used in thread pools

        - If int, sets the maximum number of threads to `limits` for each
          library selected by `user_api`.

        - If it is a dictionary `{{key: max_threads}}`, this function sets a
          custom maximum number of threads for each `key` which can be either a
          `user_api` or a `prefix` for a specific library.

        - If 'sequential_blas_under_openmp', it will chose the appropriate `limits`
          and `user_api` parameters for the specific use case of sequential BLAS
          calls within an OpenMP parallel region. The `user_api` parameter is
          ignored.

        - If None, this function does not do anything.

    user_api : {USER_APIS} or None (default=None)
        APIs of libraries to limit. Used only if `limits` is an int.

        - If "blas", it will only limit BLAS supported libraries ({BLAS_LIBS}).

        - If "openmp", it will only limit OpenMP supported libraries
          ({OPENMP_LIBS}). Note that it can affect the number of threads used
          by the BLAS libraries if they rely on OpenMP.

        - If None, this function will apply to all supported libraries.
    Nc                    s   t  jt ||d d S Nr  )r   r/   r
   r.   r  r1   r   r   r   r/     s   zthreadpool_limits.__init__c                    s   t  jt ||dS rM  )r   r#  r
   )r"  r  r1   r   r   r   r#    s   zthreadpool_limits.wrap)NN)r   r   r   rH   r/   rG  r#  r   r   r   r   r   r     s
    .r   c                   @   s  e Zd ZdZe Zdd Zedd Zdd Z	dd	 Z
d
d Zeddd eD dededdddddZeddd eD dededdd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ed)d* Zed+d, ZdS )-r
   zCollection of LibController objects for all loaded supported libraries

    Attributes
    ----------
    lib_controllers : list of `LibController` objects
        The list of library controllers of all loaded supported libraries.
    c                 C   s   g | _ |   |   d S r]   )r%  r   _warn_if_incompatible_openmpr<   r   r   r   r/   0  s   zThreadpoolController.__init__c                 C   s   |  | }||_|S r]   )__new__r%  )r"  r%  new_controllerr   r   r   _from_controllers5  s   
z&ThreadpoolController._from_controllersc                 C   s   dd | j D S )z.Return lib_controllers info as a list of dictsc                 S   s   g | ]}|  qS r   )r=   r;  r   r   r   r   =  s    z-ThreadpoolController.info.<locals>.<listcomp>)r%  r<   r   r   r   r=   ;  s   zThreadpoolController.infoc                    sH      D ]\}}t|ts|gn| |< q fdd| jD }t|S )a>  Return a ThreadpoolController containing a subset of its current
        library controllers

        It will select all libraries matching at least one pair (key, value) from kwargs
        where key is an entry of the library info dict (like "user_api", "internal_api",
        "prefix", ...) and value is the value or a list of acceptable values for that
        entry.

        For instance, `ThreadpoolController().select(internal_api=["blis", "openblas"])`
        will select all library controllers whose internal_api is either "blis" or
        "openblas".
        c                    s*   g | ] t  fd d D r qS )c                 3   s$    | ]\}}t  |d |v V  qd S r]   )rE   )r4   keyvalsr&  r   r   rU   R  s
    
z9ThreadpoolController.select.<locals>.<listcomp>.<genexpr>)anyr;   )r4   r  rU  r   r   O  s    z/ThreadpoolController.select.<locals>.<listcomp>)r;   r>  rC  r%  r
   rR  )r.   r  rS  rT  r%  r   rW  r   select?  s   

	zThreadpoolController.selectc                 C   s$   | j dddjrdddS dddS )zReturn appropriate params to use for a sequential BLAS call in an OpenMP loop

        This function takes into account the unexpected behavior of OpenBLAS with the
        OpenMP threading layer.
        rL   rr   )r2   r`   Nr  ri   rK   )rX  r%  r<   r   r   r   r?  Z  s   

zAThreadpoolController._get_params_for_sequential_blas_under_openmpr*  c                 c       | ]}d  |V  qdS z"{}"Nr  r7  r   r   r   rU   g      zThreadpoolController.<genexpr>rJ  Nr  c                C   s   t | ||dS )a  Change the maximal number of threads that can be used in thread pools.

        This function returns an object that can be used either as a callable (the
        construction of this object limits the number of threads) or as a context
        manager, in a `with` block to automatically restore the original state of the
        controlled libraries when exiting the block.

        Set the maximal number of threads that can be used in thread pools used in
        the supported libraries to `limits`. This function works for libraries that
        are already loaded in the interpreter and can be changed dynamically.

        This effect is global and impacts the whole Python process. There is no thread
        level isolation as these libraries do not offer thread-local APIs to configure
        the number of threads to use in nested parallel calls.

        Parameters
        ----------
        limits : int, dict, 'sequential_blas_under_openmp' or None (default=None)
            The maximal number of threads that can be used in thread pools

            - If int, sets the maximum number of threads to `limits` for each
              library selected by `user_api`.

            - If it is a dictionary `{{key: max_threads}}`, this function sets a
              custom maximum number of threads for each `key` which can be either a
              `user_api` or a `prefix` for a specific library.

            - If 'sequential_blas_under_openmp', it will chose the appropriate `limits`
              and `user_api` parameters for the specific use case of sequential BLAS
              calls within an OpenMP parallel region. The `user_api` parameter is
              ignored.

            - If None, this function does not do anything.

        user_api : {USER_APIS} or None (default=None)
            APIs of libraries to limit. Used only if `limits` is an int.

            - If "blas", it will only limit BLAS supported libraries ({BLAS_LIBS}).

            - If "openmp", it will only limit OpenMP supported libraries
              ({OPENMP_LIBS}). Note that it can affect the number of threads used
              by the BLAS libraries if they rely on OpenMP.

            - If None, this function will apply to all supported libraries.
        r  )r  rN  r   r   r   r4  f  s   3zThreadpoolController.limitc                 c   rY  rZ  r[  r7  r   r   r   rU     r\  c                C   s   t j| ||dS )a  Change the maximal number of threads that can be used in thread pools.

        This function returns an object that can be used as a decorator.

        Set the maximal number of threads that can be used in thread pools used in
        the supported libraries to `limits`. This function works for libraries that
        are already loaded in the interpreter and can be changed dynamically.

        Parameters
        ----------
        limits : int, dict or None (default=None)
            The maximal number of threads that can be used in thread pools

            - If int, sets the maximum number of threads to `limits` for each
              library selected by `user_api`.

            - If it is a dictionary `{{key: max_threads}}`, this function sets a
              custom maximum number of threads for each `key` which can be either a
              `user_api` or a `prefix` for a specific library.

            - If None, this function does not do anything.

        user_api : {USER_APIS} or None (default=None)
            APIs of libraries to limit. Used only if `limits` is an int.

            - If "blas", it will only limit BLAS supported libraries ({BLAS_LIBS}).

            - If "openmp", it will only limit OpenMP supported libraries
              ({OPENMP_LIBS}). Note that it can affect the number of threads used
              by the BLAS libraries if they rely on OpenMP.

            - If None, this function will apply to all supported libraries.
        r  )r  r#  rN  r   r   r   r#    s   'zThreadpoolController.wrapc                 C   s
   t | jS r]   )r,  r%  r<   r   r   r   __len__  s   
zThreadpoolController.__len__c                 C   sN   t jdkr|   dS t jdkr|   dS dt jv r!|   dS |   dS )zALoop through loaded shared libraries and store the supported onesdarwinwin32pyodideN)sysplatform_find_libraries_with_dyld+_find_libraries_with_enum_process_module_exmodules_find_libraries_pyodide$_find_libraries_with_dl_iterate_phdrr<   r   r   r   r     s   


z$ThreadpoolController._load_librariesc                    sl      }t|dstdt g S  fdd}ttjtt	tj
tj}||}td}||| dS )an  Loop through loaded libraries and return binders on supported ones

        This function is expected to work on POSIX system only.
        This code is adapted from code by Intel developer @anton-malakhov
        available at https://github.com/IntelPython/smp

        Copyright (c) 2017, Intel Corporation published under the BSD 3-Clause
        license
        dl_iterate_phdrz9Could not find dl_iterate_phdr in the C standard library.c                    s$   | j j}|r|d} | dS )Nrj   r   )contentsr   rm   _make_controller_from_path)r=   sizedatar    r<   r   r   match_library_callback  s
   

zYThreadpoolController._find_libraries_with_dl_iterate_phdr.<locals>.match_library_callback    N)	_get_libcr[   r/  r0  RuntimeWarningr   	CFUNCTYPEr   POINTERr   c_size_tr   rh  )r.   libcrm  c_func_signaturec_match_library_callbackrl  r   r<   r   rg    s"   



z9ThreadpoolController._find_libraries_with_dl_iterate_phdrc                 C   sj   |   }t|dstdt g S | }tj|j_	t
|D ]}t||}|d}| | qdS )zLoop through loaded libraries and return binders on supported ones

        This function is expected to work on OSX system only
        _dyld_image_countz;Could not find _dyld_image_count in the C standard library.rj   N)ro  r[   r/  r0  rp  rw  r   r   _dyld_get_image_namerk   r   	string_atrm   rj  )r.   rt  n_dyldr   r    r   r   r   rc    s   


z.ThreadpoolController._find_libraries_with_dyldc              	   C   sN  ddl m}m}m} d}d}d}| d}| d}|||B dt }	|	s0td	t  zpd
}
| }	 ||
  }t	
|}||	t	||t	||sTtd||jkrZn|j||
  }
q7|j||
  }t||d| }t	|}| }|D ]}||	|t	|t	|std|j}| | q|W ||	 dS ||	 w )a  Loop through loaded libraries and return binders on supported ones

        This function is expected to work on windows system only.
        This code is adapted from code by Philipp Hagemeister @phihag available
        at https://stackoverflow.com/questions/17474574
        r   )DWORDHMODULEMAX_PATHr      r   Psapikernel32FzCould not open PID    TzEnumProcessModulesEx failedNzGetModuleFileNameEx failed)ctypes.wintypesr{  r|  r}  _get_windllOpenProcessr
  getpidOSErrorr   r   EnumProcessModulesExr   r   mapcreate_unicode_bufferGetModuleFileNameExWrj  CloseHandle)r.   r{  r|  r}  PROCESS_QUERY_INFORMATIONPROCESS_VM_READLIST_LIBRARIES_ALLps_api	kernel_32	h_process	buf_countneededbufbuf_sizecount	h_modulesn_sizeh_moduler    r   r   r   rd    sT   





z@ThreadpoolController._find_libraries_with_enum_process_module_exc                 C   sZ   zddl m} W n ty   td Y dS w |j D ]}tj	|r*| 
| qdS )a  Pyodide specific implementation for finding loaded libraries.

        Adapted from suggestion in https://github.com/joblib/threadpoolctl/pull/169#issuecomment-1946696449.

        One day, we may have a simpler solution. libc dl_iterate_phdr needs to
        be implemented in Emscripten and exposed in Pyodide, see
        https://github.com/emscripten-core/emscripten/issues/21354 for more
        details.
        r   )LDSOzHUnable to import LDSO from pyodide_js._module. This should never happen.N)pyodide_js._moduler  ImportErrorr/  r0  loadedLibsByNameas_object_mapr
  r  existsrj  )r.   r  r    r   r   r   rf  P  s   

z,ThreadpoolController._find_libraries_pyodidec                    s   t |}tj| }tD ]U}| ||j}|du rq|dkr:|dr9t	
|ttfdd|jD s8qnq|||| d |dd | jD v rLqt|dr]t fd	d|jD rc| j  qdS )
z:Store a library controller if it is supported and selectedNrN   .dllc                 3   s    | ]}t  |V  qd S r]   )r[   r4   func)rN   r   r   rU     s
    
zBThreadpoolController._make_controller_from_path.<locals>.<genexpr>r   c                 s   r   r]   r  r   r   r   r   rU     r   r{   c                 3   s    | ]	}t  j|V  qd S r]   )r[   r'   r  rU  r   r   rU     s
    

)r  r
  r  basenamelowerr   _check_prefixry   endswithr   r%   r&   rV  r{   r%  r[   r   )r.   r    filenamecontroller_classr!   r   )r&  rN   r   rj  k  s4   
z/ThreadpoolController._make_controller_from_pathc                 C   s    |D ]}| |r|  S qdS )z]Return the prefix library_basename starts with

        Return None if none matches.
        N)
startswith)r.   library_basenamery   r!   r   r   r   r    s
   
z"ThreadpoolController._check_prefixc                 C   sB   dd | j D }td}d|v rd|v rt|t dS dS dS )z?Raise a warning if llvm-OpenMP and intel-OpenMP are both loadedc                 S   r   r   )r!   r;  r   r   r   r     r   zEThreadpoolController._warn_if_incompatible_openmp.<locals>.<listcomp>a  
            Found Intel OpenMP ('libiomp') and LLVM OpenMP ('libomp') loaded at
            the same time. Both libraries are known to be incompatible and this
            can cause random crashes or deadlocks on Linux when loaded in the
            same Python program.
            Using threadpoolctl may cause crashes or deadlocks. For more
            information and possible workarounds, please see
                https://github.com/joblib/threadpoolctl/blob/master/multiple_openmp.md
            r   r   N)r%  textwrapdedentr/  r0  rp  )r.   rF  msgr   r   r   rO    s   z1ThreadpoolController._warn_if_incompatible_openmpc                 C   s4   | j d}|du rtjtdtd}|| j d< |S )z Load the lib-C for unix systems.rt  Ncr#   )_system_librariesgetr   r%   r   r&   )r"  rt  r   r   r   ro    s
   
zThreadpoolController._get_libcc                 C   s2   | j |}|du rt| d}|| j |< |S )zLoad a windows DLLNr  )r  r  r   WinDLL)r"  dll_namedllr   r   r   r    s
   
z ThreadpoolController._get_windll)r   r   r   rH   rD  r  r/   rG  rR  r=   rX  r?  r  r1  r   _ALL_BLAS_LIBRARIES_ALL_OPENMP_LIBRARIESr4  r#  r]  r   rg  rc  rd  rf  rj  r  rO  ro  r  r   r   r   r   r
   !  sD    
0$)>:

r
   c               	   C   s   ddl } ddl}ddl}ddl}| jddd}|jdddd	d
dd |jdddd ||jdd }|jD ]}z	|j	|dd W q7 t
yS   td||jd Y q7w |jr\t|j t|jt dd dS )zBCommandline interface to display thread-pool information and exit.r   Nz5python -m threadpoolctl -i numpy scipy.linalg xgboostz)Display thread-pool information and exit.)usagedescriptionz-iz--importre  *r   z;Python modules to import before introspecting thread-pools.)destnargsdefaulthelpz-cz	--commandz@a Python statement to execute before introspecting thread-pools.)r  ri   )packagezWARNING: could not import)filerq   )indent)argparse	importlibjsonra  ArgumentParseradd_argument
parse_argsargvre  import_moduler  printstderrcommandexecdumpsr	   )r  r  r  ra  parseroptionsmoduler   r   r   _main  s>   

r  __main__)=rH   r
  r   ra  r   rW   r  typingr   r/  ctypes.utilr   abcr   r   	functoolsr   
contextlibr   __version____all__environ
setdefaultr	  c_uint64c_uint32r   c_uint16r   	Structurer   RTLD_NOLOADr&   AttributeErrorDEFAULT_MODEr   rJ   r|   r   r   r   r   rC  r+  r   r   r   r  ry   r  r   r  r  r	   r  r!  r1  r   r
   r  r   r   r   r   r   <module>   s    


[H? 8		

 7   ;
&
