o
    is                     @   s  d dl mZ d dl mZ d dlmZ dd ZG dd dejZdefd	d
Z	deddfddZ
dededdfddZdededdfddZdededdfddZede Zede Zede ZedgdddZedgdd Zed gd!d" Zed#gd$d% Zed&gdd'd(Zedgdd)d*Zed+g	dd,d-Zed+g	dd.d/Zed0gd1d2 Zed3gd4d5 Zed6gd7d8 Zed+g	dd9d:Zed+g	dd;d<Zed=g	dd>d?Zed@gddAdBZed@gddCdDZ edgdEdF Z!G dGdH dHej"Z#edIgddJdKZ$G dLdM dMej"Z%edNgddOdPZ&edQgddRdSZ'edTgddUdVZ(edTg	ddWdXZ)edYgdZd[ Z*edYgd\d] Z+ed^gd_d` Z,ed^gdadb Z-edcgddde Z.edcgdfdg Z/edhgdidj Z0edkgddldmZ1edng	ddodpZ2edng	ddqdrZ3edng	ddsdtZ4edng	ddudvZ5edng	ddwdxZ6edng	ddydzZ7edng	dd{d|Z8edng	dd}d~Z9ed@gdddZ:ed@g	dddZ;ed@gdddZ<ed@g	dddZ=edgdd Z>edgdddZ?edg	dddZ@edgdd ZAdS )    )_internal_types)_cuda_types)Datac                    s    fdd}|S )Nc                    s   G  fdddt j}| S )Nc                       s   e Zd Z fddZdS )z7_wrap_thrust_func.<locals>.wrapper.<locals>.FuncWrapperc                    sz   D ]} j d| d q j d  j d d j _ fdd|D } fdd	D } g|R i |S )
Nz
#include <>z$#include <thrust/execution_policy.h>z#include <thrust/functional.h>nvccc                    s   g | ]}t | qS  _Datainit.0a)envr   M/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/cupyx/jit/thrust.py
<listcomp>   s    zP_wrap_thrust_func.<locals>.wrapper.<locals>.FuncWrapper.call.<locals>.<listcomp>c                    s   i | ]}|t |  qS r   r   )r   kr   kwargsr   r   
<dictcomp>   s    zP_wrap_thrust_func.<locals>.wrapper.<locals>.FuncWrapper.call.<locals>.<dictcomp>)	generatedadd_codebackend)selfr   argsr   header	data_argsdata_kwargsfuncheadersr   r   call	   s   z<_wrap_thrust_func.<locals>.wrapper.<locals>.FuncWrapper.callN)__name__
__module____qualname__r    r   r   r   r   FuncWrapper   s    r$   )r   BuiltinFunc)r   r$   r   )r   r   wrapper   s   
z"_wrap_thrust_func.<locals>.wrapperr   )r   r'   r   r&   r   _wrap_thrust_func   s   r(   c                   @   s   e Zd ZdS )_ExecPolicyTypeN)r!   r"   r#   r   r   r   r   r)      s    r)   exec_policyc                 C   s    t | jtst| j dd S )Nz must be execution policy type)
isinstancectyper)   	TypeErrorcode)r*   r   r   r   _assert_exec_policy_type   s   r/   r   returnNc                 C   s,   t | jtjstd| j d| j dd S )N`z` must be of pointer type: `)r+   r,   r   PointerBaser-   r.   )r   r   r   r   _assert_pointer_type   s   r3   bc              
   C   s:   | j |j krtd| j d|j d| j  d|j  d	d S )Nr1   ` and `` must be of the same type: `` != `)r,   r-   r.   r   r4   r   r   r   _assert_same_type%   s   r9   c              
   C   sR   t |  t | | jj|jjkr'td| j d|j d| jj d|jj d	d S )Nr1   r5   z%` must be of the same pointer type: `r7   )r3   r,   
child_typer-   r.   typer8   r   r   r   _assert_same_pointer_type,   s   r<   c              
   C   sF   t |  | jj|jkr!td| j d|j d| jj d|j d	d S )Nz`*r5   r6   r7   r1   )r3   r,   r:   r-   r.   r8   r   r   r   _assert_pointer_of6   s   r=   zthrust::hostzthrust::devicezthrust::seqzthrust/adjacent_difference.hc                 C   `   t | t|| t|| |durtd||||g}ddd |D }td| d|jS )z3Computes the differences of adjacent elements.
    N!binary_op option is not supported, c                 S      g | ]}|j qS r   r.   r   r   r   r   r   M       z'adjacent_difference.<locals>.<listcomp>zthrust::adjacent_difference()r/   r9   r<   NotImplementedErrorjoinr	   r,   r   r*   firstlastresult	binary_opr   paramsr   r   r   adjacent_differenceC      

rN   zthrust/binary_search.hc                 G   s"  t | t| t|| dt|  krdkr4n n|d }t|dkr)|d nd}t|| tj}n;dt|  kr@dkrkn t	d|d }|d }	|d }
t|dkr[|d nd}t|| t||	 |
j}nt	d|durwt
d|||g|}d	d
d |D }td| d|S );Attempts to find the element value with binary search.
          r   N      z0Invalid number of inputs of thrust.binary_searchcomp option is not supportedr@   c                 S   rA   r   rB   r   r   r   r   r   p   rC   z!binary_search.<locals>.<listcomp>zthrust::binary_search(rD   )r/   r3   r9   lenr=   r   bool_r<   r,   r-   rF   rG   r	   r   r*   rI   rJ   r   valuecompresult_ctypevalue_first
value_lastrK   rM   r   r   r   binary_searchU   .   

	

r^   zthrust/copy.hc                 C   sP   t | t|| t|| ||||g}ddd |D }td| d|jS )zCopies the elements.
    r@   c                 S   rA   r   rB   r   r   r   r   r   }   rC   zcopy.<locals>.<listcomp>zthrust::copy(rD   )r/   r9   r<   rG   r	   r,   r   r*   rI   rJ   rK   r   rM   r   r   r   copyt   s   

ra   zthrust/count.hc                 C   sf   t | t|jtjstd|j|jkrtd||||g}ddd |D }td| dtjS )zMCounts the number of elements in [first, last) that equals to ``value``.
    z`first` must be of pointer typez+`first` and `last` must be of the same typer@   c                 S   rA   r   rB   r   r   r   r   r      rC   zcount.<locals>.<listcomp>zthrust::count(rD   )	r/   r+   r,   r   r2   r-   rG   r	   int32r   r*   rI   rJ   rY   r   rM   r   r   r   count   s   rd   zthrust/equal.hc                 C   s`   t | t|| t|| |durtd||||g}ddd |D }td| dtjS )z2Returns true if the two ranges are identical.
    N#binary_pred option is not supportedr@   c                 S   rA   r   rB   r   r   r   r   r      rC   zequal.<locals>.<listcomp>zthrust::equal(rD   )r/   r9   r<   rF   rG   r	   r   rW   )r   r*   first1last1first2binary_predr   rM   r   r   r   equal   rO   rj   c                 C   sj   t | t| t|| |durtd||||g}ddd |D }td| dt|j|jgS )z<Attempts to find the element value in an ordered range.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r      rC   zequal_range.<locals>.<listcomp>zthrust::equal_range(rD   	r/   r3   r9   rF   rG   r	   r   Tupler,   )r   r*   rI   rJ   rY   rZ   r   rM   r   r   r   equal_range   s   

