o
    װi                     @   s*  d dl Z d dlmZ d dlmZmZmZmZ d dlZd dl	m
Z
 d dlm  mZ d dlm  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 d d
lmZ d dlmZ d dlmZ G dd dZG dd dZ ddde!e"df de#e"e$e  f de#e"ef de#e"ef ddfddZ%dS )    N)chain)AnyIteratorOptionalUnion)ndarray)Device)Event)Stream)get_current_stream)_modes)_index_arith)_data_transfer)_Communicatorc                   @   sv   e Zd ZU eedf ed< eed< deedf deddfddZdeedf dd fd	d
Zddde	j
defddZdS )_ArrayPlaceholder.shapedevicereturnNc                 C   s   || _ || _d S N)r   r   )selfr   r    r   R/home/ubuntu/.local/lib/python3.10/site-packages/cupyx/distributed/array/_chunk.py__init__   s   
z_ArrayPlaceholder.__init__	new_shapec                 C   s   t || jS r   )r   r   )r   r   r   r   r   reshape    s   z_ArrayPlaceholder.reshapemode_modes.Modedtypec                 C   sh   | j ' |tju rt| j|}n||}t| j||}t	|W  d    S 1 s-w   Y  d S r   )
r   r   REPLICA_creation_basicemptyr   identity_offull_manipulation_dims
atleast_1d)r   r   r   datavaluer   r   r   
to_ndarray#   s   

$z_ArrayPlaceholder.to_ndarray)__name__
__module____qualname__tupleint__annotations__r   r   r   numpyr   r   r'   r   r   r   r   r      s   
 r   c                   @   s  e Zd ZU eeef ed< eed< ee	df ed< e
ej ed< dZeed< 		d%deeef dedee	df dee
ej  ded	dfd
dZe	d&deedf deeef dee	df dee
ej  d	d f
ddZejd	ee fddZdejdee	df d	dfddZd'ddZd(ddZdd dddeedf deeejf deeef d	dfdd Z dee	df deedf d	dfd!d"Z!d)d#d$Z"dS )*_Chunkarrayready.indexupdatesN
prevent_gcr%   r   c                 C   s.   || _ || _|| _|d ur|ng | _|| _d S r   )r0   r1   r2   r3   r4   )r   r%   r1   r2   r3   r4   r   r   r   r   9   s
   
z_Chunk.__init__r   r   c                 C   s`   t |tr	t|}t||}| t }W d    n1 sw   Y  |d u r)g }t||||S r   )
isinstancer,   r   r   r	   r/   )clsr   r   r2   r3   r%   r1   r   r   r   create_placeholderE   s   

z_Chunk.create_placeholderc                 c   sH    | j j t }|| j |V  W d    d S 1 sw   Y  d S r   )r0   r   r   
wait_eventr1   )r   streamr   r   r   on_readyV   s   
"z_Chunk.on_readyupdateidxc                 C   s   | j ||f d S r   )r3   append)r   r;   r<   r   r   r   
add_update]   s   z_Chunk.add_updatec                 C   sp   t | jtr| j}| j}n|  }| j }| }W d    n1 s%w   Y  t||| jt	| j
| jdS )N)r4   )r5   r0   r   r1   r:   copyrecordr/   r2   listr3   r4   )r   r%   r1   r9   r   r   r   r?   b   s   


z_Chunk.copyr   r   c                 C   s   t | jdkr	dS t| jtr | jd d jj}| j||| _|  @}| jD ]#\}}||j	 |t
ju r>|j| j|< q(|| j| |j| j|< q(|| j	 | j| jf| _g | _W d   dS 1 sgw   Y  dS )zApply all updates in-place.r   N)lenr3   r5   r0   r   r   r'   r:   r8   r1   r   r   funcr@   r4   )r   r   r   r9   update_datar<   r   r   r   flusho   s"   

"z_Chunk.flushtargetcommsstreamsc                 C   sJ  | }|}t |jdksJ t|jtsJ |jjj}|jjj}	|j}
|j}t	|
||}|d u r2d S t
|
||}t
|||}t|j| |j|j}|tjurX|jsX| }t|| || |||	 ||	 |	}||| |tjur|js|jj}| }|||j|< ||j W d    d S 1 sw   Y  d S d S d S )Nr   )rB   r3   r5   r0   r   r   idr2   r   _index_intersection_index_for_subindexr   
_AsyncDatar1   r4   r   r   
idempotentr?   	_transferr>   r   r:   r!   r@   )r   rF   r   r   rG   rH   	src_chunk	dst_chunksrc_devdst_devsrc_idxdst_idxintersectionsrc_new_idxdst_new_idxdata_to_transferr;   r   r9   r   r   r   apply_to   sJ   


"z_Chunk.apply_toc                 C   s   t | jtsJ t| j||}|d u rd S t| j||}|  }|| j|< || j	 W d    d S 1 s9w   Y  d S r   )
r5   r0   r   r   rJ   r2   rK   r:   r@   r1   )r   r<   r   identityrU   self_new_idxr9   r   r   r   set_identity_on_intersection   s   

"z#_Chunk.set_identity_on_intersectionc                 C   sd   t | jtrd S |  }| jD ]	\}}|| j|< q|| j W d    d S 1 s+w   Y  d S r   )r5   r0   r   r:   r3   r@   r1   )r   rZ   r9   _r<   r   r   r   #set_identity_on_overwritten_entries   s   
"z*_Chunk.set_identity_on_overwritten_entries)NNr   )r   r/   )r   r   r   N)r   N)#r(   r)   r*   r   r   r   r-   r	   r+   slicerA   r   _PartialUpdater4   r   r   r   classmethodr,   r   r7   
contextlibcontextmanagerr   r
   r:   rL   r>   r?   rE   dictr   rY   r\   r^   r   r   r   r   r/   0   s   
 












0


r/   op_modez_modes._OpModer   .	chunk_maprG   rH   r   c           
   	   C   s   t t| }tt|D ]$}|| }||  t|d t|D ]}|| }	||	| ||| q#qtt|d ddD ]!}|| }|tj	 t|D ]}|| }	||	tj	||| qNq>d S )N   )
rA   r   from_iterablevaluesrangerB   rE   rY   r   r   )
re   r   rf   rG   rH   chunks_listirO   jrP   r   r   r   _all_reduce_intersections   s$   
ro   )&rb   	itertoolsr   typingr   r   r   r   r.   cupy._core.corer   cupy._creation.basic	_creationbasicr   cupy._manipulation.dims_manipulationdimsr#   cupy.cuda.devicer   cupy.cuda.streamr	   r
   r   cupyx.distributed.arrayr   r   r   &cupyx.distributed.array._data_transferr   r   r/   r+   r,   rd   rA   ro   r   r   r   r   <module>   s>     


