U
    Jc	                     @   sd   d dl mZ d dlmZ eeedddZdeeeeeee ddd	Z	deeeed
dddZ
d
S )    )	timedelta)List)rankprefixsizec                 C   sr   g }t |D ] }| | | }|| q| | | dd |dkrnt |D ]}| | | d qT|S )aa  
    Given a store and a prefix, the method goes through the array of keys
    of the following format: ``{prefix}{idx}``, where idx is in a range
    from 0 to size, and tries to retrieve the data.

    The Rank0 process waits at the end to make sure all other processes
    finished the procedure before exiting.

    Usage

    ::

     values = get_all(store, 'torchelastic/data', 3)
     value1 = values[0] # retrieves the data for key torchelastic/data0
     value2 = values[1] # retrieves the data for key torchelastic/data1
     value3 = values[2] # retrieves the data for key torchelastic/data2

    z.FINs   FINr   )rangegetappendset)storer   r   r   Zdata_arridxdataZ	node_rank r   I/tmp/pip-unpacked-wheel-gikjz4vx/torch/distributed/elastic/utils/store.pyget_all   s    r   ,  )r   r   
world_size
key_prefixbarrier_timeoutreturnc                 C   s6   |  t|d | | | | t| |||}|S )a  
    Synchronizes ``world_size`` agents between each other using the underlying c10d store.
    The ``data`` will be available on each of the agents.

    Note: The data on the path is not deleted, as a result there can be stale data if
        you use the same key_prefix twice.
    )seconds)Zset_timeoutr   r
   r   )r   r   r   r   r   r   Z
agent_datar   r   r   synchronize/   s    r   N)r   r   r   r   r   c                 C   s$   | j dd}t| ||||| dS )z
    A global lock between agents.

    Note: Since the data is not removed from the store, the barrier can be used
        once per unique ``key_prefix``.
    zUTF-8)encodingN)encoder   )r   r   r   r   r   r   r   r   r   barrierD   s    	r   )r   )r   )datetimer   typingr   intstrr   bytesfloatr   r   r   r   r   r   <module>	   s&   (     