rm   zthrust/scan.hc           	      C   sr   t | t|| t|| |durtd||||g}|dur%|| ddd |D }td| d|jS )z0Computes an exclusive prefix sum operation.
    Nr?   r@   c                 S   rA   r   rB   r   r   r   r   r      rC   z"exclusive_scan.<locals>.<listcomp>zthrust::exclusive_scan(rD   )r/   r9   r<   rF   appendrG   r	   r,   )	r   r*   rI   rJ   rK   r
   rL   r   rM   r   r   r   exclusive_scan   s   


ro   c	                 C   s   t | t|| t|| |durtd|durtd|||||g}	|dur.|	| ddd |	D }
td|
 d|jS )	zaComputes an exclusive prefix sum operation by key.
    _assert_exec_policy_type(exec_policy)
    Nre   r?   r@   c                 S   rA   r   rB   r   r   r   r   r      rC   z)exclusive_scan_by_key.<locals>.<listcomp>zthrust::exclusive_scan_by_key(rD   )r3   r9   r<   rF   rn   rG   r	   r,   )r   r*   rf   rg   rh   rK   r
   ri   rL   r   rM   r   r   r   exclusive_scan_by_key   s   


rp   zthrust/fill.hc                 C   sF   t | t|| ||||g}ddd |D }td| dtjS )z5Assigns the value to every element in the range.
    r@   c                 S   rA   r   rB   r   r   r   r   r      rC   zfill.<locals>.<listcomp>zthrust::fill(rD   )r/   r9   rG   r	   r   voidrc   r   r   r   fill   s
   
