
    -i"                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJr  SSK	r	SSK
Jr  SSKJrJr  SSKJr  SSKJr  Sr/ S	Qr\R*                  R-                  S
S5        \R.                  S:  a  \R0                  O\R2                  r\R.                  S:  a  \R2                  O\R6                  r " S S\R:                  5      r \R>                  r  " S S\5      r# " S S\#5      r$ " S S\#5      r% " S S\#5      r& " S S\#5      r' " S S\#5      r(\$\%\'\(\&/r)\*" \+" S \) 5       5      5      r,\) V s/ s H  o RZ                  PM     sn r.\*" \+" S \) 5       5      5      r/\) V s/ s H   o R`                  S:X  d  M  U RZ                  PM"     sn r1\(Rd                  r3S r4S r5\" S S!9S" 5       r6\5" \*" \,5      \.S#9S$ 5       r7 " S% S&5      r8 " S' S(\8\5      r9\5" S)Ru                  S* \, 5       5      S)Ru                  \15      S)Ru                  \35      S+9 " S, S-\85      5       r; " S. S/5      r<S0 r=\>S1:X  a  \=" 5         gg! \! a    \RD                  r  GNf = fs  sn f s  sn f )2zthreadpoolctl

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.6.0)threadpool_limitsthreadpool_infoThreadpoolControllerLibControllerregisterKMP_DUPLICATE_LIB_OKTruel        c                   X    \ rS rSrS\4S\R                  4S\R                  4S\4/r	Sr
g)_dl_phdr_info8   	dlpi_addr	dlpi_name	dlpi_phdr
dlpi_phnum N)__name__
__module____qualname____firstlineno___SYSTEM_UINTctypesc_char_pc_void_p_SYSTEM_UINT_HALF_fields___static_attributes__r       @/var/www/html/venv/lib/python3.13/site-packages/threadpoolctl.pyr   r   8   s2    	l#	foo&	foo&	()	Hr#   r   c                       \ rS rSrSr\SSSS.S j5       rS rS r\	S 5       r
\S	 5       r\S
 5       r\S 5       rS rS rSrg)r   H   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                    X0l         X l        Xl        [        R                  " U[
        S9U l        U R                  5       u  U l        U l	        U R                  5       U l        U R                  5         g)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*   s       r$   __init__LibController.__init__l   sZ      kk(>373E3E3G0T0'')&&(r#   c                     SnU R                   U R                  U R                  S.[        U 5      R	                  5        VVs0 s H  u  p#X!;  d  M  X#_M     snnE$ s  snnf )&Return relevant info wrapped in a dict)r0   r*   r2   r3   )user_apiinternal_apinum_threads)r<   r=   r>   varsitems)r7   hidden_attrskvs       r$   infoLibController.infow   sc    O --++
 !%T
 0 0 2L 2a6Ktqt 2L	
 	
 Ms   AAc                     g)z>Set additional attributes meant to be exposed in the info dictNr   r7   s    r$   r6   'LibController.set_additional_attributes       r#   c                 "    U R                  5       $ )zqExposes the current thread limit as a dynamic property

This is not meant to be used or overriden by subclasses.
)get_num_threadsrG   s    r$   r>   LibController.num_threads   s     ##%%r#   c                     g)z5Return the maximum number of threads available to useNr   rG   s    r$   rK   LibController.get_num_threads   rI   r#   c                     g)z(Set the maximum number of threads to useNr   )r7   r>   s     r$   set_num_threadsLibController.set_num_threads   rI   r#   c                     g)z(Return the version of the shared libraryNr   rG   s    r$   r4   LibController.get_version   rI   r#   c                     g)z8Return the affixes for the symbols of the shared library) rU   r   rG   s    r$   r1   LibController._find_affixes   s    r#   c                 d    [        U R                  U R                   U U R                   3S5      $ )zBReturn the symbol of the shared library accounding for the affixesN)getattrr0   r2   r3   )r7   names     r$   _get_symbolLibController._get_symbol   s5    KKD//0t7J7J6KLd
 	
