U
    9%e                     @   s`  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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d	d
dddgZejdd ejdkrejnejZejdkrejnejZG dd dejZz
ejZW n e k
r   ej!ZY nX 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&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&j.Z/d"d Z0d#d$ Z1ed%d&d'd( Z2e1e(e*e+d)d*d
 Z3G d+d, d,Z4G d-d. d.e4eZ5e1d/6d0d e*D d/6e-d/6e/d1G d2d	 d	e4Z7G d3d dZ8d4d5 Z9e:d6kr\e9  dS )7zthreadpoolctl

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.2.0threadpool_limitsthreadpool_infoThreadpoolControllerLibControllerregisterZ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_infoZ	dlpi_addr	dlpi_nameZ	dlpi_phdrZ
dlpi_phnumN)	__name__
__module____qualname___SYSTEM_UINTctypesc_char_pZc_void_p_SYSTEM_UINT_HALFZ_fields_ r   r   L/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/threadpoolctl.pyr   6   s
   r   c                   @   sh   e Zd ZdZedddddZedd Zdd	 Zed
d Z	e
dd Ze
dd Ze
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prefixc                C   s2   || _ || _tj|td| _|  | _|   dS )z0This is not meant to be overriden by subclasses.modeN)	r   r   r   CDLL_RTLD_NOLOADdynlibget_versionversionset_additional_attributes)selfr   r   r   r   r   __init__j   s
    
zLibController.__init__c                 C   s(   | j | j| jdt| }|d |S )ziReturn relevant info wrapped in a dict

        This is not meant to be overriden by subclasses.
        )user_apiinternal_apinum_threadsr    )r&   r'   r(   varspop)r$   Zexposed_attrsr   r   r   infos   s    
zLibController.infoc                 C   s   dS )z>Set additional attributes meant to be exposed in the info dictNr   r$   r   r   r   r#      s    z'LibController.set_additional_attributesc                 C   s   |   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   r(      s    zLibController.num_threadsc                 C   s   dS )z5Return the maximum number of threads available to useNr   r,   r   r   r   r-      s    zLibController.get_num_threadsc                 C   s   dS )z(Set the maximum number of threads to useNr   )r$   r(   r   r   r   set_num_threads   s    zLibController.set_num_threadsc                 C   s   dS )z(Return the version of the shared libraryNr   r,   r   r   r   r!      s    zLibController.get_version)r   r   r   __doc__r   r%   r+   r#   propertyr(   r   r-   r.   r!   r   r   r   r   r   F   s   #



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 )OpenBLASControllerzController class for OpenBLASblasopenblas)Zlibopenblaslibblas)openblas_get_num_threadsopenblas_get_num_threads64_c                 C   s   |   | _|  | _d S N_get_threading_layerthreading_layer_get_architecturearchitecturer,   r   r   r   r#      s    
z,OpenBLASController.set_additional_attributesc                 C   s"   t | jdt | jddd }| S )Nr5   r6   c                   S   s   d S r7   r   r   r   r   r   <lambda>       z4OpenBLASController.get_num_threads.<locals>.<lambda>getattrr    r$   get_funcr   r   r   r-      s    z"OpenBLASController.get_num_threadsc                 C   s$   t | jdt | jddd }||S )NZopenblas_set_num_threadsZopenblas_set_num_threads64_c                 S   s   d S r7   r   r(   r   r   r   r=      r>   z4OpenBLASController.set_num_threads.<locals>.<lambda>r?   r$   r(   Zset_funcr   r   r   r.      s      z"OpenBLASController.set_num_threadsc                 C   sT   t | jdt | jdd }|d kr$d S tj|_|  }|d dkrP|d dS d S )NZopenblas_get_configZopenblas_get_config64_r   s   OpenBLAS   utf-8)r@   r    r   r   restypesplitdecode)r$   Z
get_configconfigr   r   r   r!      s    
zOpenBLASController.get_versionc                 C   sF   t | jdt | jdd}|dkr$dS | }|dkr6dS |dkrBdS d	S )
z&Return the threading layer of OpenBLASopenblas_get_parallelZopenblas_get_parallel64_Nunknown   openmprE   pthreadsdisabledr?   )r$   rK   r:   r   r   r   r9      s    z'OpenBLASController._get_threading_layerc                 C   s8   t | jdt | jdd}|dkr$dS tj|_| dS )z,Return the architecture detected by OpenBLASZopenblas_get_corenameZopenblas_get_corename64_NrF   r@   r    r   r   rG   rI   )r$   Zget_corenamer   r   r   r;      s    z$OpenBLASController._get_architectureNr   r   r   r/   r&   r'   filename_prefixescheck_symbolsr#   r-   r.   r!   r9   r;   r   r   r   r   r1      s   
r1   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 BLISr2   Zblis)Zlibblisr4   )bli_thread_get_num_threadsc                 C   s   |   | _|  | _d S r7   r8   r,   r   r   r   r#      s    
z(BLISController.set_additional_attributesc                 C   s(   t | jddd }| }|dkr$dS |S )NrV   c                   S   s   d S r7   r   r   r   r   r   r=      r>   z0BLISController.get_num_threads.<locals>.<lambda>rE   r?   )r$   rB   r(   r   r   r   r-      s    zBLISController.get_num_threadsc                 C   s   t | jddd }||S )NZbli_thread_set_num_threadsc                 S   s   d S r7   r   rC   r   r   r   r=      r>   z0BLISController.set_num_threads.<locals>.<lambda>r?   rD   r   r   r   r.      s      zBLISController.set_num_threadsc                 C   s.   t | jdd }|d krd S tj|_| dS )NZbli_info_get_version_strrF   rQ   )r$   Zget_version_r   r   r   r!     s
    zBLISController.get_versionc                 C   s    | j  rdS | j  rdS dS )z"Return the threading layer of BLISrN   rO   rP   )r    Zbli_info_get_enable_openmpZbli_info_get_enable_pthreadsr,   r   r   r   r9     s
    

z#BLISController._get_threading_layerc                 C   sP   t | jdd}t | jdd}|dks,|dkr0dS tj|_tj|_|| dS )z(Return the architecture detected by BLISbli_arch_query_idNbli_arch_stringrF   )r@   r    r   c_intrG   r   rI   )r$   rX   rY   r   r   r   r;     s    z BLISController._get_architectureNrR   r   r   r   r   rU      s   rU   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 MKLr2   Zmkl)Z	libmkl_rtZmkl_rtr4   )MKL_Get_Max_Threadsc                 C   s   |   | _d S r7   )r9   r:   r,   r   r   r   r#   )  s    z'MKLController.set_additional_attributesc                 C   s   t | jddd }| S )Nr\   c                   S   s   d S r7   r   r   r   r   r   r=   -  r>   z/MKLController.get_num_threads.<locals>.<lambda>r?   rA   r   r   r   r-   ,  s    zMKLController.get_num_threadsc                 C   s   t | jddd }||S )NZMKL_Set_Num_Threadsc                 S   s   d S r7   r   rC   r   r   r   r=   1  r>   z/MKLController.set_num_threads.<locals>.<lambda>r?   rD   r   r   r   r.   0  s    zMKLController.set_num_threadsc                 C   s\   t | jdsd S td}| j|d |jd}td|}|d k	rT|	 d }|
 S )NMKL_Get_Version_String   rF   zVersion ([^ ]+) r   )hasattrr    r   create_string_bufferr]   valuerI   researchgroupsstrip)r$   resr"   groupr   r   r   r!   4  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 MKLZMKL_Set_Threading_Layerc                 S   s   dS )NrW   r   )layerr   r   r   r=   G  r>   z4MKLController._get_threading_layer.<locals>.<lambda>intel
sequentialZpgiZgnuZtbbznot specified)r   rE   rM         rW   rW   r?   )r$   Zset_threading_layerZ	layer_mapr   r   r   r9   A  s      z"MKLController._get_threading_layerN)r   r   r   r/   r&   r'   rS   rT   r#   r-   r.   r!   r9   r   r   r   r   r[   !  s   r[   c                   @   s4   e 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 OpenMPrN   )libiompZlibgomplibompZvcompc                 C   s   t | jddd }| S )NZomp_get_max_threadsc                   S   s   d S r7   r   r   r   r   r   r=   \  r>   z2OpenMPController.get_num_threads.<locals>.<lambda>r?   rA   r   r   r   r-   [  s    z OpenMPController.get_num_threadsc                 C   s   t | jddd }||S )NZomp_set_num_threadsc                 S   s   d S r7   r   rC   r   r   r   r=   `  r>   z2OpenMPController.set_num_threads.<locals>.<lambda>r?   rD   r   r   r   r.   _  s    z OpenMPController.set_num_threadsc                 C   s   d S r7   r   r,   r   r   r   r!   c  s    zOpenMPController.get_versionN)
r   r   r   r/   r&   r'   rS   r-   r.   r!   r   r   r   r   rm   T  s   rm   c                 c   s   | ]}|j V  qd S r7   r&   .0libr   r   r   	<genexpr>m  s     rt   c                 C   s   g | ]
}|j qS r   )r'   rq   r   r   r   
<listcomp>n  s     ru   c                 c   s   | ]}|j D ]
}|V  qqd S r7   )rS   )rr   rs   r   r   r   r   rt   p  s       c                 C   s   g | ]}|j d kr|jqS )r2   )r&   r'   rq   r   r   r   ru   r  s    
 c                 C   s2   t |  t| j t| j t| j dS )zRegister a new controllerN)	_ALL_CONTROLLERSappend_ALL_USER_APISr&   _ALL_INTERNAL_APISr'   _ALL_PREFIXESextendrS   )