rr   zthrust/find.hc                 C   N   t | t| t|| ||||g}ddd |D }td| d|jS )z>Finds the first iterator whose value equals to ``value``.
    r@   c                 S   rA   r   rB   r   r   r   r   r      rC   zfind.<locals>.<listcomp>zthrust::find(rD   r/   r3   r9   rG   r	   r,   rc   r   r   r   find   s   
ru   zthrust/gather.hc                 C   sZ   t | t| t|| t|| |||||g}ddd |D }td| d|jS )zFCopies elements from source into destination  according to a map.
    r@   c                 S   rA   r   rB   r   r   r   r   r     rC   zgather.<locals>.<listcomp>zthrust::gather(rD   )r/   r3   r9   r<   rG   r	   r,   )r   r*   	map_firstmap_lastinput_firstrK   r   rM   r   r   r   gather      

ry   c                 C   r>   )z0Computes an inclusive prefix sum operation.
    Nr?   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   z"inclusive_scan.<locals>.<listcomp>zthrust::inclusive_scan(rD   rE   rH   r   r   r   inclusive_scan  s   

r{   c           
      C   sz   t | t| t|| t|| |durtd|dur"td|||||g}ddd |D }	td|	 d|jS )	z7Computes an inclusive prefix sum operation by key.
    Nre   r?   r@   c                 S   rA   r   rB   r   r   r   r   r   ,  rC   z)inclusive_scan_by_key.<locals>.<listcomp>zthrust::inclusive_scan_by_key(rD   )r/   r3   r9   r<   rF   rG   r	   r,   )
r   r*   rf   rg   rh   rK   ri   rL   r   rM   r   r   r   inclusive_scan_by_key  s   

r|   zthrust/inner_product.hc           
      C   sr   t | t|| t|| |durtd|durtd|||||g}ddd |D }	td|	 d|jS )	z/Calculates an inner product of the ranges.
    Nz"binary_op1 option is not supportedz"binary_op2 option is not supportedr@   c                 S   rA   r   rB   r   r   r   r   r   >  rC   z!inner_product.<locals>.<listcomp>zthrust::inner_product(rD   rE   )
r   r*   rf   rg   rh   r
   
binary_op1
binary_op2r   rM   r   r   r   inner_product0  s   

r   zthrust/sort.hc                 C   \   t | t| t|| |durtd|||g}ddd |D }td| dtjS )z<Returns true if the range is sorted in ascending order.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r   O  rC   zis_sorted.<locals>.<listcomp>zthrust::is_sorted(rD   )r/   r3   r9   rF   rG   r	   r   rW   r   r*   rI   rJ   rZ   r   rM   r   r   r   	is_sortedE     

