a
    h#                  	   @   s   d dl Z 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 g dZeeZdZdZd	Zdeeeeeeee dddZdd Zdd ZejdddZdS )    N)closing)Optional)
get_logger)barrier)create_c10d_storeget_free_portget_socket_with_portzAddress already in usezSocket Timeoutz_tcp_store/num_members   X  T   )	is_serverserver_addrserver_port
world_sizetimeoutwait_for_workers	use_libuvc              
   C   sT  |d urt d tjdddk}|dkrH|dkrHtd| d| |dkr\t d| |dkrh|nd}|dkrz|}	nt }	t d	||	|| || zBtj	||	|| t
j|d
||d}
|rt|
||d t d |
W S  tyL } zZt|tkr6||k rt d|	|| |d7 }ntd| d|	 d|n W Y d }~qld }~0 0 qld S )Nzargument use_libuv is deprecated and ignored. Set USE_LIBUV environment variable to "0" to disable libuv, or "1" to enable it. If the env var is not set, libuv will be used by default.Z	USE_LIBUV1r	   r
   zCserver_port must be specified when world_size > 1, got server_port=z, world_size=z+sever_port: %s, specified, ignoring retriesziCreating c10d store on %s:%s
  world_size  : %s
  is_server   : %s
  timeout(sec): %s
  use_libuv   : %s
)seconds)Z	host_nameportr   Z	is_masterr   r   r   )r   zSuccessfully created c10d storez)port: %s already in use, attempt: [%s/%s]zon z, port: z already in use)loggerwarningosenvironget
ValueErrorinfor   distZTCPStoredatetime	timedelta_check_full_rankRuntimeErrorstr_ADDRESS_IN_USE)r   r   r   r   r   r   retriesr   attemptr   storee r)   Y/var/www/auris/lib/python3.9/site-packages/torch/distributed/elastic/utils/distributed.pyr      sl    





r   c              
   C   s`   zt | |t|d W nF tyZ } z.t|tkrDtd| d|n W Y d }~n
d }~0 0 d S )N)Z
key_prefixZbarrier_timeoutztimed out waiting for all z members to join)r   _TCP_STORE_INITr"   r#   _SOCKET_TIMEOUTTimeoutError)r'   r   r   r(   r)   r)   r*   r!   v   s    
r!   c                  C   s>   t  } t|  |  d W  d   S 1 s00    Y  dS )a  
    Returns an unused port on localhost.

    This function finds an unused port on localhost by opening to socket to bind
    to a port and then closing it.

    Returns:
        int: an unused port on localhost

    Example:
        >>> # xdoctest: +SKIP("Nondeterministic")
        >>> get_free_port()
        63976

    .. note::
        The port returned by :func:`get_free_port` is not reserved and may be
        taken by another process after this function returns.
    r
   N)r   r   getsockname)sockr)   r)   r*   r      s    
r   )returnc                  C   s   t jddt jt jd} | D ]z}|\}}}}}t  |||}z|d |d |W   S  ty } z"|  tj	d|d W Y d}~qd}~0 0 qt
ddS )	a  
    Returns a free port on localhost that is "reserved" by binding a temporary
    socket on it. Close the socket before passing the port to the entity
    that requires it. Usage example

    ::

    sock = _get_socket_with_port()
    with closing(sock):
        port = sock.getsockname()[1]
        sock.close()
        # there is still a race-condition that some other process
        # may grab this port before func() runs
        func(port)
    	localhostN)hostr   familytype)r1   r   r   zSocket creation attempt failed.)exc_infozFailed to create a socket)socketgetaddrinfo	AF_UNSPECSOCK_STREAMbindlistenOSErrorcloser   r   r"   )addrsaddrr3   r4   proto_sr(   r)   r)   r*   r      s    


&r   )r	   r
   r   Tr   N)r   r   r6   
contextlibr   typingr   Ztorch.distributedZdistributedr   Z'torch.distributed.elastic.utils.loggingr   Z%torch.distributed.elastic.utils.storer   __all____name__r   r$   r,   r+   boolr#   intfloatr   r!   r   r   r)   r)   r)   r*   <module>	   s<         X