o
    oi                     @   s~   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 eZedZG d	d
 d
eZdS )    N)	lru_cache)Optional)RequirementCache)override)ClusterEnvironment)find_free_network_portmpi4pyc                   @   s$  e Zd ZdZd#ddZeedefddZeede	fdd	Z
eedefd
dZeedefddZeeddefddZededdfddZeeddefddZededdfddZeeddefddZedefddZde	fddZdefdd Zd#d!d"ZdS )$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                 C   s@   t sttt ddlm} |j| _d | _d | _d | _	d | _
d S )Nr   MPI)_MPI4PY_AVAILABLEModuleNotFoundErrorstrr   r   
COMM_WORLD_comm_world_comm_local
_node_rank_main_address
_main_port)selfr    r   ]/home/ubuntu/.local/lib/python3.10/site-packages/lightning/fabric/plugins/environments/mpi.py__init__&   s   
zMPIEnvironment.__init__c                 C   s   dS )NTr   r   r   r   r   creates_processes_externally2   s   z+MPIEnvironment.creates_processes_externallyc                 C      | j d u r
|  | _ | j S N)r   _get_main_addressr   r   r   r   main_address7      

zMPIEnvironment.main_addressc                 C   r   r   )r   _get_main_portr   r   r   r   	main_port>   r    zMPIEnvironment.main_portc                  C   s"   t sdS ddlm}  | j dkS )zbReturns ``True`` if the `mpi4py` package is installed and MPI returns a world size greater than 1.Fr   r      )r   r   r   r   Get_sizer   r   r   r   detectE   s   zMPIEnvironment.detectr#   c                 C   
   | j  S r   )r   r$   r   r   r   r   
world_sizeP      
zMPIEnvironment.world_sizesizec                 C      t d d S )NzYMPIEnvironment.set_world_size was called, but setting world size is not allowed. Ignored.logdebug)r   r)   r   r   r   set_world_sizeU      zMPIEnvironment.set_world_sizec                 C   r&   r   )r   Get_rankr   r   r   r   global_rankY   r(   zMPIEnvironment.global_rankrankc                 C   r*   )Nz[MPIEnvironment.set_global_rank was called, but setting global rank is not allowed. Ignored.r+   )r   r2   r   r   r   set_global_rank^   r/   zMPIEnvironment.set_global_rankc                 C   s*   | j d u r	|   | j d usJ | j  S r   )r   _init_comm_localr0   r   r   r   r   
local_rankb   s   

zMPIEnvironment.local_rankc                 C   s&   | j d u r	|   | j d usJ | j S r   )r   r4   r   r   r   r   	node_rankj   s   
zMPIEnvironment.node_rankc                 C   s   | j jt ddS Nr   root)r   bcastsocketgethostnamer   r   r   r   r   q   s   z MPIEnvironment._get_main_addressc                 C   s   | j jt ddS r7   )r   r:   r   r   r   r   r   r!   t   s   zMPIEnvironment._get_main_portc                 C   sb   t  }| jj|dd}|d urtt|ng }| jj|dd}||| _| jj	| jd| _
d S )Nr   r8   )color)r;   r<   r   gathersortedsetr:   indexr   Splitr   )r   hostnameall_hostnamesunique_hostsr   r   r   r4   w   s   zMPIEnvironment._init_comm_local)r
   N)__name__
__module____qualname____doc__r   propertyr   boolr   r   r   intr"   staticmethodr%   r   r'   r.   r1   r3   r5   r6   r   r!   r4   r   r   r   r   r	      sB    
	r	   )loggingr;   	functoolsr   typingr    lightning_utilities.core.importsr   typing_extensionsr   9lightning.fabric.plugins.environments.cluster_environmentr   /lightning.fabric.plugins.environments.lightningr   	getLoggerrF   r,   r   r	   r   r   r   r   <module>   s   
