
    J/Ph                         d dl mZ d dlZd dlmZ d dlmZmZm	Z	m
Z
 d dlmZ d dlmZ  ed           e	d           G d	 d
e
                                  Z ed           e	d           G d de
                                  Z G d de
          Z ed           G d de
                      Zedk    r ej                     dS dS )    )contextmanagerN)cuda)unittestskip_on_cudasimskip_if_external_memmgrCUDATestCase)captured_stderr)configznot supported on CUDASIMz0Deallocation specific to Numba memory managementc                       e Zd Zd Zd ZdS )TestDeallocationc                    t          j                    j        j        }|                                 |                     t          |          d           t          t          j	                  D ]N}t          j
        t          j        d                     |                     t          |          |dz              Ot          j
        t          j        d                     |                     t          |          d           d S )Nr      )r   current_contextmemory_managerdeallocationsclearassertEquallenranger
   CUDA_DEALLOCS_COUNT	to_devicenparange)selfdeallocsis      k/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/cuda/tests/cudadrv/test_deallocations.pytest_max_pending_countz'TestDeallocation.test_max_pending_count   s    '))8FX***v122 	3 	3AN29Q<<(((S]]AE2222ry||$$$X*****    c                    t          j                    }|j        j        }|                                 |                     t          |          d           |                                }d}t          j	        }	 ||j
        z  t          _	        |                     |j        |d           t          j        t          j        |dz  t          j                             |                     t          |          d           t          j        t          j        |j        |j        z
  t          j                             |                     t          |          d           t          j        t          j        dt          j                             |                     t          |          d           |t          _	        d S # |t          _	        w xY w)Nr   i@B r   )delta   )dtype)r   r   r   r   r   r   r   get_memory_infor
   CUDA_DEALLOCS_RATIOtotalassertAlmostEqual_max_pending_bytesr   r   onesint8_size)r   ctxr   mimax_pending	old_ratios         r   test_max_pending_bytesz'TestDeallocation.test_max_pending_bytes   s   "$$%3X***  "".		3)4rx)?F& ""8#>)* # , , ,
 N27;!#327CCCDDDS]]A... N278#>#+>$29;B B B C C CS]]A... N271BG444555S]]A... *3F&&&F&2222s   :E G GN)__name__
__module____qualname__r   r0    r   r   r   r      s2        + + +#3 #3 #3 #3 #3r   r   z&defer_cleanup has no effect in CUDASIMc                        e Zd Zd Zd Zd ZdS )TestDeferCleanupc                    t          j        d          }t          j        |          }t          j                    j        j        }|                                 |                     t          |          d           t          j
                    5  t          j        |          }~|                     t          |          d           ~|                     t          |          d           |                                 |                     t          |          d           d d d            n# 1 swxY w Y   |                                 |                     t          |          d           d S N   r   r   r"   r   r   r   r   r   r   r   r   r   r   defer_cleanupr   harrdarr1r   darr2s        r   
test_basiczTestDeferCleanup.test_basicE   so   y||t$$'))8FX***!! 	/ 	/N4((ES]]A...S]]A...NNS]]A...	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	X*****s   BD00D47D4c                    t          j        d          }t          j        |          }t          j                    j        j        }|                                 |                     t          |          d           t          j
                    5  t          j
                    5  t          j        |          }~|                     t          |          d           ~|                     t          |          d           |                                 |                     t          |          d           d d d            n# 1 swxY w Y   |                                 |                     t          |          d           d d d            n# 1 swxY w Y   |                                 |                     t          |          d           d S r8   r:   r<   s        r   test_nestedzTestDeferCleanup.test_nestedW   s   y||t$$'))8FX***!! 
	/ 
	/#%% 3 3t,,  X222  X222     X2223 3 3 3 3 3 3 3 3 3 3 3 3 3 3 NNS]]A...
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 
	/ 	X*****s7   F$BE8FE	FE	:FFFc                    t          j        d          }t          j        |          }t          j                    j        j        }|                                 |                     t          |          d            G d dt                    }|                     |          5  t          j                    5  t          j        |          }~|                     t          |          d           |                                 |                     t          |          d           |# 1 swxY w Y   	 d d d            n# 1 swxY w Y   |                                 |                     t          |          d           ~|                     t          |          d           |                                 |                     t          |          d           d S )Nr9   r   c                       e Zd ZdS )4TestDeferCleanup.test_exception.<locals>.CustomErrorNr1   r2   r3   r4   r   r   CustomErrorrE   s           Dr   rG   r   )r   r   r   r   r   r   r   r   r   r   	ExceptionassertRaisesr;   )r   r=   r>   r   rG   r?   s         r   test_exceptionzTestDeferCleanup.test_exceptionl   s   y||t$$'))8FX***	 	 	 	 	) 	 	 	 {++ 	" 	"#%% " "t,,  X222     X222!!" " " " " " " " "	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	X***X***X*****s1   %E9A2D++D/	/E2D/	3EE