r   c                 C   \   t | t| t|| |durtd|||g}ddd |D }td| d|jS )z=Returns the last iterator for which the range is sorted.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r   ]  rC   z#is_sorted_until.<locals>.<listcomp>zthrust::is_sorted_until(rD   r/   r3   r9   rF   rG   r	   r,   r   r   r   r   is_sorted_untilS  r   r   c                 G   "  t | t| t|| dt|  krdkr4n n|d }t|dkr)|d nd}t|| |j}n;dt|  kr@dkrkn td|d }|d }	|d }
t|dkr[|d nd}t|| t||	 |
j}ntd|durwtd|||g|}d		d
d |D }t
d| d|S )rP   rQ   rR   r   NrS   rT   z.Invalid number of inputs of thrust.lower_boundrU   r@   c                 S   rA   r   rB   r   r   r   r   r   |  rC   zlower_bound.<locals>.<listcomp>zthrust::lower_bound(rD   r/   r3   r9   rV   r=   r,   r<   r-   rF   rG   r	   rX   r   r   r   lower_bounda  r_   r   c                   @      e Zd ZdefddZdS )_ConstantIteratorr0   c                 C      | j }d| dS )Nzthrust::constant_iterator<r   r:   r   
value_typer   r   r   __str__     z_ConstantIterator.__str__Nr!   r"   r#   strr   r   r   r   r   r         r   z#thrust/iterator/constant_iterator.hc                 C   B   |durt d|g}ddd |D }td| dt|jS )3Finds the first positions whose values differ.
    Nindex_type is not supportedr@   c                 S   rA   r   rB   r   r   r   r   r     rC   z*make_constant_iterator.<locals>.<listcomp>zthrust::make_constant_iterator(rD   )rF   rG   r	   r   r,   r   xir   rM   r   r   r   make_constant_iterator     
r   c                   @   r   )_CountingIteratorr0   c                 C   r   )Nzthrust::counting_iterator<r   r   r   r   r   r   r     r   z_CountingIterator.__str__Nr   r   r   r   r   r     r   r   z#thrust/iterator/counting_iterator.hc                 C   r   )r   Nr   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   z*make_counting_iterator.<locals>.<listcomp>zthrust::make_counting_iterator(rD   )rF   rG   r	   r   r,   r   r   r   r   make_counting_iterator  r   r   zthrust/mismatch.hc                 C   sl   t | t|| t|| |durtd||||g}ddd |D }td| dt|j|jgS )r   Nzpred option is not supportedr@   c                 S   rA   r   rB   r   r   r   r   r     rC   zmismatch.<locals>.<listcomp>zthrust::mismatch(rD   )	r/   r9   r<   rF   rG   r	   r   rl   r,   )r   r*   rf   rg   rh   predr   rM   r   r   r   mismatch  s   


r   zthrust/reduce.hc           	      C   s|   t | t| t|| |||g}|dur|| |j}n|jj}|dur+tdddd |D }td| d|S )z!Generalization of summation.
    Nr?   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   zreduce.<locals>.<listcomp>zthrust::reduce(rD   )	r/   r3   r9   rn   r,   r:   rF   rG   r	   )	r   r*   rI   rJ   r
   rL   r   return_typerM   r   r   r   reduce  s   


r   c	                 C   s   t | t| t|| t| t| t| ||||||g}	|dur)td|dur1tdddd |	D }
td|
 dt|j|jgS )	z1Generalization of reduce to key-value pairs.
    Nre   r?   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   z!reduce_by_key.<locals>.<listcomp>zthrust::reduce_by_key(rD   rk   )r   r*   
keys_first	keys_lastvalues_firstkeys_outputvalues_outputri   rL   r   rM   r   r   r   reduce_by_key  s$   

r   zthrust/remove.hc                 C   rs   )ARemoves from the range all elements that are equal to value.
    r@   c                 S   rA   r   rB   r   r   r   r   r     rC   zremove.<locals>.<listcomp>zthrust::remove(rD   rt   rc   r   r   r   remove  s   
r   c                 C   sX   t | t| t|| t| |||||g}ddd |D }td| d|jS )r   r@   c                 S   rA   r   rB   r   r   r   r   r   
  rC   zremove_copy.<locals>.<listcomp>zthrust::remove_copy(rD   rt   )r   r*   rI   rJ   rK   rY   r   rM   r   r   r   remove_copy  s   