controllerr   r   r   r   x  s    
c                     s    fdd}|S )Nc                    s   | j d k	r| j j | _ | S r7   )r/   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_APISZ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    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limitsr&   c                C   s6   || _ | ||\| _| _| _| j  | _|   d S r7   )_controller_check_params_limits	_user_api	_prefixesr+   _original_info_set_threadpool_limitsr$   r|   r   r&   r   r   r   r%     s     z_ThreadpoolLimiter.__init__c                 C   s   | S r7   r   r,   r   r   r   	__enter__  s    z_ThreadpoolLimiter.__enter__c                 C   s   |    d S r7   )restore_original_limits)r$   typera   	tracebackr   r   r   __exit__  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   r&   )_ThreadpoolLimiterDecorator)clsr|   r   r&   r   r   r   wrap  s
      z_ThreadpoolLimiter.wrapc                 C   s,   t | jj| jD ]\}}||d  qdS )z,Set the limits back to their original valuesr(   N)zipr   lib_controllersr   r.   )r$   lib_controllerZoriginal_infor   r   r   r     s
     z*_ThreadpoolLimiter.restore_original_limitsc                    s   i }g }| j D ]b  fdd| jD }t|}t|}|dkrH| }n |dkrVd}nt|}|  || < q|rt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 )r&   r(   r   rr   Zlib_inforp   r   r   ru     s   z?_ThreadpoolLimiter.get_original_num_threads.<locals>.<listcomp>rE   r   Nz1Multiple value possible for following user apis: , z. Returning the minimum.)
