o
    ۾i                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZ ejs<G dd dejZG dd	 d	eZed
G dd de
Zed
G dd de
Zedkr^e	  dS dS )    N)cuda)config)unittestCUDATestCaseskip_on_cudasim)
linux_onlyc                       sT   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	e
dd Z  ZS )DeviceOnlyEMMPluginz
        Dummy EMM Plugin implementation for testing. It memorises which plugin
        API methods have been called so that the tests can check that Numba
        called into the plugin as expected.
        c                    s@   t  j|i | i | _d| _d| _d| _d| _d| _d| _d S )Nr   F)	super__init__allocationscountinitializedmemalloc_calledreset_calledget_memory_info_calledget_ipc_handle_called)selfargskwargs	__class__ ]/home/ubuntu/.local/lib/python3.10/site-packages/numba/cuda/tests/cudadrv/test_emm_plugins.pyr
      s   
zDeviceOnlyEMMPlugin.__init__c                    sr   | j stdd| _|  jd7  _| j || j < | j fdd}t| j}t	 }t
jjj||||dS )Nz!memalloc called before initializeT   c                      s
    = d S Nr   r   alloc_countfinalizer_allocsr   r   	finalizer4   s   
z/DeviceOnlyEMMPlugin.memalloc.<locals>.finalizer)r   )r   RuntimeErrorr   r   r   weakrefproxycontextctypesc_void_pr   cudadrvdriverAutoFreePointer)r   sizer   ctxptrr   r   r   memalloc    s   

zDeviceOnlyEMMPlugin.memallocc                 C   
   d| _ d S NT)r   r   r   r   r   
initialize>   s   
zDeviceOnlyEMMPlugin.initializec                 C   r,   r-   )r   r.   r   r   r   resetB   s   
zDeviceOnlyEMMPlugin.resetc                 C   s   d| _ tjdddS )NT    @   )freetotal)r   r   
MemoryInfor.   r   r   r   get_memory_infoK   s   z#DeviceOnlyEMMPlugin.get_memory_infoc                 C   s   d| _ d|jj S )NTzDummy IPC handle for alloc %s)r   device_pointervalue)r   memoryr   r   r   get_ipc_handleP   s   z"DeviceOnlyEMMPlugin.get_ipc_handlec                 C      dS )Nr   r   r.   r   r   r   interface_versionW   s   z%DeviceOnlyEMMPlugin.interface_version)__name__
__module____qualname____doc__r
   r+   r/   r0   r6   r:   propertyr<   __classcell__r   r   r   r   r      s    	r   c                   @   s   e Zd ZdZedd ZdS )BadVersionEMMPluginz?A plugin that claims to implement a different interface versionc                 C   r;   )N   r   r.   r   r   r   r<   _   s   z%BadVersionEMMPlugin.interface_versionN)r=   r>   r?   r@   rA   r<   r   r   r   r   rC   \   s    rC   z+EMM Plugins not supported on CUDA simulatorc                       sX   e Zd ZdZ fddZ fddZdd Zdd	 Zd
d Zdd Z	e
dd Z  ZS )TestDeviceOnlyEMMPluginzu
    Tests that the API of an EMM Plugin that implements device allocations
    only is used correctly by Numba.
    c                    s    t    t  tt d S r   )r	   setUpr   closeset_memory_managerr   r.   r   r   r   rF   k      
zTestDeviceOnlyEMMPlugin.setUpc                    s    t    t  d tjj_d S r   )r	   tearDownr   rG   r%   r&   _memory_managerr.   r   r   r   rJ   q   rI   z TestDeviceOnlyEMMPlugin.tearDownc                 C   s   t  j}td}t |}| |j | |j	d | |j
d |j td}t |}| |j	d | |j
d |j ~| d|j
 | d|j
 ~| d|j
 d S )N
   r      rD   )r   current_contextmemory_managernparangedevice_array_like
assertTruer   assertEqualr   r   nbytesassertNotInassertIn)r   mgrarr_1d_arr_1arr_2d_arr_2r   r   r   test_memallocw   s   




z%TestDeviceOnlyEMMPlugin.test_memallocc                 C   s   |  t jj d S r   )rS   r   rN   rO   r   r.   r   r   r   test_initialized_in_context   s   z3TestDeviceOnlyEMMPlugin.test_initialized_in_contextc                 C   s"   t  }|  | |jj d S r   )r   rN   r0   rS   rO   r   )r   r)   r   r   r   
test_reset   s   z"TestDeviceOnlyEMMPlugin.test_resetc                 C   s>   t  }| }| |jj | |jd | |jd d S )Nr1   r2   )	r   rN   r6   rS   rO   r   rT   r3   r4   )r   r)   meminfor   r   r   test_get_memory_info   s
   z,TestDeviceOnlyEMMPlugin.test_get_memory_infoc                 C   sD   t d}t|}| }t }| |jj | 	d|j
 d S )NrD   zDummy IPC handle for alloc 1)rP   rQ   r   rR   r:   rN   rS   rO   r   rW   _ipc_handle)r   arrd_arripchr)   r   r   r   test_get_ipc_handle   s   

z+TestDeviceOnlyEMMPlugin.test_get_ipc_handle)r=   r>   r?   r@   rF   rJ   r]   r^   r_   ra   r   rf   rB   r   r   r   r   rE   d   s    rE   c                   @   s   e Zd ZdZdd ZdS )TestBadEMMPluginVersionzV
    Ensure that Numba rejects EMM Plugins with incompatible version
    numbers.
    c                 C   sJ   |  t}tt W d    n1 sw   Y  | dt|j d S )Nzversion 1 required)assertRaisesr   r   rH   rC   rW   str	exception)r   raisesr   r   r   test_bad_plugin_version   s   z/TestBadEMMPluginVersion.test_bad_plugin_versionN)r=   r>   r?   r@   rl   r   r   r   r   rg      s    rg   __main__)r#   numpyrP   r    numbar   
numba.corer   numba.cuda.testingr   r   r   numba.tests.supportr   ENABLE_CUDASIMHostOnlyCUDAMemoryManagerr   rC   rE   rg   r=   mainr   r   r   r   <module>   s"    QM