U
    ?h¶  ã                   @   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sxG 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 )é    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 | _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__© ú[/var/www/html/venv/lib/python3.8/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   ©Zalloc_countZfinalizer_allocsr   r   Ú	finalizer4   s    z/DeviceOnlyEMMPlugin.memalloc.<locals>.finalizer)r   )r   ÚRuntimeErrorr   r   r   ÚweakrefÚproxyÚcontextÚctypesZc_void_pr   ÚcudadrvÚdriverZAutoFreePointer)r   Úsizer   ÚctxZptrr   r   r   Úmemalloc    s    

ÿzDeviceOnlyEMMPlugin.memallocc                 C   s
   d| _ d S ©NT)r   ©r   r   r   r   Ú
initialize>   s    zDeviceOnlyEMMPlugin.initializec                 C   s
   d| _ d S 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   Z
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   Zdevice_pointerÚvalue)r   Zmemoryr   r   r   Úget_ipc_handleP   s    z"DeviceOnlyEMMPlugin.get_ipc_handlec                 C   s   dS )Nr   r   r(   r   r   r   Úinterface_versionW   s    z%DeviceOnlyEMMPlugin.interface_version)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r&   r)   r*   r/   r1   Úpropertyr2   Ú__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   s   dS )Né   r   r(   r   r   r   r2   _   s    z%BadVersionEMMPlugin.interface_versionN)r3   r4   r5   r6   r7   r2   r   r   r   r   r9   \   s   r9   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   r<   k   s    
zTestDeviceOnlyEMMPlugin.setUpc                    s    t ƒ  ¡  t ¡  d tjj_d S r   )r	   ÚtearDownr   r=   r"   r#   Z_memory_managerr(   r   r   r   r?   q   s    
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   é   r:   )r   Úcurrent_contextÚmemory_managerÚnpÚarangeÚdevice_array_likeÚ
assertTruer   ÚassertEqualr   r   ÚnbytesZassertNotInÚassertIn)r   ZmgrZarr_1Zd_arr_1Zarr_2Zd_arr_2r   r   r   Útest_memallocw   s    




z%TestDeviceOnlyEMMPlugin.test_memallocc                 C   s   |   t ¡ jj¡ d S r   )rG   r   rB   rC   r   r(   r   r   r   Útest_initialized_in_context”   s    z3TestDeviceOnlyEMMPlugin.test_initialized_in_contextc                 C   s"   t  ¡ }| ¡  |  |jj¡ d S r   )r   rB   r*   rG   rC   r   )r   r%   r   r   r   Ú
test_reset™   s    z"TestDeviceOnlyEMMPlugin.test_resetc                 C   s>   t  ¡ }| ¡ }|  |jj¡ |  |jd¡ |  |jd¡ d S )Nr+   r,   )	r   rB   r/   rG   rC   r   rH   r-   r.   )r   r%   Z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 )Nr:   zDummy IPC handle for alloc 1)rD   rE   r   rF   r1   rB   rG   rC   r   rJ   Z_ipc_handle)r   ZarrZd_arrZipchr%   r   r   r   Útest_get_ipc_handle¥   s    

z+TestDeviceOnlyEMMPlugin.test_get_ipc_handle)r3   r4   r5   r6   r<   r?   rK   rL   rM   rN   r   rO   r8   r   r   r   r   r;   d   s   r;   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   s6   |   t¡}t t¡ W 5 Q R X |  dt|jƒ¡ d S )Nzversion 1 required)ZassertRaisesr   r   r>   r9   rJ   ÚstrÚ	exception)r   Zraisesr   r   r   Útest_bad_plugin_version¹   s    z/TestBadEMMPluginVersion.test_bad_plugin_versionN)r3   r4   r5   r6   rS   r   r   r   r   rP   ²   s   rP   Ú__main__)r!   ÚnumpyrD   r   Znumbar   Z
numba.corer   Znumba.cuda.testingr   r   r   Znumba.tests.supportr   ZENABLE_CUDASIMZHostOnlyCUDAMemoryManagerr   r9   r;   rP   r3   Úmainr   r   r   r   Ú<module>   s   QM