r   r   setlenr*   minrw   warningswarnjoin)r$   r(   Zwarning_apisr   Zn_limitslimitr   rp   r   get_original_num_threads  s0    




z+_ThreadpoolLimiter.get_original_num_threadsc                    s  t  tr$ dkr$| j  \ } dks6t  tr|dkrDt}n&|tkrT|g}ntdt d| d dk	r fdd|D  g }nnt  trdd  D  nt  t	rd	d  j
D  t  tstd
t  ddd  D }dd  D } ||fS )zCSuitable values for the _limits, _user_api and _prefixes attributesZsequential_blas_under_openmpNzuser_api must be either in z or None. Got z	 instead.c                    s   i | ]
}| qS r   r   rr   apir   r   r   
<dictcomp>  s      z4_ThreadpoolLimiter._check_params.<locals>.<dictcomp>c                 S   s   i | ]}|d  |d qS r   r(   r   r   r   r   r   r     s     c                 S   s   i | ]}|j |jqS r   r   rr   r   r   r   r   r     s    zUlimits must either be an int, a list, a dict, or 'sequential_blas_under_openmp'. Got z insteadc                 S   s   g | ]}|t kr|qS r   )rz   )rr   r   r   r   r   ru     s      z4_ThreadpoolLimiter._check_params.<locals>.<listcomp>c                 S   s   g | ]}|t kr|qS r   )rx   r   r   r   r   ru     s      )
isinstancestrr   ,_get_params_for_sequential_blas_under_openmpvaluesintrx   
ValueErrorlistr
   r   dict	TypeErrorr   )r$   r   r&   prefixesr   r   r   r     s>    