r   zthrust/replace.hc                 C   sZ   t | t| t|| t|| |||||g}ddd |D }td| dtjS )KReplaces every element in the range equal to old_value with new_value.
    r@   c                 S   rA   r   rB   r   r   r   r   r     rC   zreplace.<locals>.<listcomp>zthrust::replace(rD   r/   r3   r9   rG   r	   r   rq   )r   r*   rI   rJ   	old_value	new_valuer   rM   r   r   r   replace  rz   r   c           	      C   sd   t | t| t|| t| t|| ||||||g}ddd |D }td| d|jS )r   r@   c                 S   rA   r   rB   r   r   r   r   r   )  rC   z replace_copy.<locals>.<listcomp>zthrust::replace_copy(rD   rt   )	r   r*   rI   rJ   rK   r   r   r   rM   r   r   r   replace_copy  s   

r   zthrust/reverse.hc                 C   sL   t | t| t|| |||g}ddd |D }td| dtjS )Reverses a range.
    r@   c                 S   rA   r   rB   r   r   r   r   r   9  rC   zreverse.<locals>.<listcomp>zthrust::reverse(rD   r   )r   r*   rI   rJ   r   rM   r   r   r   reverse1  s   

r   c                 C   V   t | t| t|| t| ||||g}ddd |D }td| d|jS )r   r@   c                 S   rA   r   rB   r   r   r   r   r   F  rC   z reverse_copy.<locals>.<listcomp>zthrust::reverse_copy(rD   rt   r`   r   r   r   reverse_copy=  s   
r   zthrust/scatter.hc                 C   s`   t | t| t|| t| t| |||||g}ddd |D }td| dtjS )zMCopies elements from source range into an output range according to map.
    r@   c                 S   rA   r   rB   r   r   r   r   r   T  rC   zscatter.<locals>.<listcomp>zthrust::scatter(rD   r   )r   r*   rI   rJ   maprK   r   rM   r   r   r   scatterJ  s   
r   zthrust/sequence.hc                 C   sz   t | t| t|| |||g}|dur|| |dur)t|| || ddd |D }td| dtjS )z0Fills the range with a sequence of numbers.
    Nr@   c                 S   rA   r   rB   r   r   r   r   r   h  rC   zsequence.<locals>.<listcomp>zthrust::sequence(rD   )r/   r3   r9   rn   rG   r	   r   rq   )r   r*   rI   rJ   r
   stepr   rM   r   r   r   sequence[  s   




r   zthrust/set_operations.hc           
      C   l   t | t|| t|| t| ||||||g}|dur"tdddd |D }	td|	 d|jS )zKConstructs a sorted range that is the set difference of sorted inputs.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r   y  rC   z"set_difference.<locals>.<listcomp>zthrust::set_difference(rD   r/   r9   r3   rF   rG   r	   r,   
r   r*   rf   rg   rh   last2rK   rZ   r   rM   r   r   r   set_differencel     

r   c              	   C      t | t|| t|| t| t| t| t|	 |||||||||	g	}|
dur1tdddd |D }td| dt|j|	jgS )z>Constructs the key-value set difference of sorted inputs.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   z)set_difference_by_key.<locals>.<listcomp>zthrust::set_difference_by_key(rD   	r/   r9   r3   rF   rG   r	   r   rl   r,   r   r*   keys_first1
keys_last1keys_first2
keys_last2values_first1values_first2keys_resultvalues_resultrZ   r   rM   r   r   r   set_difference_by_key}  "   



r   c           
      C   r   )zMConstructs a sorted range that is the set intersection of sorted inputs.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   z$set_intersection.<locals>.<listcomp>zthrust::set_intersection(rD   r   r   r   r   r   set_intersection  r   r   c
                 C   s   t | t|| t|| t| t| t| ||||||||g}