r#   )r2   r3   r0   r(   r*   r)   r5   )r   r   r   r   __doc__r   r8   rD   r6   propertyr>   r   rK   rP   r4   r1   rZ   r"   r   r#   r$   r   r   H   s    !F #'T ) )
M & & D D 7 7 7 7
r#   r   c                       \ rS rSrSrSrSrSrSrSr	\
" S \R                  " \\	5       5       5      rS	 rS
 rS rS rS rS rS rSrg)OpenBLASController   zController class for OpenBLASblasopenblas)libopenblaslibblaslibscipy_openblas)rU   scipy_)rU   64__64c              #   4   #    U  H  u  pU S U 3v   M     g7f)openblas_get_num_threadsNr   ).0r)   suffixs      r$   	<genexpr>OpenBLASController.<genexpr>   s%      SNF (*6(3Ss   c                     [         R                  " U R                  U R                  5       H*  u  p[	        U R
                  U SU 35      (       d  M'  X4s  $    g Nrj   )	itertoolsproduct_symbol_prefixes_symbol_suffixeshasattrr0   )r7   r)   rl   s      r$   r1    OpenBLASController._find_affixes   sQ    '//!!4#8#8
NF t{{vh.Fvh$OPP~%	
r#   c                 X    U R                  5       U l        U R                  5       U l        g N_get_threading_layerthreading_layer_get_architecturearchitecturerG   s    r$   r6   ,OpenBLASController.set_additional_attributes   $    #88: 224r#   c                 :    U R                  S5      nUb  U" 5       $ g rp   rZ   )r7   get_num_threads_funcs     r$   rK   "OpenBLASController.get_num_threads   s&    #//0JK+'))r#   c                 <    U R                  S5      nUb  U" U5      $ g )Nopenblas_set_num_threadsr   )r7   r>   set_num_threads_funcs      r$   rP   "OpenBLASController.set_num_threads   s(    #//0JK+'44r#   c                     U R                  S5      nUbH  [        R                  Ul        U" 5       R	                  5       nUS   S:X  a  US   R                  S5      $ g g )Nopenblas_get_configr   s   OpenBLAS   utf-8)rZ   r   r   restypesplitdecode)r7   get_version_funcconfigs      r$   r4   OpenBLASController.get_version   sa      ++,AB''-$%'--/FayK'ay''00r#   c                 X    U R                  S5      nUb  U" 5       nUS:X  a  gUS:X  a  ggg)z&Return the threading layer of OpenBLASopenblas_get_parallel   openmpr   pthreadsdisabledunknownr   )r7   get_threading_layer_funcr{   s      r$   rz   'OpenBLASController._get_threading_layer   s>    #'#3#34K#L #/68O!# A%!r#   c                     U R                  S5      nUb+  [        R                  Ul        U" 5       R	                  S5      $ g)z,Return the architecture detected by OpenBLASopenblas_get_corenameNr   )rZ   r   r   r   r   )r7   get_architecture_funcs     r$   r|   $OpenBLASController._get_architecture   s>     $ 0 01H I ,,2OO!)(*11'::r#   r}   r{   N)r   r   r   r   r\   r<   r=   filename_prefixesrs   rt   tuplerq   rr   check_symbolsr1   r6   rK   rP   r4   rz   r|   r"   r   r#   r$   r_   r_      sl    'HLG%)  '//0@BRS M
&5

r#   r_   c                   L    \ rS rSrSrSrSrSrSrS r	S r
S	 rS
 rS rS rSrg)BLISController   zController class for BLISra   blis)libblisrd   )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                 X    U R                  5       U l        U R                  5       U l        g rx   ry   rG   s    r$   r6   (BLISController.set_additional_attributes   r   r#   c                 T    [        U R                  SS 5      nU" 5       nUS:X  a  S$ U$ )Nr   c                      g rx   r   r   r#   r$   <lambda>0BLISController.get_num_threads.<locals>.<lambda>   s    dr#   r   rX   r0   r7   get_funcr>   s      r$   rK   BLISController.get_num_threads   s0    4;;(DlSj  2%q6;6r#   c                 B    [        U R                  SS 5      nU" U5      $ )Nr   c                     g rx   r   r>   s    r$   r   0BLISController.set_num_threads.<locals>.<lambda>  s    4r#   r   r7   r>   set_funcs      r$   rP   BLISController.set_num_threads  s&    KK57O
 $$r#   c                     [        U R                  SS 5      nUc  g [        R                  Ul        U" 5       R                  S5      $ )Nr   r   )rX   r0   r   r   r   r   )r7   get_version_s     r$   r4   BLISController.get_version  s=    t{{,FM%~$$W--r#   c                     [        U R                  SS 5      " 5       (       a  g[        U R                  SS 5      " 5       (       a  gg)z"Return the threading layer of BLISr   c                      gNFr   r   r#   r$   r   5BLISController._get_threading_layer.<locals>.<lambda>  s    er#   r   r   c                      gr   r   r   r#   r$   r   r     s    %r#   r   r   r   rG   s    r$   rz   #BLISController._get_threading_layer  s9    4;; <mLNNT[["@-PRRr#   c                     [        U R                  SS5      n[        U R                  SS5      nUb  Uc  g[        R                  Ul        [        R
                  Ul        U" U" 5       5      R                  S5      $ )z(Return the architecture detected by BLISr   Nr   r   )rX   r0   r   c_intr   r   r   )r7   r   r   s      r$   r|    BLISController._get_architecture  sl    #DKK1DdK!$++/@$G$(? %+LL!"(//023::7CCr#   r   N)r   r   r   r   r\   r<   r=   r   r   r6   rK   rP   r4   rz   r|   r"   r   r#   r$   r   r      s;    #HL.M57%.Dr#   r   c                      ^  \ rS rSrSrSrSrSrSr\	S 5       r
