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m	Z	 d dlm
Z
 d dlmZmZ d dlZd dlZd dlmZmZ d dlmZ efd	d
ZedG dd deZedG dd deZedkrhe  dS dS )    N)
namedtuple)voidint32float32float64)guvectorize)cuda)skip_on_cudasimCUDATestCase)NumbaPerformanceWarningTypingError)override_configc                 C   sT   t t| d d d d f | d d d d f | d d d d f gddddd }|S )Nz(m,n),(n,p)->(m,p)r   targetc           	   
   S   sv   | j \}}|j \}}t|D ]*}t|D ]#}d|||f< t|D ]}|||f  | ||f |||f  7  < q qqd S Nr   shaperange)	ABCmnpijk r   a/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/numba/cuda/tests/cudapy/test_gufunc.py
matmulcore   s   

*z*_get_matmulcore_gufunc.<locals>.matmulcore)r   r   )dtyper   r   r   r   _get_matmulcore_gufunc   s   >
	r!   z&ufunc API unsupported in the simulatorc                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/S )0TestCUDAGufuncc                 C   sz   t  }d}tj|d d tjd|dd}tj|d d tjd|dd}|||}t||}| t|| d S N      r       r!   nparanger   reshapematmul
assertTrueallcloseselfgufunc	matrix_ctr   r   r   Goldr   r   r   test_gufunc_small!      
z TestCUDAGufunc.test_gufunc_smallc                 C   s   t  }d}tj|d d tjd|dd}tj|d d tjd|dd}t|}||| }t||}| 	t
|| d S r#   )r!   r)   r*   r   r+   r   	to_devicecopy_to_hostr,   r-   r.   )r0   r1   r2   r   r   dBr   r3   r   r   r   test_gufunc_auto_transfer/   s   
z(TestCUDAGufunc.test_gufunc_auto_transferc                 C   sz   t  }d}tj|d d tjd|dd}tj|d d tjd|dd}|||}t||}| t|| d S )N  r$   r%   r&   r'   r(   r/   r   r   r   test_gufunc?   r5   zTestCUDAGufunc.test_gufuncc                 C   s~   t  }d}tj|d d tjddddd}tj|d d tjddddd}|||}t||}| t|| d S )Nd   r$   r%   r&      r'   r(   r/   r   r   r   test_gufunc_hidimM   s   $$
z TestCUDAGufunc.test_gufunc_hidimc                 C   sp   t td}tjddd}tjdd}t||}|||}tj|| ||t|d}tj|| d S )Nr&   
      )r?      rA   )	r!   r   r)   randomrandnr,   testingassert_allclosetile)r0   r1   XYgoldres1res2r   r   r   test_gufunc_new_axisY   s   

z#TestCUDAGufunc.test_gufunc_new_axisc                 C   s   t  }d}tj|d d tjd|dd}tj|d d tjd|dd}t }t||}t||}tjd|j	|d}|||||d}|j
|d	}	|  t||}
| t|	|
 d S )
Nr:   r$   r%   r&   r'   )r:   r$   r'   )r   r    stream)outrM   )rM   )r!   r)   r*   r   r+   r   rM   r6   device_arrayr    r7   synchronizer,   r-   r.   )r0   r1   r2   r   r   rM   dAr8   dCr   r3   r   r   r   test_gufunc_streamh   s"   z!TestCUDAGufunc.test_gufunc_streamc                 C   sj   t ttd d  td d  gddddd }tjdtjdd }t|}|||d	 tj|| d S )
N(x)->(x)r   r   c                 S       t |jD ]}| | ||< qd S Nr   sizer   r   r   r   r   r   copy      z&TestCUDAGufunc.test_copy.<locals>.copyr?   r&   rA   rN   r   r   r   r)   r*   
zeros_likerD   rE   r0   rZ   r   r   r   r   r   	test_copy   s   

zTestCUDAGufunc.test_copyc                 C   sl   t td d  td d  fgddddd }tjdtjdd }t|}|||d	 | t|| d S )
NrT   r   r   c                 S   rU   rV   rW   rY   r   r   r   rZ      r[   z9TestCUDAGufunc.test_copy_unspecified_return.<locals>.copyr?   r&   rA   r\   )r   r   r)   r*   r^   r-   r.   r_   r   r   r   test_copy_unspecified_return   s   

