
    sh                     >   S SK r S SKrS SKrS SKJr  S SKJr  S SKJ	r	J
r
Jr  S SKJr  \R                  (       d    " S S\R                  5      r " S S	\5      r\" S
5       " S S\
5      5       r\" S
5       " S S\
5      5       r\S:X  a  \	R*                  " 5         gg)    N)cuda)config)unittestCUDATestCaseskip_on_cudasim)
linux_onlyc                   Z   ^  \ rS rSrSrU 4S jrS rS rS rS r	S r
\S	 5       rS
rU =r$ )DeviceOnlyEMMPlugin   z
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                    > [         TU ]  " U0 UD6  0 U l        SU l        SU l        SU l        SU l        SU l        SU l        g )Nr   F)	super__init__allocationscountinitializedmemalloc_calledreset_calledget_memory_info_calledget_ipc_handle_called)selfargskwargs	__class__s      }/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/numba/cuda/tests/cudadrv/test_emm_plugins.pyr   DeviceOnlyEMMPlugin.__init__   sP    Gd-f-  "DDJ  %D#(D  %D*/D').D&    c                   ^^ U R                   (       d  [        S5      eSU l        U =R                  S-  sl        U R                  mXR                  T'   U R                  mUU4S jn[
        R                  " U R                  5      n[        R                  " T5      n[        R                  R                  R                  X4UUS9$ )Nz!memalloc called before initializeT   c                     > TT 	 g N )alloc_countfinalizer_allocss   r   	finalizer/DeviceOnlyEMMPlugin.memalloc.<locals>.finalizer4   s    $[1r   )r$   )r   RuntimeErrorr   r   r   weakrefproxycontextctypesc_void_pr   cudadrvdriverAutoFreePointer)r   sizer$   ctxptrr"   r#   s        @@r   memallocDeviceOnlyEMMPlugin.memalloc    s     ##"#FGG#'D  JJ!OJ**K,0[)  $//2
 ---C//+.C<<&&66sAJ 7 L Lr   c                     SU l         g NT)r   r   s    r   
initializeDeviceOnlyEMMPlugin.initialize>   s
    #Dr   c                     SU l         g r5   )r   r6   s    r   resetDeviceOnlyEMMPlugin.resetB   s     !%Dr   c                 :    SU l         [        R                  " SSS9$ )NT    @   )freetotal)r   r   
MemoryInfor6   s    r   get_memory_info#DeviceOnlyEMMPlugin.get_memory_infoK   s    *.D'??"55r   c                 B    SU l         SUR                  R                  -  $ )NTzDummy IPC handle for alloc %s)r   device_pointervalue)r   memorys     r   get_ipc_handle"DeviceOnlyEMMPlugin.get_ipc_handleP   s#     *.D&2V5J5J5P5PPPr   c                     g)Nr   r!   r6   s    r   interface_version%DeviceOnlyEMMPlugin.interface_versionW   s     r   )r   r   r   r   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r2   r7   r:   rB   rH   propertyrK   __static_attributes____classcell__r   s   @r   r
   r
      s<    		/	L<	$	%	6
	Q 
	 
	r   r
   c                   (    \ rS rSrSr\S 5       rSrg)BadVersionEMMPlugin\   z?A plugin that claims to implement a different interface versionc                     g)N   r!   r6   s    r   rK   %BadVersionEMMPlugin.interface_version_   s    r   r!   N)rM   rN   rO   rP   rQ   rR   rK   rS   r!   r   r   rW   rW   \   s    M		 
	r   rW   z+EMM Plugins not supported on CUDA simulatorc                   `   ^  \ rS rSrSrU 4S jrU 4S jrS rS rS r	S r
\S	 5       rS
rU =r$ )TestDeviceOnlyEMMPlugind   zi
Tests that the API of an EMM Plugin that implements device allocations
only is used correctly by Numba.
c                    > [         TU ]  5         [        R                  " 5         [        R                  " [
        5        g r    )r   setUpr   closeset_memory_managerr
   r   r   s    r   r`   TestDeviceOnlyEMMPlugin.setUpk   s$    

 34r   c                    > [         TU ]  5         [        R                  " 5         S [        R                  R
                  l        g r    )r   tearDownr   ra   r,   r-   _memory_managerrc   s    r   rf    TestDeviceOnlyEMMPlugin.tearDownq   s)    

.2+r   c                    [         R                  " 5       R                  n[        R                  " S5      n[         R
                  " U5      nU R                  UR                  5        U R                  UR                  S5        U R                  UR                  S   UR                  5        [        R                  " S5      n[         R
                  " U5      nU R                  UR                  S5        U R                  UR                  S   UR                  5        AU R                  SUR                  5        U R                  SUR                  5        AU R                  SUR                  5        g )N
   r      rZ   )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_2s         r   test_memalloc%TestDeviceOnlyEMMPlugin.test_memallocw   s   ""$33 		"((/++,A&+U\\: 		!((/A&+U\\: COO,a) COO,r   c                 t    U R                  [        R                  " 5       R                  R                  5        g r    )rq   r   rl   rm   r   r6   s    r   test_initialized_in_context3TestDeviceOnlyEMMPlugin.test_initialized_in_context   s%     	,,.==IIJr   c                     [         R                  " 5       nUR                  5         U R                  UR                  R
                  5        g r    )r   rl   r:   rq   rm   r   )r   r0   s     r   
test_reset"TestDeviceOnlyEMMPlugin.test_reset   s1    ""$		**778r   c                    [         R                  " 5       nUR                  5       nU R                  UR                  R
                  5        U R                  UR                  S5        U R                  UR                  S5        g )Nr=   r>   )	r   rl   rB   rq   rm   r   rr   r?   r@   )r   r0   meminfos      r   test_get_memory_info,TestDeviceOnlyEMMPlugin.test_get_memory_info   s^    ""$%%'**AABr*+r   c                 (   [         R                  " S5      n[        R                  " U5      nUR	                  5       n[        R
                  " 5       nU R                  UR                  R                  5        U R                  SUR                  5        g )NrZ   zDummy IPC handle for alloc 1)rn   ro   r   rp   rH   rl   rq   rm   r   ru   _ipc_handle)r   arrd_arripchr0   s        r   test_get_ipc_handle+TestDeviceOnlyEMMPlugin.test_get_ipc_handle   sj    
 iil&&s+##%""$**@@A4d6F6FGr   r!   )rM   rN   rO   rP   rQ   r`   rf   r{   r~   r   r   r   r   rS   rT   rU   s   @r   r]   r]   d   s=    
53-:K
9
, 	H 	Hr   r]   c                       \ rS rSrSrS rSrg)TestBadEMMPluginVersion   zJ
Ensure that Numba rejects EMM Plugins with incompatible version
numbers.
c                     U R                  [        5       n[        R                  " [        5        S S S 5        U R                  S[        WR                  5      5        g ! , (       d  f       N4= f)Nzversion 1 required)assertRaisesr&   r   rb   rW   ru   str	exception)r   raisess     r   test_bad_plugin_version/TestBadEMMPluginVersion.test_bad_plugin_version   sI    |,##$78 -*C0@0@,AB -,s   A
A-r!   N)rM   rN   rO   rP   rQ   r   rS   r!   r   r   r   r      s    
Cr   r   __main__)r*   numpyrn   r'   numbar   
numba.corer   numba.cuda.testingr   r   r   numba.tests.supportr   ENABLE_CUDASIMHostOnlyCUDAMemoryManagerr
   rW   r]   r   rM   mainr!   r   r   <module>r      s         F F *Od<< Ob1  >?JHl JH @JHZ >?	Cl 	C @	C zMMO r   