\	S 5       rU 4S	 jrS
 rS rS rS rSS jrS rS rSrU =r$ )FlexiBLASControlleri*  zController class for FlexiBLASra   	flexiblas)libflexiblas)flexiblas_get_num_threadsflexiblas_set_num_threadsflexiblas_get_versionflexiblas_listflexiblas_list_loadedflexiblas_current_backendc                      U R                  SS9$ )NTloaded)_get_backend_listrG   s    r$   loaded_backends#FlexiBLASController.loaded_backends9  s    %%T%22r#   c                 "    U R                  5       $ rx   )_get_current_backendrG   s    r$   current_backend#FlexiBLASController.current_backend=  s    ((**r#   c                 `   > [         TU ]  5       nU R                  US'   U R                  US'   U$ )r;   r   r   )superrD   r   r   )r7   exposed_attrs	__class__s     r$   rD   FlexiBLASController.infoA  s8     +/+?+?'(+/+?+?'(r#   c                 ,    U R                  SS9U l        g )NFr   )r   available_backendsrG   s    r$   r6   -FlexiBLASController.set_additional_attributesK  s    "&"8"8"8"Fr#   c                 T    [        U R                  SS 5      nU" 5       nUS:X  a  S$ U$ )Nr   c                      g rx   r   r   r#   r$   r   5FlexiBLASController.get_num_threads.<locals>.<lambda>O  s    Tr#   r   r   r   r   s      r$   rK   #FlexiBLASController.get_num_threadsN  s0    4;;(C\Rj  2%q6;6r#   c                 B    [        U R                  SS 5      nU" U5      $ )Nr   c                     g rx   r   r   s    r$   r   5FlexiBLASController.set_num_threads.<locals>.<lambda>W  s    $r#   r   r   s      r$   rP   #FlexiBLASController.set_num_threadsU  s&    KK46N
 $$r#   c                    [        U R                  SS 5      nUc  g [        R                  " 5       n[        R                  " 5       n[        R                  " 5       nU" [        R                  " U5      [        R                  " U5      [        R                  " U5      5        UR
                   SUR
                   SUR
                   3$ )Nr   .)rX   r0   r   r   byrefvalue)r7   r   majorminorpatchs        r$   r4   FlexiBLASController.get_version[  s    t{{,CTJV\\%(&,,u*=v||E?RS++a}Aekk];;r#   c                 j   SU(       a  SOS 3n[        U R                  US5      nUc  gU" SSS5      n/ n[        U5       Hn  n[        R                  " S5      nU" USU5        UR
                  R                  S5      S:w  d  MD  UR                  UR
                  R                  S5      5        Mp     U$ )	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   _loadedrU   Nr      r   __FALLBACK__)rX   r0   ranger   create_string_bufferr   r   append)r7   r   	func_nameget_backend_list_
n_backendsbackendsibackend_names           r$   r   %FlexiBLASController._get_backend_listf  s     %&Yb$AB	#DKKDA$&tQ2
z"A!66t<LlD!4!!((1^C  2 2 9 9' BC # r#   c                     [        U R                  SS5      nUc  g[        R                  " S5      nU" U[        R                  " U5      5        UR
                  R                  S5      $ )zReturn the backend of FlexiBLASr   Nr   r   )rX   r0   r   r   sizeofr   r   )r7   get_backend_backends      r$   r   (FlexiBLASController._get_current_backend}  sV    t{{,GN--d3WfmmG45}}##G,,r#   c                    XR                   ;  a  XR                  ;   a  [        U R                  SS 5      nO[        U R                  SS 5      nU" [	        U5      R                  S5      5      nUS:X  a  [        SU< SU R                   S	35      eU R                  R                  5         [        U R                  S
