
    0FieW                    N    d dl mZ d dlZd dlmZ d dlmZ  G d de          ZdS )    )annotationsN)SchedulerPlugin)TupleComparablec                  4    e Zd ZdZd ZdddddZd Zd ZdS )GraphLayoutaK  Dynamic graph layout during computation

    This assigns (x, y) locations to all tasks quickly and dynamically as new
    tasks are added.  This scales to a few thousand nodes.

    It is commonly used with distributed/dashboard/components/scheduler.py::TaskGraph, which
    is rendered at /graph on the diagnostic dashboard.
    c                   dt          j                     | _        i | _        i | _        i | _        || _        i | _        i | _        d| _	        d| _
        d| _        g | _        g | _        g | _        g | _        g | _        | j        j        rpd |j                                        D             }d |j                                        D             }|                     | j        | j        j        ||           d S d S )Nzgraph-layout-r   c                8    i | ]\  }}|d  |j         D             S )c                    g | ]	}|j         
S  )key).0dss     Dlib/python3.11/site-packages/distributed/diagnostics/graph_layout.py
<listcomp>z3GraphLayout.__init__.<locals>.<dictcomp>.<listcomp>&   s    555rBF555    )dependenciesr   ktss      r   
<dictcomp>z(GraphLayout.__init__.<locals>.<dictcomp>%   s?       Ar 55R_555  r   c                $    i | ]\  }}||j         S r   )priorityr   s      r   r   z(GraphLayout.__init__.<locals>.<dictcomp>)   s     LLL51b2;LLLr   )tasksr   r   )uuiduuid4namexy	collision	schedulerindex
index_edgenext_y
next_indexnext_edge_indexnew	new_edgesstate_updatesvisible_updatesvisible_edge_updatesr   itemsupdate_graph)selfr    r   r   s       r   __init__zGraphLayout.__init__   s   2DJLL22	"
 !$&!> 	 &_2244  L MLIO4I4I4K4KLLLHn*)!	      	 	r   N)r   r   r   c                   t          |fdd          }|r|                                }| j        v s	|j        vr*|                    |d          }|rt           fd|D                       s>|                    |           |                    t          |fdd                     t          fd|D                       t           fd|D                       }	t           fd	|D                       d
z   }
nd}
 j
        }	 xj
        d
z  c_
        |
|	f j        v r*|
|	}} j        |
|	f         \  }
}	|	dz  }	|
|	f j        ||f<   n|
|	f j        |
|	f<   |
 j        |<   |	 j        |<    j         j        |<    j        d
z    _         j                            |           |D ]?}||f} j         j        |<    xj        d
z  c_         j                            |           @|d S d S )Nc                J    t                              | d                    S Nr   r   getr   r   s    r   <lambda>z*GraphLayout.update_graph.<locals>.<lambda>5   s    a1C1C!D!D r   T)r   reverser   c              3  *   K   | ]}|j         v V  d S N)r   r   depr-   s     r   	<genexpr>z+GraphLayout.update_graph.<locals>.<genexpr>=   s)      99S3$&=999999r   c                J    t                              | d                    S r1   r2   r4   s    r   r5   z*GraphLayout.update_graph.<locals>.<lambda>B   s    /(,,q!:L:L*M*M r   c              3  V   K   | ]#}t          j        |         j                  V  $d S r8   )lenr   
dependents)r   r:   r    s     r   r;   z+GraphLayout.update_graph.<locals>.<genexpr>H   sF       % %ADIOC0;<<% % % % % %r   c              3  x   K   | ]4}j         |         t          j        |         j                  z  z  V  5d S r8   )r   r>   r   r?   )r   r:   r    r-   
total_depss     r   r;   z+GraphLayout.update_graph.<locals>.<genexpr>K   sY         sc)/#*>*I&J&JJZW     r   c              3  2   K   | ]}j         |         V  d S r8   )r   r9   s     r   r;   z+GraphLayout.update_graph.<locals>.<genexpr>O   s)      88CDF3K888888r      r   g?)sortedpopr   r   r3   allappendextendsummaxr#   r   r   r$   r!   r&   r%   r"   r'   )r-   r    r   r   r   kwargsstackr   depsr   r   old_xold_yr:   edgerA   s   `` `           @r   r,   zGraphLayout.update_graph1   s    DDDDd
 
 
  /	,))++Cdf}}9? : :##C,,D !9999D99999 =LL%%%LL  M M M M$(     !$ % % % %HL% % % " "J       #'    A 88884888881<AAKq 1v'' !u~q!f-1S011vue|,,*+Q1v&DF3KDF3K"oDJsO"o1DOHOOC    , ,Sz(,(<%$$)$$%%d++++_  /	, /	, /	, /	, /	,r   c                   |dk    r)| j                             | j        |         |f           d S | j                            | j        |         df           | j        j        |         }|j        D ]?}||j        f}| j                            | j	        
                    |          df           @|j        D ]=}| j                            | j	        
                    |j        |f          df           >	 | j        | j        |         | j        |         f= n# t          $ r Y nw xY w| j        | j        | j        fD ]}	|	|= d S )N	forgottenFalse)r(   rG   r!   r)   r    r   r?   r   r*   r"   rE   r   r   r   r   KeyError)
r-   r   startfinishargsrK   taskr:   rP   
collections
             r   
transitionzGraphLayout.transitionh   sw   [  %%tz#&?@@@@@ ''C'(BCCC>',D W WSW~)00$/2E2Ed2K2KW1UVVVV(  )00_((#'388'B   NDF3K#=>>     $vtvtz: $ $
sOO$ $s   6 D 
D$#D$c                   g | _         g | _        g | _        g | _        g | _        i | _        d| _        i | _        d| _        | j	        D ]}| j        | j        |<   | xj        dz  c_        | j         
                    |           | j        j        |         j        D ]D}|j        |f}| j        | j        |<   | xj        dz  c_        | j        
                    |           EdS )zReset the index and refill new and new_edges

        From time to time TaskGraph wants to remove invisible nodes and reset
        all of its indices.  This helps.
        r   rC   N)r&   r'   r)   r(   r*   r!   r$   r"   r%   r   rG   r    r   r   r   )r-   r   r:   rP   s       r   reset_indexzGraphLayout.reset_index~   s     !$&!
 6 	, 	,C"oDJsOOOq OOHOOC   ~+C0= , ,~(,(<%$$)$$%%d++++	,		, 	,r   )__name__
__module____qualname____doc__r.   r,   rZ   r\   r   r   r   r   r   	   sn           > *.D5, 5, 5, 5, 5,n$ $ $,, , , , ,r   r   )
__future__r   r   distributed.diagnostics.pluginr   distributed.utilsr   r   r   r   r   <module>rd      s    " " " " " "  : : : : : : - - - - - -N, N, N, N, N,/ N, N, N, N, N,r   