z+TestCUDAGufunc.test_copy_unspecified_returnc                 C   sn   t ttd d  td d  gddddd }tjdtjdd }t|}|||d	 | t|| d S )
NrT   r   r   c                 S   rU   rV   rW   rY   r   r   r   rZ      r[   z*TestCUDAGufunc.test_copy_odd.<locals>.copy   r&   rA   r\   )r   r   r   r)   r*   r^   r-   r.   r_   r   r   r   test_copy_odd   s   

zTestCUDAGufunc.test_copy_oddc                 C   s   t ttd d d d f td d d d f gddddd }tjdtjddd	d
 }t|}|||d | t|| d S )Nz(x, y)->(x, y)r   r   c                 S   s@   t |jd D ]}t |jd D ]}| ||f |||f< qqd S )Nr   rA   r   r   )r   r   xyr   r   r   copy2d   s
   z*TestCUDAGufunc.test_copy2d.<locals>.copy2d   r&   r'      rA   r\   )	r   r   r   r)   r*   r+   r^   r-   r.   )r0   rg   r   r   r   r   r   test_copy2d   s   ,

zTestCUDAGufunc.test_copy2dc                    s   t ttd d  td d  gddddd  tj fdd}tdd	}t|}d
}| 	t
| |d || W d    d S 1 sHw   Y  d S )Nz(n)->(n)r   r   c                 S   s$   t | jd D ]}| | ||< qd S r   rd   )r   br   r   r   r   gufunc_copy   s   zDTestCUDAGufunc.test_not_supported_call_from_jit.<locals>.gufunc_copyc                    s
    | |S rV   r   )r   rk   rl   r   r   cuda_jit   s   
zATestCUDAGufunc.test_not_supported_call_from_jit.<locals>.cuda_jit   r   z#Untyped global name 'gufunc_copy'.*)rA   rA   )r   r   r   r   jitr)   r*   astyper^   assertRaisesRegexr   )r0   rn   r   rk   msgr   rm   r    test_not_supported_call_from_jit   s   

"z/TestCUDAGufunc.test_not_supported_call_from_jitc              	   C   s  t dgddddd }tjdd}tjdd}t|jd	 d}td
dL tj	dd-}|||| | 
|d	 jt | dt|d	 j | dt|d	 j W d    n1 sew   Y  W d    d S W d    d S 1 s}w   Y  d S )N(void(float32[:], float32[:], float32[:])(n),(n)->(n)r   r   c                 S   0   | j d }t|D ]}| | ||  ||< q	d S r   r   ark   distlenr   r   r   r   numba_dist_cuda      
zMTestCUDAGufunc.test_inefficient_launch_configuration.<locals>.numba_dist_cudaro   r   r   CUDA_LOW_OCCUPANCY_WARNINGSrA   Trecordz	Grid sizezlow occupancy)r   r)   rB   randrq   zerosr   r   warningscatch_warningsassertEqualcategoryr   assertInstrmessage)r0   r|   ry   rk   rz   wr   r   r   %test_inefficient_launch_configuration   s"   
"z4TestCUDAGufunc.test_inefficient_launch_configurationc              	   C   s   t dgdddddd }tjdd	d
}tjdd	d
}t|}tdd5 tj	dd}|||| | 
t|d W d    n1 sOw   Y  W d    d S W d    d S 1 sgw   Y  d S )Nru   rv   Tr   )nopythonr   c                 S   rw   r   r   rx   r   r   r   numba_dist_cuda2   r}   zLTestCUDAGufunc.test_efficient_launch_configuration.<locals>.numba_dist_cuda2i   r   )i   r$   r~   rA   r   r   )r   r)   rB   r   rq   r+   r^   r   r   r   r   r{   )r0   r   ry   rk   rz   r   r   r   r   #test_efficient_launch_configuration   s&   