S 5      nU R                   R                  U5      nU" U5      nUS:X  a  [        SU< S35      eg)zSwitch 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                     gNr   r   _s    r$   r   4FlexiBLASController.switch_backend.<locals>.<lambda>  s    UWr#   flexiblas_load_backend_libraryc                     gr  r   r  s    r$   r   r    s    Rr#   r   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                     gr  r   r  s    r$   r   r    s    r#   zFailed to switch to backend r   N)
r   r   rX   r0   strencodeRuntimeErrorr*   _load_librariesindex)r7   r
  	load_funcresswitch_funcidxs         r$   switch_backend"FlexiBLASController.switch_backend  s     ...111#DKK1I<X	#KK!A<	 CL//89Cby"-g[ 9K..//VX  KK'')dkk+=|L""((1#"9!=g[JKK r#   )r   )F)r   r   r   r   r\   r<   r=   r   r   r]   r   r   rD   r6   rK   rP   r4   r   r   r   r"   __classcell__r   s   @r$   r   r   *  su    (HL)M 3 3 + +G7%	<.- L  Lr#   r   c                   F    \ rS rSrSrSrSrSrSrS r	S r
S	 rS
 rS rSrg)MKLControlleri  zController class for MKLra   mkl)	libmkl_rtmkl_rtrd   )MKL_Get_Max_ThreadsMKL_Set_Num_ThreadsMKL_Get_Version_StringMKL_Set_Threading_Layerc                 .    U R                  5       U l        g rx   )rz   r{   rG   s    r$   r6   'MKLController.set_additional_attributes  s    #88:r#   c                 @    [        U R                  SS 5      nU" 5       $ )Nr)  c                      g rx   r   r   r#   r$   r   /MKLController.get_num_threads.<locals>.<lambda>      tr#   r   r7   r   s     r$   rK   MKLController.get_num_threads      4;;(=|Lzr#   c                 B    [        U R                  SS 5      nU" U5      $ )Nr*  c                     g rx   r   r   s    r$   r   /MKLController.set_num_threads.<locals>.<lambda>      SWr#   r   r   s      r$   rP   MKLController.set_num_threads  !    4;;(=?WX$$r#   c                 N   [        U R                  S5      (       d  g [        R                  " S5      nU R                  R	                  US5        UR
                  R                  S5      n[        R                  " SU5      nUb  UR                  5       S   nUR                  5       $ )Nr+     r   zVersion ([^ ]+) r   )ru   r0   r   r   r+  r   r   researchgroupsstrip)r7   r  r5   groups       r$   r4   MKLController.get_version  s    t{{$<==))#.**34))""7+		-w7llnQ'G}}r#   c                 X    [        U R                  SS 5      nSSSSSSS	.nX!" S
5         $ )z!Return the threading layer of MKLr,  c                     gr  r   )layers    r$   r   4MKLController._get_threading_layer.<locals>.<lambda>  s    "r#   intel
sequentialpgignutbbznot specified)r   r   r         r   r   r   )r7   set_threading_layer	layer_maps      r$   rz   "MKLController._get_threading_layer  sJ    
 &KK24D
 
	 ,R011r#   )r{   N)r   r   r   r   r\   r<   r=   r   r   r6   rK   rP   r4   rz   r"   r   r#   r$   r%  r%    s5    "HL:M;%2r#   r%  c                   :    \ rS rSrSrSrSrSrSrS r	S r
S rS	rg
)OpenMPControlleri  zController class for OpenMPr   )libiomplibgomplibompvcomp)omp_get_max_threadsomp_get_num_threadsc                 @    [        U R                  SS 5      nU" 5       $ )NrX  c                      g rx   r   r   r#   r$   r   2OpenMPController.get_num_threads.<locals>.<lambda>  r2  r#   r   r3  s     r$   rK    OpenMPController.get_num_threads  r5  r#   c                 B    [        U R                  SS 5      nU" U5      $ )Nomp_set_num_threadsc                     g rx   r   r   s    r$   r   2OpenMPController.set_num_threads.<locals>.<lambda>  r9  r#   r   r   s      r$   rP    OpenMPController.set_num_threads  r;  r#   c                     g rx   r   rG   s    r$   r4   OpenMPController.get_version  s    r#   r   N)r   r   r   r   r\   r<   r=   r   r   rK   rP   r4   r"   r   r#   r$   rS  rS    s+    %HLAM
%r#   rS  c              #   8   #    U  H  oR                   v   M     g 7frx   )r<   rk   libs     r$   rm   rm     s     C2B3,,2B   c              #   J   #    U  H  oR                     H  o"v   M     M     g 7frx   )r   )rk   rg  r)   s      r$   rm   rm     s     O*39N9Nv9N*s   !#ra   c                     [         R                  U 5        [        R                  U R                  5        [        R                  U R
                  5        [        R                  U R                  5        g)zRegister a new controllerN)	_ALL_CONTROLLERSr   _ALL_USER_APISr<   _ALL_INTERNAL_APISr=   _ALL_PREFIXESextendr   )
controllers    r$   r   r     sL    J'*--.j556556r#   c                     ^ ^ U U4S jnU$ )Nc                 d   > U R                   b!  U R                   R                  " T0 TD6U l         U $ rx   )r\   format)oargskwargss    r$   	decorator$_format_docstring.<locals>.decorator  s-    99 		(($9&9AIr#   r   )ru  rv  rw  s   `` r$   _format_docstringry    s    
 r#   i'  )maxsizec                 @    [         R                  R                  U 5      $ )zCSmall caching wrapper around os.path.realpath to limit system calls)ospathrealpathr(   s    r$   	_realpathr  !  s     77H%%r#   )	USER_APISINTERNAL_APISc                  2    [        5       R                  5       $ )ad  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   rD   r   r#   r$   r
   r
   '  s       !&&((r#   c                   j    \ rS rSrSrSSS.S jrS rS r\SSS.S j5       r	S	 r
