o
    i*                     @   s  d dl Z d dl mZ d dlmZ d dl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 d dlmZ ded deedf dededed f
ddZdeded deedf dededeee
 eee
f f fddZded deedf ddfddZded deedf ddfddZded deedf ddfddZded deedf de fdd Z!dedefd!d"Z"dS )#    N)Sequence)chain)ndarray)Device)Stream)get_current_stream)_array)_chunk)_data_transfer)_index_arith)_modesargsz_array.DistributedArraykwargsdevchunk_ireturnz_data_transfer._PartialUpdatec           	      C   s   g }d}t | | D ]}|j| | j}|r|rd} n|}q|r$|S t | | D ]}t |j D ]}||j q5q+g S NTF)r   values_chunks_mapupdatesfrom_iterableflush_mode)	r   r   r   r   r   at_most_one_updateargupdates_nowchunk r   a/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/cupyx/distributed/array/_elementwise.py_find_updates   s"   
r   streamc                    s@   fdd  fdd|D } fdd|  D }||fS )Nc                    s    | j    }|j |jS N)r   
wait_eventreadyarray)d_arrayr   )r   r   r    r   r   access_array7   s   z+_prepare_chunks_array.<locals>.access_arrayc                    s   g | ]} |qS r   r   .0r   r&   r   r   
<listcomp><   s    z)_prepare_chunks_array.<locals>.<listcomp>c                    s   i | ]	\}}| |qS r   r   )r(   keyr   r)   r   r   
<dictcomp>=   s    z)_prepare_chunks_array.<locals>.<dictcomp>)items)r    r   r   r   r   
arg_arrayskwarg_arraysr   )r&   r   r   r    r   _prepare_chunks_array1   s   r0   c                 C   s2   dd | D | d d < | dd | D  d S )Nc                 S   s   g | ]}| tjqS r   _to_op_moder   REPLICAr'   r   r   r   r*   E   s    z/_change_all_to_replica_mode.<locals>.<listcomp>c                 s   s$    | ]\}}|| tjfV  qd S r!   r1   )r(   kr   r   r   r   	<genexpr>F   s    
z._change_all_to_replica_mode.<locals>.<genexpr>)updater-   )r   r   r   r   r   _change_all_to_replica_modeB   s   
r7   c              	   C   s  t |}t|| d }i }|p| D ]}|j} | D ]\}}g ||< t| t }	t|D ]\}
}t||||
}t	|	||||
\}}d }t
|| D ]}t|tjrg|d u s]J tj|j|j|}qO|d u r| |i |}|j}tj||	 |||fd}|| | |sq1d gt| }i }|D ]^\}}t|D ]\}}t|tjr|j||< q|| ||< q| D ]\}}t|tjr|j||< q|| ||< q|	|j | |i |}|j}|	 }tj||||fd}||| qq1W d    n	1 sw   Y  qt
| D ]}t|jttjfs tdqd  }}|p,| D ]}|j}|j} |d us=J t !|||t"j#|S )N
prevent_gc;Kernels returning other than single array are not supported)$listr7   r   	index_mapr-   r   r   	enumerater   r0   r   
isinstancer	   _ArrayPlaceholder_Chunkcreate_placeholdershapedevicedtyperecordappendlenr$   r"   r#   r
   
_AsyncData
add_updater   r   RuntimeError_commsr   DistributedArrayr   r3   )kernelr   r   	out_dtypeout_chunks_mapr   r<   r   idxsr    r   idxr   r.   r/   	out_chunkdata	out_array
arg_sliceskwarg_slicesr6   ir4   out_update_arrayr#   
out_updater   rB   commsr   r   r   _execute_kernelK   s   




<r[   c              
   C   s  t |dksJ t |dkrtd|rtdt|}t|D ]\}}|tj||< t|| j	
 D ]}|tj q6q |\}}t| tjjjro| j|j|jf}|du rktd| j d|jj|jjf |j}	nt| tjjjsyJ | |jj|jjfd\}
}	}
t |	dkrt|	 td	|	d
 }|j}|j}i }t|j	
 D ]}|jjj}| }t !|jj|}t|j	
 D ]i}t"#|j$|j$|}|du rq|jjj}tj%j&'||dkrt()|||j$|j|j}n	tjj*|j| |+|j, t"-|j$||}t"-|j$||}| j.dksJ | t/0t1|j| t/0t1|j| ||  qt2j3||4 |j$|j	d}|5|g 6| W d   n	1 sQw   Y  qt(7|||tj|S )z&Arguments must be in the replica mode.   zsElement-wise operation over more than two distributed arrays is not supported unless they share the same index_map.zLKeyword argument is not supported unless arguments share the same index_map.Nz#Could not guess the return type of z with arguments of type r      r:   r   r8   )8rG   rJ   r;   r=   r2   r   r3   r   r   r   r   r   r>   cupy_core_kernelufunc_ops_guess_routine_from_in_typesrD   nametype	out_typesElementwiseKernel_decide_params_typeprintrB   rK   r$   rC   idon_ready_creation_basicemptyr   _index_intersectionindexcudaruntimedeviceCanAccessPeerr   _make_chunk_async_check_peer_accessr"   r#   _index_for_subindexnintypingcastr   r	   r@   rE   
setdefaultrF   rL   )rM   r   r   rW   r   r   aboprf   _rD   rB   rZ   rO   a_chunka_devr    rT   b_chunkintersectionb_dev	a_new_idx	b_new_idxrR   r   r   r   _execute_peer_access   s   





#r   c                 C   s<   d }t | | D ]}|d u r|j}q	|j|kr dS q	dS r   )r   r   r<   )r   r   r<   r   r   r   r   _is_peer_access_needed  s   
r   c                 C   sN   t || D ]}t|tjstdqt||}|r!t| ||S t| ||S )NzFMixing a distributed array with a non-distributed one is not supported)	r   r   r>   r   rL   rJ   r   r   r[   )rM   r   r   r   needs_peer_accessr   r   r   _execute  s   
r   )#rw   r   	itertoolsr   r^   cupy._creation.basic	_creationbasicrl   cupy._core.corer   cupy.cuda.devicer   cupy.cuda.streamr   r   cupyx.distributed.arrayr   r	   r
   r   r   dictstrintr;   r   tupler0   r7   r[   r   boolr   r   r   r   r   r   <module>   s    





	

a

X