"z2TestCUDAGufunc.test_efficient_launch_configurationc                 C   s   dd }t ttd d  td d  gdddd| | t}t ttd d  td d  gdddd| W d    n1 s@w   Y  | dt|j d S )	Nc                 S      d S rV   r   r   r   r   r   r   foo      z.TestCUDAGufunc.test_nopython_flag.<locals>.foorT   r   T)r   r   Fznopython flag must be True)r   r   r   assertRaises	TypeErrorr   r   	exception)r0   r   raisesr   r   r   test_nopython_flag   s    z!TestCUDAGufunc.test_nopython_flagc                 C   s   dd }|  t}tttd d  td d  gddddd| W d    n1 s+w   Y  d}t|j}| |d t| | |t|d  	 
d	}d
d |D }| tddgt| d S )Nc                 S   r   rV   r   r   r   r   r   r     r   z.TestCUDAGufunc.test_invalid_flags.<locals>.foorT   r   TF)r   what1ever2z/The following target options are not supported:,c                 S   s   g | ]}| d qS )z'" )strip).0r   r   r   r   
<listcomp>  s    z5TestCUDAGufunc.test_invalid_flags.<locals>.<listcomp>r   r   )r   r   r   r   r   r   r   r   r{   r   splitset)r0   r   r   headrs   itemsr   r   r   test_invalid_flags  s   
z!TestCUDAGufunc.test_invalid_flagsc                 C   s   t ttd d  td d  gddddd }tjdtjd }}| t}||||d W d    n1 s8w   Y  d	}| t|j	| d S )
NrT   r   r   c                 S   r   rV   r   )inprN   r   r   r   r        z2TestCUDAGufunc.test_duplicated_output.<locals>.foor?   r&   r\   z<cannot specify argument 'out' as both positional and keyword)
r   r   r   r)   r   r   
ValueErrorr   r   r   )r0   r   r   rN   r   rs   r   r   r   test_duplicated_output  s   $
z%TestCUDAGufunc.test_duplicated_outputc                 C   sp   t td d  td d  td d  fgddddd }|||}tjt|t| dd}tj|| d S )Nz(n),(n)->()r   r   c                 S   s6   d}t t| D ]}|| | ||  7 }q||d< d S r   )r   r{   )re   rf   rsr   r   r   r   	gu_reduce  s   z1TestCUDAGufunc.check_tuple_arg.<locals>.gu_reducerA   )axis)r   r   r)   sumasarrayrD   assert_equal)r0   ry   rk   r   r   expectedr   r   r   check_tuple_arg  s   &

zTestCUDAGufunc.check_tuple_argc                 C   s   d}d}|  || d S )N)      ?       @      @      @      @      @)      ?      @      @      @      @      @)r   r0   ry   rk   r   r   r   test_tuple_of_tuple_arg+  s   z&TestCUDAGufunc.test_tuple_of_tuple_argc                 C   sR   t dd}|dddd|ddddf}|d	d
dd|ddddf}| || d S )NPointre   rf   zr   r   r   r   r   r   r   r   r   r   r   r   )r   r   )r0   r   ry   rk   r   r   r   test_tuple_of_namedtuple_arg2  s   
z+TestCUDAGufunc.test_tuple_of_namedtuple_argc                 C   s8   t dt df}t dt df}| || d S )Nr   r   r   r   )r)   r   r   r   r   r   r   test_tuple_of_array_arg:  s   z&TestCUDAGufunc.test_tuple_of_array_argc                 C   s   t  }| |jd d S )Nr   )r!   r   __name__)r0   r1   r   r   r   test_gufunc_nameA  s   zTestCUDAGufunc.test_gufunc_namec                 C   s~   |  t}tttd d  td d  gddddd }W d    n1 s'w   Y  t|j}| d| | d| d S )Nz(m)->(m)r   r   c                 S   r   rV   r   )re   rf   r   r   r   fG  r   z.TestCUDAGufunc.test_bad_return_type.<locals>.fz+guvectorized functions cannot return valueszspecifies int32 return type)r   r   r   r   r   r   r   )r0   ter   rs   r   r   r   test_bad_return_typeE  s   $
z#TestCUDAGufunc.test_bad_return_typec                 C   s
  t td d  td d  td d  fgddddd }td}| t}|| W d    n1 s4w   Y  t|j}| d| | d| | d	| | t}||||| W d    n1 sgw   Y  t|j}| d| | d| | d