\
rS
 rS rS rSrg)_ThreadpoolLimiteri:  a  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                    Xl         U R                  X#5      u  U l        U l        U l        U R                   R                  5       U l        U R                  5         g rx   )_controller_check_params_limits	_user_api	_prefixesrD   _original_info_set_threadpool_limitsr7   rp  r  r<   s       r$   r8   _ThreadpoolLimiter.__init__E  sM    %7;7I7I8
4dndn #..335##%r#   c                     U $ rx   r   rG   s    r$   	__enter___ThreadpoolLimiter.__enter__M  s    r#   c                 $    U R                  5         g rx   )restore_original_limits)r7   typer   	tracebacks       r$   __exit___ThreadpoolLimiter.__exit__P  s    $$&r#   c                    [        XUS9$ )z@Return an instance of this class that can be used as a decorator)rp  r  r<   )_ThreadpoolLimiterDecorator)clsrp  r  r<   s       r$   wrap_ThreadpoolLimiter.wrapS  s     +!8
 	
r#   c                     [        U R                  R                  U R                  5       H  u  pUR	                  US   5        M     g)z,Set the limits back to their original valuesr>   N)zipr  lib_controllersr  rP   )r7   lib_controlleroriginal_infos      r$   r  *_ThreadpoolLimiter.restore_original_limitsZ  s@    -0,,d.A.A.
)N **=+GH.
r#   c                    0 n/ nU R                    H  nU R                   Vs/ s H  nUS   U:X  d  M  US   PM     nn[        U5      n[        U5      nUS:X  a  UR	                  5       nO%US:X  a  SnO[        U5      nUR                  U5        XqU'   M     U(       a+  [        R                  " SSR                  U5      -   S-   5        U$ s  snf )	zeOriginal num_threads from before calling threadpool_limits

