o
    8ήc                     @   s   d dl Z d dlZd dlmZmZmZ d dlmZm	Z	 d dlm
Z
 e
dG dd de	ZG dd	 d	e	Ze
dG d
d de	ZedkrJe  dS dS )    N)driverdrvapidevices)unittestContextResettingTestCase)skip_on_cudasimz,CUDA Memory API unsupported in the simulatorc                       sh   e Zd Z fddZ f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  ZS )TestCudaMemoryc                       t    t | _d S NsupersetUpr   get_contextcontextself	__class__ P/tmp/pip-target-vg8gfxp4/lib/python/numba/cuda/tests/cudadrv/test_cuda_memory.pyr         
zTestCudaMemory.setUpc                       | ` tt|   d S r
   )r   r   r   tearDownr   r   r   r   r         zTestCudaMemory.tearDownc                 C   sF   |  t| t| tjrtjj}ntj}|  t	|j
| d S r
   )
assertTruer   is_device_memoryrequire_device_memoryUSE_NV_BINDINGbindingCUdeviceptrr   cu_device_ptr
isinstancedevice_ctypes_pointer)r   objexpected_classr   r   r   	_template   s   



zTestCudaMemory._templatec                 C      | j d}| | d S N   )r   memallocr%   r   devmemr   r   r   test_device_memory      z!TestCudaMemory.test_device_memoryc                 C   s    | j d}| |d d S )Nr(   
   )r   r)   r%   viewr*   r   r   r   test_device_view"   s   zTestCudaMemory.test_device_viewc                 C   s   | j jddd}| | d S )Nr(   Tmapped)r   memhostallocr%   r*   r   r   r   test_host_alloc&   s   zTestCudaMemory.test_host_allocc                 C   s:   t d}| jj||jj|j|jj dd}| 	| d S )Nr.   Tr1   )
nparanger   mempinctypesdatasizedtypeitemsizer%   )r   aryr+   r   r   r   test_pinned_memory*   s   
z!TestCudaMemory.test_pinned_memoryc                 C   r&   r'   )r   memallocmanagedr%   r*   r   r   r   test_managed_memory1   r-   z"TestCudaMemory.test_managed_memoryc                    s>   dd   fdd}j d}||dd ||dd d S )	Nc                 S   s   t jrt| jS | jjS r
   )r   r   inthandlevalue)memr   r   r   
handle_val8   s   
z7TestCudaMemory.test_derived_pointer.<locals>.handle_valc                    s   |  |} |j |  | jd  ||  |j | |} |j |   |j |   ||d   |j | jd ~| jd ~| jd d S )N         )r/   assertEqualownerrefct)moffsetv1v2rE   r   r   r   check>   s   

z2TestCudaMemory.test_derived_pointer.<locals>.checkr(   r   )rL   rM   rH   )r   r)   )r   rQ   rL   r   rP   r   test_derived_pointer5   s
   z#TestCudaMemory.test_derived_pointerc                    s   t d}dg  fdd}tj| j|d|d}|  d d ~|  d d tj| j|d|d}| }~|  d d ~|  d d d S )	Nl   >[= r   c                      s    d  d7  < d S )Nr   rH   r   r   dtor_invokedr   r   dtorZ   s   z0TestCudaMemory.test_user_extension.<locals>.dtor(   )r   pointerr:   	finalizerrH   rF   )r8   c_void_pr   MemoryPointerr   rI   own)r   fake_ptrrU   ptrownedr   rS   r   test_user_extensionT   s"   


z"TestCudaMemory.test_user_extension)__name__
__module____qualname__r   r   r%   r,   r0   r4   r>   r@   rR   r_   __classcell__r   r   r   r   r   
   s    
r   c                       s@   e Zd Z fddZ fddZdd Zdd Zd	d
 Z  ZS )TestCudaMemoryFunctionsc                    r	   r
   r   r   r   r   r   r   o   r   zTestCudaMemoryFunctions.setUpc                    r   r
   )r   r   rd   r   r   r   r   r   r   s   r   z TestCudaMemoryFunctions.tearDownc                 C   sn   t jdt jd}t jdt jd}|j|jj }| j|}t	||| t
||| | t ||k d S Nd   r;   )r5   r6   uint32r:   r;   r<   r   r)   r   host_to_devicedevice_to_hostr   all)r   hstaryhstary2szdevaryr   r   r   test_memcpyw   s   z#TestCudaMemoryFunctions.test_memcpyc                 C   s   t d}d}|jd }| j|}t|d| t j||d}t||| t j	dg| t dd}| 
t ||k d S )Nrh   r.      rg   l   +WW )r5   r;   r<   r   r)   r   device_memsetemptyrj   arrayr   rk   )r   r;   nrn   ro   rl   rm   r   r   r   test_memset   s   

z#TestCudaMemoryFunctions.test_memsetc                 C   s   t jdt jd}t |}|j|jj }| j|}| j|}t	
||| t	||| t	||| | t ||k d S re   )r5   r6   rh   
empty_liker:   r;   r<   r   r)   r   ri   device_to_devicerj   r   rk   )r   hsthst2rn   dev1dev2r   r   r   test_d2d   s   
z TestCudaMemoryFunctions.test_d2d)	r`   ra   rb   r   r   rp   rv   r}   rc   r   r   r   r   rd   n   s    rd   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestMVExtentc                 C   sP   t d}|jj|j }t|\}}| |jj	|k | |t
|k d S )Nrf   )r5   r6   r;   r<   r:   r   host_memory_extentsr   r8   r9   host_memory_sizer   r=   aryszser   r   r   test_c_contiguous_array   s
   
z$TestMVExtent.test_c_contiguous_arrayc                 C   sd   t t ddd}|jjt |j }t	|\}}| 
|jj|k | 
|t|k d S )Nrf   rF   2   )r5   asfortranarrayr6   reshaper;   r<   prodshaper   r   r   r8   r9   r   r   r   r   r   test_f_contiguous_array   s
   z$TestMVExtent.test_f_contiguous_arrayc                 C   sP   t t d}|jj}t|\}}| |jj	|k | |t
|k d S )Ni  )r5   asarrayrh   r;   r<   r   r   r   r8   r9   r   r   r   r   r   test_single_element_array   s
   z&TestMVExtent.test_single_element_arrayc                 C   s@   G dd dt j}|ddd}t|}| t ||k d S )Nc                   @   s    e Zd ZdejfdejfgZdS )z1TestMVExtent.test_ctypes_struct.<locals>.mystructxyN)r`   ra   rb   r8   c_int_fields_r   r   r   r   mystruct   s    r   {   i  )r   r   )r8   	Structurer   r   r   sizeof)r   r   r9   rn   r   r   r   test_ctypes_struct   s   
zTestMVExtent.test_ctypes_structc                 C   s,   t d}t|}| t ||k d S )NgX9v?)r8   c_doubler   r   r   r   )r   r9   rn   r   r   r   test_ctypes_double   s   

zTestMVExtent.test_ctypes_doubleN)r`   ra   rb   r   r   r   r   r   r   r   r   r   r~      s    r~   __main__)r8   numpyr5   numba.cuda.cudadrvr   r   r   numba.cuda.testingr   r   r   r   rd   r~   r`   mainr   r   r   r   <module>   s    c-$