z _ThreadpoolLimiter._check_paramsc                 C   sh   | j dkrdS | jjD ]L}|j| j kr4| j |j }n|j| j kr| j |j }nq|dk	r|| 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   r&   r.   )r$   r   r(   r   r   r   r   #  s    
z)_ThreadpoolLimiter._set_threadpool_limits)r   r   r   r/   r%   r   r   classmethodr   r   
unregisterr   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 r7   )r   r   r   r   r   r   r   r   r   r%   >  s
     z$_ThreadpoolLimiterDecorator.__init__c                 C   s   | j  | _|   | S r7   )r   r+   r   r   r,   r   r   r   r   D  s    z%_ThreadpoolLimiterDecorator.__enter__)r   r   r   r/   r%   r   r   r   r   r   r   ;  s   r   r   c                 c   s   | ]}d | d V  qdS )"Nr   r   r   r   r   rt   N  s     r   Z	BLAS_LIBSZ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   )superr%   r
   r$   r   r&   	__class__r   r   r%     s    zthreadpool_limits.__init__c                    s   t  jt ||dS r   )r   r   r
   )r   r   r&   r   r   r   r     s    zthreadpool_limits.wrap)NN)NN)r   r   r   r/   r%   r   r   __classcell__r   r   r   r   r   M  s   .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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 r7   )r   _load_libraries_warn_if_incompatible_openmpr,   r   r   r   r%     s    zThreadpoolController.__init__c                 C   s   |  | }||_|S r7   )__new__r   )r   r   Znew_controllerr   r   r   _from_controllers  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   ru     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 |kV  qd S r7   )r@   )rr   keyvalsr   r   r   rt     s   z9ThreadpoolController.select.<locals>.<listcomp>.<genexpr>)anyitems)rr   r   r   r   ru     s
   z/ThreadpoolController.select.<locals>.<listcomp>)r   r   r   r   r
   r   )r$   r   r   r   r   r   r   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.
        r3   rN   )r'   r:   Nr   rE   r2   )r   r   r,   r   r   r   r     s     
zAThreadpoolController._get_params_for_sequential_blas_under_openmpr   c                 c   s   | ]}d  |V  qdS z"{}"Nr}   r   r   r   r   rt     s     zThreadpoolController.<genexpr>r   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   r   r   r   r   r     s    3zThreadpoolController.limitc                 c   s   | ]}d  |V  qdS r   r   r   r   r   r   rt     s     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   r   r   r   r   r     s    'zThreadpoolController.wrapc                 C   s
   t | jS r7   )r   r   r,   r   r   r   __len__,  s    zThreadpoolController.__len__c                 C   s4   t jdkr|   nt jdkr(|   n|   dS )zALoop through loaded shared libraries and store the supported onesdarwinwin32N)sysplatform_find_libraries_with_dyld+_find_libraries_with_enum_process_module_ex$_find_libraries_with_dl_iterate_phdrr,   r   r   r   r   /  s
    



z$ThreadpoolController._load_librariesc                    s`      }t|ds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_phdrc                    s$   | j j}|r |d} | dS )NrF   r   )contentsr   rI   _make_controller_from_path)r+   sizedatar   r,   r   r   match_library_callbackH  s
    

zYThreadpoolController._find_libraries_with_dl_iterate_phdr.<locals>.match_library_callbackr>   N)
	_get_libcr_   r   Z	CFUNCTYPErZ   ZPOINTERr   Zc_size_tr   r   )r$   libcr   Zc_func_signatureZc_match_library_callbackr   r   r,   r   r   8  s    



z9ThreadpoolController._find_libraries_with_dl_iterate_phdrc                 C   s^   |   }t|dsg S | }tj|j_t|D ](}t||}|	d}| 
| q0dS )zLoop through loaded libraries and return binders on supported ones

        This function is expected to work on OSX system only
        _dyld_image_countrF   N)r   r_   r   r   r   Z_dyld_get_image_namerG   rangeZ	string_atrI   r   )r$   r   Zn_dyldir   r   r   r   r   ]  s    


z.ThreadpoolController._find_libraries_with_dyldc              	   C   sF  ddl m}m}m} d}d}d}| d}| d}|||B dt }	|	s`td	t  zd
}
| }||
  }t