|	dur,tdddd |
D }td| dt|j|jgS )z@Constructs the key-value set intersection of sorted inputs.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   z+set_intersection_by_key.<locals>.<listcomp>z thrust::set_intersection_by_key(rD   r   )r   r*   r   r   r   r   r   r   r   rZ   r   rM   r   r   r   set_intersection_by_key  s    



r   c           
      C   r   )z@Constructs a sorted range that is the symmetric difference.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   z,set_symmetric_difference.<locals>.<listcomp>z!thrust::set_symmetric_difference(rD   r   r   r   r   r   set_symmetric_difference  r   r   c              	   C   r   )zDConstructs the key-value symmetric difference of sorted inputs.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   z3set_symmetric_difference_by_key.<locals>.<listcomp>z(thrust::set_symmetric_difference_by_key(rD   r   r   r   r   r   set_symmetric_difference_by_key  r   r   c           
      C   r   )zFConstructs a sorted range that is the set union of sorted inputs.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   zset_union.<locals>.<listcomp>zthrust::set_union(rD   r   r   r   r   r   	set_union  r   r   c              	   C   r   )z5Constructs the key-value union of sorted inputs.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   z$set_union_by_key.<locals>.<listcomp>zthrust::set_union_by_key(rD   r   r   r   r   r   set_union_by_key  r   r   c                 C   r   )>Sorts the elements in [first, last) into ascending order.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r   "  rC   zsort.<locals>.<listcomp>zthrust::sort(rD   r/   r3   r9   rF   rG   r	   r   rq   r   r   r   r   sort     

r   c                 C   f   t | t| t|| t| |durtd||||g}ddd |D }td| dtjS )Performs key-value sort.
    NrU   r@   c                 S   rA   r   rB   r   r   r   r   r   3  rC   zsort_by_key.<locals>.<listcomp>zthrust::sort_by_key(rD   r   r   r*   r   r   r   rZ   r   rM   r   r   r   sort_by_key&     
r   c                 C   r   )r   NrU   r@   c                 S   rA   r   rB   r   r   r   r   r   F  rC   zstable_sort.<locals>.<listcomp>zthrust::stable_sort(rD   r   r   r   r   r   stable_sort;  r   r   c                 C   r   )r   NrU   r@   c                 S   rA   r   rB   r   r   r   r   r   W  rC   z&stable_sort_by_key.<locals>.<listcomp>zthrust::stable_sort_by_key(rD   r   r   r   r   r   stable_sort_by_keyJ  r   r   zthrust/swap.hc                 C   r   )z-Swaps each of the elements in the range.
    r@   c                 S   rA   r   rB   r   r   r   r   r   e  rC   zswap_ranges.<locals>.<listcomp>zthrust::swap_ranges(rD   rt   )r   r*   rf   rg   rh   r   rM   r   r   r   swap_ranges[  s   
r   zthrust/unique.hc                 C   r   )z4Removes all but the first element of the group.
    Nre   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   zunique.<locals>.<listcomp>zthrust::unique(rD   r   )r   r*   rI   rJ   ri   r   rM   r   r   r   uniqueu  r   r   c                 C   sr   t | t| t|| t| ||||g}|durtdddd |D }td| dt|j|jgS )zUniques key-value pairs.
    Nre   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   z!unique_by_key.<locals>.<listcomp>zthrust::unique_by_key(rD   rk   )r   r*   r   r   r   ri   r   rM   r   r   r   unique_by_key  s   

r   c                 G   r   )rP   rQ   rR   r   NrS   rT   z.Invalid number of inputs of thrust.upper_boundrU   r@   c                 S   rA   r   rB   r   r   r   r   r     rC   zupper_bound.<locals>.<listcomp>zthrust::upper_bound(rD   r   rX   r   r   r   upper_bound  r_   r   )N)NN)NNN)B	cupyx.jitr   r   cupyx.jit._internal_typesr   r	   r(   TypeBaser)   r/   r3   r9   r<   r=   hostdeviceseqrN   r^   ra   rd   rj   rm   ro   rp   rr   ru   ry   r{   r|   r   r   r   r   r2   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s    