| d S )Nz(m),(m)->(m)r   r   c                 S   r   rV   r   r   r   r   r   r   P     z;TestCUDAGufunc.test_incorrect_number_of_pos_args.<locals>.fr'   %gufunc accepts 2 positional argumentszor 3 positional argumentsGot 1 positional argument.zGot 4 positional arguments.	r   r   r)   r*   r   r   r   r   r   r0   r   arrr   rs   r   r   r   !test_incorrect_number_of_pos_argsO  s&   $




z0TestCUDAGufunc.test_incorrect_number_of_pos_argsN)r   
__module____qualname__r4   r9   r;   r>   rL   rS   r`   ra   rc   rj   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"      s0    
r"   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestMultipleOutputsc                 C   s   t ttd d  td d  td d  gddddd }tjdtjdd }t|}t|}|||| tj|| tj|| d S )	N(x)->(x),(x)r   r   c                 S   s,   t |jD ]}| | ||< | | ||< qd S rV   rW   r   r   r   r   r   r   r   rZ   m  s   zKTestMultipleOutputs.test_multiple_outputs_same_type_passed_in.<locals>.copyr?   r&   rA   r]   )r0   rZ   r   r   r   r   r   r   )test_multiple_outputs_same_type_passed_inl  s   &


z=TestMultipleOutputs.test_multiple_outputs_same_type_passed_inc                 C   s   t ttd d  td d  td d  gddddd }tjdtjdd }t|}t|}|||| tj|| tj|d	 | d S )
Nr   r   r   c                 S   0   t |jD ]}| | ||< | | d ||< qd S Nr$   rW   r   r   r   r   copy_and_double~     zRTestMultipleOutputs.test_multiple_outputs_distinct_values.<locals>.copy_and_doubler?   r&   rA   r$   r]   r0   r   r   r   r   r   r   r   %test_multiple_outputs_distinct_values|  s   &


z9TestMultipleOutputs.test_multiple_outputs_distinct_valuesc                 C   s|   t ttd d  td d  td d  gddddd }tjdtjdd }||\}}tj|| tj|d	 | d S )
Nr   r   r   c                 S   r   r   rW   r   r   r   r   r     r   zLTestMultipleOutputs.test_multiple_output_allocation.<locals>.copy_and_doubler?   r&   rA   r$   )r   r   r   r)   r*   rD   rE   r   r   r   r   test_multiple_output_allocation  s   &
z3TestMultipleOutputs.test_multiple_output_allocationc                 C   s   t ttd d  td d  td d  gddddd }tjdtjdd }t|}tj|tjd}|||| tj|| tj|td	 | d S )
Nr   r   r   c                 S   r   )Nr   rW   r   r   r   r   copy_and_multiply  r   zJTestMultipleOutputs.test_multiple_output_dtypes.<locals>.copy_and_multiplyr?   r&   rA   r   )	r   r   r   r   r)   r*   r^   rD   rE   )r0   r   r   r   r   r   r   r   test_multiple_output_dtypes  s   &

z/TestMultipleOutputs.test_multiple_output_dtypesc                 C   s  t td d  td d  td d  td d  fgddddd }td}| t}|| W d    n1 s9w   Y  t|j}| d| | d| | d	| | t}|||||| W d    n1 smw   Y  t|j}| d| | d| | d
| d S )Nz(m),(m)->(m),(m)r   r   c                 S   r   rV   r   )re   rf   r   r   r   r   r   r     r   z@TestMultipleOutputs.test_incorrect_number_of_pos_args.<locals>.fr'   r   zor 4 positional argumentsr   zGot 5 positional arguments.r   r   r   r   r   r     s&   .




z5TestMultipleOutputs.test_incorrect_number_of_pos_argsN)r   r   r   r   r   r   r   r   r   r   r   r   r   j  s    r   __main__)numpyr)   collectionsr   numbar   r   r   r   r   r   numba.cuda.testingr	   r
   unittestr   numba.core.errorsr   r   numba.tests.supportr   r!   r"   r   r   mainr   r   r   r   <module>   s(      M\