
      i                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ  e j                  e      Z ed      Z G d	 d
e      Zy)    N)	lru_cache)Optional)RequirementCache)override)ClusterEnvironment)find_free_network_portmpi4pyc                      e Zd ZdZddZeedefd              Zeede	fd              Z
eedefd              Zeedefd              Ze ed	      defd
              Zededdfd       Ze ed	      defd              Zededdfd       Ze ed	      defd              Zedefd       Zde	fdZdefdZddZy)MPIEnvironmentzAn environment for running on clusters with processes created through MPI.

    Requires the installation of the `mpi4py` package. See also: https://github.com/mpi4py/mpi4py

    returnNc                     t         st        t        t                     ddlm} |j
                  | _        d | _        d | _        d | _	        d | _
        y )Nr   MPI)_MPI4PY_AVAILABLEModuleNotFoundErrorstrr	   r   
COMM_WORLD_comm_world_comm_local
_node_rank_main_address
_main_port)selfr   s     ~/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning/fabric/plugins/environments/mpi.py__init__zMPIEnvironment.__init__&   sC     %c*;&<==>>/3)-,0)-    c                      y)NT r   s    r   creates_processes_externallyz+MPIEnvironment.creates_processes_externally2   s     r   c                 \    | j                   | j                         | _         | j                   S N)r   _get_main_addressr   s    r   main_addresszMPIEnvironment.main_address7   s-     %!%!7!7!9D!!!r   c                 \    | j                   | j                         | _         | j                   S r"   )r   _get_main_portr   s    r   	main_portzMPIEnvironment.main_port>   s(     ??""113DOr   c                  v    t         sy	 ddlm}  | j                  j                         dkD  S # t        $ r Y yw xY w)zbReturns ``True`` if the `mpi4py` package is installed and MPI returns a world size greater than 1.Fr   r      )r   r	   r   ImportErrorr   Get_sizer   s    r   detectzMPIEnvironment.detectE   s@     !	" ~~&&(1,,  		s   , 	88r)   c                 6    | j                   j                         S r"   )r   r+   r   s    r   
world_sizezMPIEnvironment.world_sizeT        ((**r   sizec                 .    t         j                  d       y )NzYMPIEnvironment.set_world_size was called, but setting world size is not allowed. Ignored.logdebug)r   r0   s     r   set_world_sizezMPIEnvironment.set_world_sizeY   s    		mnr   c                 6    | j                   j                         S r"   )r   Get_rankr   s    r   global_rankzMPIEnvironment.global_rank]   r/   r   rankc                 .    t         j                  d       y )Nz[MPIEnvironment.set_global_rank was called, but setting global rank is not allowed. Ignored.r2   )r   r9   s     r   set_global_rankzMPIEnvironment.set_global_rankb   s    		opr   c                     | j                   | j                          | j                   J | j                   j                         S r"   )r   _init_comm_localr7   r   s    r   
local_rankzMPIEnvironment.local_rankf   sC     #!!#+++((**r   c                 n    | j                   | j                          | j                   J | j                   S r"   )r   r=   r   s    r   	node_rankzMPIEnvironment.node_rankn   s2    ??"!!#***r   c                 `    | j                   j                  t        j                         d      S Nr   root)r   bcastsocketgethostnamer   s    r   r#   z MPIEnvironment._get_main_addressu   s'    %%f&8&8&:%CCr   c                 L    | j                   j                  t               d      S rB   )r   rE   r   r   s    r   r&   zMPIEnvironment._get_main_portx   s"    %%&<&>Q%GGr   c                 R   t        j                         }| j                  j                  |d      }|t	        t        |            ng }| j                  j                  |d      }|j                  |      | _        | j                  j                  | j                        | _
        y )Nr   rC   )color)rF   rG   r   gathersortedsetrE   indexr   Splitr   )r   hostnameall_hostnamesunique_hostss       r   r=   zMPIEnvironment._init_comm_local{   s    %%'((//q/A5B5Nvc-01TV''--l-C&,,X6++111Hr   )r   N)__name__
__module____qualname____doc__r   propertyr   boolr    r   r$   intr'   staticmethodr,   r   r.   r5   r8   r;   r>   r@   r#   r&   r=   r   r   r   r   r      s   
. d    "c "  "
 3   
 -D -  - q\+C +  + o3 o4 o o q\+S +  + qC qD q q q\+C +  + 3  D3 DH HIr   r   )loggingrF   	functoolsr   typingr    lightning_utilities.core.importsr   typing_extensionsr   9lightning.fabric.plugins.environments.cluster_environmentr   /lightning.fabric.plugins.environments.lightningr   	getLoggerrS   r3   r   r   r   r   r   <module>rc      sM        = & X Rg!$X. dI' dIr   