EN)r1   r2   r3   r@   rB   rK   r4   r   r   r6   r6   B   sA        + + +$+ + +*+ + + + +r   r6   c                       e Zd Zd ZdS )TestDeferCleanupAvailc                 `    t          j                    5  	 d d d            d S # 1 swxY w Y   d S N)r   r;   )r   s    r   test_context_managerz*TestDeferCleanupAvail.test_context_manager   sx    !! 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   #''N)r1   r2   r3   rP   r4   r   r   rM   rM      s#            r   rM   c                   X    e Zd ZdZed             Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 ZdS )TestDelzJ
    Ensure resources are deleted properly without ignored exception.
    c              #      K   t                      5 }d V  |j                                         d d d            n# 1 swxY w Y   |                     |                                           d S rO   )r	   r   r   assertFalsegetvalue)r   r,   caps      r   check_ignored_exceptionzTestDel.check_ignored_exception   s       	&#EEE##%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	(((((s   ;??c                     t          j                    }|                                }|                     |          5  ~d d d            d S # 1 swxY w Y   d S rO   )r   r   create_streamrW   )r   r,   streams      r   test_streamzTestDel.test_stream   s    "$$""$$))#.. 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   AAAc                     t          j                    }|                                }|                     |          5  ~d d d            d S # 1 swxY w Y   d S rO   )r   r   create_eventrW   )r   r,   events      r   
test_eventzTestDel.test_event   s    "$$  ""))#.. 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	r\   c                     t          j                    }|                    d          }|                     |          5  ~d d d            d S # 1 swxY w Y   d S N    r   r   memhostallocrW   r   r,   mems      r   test_pinned_memoryzTestDel.test_pinned_memory   s    "$$r""))#.. 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   AAAc                     t          j                    }|                    dd          }|                     |          5  ~d d d            d S # 1 swxY w Y   d S )Nrc   T)mappedrd   rf   s      r   test_mapped_memoryzTestDel.test_mapped_memory   s    "$$r$//))#.. 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s    AAAc                     t          j                    }|                    d          }|                     |          5  ~d d d            d S # 1 swxY w Y   d S rb   )r   r   memallocrW   rf   s      r   test_device_memoryzTestDel.test_device_memory   s    "$$ll2))#.. 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	ri   c                     t          j                    }|                    d          }|                     |          5  ~d d d            d S # 1 swxY w Y   d S rb   )r   r   memallocmanagedrW   rf   s      r   test_managed_memoryzTestDel.test_managed_memory   s    "$$!!"%%))#.. 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	ri   c                 ~    G d dt                     }t          j        d          }t          j                    }|j                                         |                     |          5  t          j        |          5  	 d d d            n# 1 swxY w Y   t          j        |          5  	 d d d            n# 1 swxY w Y   t          j	                    5  t          j        |          5  	 d d d            n# 1 swxY w Y   t          j        |          5  	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   	 t          j        |          5  |# 1 swxY w Y   n8# |$ r0 t          j        |          5  	 d d d            n# 1 swxY w Y   Y nw xY wd d d            d S # 1 swxY w Y   d S )Nc                       e Zd ZdS );TestDel.test_pinned_contextmanager.<locals>.PinnedExceptionNrF   r4   r   r   PinnedExceptionru      rH   r   rv   r   )
rI   r   zerosr   r   r   r   rW   pinnedr;   )r   rv   arrr,   s       r   test_pinned_contextmanagerz"TestDel.test_pinned_contextmanager      	 	 	 	 	i 	 	 	 hqkk"$$!!!))#.. 	 	S!!                S!!                 #%%  [%%                [%%                              [%% * *))* * * * * * * * *"   [%%                !	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	,  )F2>B F2B	F2B	F2+B9-F29B=	=F2 B=	F2D>,C:.D>:C>>D>C>D>D'D>'D++D>.D+/D>2F2>E	F2E	F2
E-E!!E%	%E-(E%	)E-,F2-F"FF"FF"FF"F2!F""F22F69F6c                 ~    G d dt                     }t          j        d          }t          j                    }|j                                         |                     |          5  t          j        |          5  	 d d d            n# 1 swxY w Y   t          j        |          5  	 d d d            n# 1 swxY w Y   t          j	                    5  t          j        |          5  	 d d d            n# 1 swxY w Y   t          j        |          5  	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   	 t          j        |          5  |# 1 swxY w Y   n8# |$ r0 t          j        |          5  	 d d d            n# 1 swxY w Y   Y nw xY wd d d            d S # 1 swxY w Y   d S )Nc                       e Zd ZdS );TestDel.test_mapped_contextmanager.<locals>.MappedExceptionNrF   r4   r   r   MappedExceptionr      rH   r   r   r   )
rI   r   rw   r   r   r   r   rW   rk   r;   )r   r   ry   r,   s       r   test_mapped_contextmanagerz"TestDel.test_mapped_contextmanager   r{   r|   N)r1   r2   r3   __doc__r   rW   r[   r`   rh   rl   ro   rr   rz   r   r4   r   r   rR   rR      s          ) ) ^)              :    r   rR   __main__)
contextlibr   numpyr   numbar   numba.cuda.testingr   r   r   r   numba.tests.supportr	   
numba.corer
   r   r6   rM   rR   r1   mainr4   r   r   <module>r      s   % % % % % %          G G G G G G G G G G G G / / / / / /       +,,KLL13 13 13 13 13| 13 13 ML -,13h 9::KLL?+ ?+ ?+ ?+ ?+| ?+ ?+ ML ;:?+D    L    +,,g g g g gl g g -,gT zHMOOOOO r   