o
    ۾i                     @   s   d dl Z d dlmZ d dlZd dlmZ d dlmZmZm	Z	 d dl
mZ 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)byrefcuda)unittestCUDATestCaseskip_on_cudasim)driverc                       s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	TestContextStackc                       t    t  d S N)supersetUpr   closeself	__class__ _/home/ubuntu/.local/lib/python3.10/site-packages/numba/cuda/tests/cudadrv/test_context_stack.pyr      s   
zTestContextStack.setUpc                 C   sV   |  tjjd  tjd  | ttjjjd W d    d S 1 s$w   Y  d S Nr   )assertIsr   gpuscurrentassertEqualintidr   r   r   r   test_gpus_current   s   "z"TestContextStack.test_gpus_currentc                 C   s   |  ttjd d S r   )assertGreaterlenr   r   r   r   r   r   test_gpus_len   s   zTestContextStack.test_gpus_lenc                 C   s   t tj}| t|d d S r   )listr   r   r   r   )r   gpulistr   r   r   test_gpus_iter   s   
zTestContextStack.test_gpus_iter)__name__
__module____qualname__r   r   r   r"   __classcell__r   r   r   r   r	   
   s
    r	   c                       sh   e Zd Z fddZdd Zeeej	dk de
ddd	 Zeeej	dk dd
d Z  ZS )TestContextAPIc                    r
   r   r   tearDownr   r   r   r   r   r   r)         
zTestContextAPI.tearDownc                 C   s   zt   }W n ty   | d Y nw | |jtj | 	|j|d  | |j
tj | 	|j
|d  | |j|j
 d S )Nz/EMM Plugin does not implement get_memory_info()r      )r   current_contextget_memory_infoNotImplementedErrorskipTestassertIsInstancefreenumbersNumberr   totalassertLessEqual)r   memr   r   r   test_context_memory#   s   z"TestContextAPI.test_context_memory   zneed more than 1 gpusCUDA HW requiredc              	   C   s   t jdd }t jd * | t}|  W d    n1 s w   Y  | dt|j W d    d S 1 s9w   Y  d S )Nc                   S   s0   t jd 	 W d    d S 1 sw   Y  d S Nr+   )r   r   r   r   r   r   
switch_gpu5   s   "z@TestContextAPI.test_forbidden_context_switch.<locals>.switch_gpur   zCannot switch CUDA-context.)r   require_contextr   assertRaisesRuntimeErrorassertInstr	exception)r   r;   raisesr   r   r   test_forbidden_context_switch1   s   
"z,TestContextAPI.test_forbidden_context_switchc                 C   sL   dd }t jd  | }W d    n1 sw   Y  | t|d d S )Nc                   S   s:   t jd  t  jjW  d    S 1 sw   Y  d S r:   )r   r   r,   devicer   r   r   r   r   r;   B   s   
$z?TestContextAPI.test_accepted_context_switch.<locals>.switch_gpur   r+   )r   r   r   r   )r   r;   devidr   r   r   test_accepted_context_switch@   s
   z+TestContextAPI.test_accepted_context_switch)r#   r$   r%   r)   r7   r   skipIfr   r   r   r   rC   rF   r&   r   r   r   r   r'      s    r'   r9   c                       s<   e Zd Z fddZdd fddZdd Zd	d
 Z  ZS )Test3rdPartyContextc                    r
   r   r(   r   r   r   r   r)   M   r*   zTest3rdPartyContext.tearDownc                   C   s   d S r   r   r   r   r   r   <lambda>Q   s    zTest3rdPartyContext.<lambda>c              	   C   s   t j }t jrt jd}||}nd}t j }|t|| z:t t	
| |}|  t }t jrC| t|jt|j n
| |jj|jj |  W |  || d S |  || w r   )r   USE_NV_BINDINGbindingCUdevicecuDevicePrimaryCtxRetaindrvapi
cu_contextr   Contextweakrefproxypushr   r,   r   r   handlevaluepopcuDevicePrimaryCtxRelease)r   
extra_work
the_driverdevhctxctxmy_ctxr   r   r   test_attached_primaryQ   s&   
z)Test3rdPartyContext.test_attached_primaryc              
   C   s   t j }t jrd}t jd}|||}nt j }|t|dd z6zt	  W n t
yD } z| dt| W Y d }~nd }~ww | d W || d S W || d S || w )Nr   z1Numba cannot operate on non-primary CUDA context zNo RuntimeError raised)r   rJ   rK   rL   cuCtxCreaterN   rO   r   r   r,   r>   r?   r@   failcuCtxDestroy)r   rY   flagsrZ   r[   er   r   r   test_attached_non_primaryk   s&   
z-Test3rdPartyContext.test_attached_non_primaryc                    s    fdd}  | d S )Nc                     sP   ddl m}  | jdd }| d}|d |  t| ttd d S )Nr   r   c                 S   s   t | jD ]}|| |< qd S r   )rangesize)air   r   r   foo   s   
zUTest3rdPartyContext.test_cudajit_in_attached_primary_context.<locals>.do.<locals>.foo
   )r+   r+   )numbar   jitdevice_arrayr   r    copy_to_hostre   )r   ri   rg   r   r   r   do   s   

 zHTest3rdPartyContext.test_cudajit_in_attached_primary_context.<locals>.do)r^   )r   ro   r   r   r   (test_cudajit_in_attached_primary_context   s   z<Test3rdPartyContext.test_cudajit_in_attached_primary_context)r#   r$   r%   r)   r^   rd   rp   r&   r   r   r   r   rH   K   s
    rH   __main__)r2   ctypesr   rQ   rk   r   numba.cuda.testingr   r   r   numba.cuda.cudadrvr   r	   r'   rH   r#   mainr   r   r   r   <module>   s    .D