Return a dict `{user_api: num_threads}`.
r<   r>   r   r   Nz1Multiple value possible for following user apis: , z. Returning the minimum.)
r  r  setlenpopminr   warningswarnjoin)r7   r>   warning_apisr<   lib_infor  n_limitslimits           r$   get_original_num_threads+_ThreadpoolLimiter.get_original_num_threadsd  s    
 H !% 3 3 3HJ'83 (' 3  
 [F6{H1}

QF##H-$)!# '& MMC))L)*,- 3s
   C	Cc                 :   [        U[        5      (       a1  US:X  a+  U R                  R                  5       R	                  5       u  nnUb  [        U[
        5      (       aF  Uc  [        nO$U[        ;   a  U/nO[        S[         SU S35      eUb  U Vs0 s H  o3U_M     nn/ nO[        U[        5      (       a  U Vs0 s H  oUS   US   _M     nnOF[        U[        5      (       a1  UR                   Vs0 s H  nUR                  UR                  _M     nn[        U[        5      (       d  [        S[        U5       S35      eU Vs/ s H  ow[         ;   d  M  UPM     nnU Vs/ s H  o3[        ;   d  M  UPM     nnXU4$ s  snf s  snf s  snf s  snf s  snf )	zCSuitable values for the _limits, _user_api and _prefixes attributessequential_blas_under_openmpzuser_api must be either in z or None. Got z	 instead.r)   r>   zUlimits must either be an int, a list, a dict, or 'sequential_blas_under_openmp'. Got z instead)
isinstancer  r  ,_get_params_for_sequential_blas_under_openmpvaluesintrl  
ValueErrorlistr   r  r)   r>   dict	TypeErrorr  rn  )r7   r  r<   apiprefixesr  r  r)   s           r$   r   _ThreadpoolLimiter._check_params  s    fc""v1O'O   MMOVVX >Z44)^+$: 1.1Aj	+ 
 !19:#v+:H&$'' QWPVHX&(??PV   F$899
 +1*@*@*@ #))>+E+EE*@  
 fd++;;?<.R  .4OV67NVHO'-Gv1FvHG))9 ; PGs*   F>F	6!F
FF'F8Fc                 Z   U R                   c  gU R                  R                   H  nUR                  U R                   ;   a  U R                   UR                     nO6UR                  U R                   ;   a  U R                   UR                     nOMm  Uc  Mr  UR                  U5        M     g)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<   rP   )r7   r  r>   s      r$   r  )_ThreadpoolLimiter._set_threadpool_limits  s     <<"..>>N $$4"ll>+@+@A((DLL8"ll>+B+BC&..{; ?r#   r  r  r  r  r  )r   r   r   r   r\   r8   r  r  classmethodr  r  
unregisterr  r  r  r"   r   r#   r$   r  r  :  sT     .2D &' (,t 
 
I )J"H1*f<r#   r  c                   .    \ rS rSrSrSSS.S jrS rSrg)r  i  z8Same as _ThreadpoolLimiter but to be used as a decoratorNr  c                X    U R                  X#5      u  U l        U l        U l        Xl        g rx   )r  r  r  r  r  r  s       r$   r8   $_ThreadpoolLimiterDecorator.__init__  s*    7;7I7I8
4dndn &r#   c                 d    U R                   R                  5       U l        U R                  5         U $ rx   )r  rD   r  r  rG   s    r$   r  %_ThreadpoolLimiterDecorator.__enter__  s,     #..335##%r#   r  )r   r   r   r   r\   r8   r  r"   r   r#   r$   r  r    s    B-1D &r#   r  r  c              #   .   #    U  H  nS U S 3v   M     g7f)"Nr   rk   r  s     r$   rm   rm     s     =ns!C5
ns   r  	BLAS_LIBSOPENMP_LIBSc                   J   ^  \ rS rSrSrSU 4S jjr\SU 4S jj5       rSrU =r	$ )r	   i  a  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.
c                 2   > [         TU ]  [        5       XS9  g Nr  )r   r8   r   )r7   r  r<   r   s      r$   r8   threadpool_limits.__init__  s    -/Rr#   c                 0   > [         TU ]  [        5       XS9$ r  )r   r  r   )r  r  r<   r   s      r$   r  threadpool_limits.wrap  s    w|026|UUr#   r   )NN)
r   r   r   r   r\   r8   r  r  r"   r"  r#  s   @r$   r	   r	     s%    ,\S V Vr#   r	   c                      \ rS rSrSr\" 5       rS r\S 5       r	S r
S rS r\" SR                  S	 \ 5       5      SR                  \5      SR                  \5      S
9SSS.S j5       r\" SR                  S \ 5       5      SR                  \5      SR                  \5      S
9SSS.S j5       rS rS rS rS rS rS rS rS rS r\S 5       r\S 5       rSrg)r   i!  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                 R    / U l         U R                  5         U R                  5         g rx   )r  r  _warn_if_incompatible_openmprG   s    r$   r8   ThreadpoolController.__init__0  s"    !))+r#   c                 4    U R                  U 5      nXl        U$ rx   )__new__r  )r  r  new_controllers      r$   _from_controllers&ThreadpoolController._from_controllers5  s    S))8&r#   c                 `    U R                    Vs/ s H  oR                  5       PM     sn$ s  snf )z.Return lib_controllers info as a list of dicts)r  rD   )r7   r  s     r$   rD   ThreadpoolController.info;  s)    <@<P<PQ<P.##%<PQQQs   +c                 0  ^ UR                  5        H!  u  p#[        U[        5      (       d  U/OUX'   M#     U R                   V^s/ s H/  m[	        U4S jUR                  5        5       5      (       d  M-  TPM1     nn[
        R                  U5      $ s  snf )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              3   H   >#    U  H  u  p[        TUS 5      U;   v   M     g 7frx   )rX   )rk   keyvalsr  s      r$   rm   .ThreadpoolController.select.<locals>.<genexpr>R  s(      !/IC T2d:!/s   ")r@   r  r  r  anyr   r  )r7   rv  r  r  r  r  s       ` r$   selectThreadpoolController.select?  s      IC(24(>(>4&DFK (
 #'"6"6
"6 !'  "6 	 
 $55oFF
s   ,B6Bc                 T    U R                  SSS9R                  (       a  SSS.$ SS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.
rb   r   )r=   r{   Nr  r   ra   )r  r  rG   s    r$   r  AThreadpoolController._get_params_for_sequential_blas_under_openmpZ  s<     ;;#X  

/ #5500r#   r  c              #   D   #    U  H  nS R                  U5      v   M     g7fz"{}"Nrs  r  s     r$   rm   ThreadpoolController.<genexpr>g       I.3FMM#...    r  Nr  c                    [        XUS9$ )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  r7   r  r<   s      r$   r  ThreadpoolController.limitf  s    f "$IIr#   c              #   D   #    U  H  nS R                  U5      v   M     g7fr  r  r  s     r$   rm   r    r  r  c                *    [         R                  XUS9$ )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  s      r$   r  ThreadpoolController.wrap  s    N "&&tX&NNr#   c                 ,    [        U R                  5      $ rx   )r  r  rG   s    r$   __len__ThreadpoolController.__len__  s    4''((r#   c                    [         R                  S:X  a  U R                  5         g[         R                  S:X  a  U R                  5         gS[         R                  ;   a  U R                  5         gU R                  5         g)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_phdrrG   s    r$   r  $ThreadpoolController._load_libraries  sV    <<8#**,\\W$<<>#++%((*557r#   c                   ^  T R                  5       n[        US5      (       d  [        R                  " S[        5        / $ U 4S jn[
        R                  " [
        R                  [
        R                  " [        5      [
        R                  [
        R                  5      nU" U5      n[
        R                  " S5      nUR                  XE5        g)a>  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                    > U R                   R                  nU(       a"  UR                  S5      nTR                  U5        g)Nr   r   )contentsr   r   _make_controller_from_path)rD   sizedatar(   r7   s       r$   match_library_callbackYThreadpoolController._find_libraries_with_dl_iterate_phdr.<locals>.match_library_callback  s6    }}..H#??73 //9r#   r#   N)	_get_libcru   r  r  RuntimeWarningr   	CFUNCTYPEr   POINTERr   c_size_tr   r	  )r7   libcr  c_func_signaturec_match_library_callbackr  s   `     r$   r  9ThreadpoolController._find_libraries_with_dl_iterate_phdr  s     ~~t.//MMK I	 "++LLNN=)OOOO	
 $44J#K s#5<r#   c                    U R                  5       n[        US5      (       d  [        R                  " S[        5        / $ UR                  5       n[        R                  UR                  l	        [        U5       HJ  n[        R                  " UR                  U5      5      nUR                  S5      nU R                  U5        ML     g)zyLoop 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.r   N)r  ru   r  r  r  r  r   r   _dyld_get_image_namer   r   	string_atr   r  )r7   r  n_dyldr  r(   s        r$   r  .ThreadpoolController._find_libraries_with_dyld  s    
 ~~t011MMM I''),2OO!!)vA''(A(A!(DEHw/H ++H5 r#   c           	      P   SSK JnJnJn  SnSnSnU R	                  S5      nU R	                  S5      nUR                  XE-  S[        R                  " 5       5      n	U	(       d!  [        S	[        R                  " 5        35      e S
n
U" 5       n X*-  " 5       n[        R                  " U5      nUR                  U	[        R                  " U5      U[        R                  " U5      U5      (       d  [        S5      eXR                  :  a  OUR                  X-  -  n
M  UR                  X-  -  n[        X,SU 5      nSU-  n[        R                  " U5      nU" 5       nU H  nUR!                  U	U[        R                  " U5      [        R                  " U5      5      (       d  [        S5      eUR                  n[#        U5      U:X  a!  [$        R&                  " SU< 3[(        5        M  U R+                  U5        M     UR-                  U	5        g! UR-                  U	5        f = f)zLoop 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      rM  Psapikernel32FzCould not open PID    zEnumProcessModulesEx failedN
   zGetModuleFileNameEx failedzCould not get the full path of a dynamic library (path too long). This library will be ignored and threadpoolctl might not be able to control or display information about all loaded libraries. Here's the truncated path: )ctypes.wintypesr!  r"  r#  _get_windllOpenProcessr|  getpidOSErrorr   r  EnumProcessModulesExr   r   mapcreate_unicode_bufferGetModuleFileNameExWr  r  r  r  r  CloseHandle)r7   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max_pathn_sizeh_moduler(   s                       r$   r  @ThreadpoolController._find_libraries_with_enum_process_module_ex  s    	=<$*! !!!'*$$Z0	))%7		
	 /		}=>>1	-IWF *-!==-22LL%LL(&  ""?@@||+"LLX-BC	  LLX%:;EG%[1I
 H}H..x8CWF%22xc):FLL<P  "">??99x=H,MMH IQ|U ' 33H=% &( !!),I!!),s   E<H H%c                     SSK Jn  UR
                  R                  5        H:  n[        R                  R                  U5      (       d  M)  U R                  U5        M<     g! [         a    [        R                  " S5         gf = f)ar  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._modulerD  ImportErrorr  r  loadedLibsByNameas_object_mapr|  r}  existsr  )r7   rD  r(   s      r$   r  ,ThreadpoolController._find_libraries_pyodide\  so    	/ --;;=H
 ww~~h''//9 >  	MM 	s   A! ! BBc                 v  ^^ [        U5      n[        R                  R                  U5      R	                  5       n[
         H  nU R                  X#R                  5      nUc  M#  US:X  aZ  UR                  S5      (       aB  [        R                  " U[        5      m[        U4S jUR                   5       5      (       d  M  OM  U" XU S9mUS U R                   5       ;   a  M  [        US5      (       a&  [        U4S jUR                   5       5      (       d  M  U R                  R!                  T5        M     g)	z:Store a library controller if it is supported and selectedNrd   .dllc              3   <   >#    U  H  n[        TU5      v   M     g 7frx   )ru   )rk   funcrd   s     r$   rm   BThreadpoolController._make_controller_from_path.<locals>.<genexpr>  s"      $BD  ..$Bs   r'   c              3   8   #    U  H  oR                   v   M     g 7frx   r  rf  s     r$   rm   rO    s     I4HSLL4Hrh  r   c              3   P   >#    U  H  n[        TR                  U5      v   M     g 7frx   )ru   r0   )rk   rN  r  s     r$   rm   rO    s)      E:D --t44:s   #&)r  r|  r}  basenamelowerrk  _check_prefixr   endswithr   r.   r/   r  r   r  ru   r   )r7   r(   filenamecontroller_classr)   r  rd   s        @@r$   r  /ThreadpoolController._make_controller_from_pathw  s'    X& 77##H-335 !1''2T2TUF ~ "$$V,,$kk(LAG $4$B$B   !	  .!N ID4H4HII+_== E,::E B B $$++N;] !1r#   c                 J    U H  nUR                  U5      (       d  M  Us  $    g)zMReturn the prefix library_basename starts with

Return None if none matches.
N)
startswith)r7   library_basenamer   r)   s       r$   rT  "ThreadpoolController._check_prefix  s)    
 (F**622 ( r#   c                     U R                    Vs/ s H  oR                  PM     nn[        R                  " S5      nSU;   a#  SU;   a  [        R
                  " U[        5        gggs  snf )z?Raise a warning if llvm-OpenMP and intel-OpenMP are both loadeda  
            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
            rV  rT  N)r  r)   textwrapdedentr  r  r  )r7   r  r  msgs       r$   r  1ThreadpoolController._warn_if_incompatible_openmp  sb    @D@T@TU@Tn))@TUoo

 xI$9MM#~. %: Vs   A&c                     U R                   R                  S5      nUc0  [        R                  " [	        S5      [
        S9nXR                   S'   U$ )z Load the lib-C for unix systems.r  cr,   )_system_librariesgetr   r.   r   r/   )r  r  s     r$   r  ThreadpoolController._get_libc  sI     $$((0< ;;|C0|DD,0!!&)r#   c                     U R                   R                  U5      nUc'  [        R                  " U S35      nX R                   U'   U$ )zLoad a windows DLLrL  )rd  re  r   WinDLL)r  dll_namedlls      r$   r*   ThreadpoolController._get_windll  sG     ##''1;--8*D 12C.1!!(+
r#   )r  ) r   r   r   r   r\   r  rd  r8   r  r  rD   r  r  ry  r  rl  _ALL_BLAS_LIBRARIES_ALL_OPENMP_LIBRARIESr  r  r  r  r  r  r  r  r  rT  r  r  r*  r"   r   r#   r$   r   r   !  s2    ,
  
RG6
1 ))I.II))/0II34
 #T .J
.J` ))I.II))/0II34
 "D "O
"OH)	8'=R6.H-T:68<t/"    r#   r   c            	         SSK n SSKnSSKnSSKnU R	                  SSS9nUR                  SSSS	S
SS9  UR                  SSSS9  UR                  UR                  SS 5      nUR                   H  n UR                  USS9  M     UR                  (       a  [        UR                  5        [        UR                  [!        5       SS95        g! [         a    [        SXcR                  S9   M  f = f)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--importr  *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.)ru  r   )packagezWARNING: could not import)filer   )indent)argparse	importlibjsonr   ArgumentParseradd_argument
parse_argsargvr  import_modulerF  printstderrcommandexecdumpsr
   )ry  rz  r{  r   parseroptionsmodules          r$   _mainr    s    $$E? % F J   O   -G//	H##FD#9 " W__	$**_&q*
12  	H-vJJG	Hs   4CC21C2__main__)?r\   r|  r>  r   r   rq   r^  typingr   r  ctypes.utilr   abcr   r   	functoolsr   
contextlibr   __version____all__environ
setdefaultrz  c_uint64c_uint32r   c_uint16r    	Structurer   RTLD_NOLOADr/   AttributeErrorDEFAULT_MODEr   r_   r   r   r%  rS  rk  r  r  rl  r=   rm  rn  r<   rl  r   rm  r   ry  r  r
   r  r  r  r	   r   r  r   )rg  s   0r$   <module>r     s   
 	 
      $ #  '( 

  ,f 5 #&++"5v6??'*{{U':FOO F$$ '>>L
X
C X
vE EP<D] <D~}L- }L@52M 52p} 6   cC2BCCD2BC2B3&&2BC O*OO !1 0LLF4JC 0  ):: 7 5& &
 T.1AST) U)$V< V<r"46F $ ii=n==ii+,		/0
4V* 4V
4VnA AH#3L z	G O&  '&&L'D Ds$   H= 5I&I=I=II