|}||	t
||t
||std||jkrq|j||
  }
ql|j||
  }t||d| }t
|}| }|D ]:}||	|t
|t
|s td|j}| | qW 5 |	|	 X dS )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_PATHi      rk   ZPsapikernel32FzCould not open PID    zEnumProcessModulesEx failedNzGetModuleFileNameEx failed)Zctypes.wintypesr   r   r   _get_windllZOpenProcessr   getpidOSErrorZCloseHandler   ZsizeofZEnumProcessModulesExZbyrefra   mapZcreate_unicode_bufferZGetModuleFileNameExWr   )r$   r   r   r   ZPROCESS_QUERY_INFORMATIONZPROCESS_VM_READZLIST_LIBRARIES_ALLZps_apiZ	kernel_32Z	h_processZ	buf_countneededbufZbuf_sizecountZ	h_modulesZn_sizeZh_moduler   r   r   r   r   p  sX    

  



   z@ThreadpoolController._find_libraries_with_enum_process_module_exc                    s   t |}tj| }tD ]p}| ||j}|dkr8q|dkrt|drt	
|t t fdd|jD stqnq|||d}| j| qdS )z:Store a library controller if it is supported and selectedNr4   .dllc                 3   s   | ]}t  |V  qd S r7   )r_   )rr   funcr4   r   r   rt     s   zBThreadpoolController._make_controller_from_path.<locals>.<genexpr>r   )r   r   r   basenamelowerrv   _check_prefixrS   endswithr   r   r   r   rT   r   rw   )r$   r   filenamecontroller_classr   r   r   r   r   r     s     
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$   Zlibrary_basenamerS   r   r   r   r   r     s    

z"ThreadpoolController._check_prefixc                 C   s:   dd | j D }td}d|kr6d|kr6t|t dS )z?Raise a warning if llvm-OpenMP and intel-OpenMP are both loadedc                 S   s   g | ]
}|j qS r   )r   r   r   r   r   ru     s     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
            ro   rn   N)r   textwrapdedentr   r   RuntimeWarning)r$   r   msgr   r   r   r     s    
	z1ThreadpoolController._warn_if_incompatible_openmpc                 C   sf   | j d}|dkrbtd}|dkrJtdtjj dtjj dt	 dS t
j|td}|| j d< |S )z Load the lib-C for unix systems.r   Ncz,libc not found. The ctypes module in Python .z is maybe too old for this OS.r   )_system_librariesgetr   r   r   r   version_infomajorminorr   r   r   r   )r   r   Z	libc_namer   r   r   r     s    
zThreadpoolController._get_libcc                 C   s2   | j |}|dkr.t| d}|| j |< |S )zLoad a windows DLLNr   )r  r  r   ZWinDLL)r   Zdll_nameZdllr   r   r   r     s
    
z ThreadpoolController._get_windll)r   r   r   r/   r   r  r%   r   r   r+   r   r   r   r   rx   _ALL_BLAS_LIBRARIES_ALL_OPENMP_LIBRARIESr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
     s@   
0$	%>-

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 qn t
k
r   td||jd Y qnX qn|jrt|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--importmodules*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  rE   )packagezWARNING: could not import)filerM   )indent)argparse	importlibjsonr   ArgumentParseradd_argument
parse_argsargvr
  import_moduleImportErrorprintstderrcommandexecdumpsr	   )r  r  r  r   parseroptionsmoduler   r   r   _main  s<    

r$  __main__);r/   r   rb   r   r   r   typingr   r   ctypes.utilr   abcr   r   	functoolsr   
contextlibr   __version____all__environ
setdefaultr   Zc_uint64Zc_uint32r   Zc_uint16r   Z	Structurer   RTLD_NOLOADr   AttributeErrorZDEFAULT_MODEr   r1   rU   r[   rm   rv   r   r   rx   ry   rz   r  rS   r  r   r   r   r	   r   r   r   r   r
   r$  r   r   r   r   r   <module>   s~   

TP73	

 7   
&
