a
    þ
þd-  ã                   @   sÂ   d dl Z d dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
 d dlZzd dlmZ W n eyn   dZY n0 dZeedƒZdd	„ Zd
d„ Ze	dƒedƒG dd„ de
ƒƒƒZedkr¾e ¡  dS )é    N)Úcuda)Úskip_on_cudasimÚskip_under_cuda_memcheckÚCUDATestCase)ÚThreadPoolExecutorFTÚget_contextc                     s”   t jdd„ ƒ‰ ‡ fdd„} dd„ tdƒD ƒ}t d¡}|d  d	7  < td
d0}| | |¡D ]}tj ||¡ q^W d   ƒ n1 s†0    Y  d S )Nc                 S   s   | d  d7  < d S )Nr   é   © ©Úxr	   r	   úr/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/numba/cuda/tests/cudapy/test_multithreads.pyÚfoo   s    z'check_concurrent_compiling.<locals>.fooc                    s   ˆ d | ƒ | S )N)r   r   r	   r
   ©r   r	   r   Úuse_foo   s    z+check_concurrent_compiling.<locals>.use_fooc                 S   s   g | ]}t  t d ¡¡‘qS )é
   )r   Ú	to_deviceÚnpÚarange)Ú.0Úir	   r	   r   Ú
<listcomp>   ó    z.check_concurrent_compiling.<locals>.<listcomp>r   r   r   é   )Úmax_workers)	r   ZjitÚranger   r   r   ÚmapÚtestingÚassert_equal)r   ZarraysÚexpectedÚeZaryr	   r   r   Úcheck_concurrent_compiling   s    

r    c              
   C   sD   z
t ƒ  W n*   t ¡ }|  d dd|g¡¡ Y n0 |  d ¡ d S )NÚ
Ú zP================================================================================)r    Ú	tracebackÚ
format_excÚputÚjoin)ÚqÚmsgr	   r	   r   Úspawn_process_entry&   s    
r)   zHangs cuda-memcheckzdisabled for cudasimc                   @   sH   e Zd Ze e d¡dd„ ƒZe e d¡dd„ ƒZdd„ Z	d	d
„ Z
dS )ÚTestMultiThreadCompilingzno concurrent.futuresc                 C   s
   t ƒ  d S ©N)r    )Úselfr	   r	   r   Útest_concurrent_compiling5   s    z2TestMultiThreadCompiling.test_concurrent_compilingzno multiprocessing.get_contextc                 C   sv   t  ¡  t d¡}| ¡ }|jt|fd}| ¡  z| ¡ }W | 	¡  n
| 	¡  0 |d urbt
|ƒ‚|  |jdd¡ d S )NÚspawn©ÚtargetÚargsr   ztest failed in child process)r   Zget_current_deviceÚmultiprocessingr   ÚQueueÚProcessr)   ÚstartÚgetr&   ÚAssertionErrorÚassertEqualÚexitcode)r,   Úctxr'   ÚpÚerrr	   r	   r   Ú!test_spawn_concurrent_compilation9   s    

z:TestMultiThreadCompiling.test_spawn_concurrent_compilationc                 C   s\   dd„ }t  dd¡}t  |¡}t |¡}tj|||gd}| ¡  | ¡  t j	 
||¡ d S )Nc                 S   s   |   ¡ |d d …< d S r+   )Úcopy_to_host)ÚarrÚoutr	   r	   r   Úd2hL   s    zITestMultiThreadCompiling.test_invalid_context_error_with_d2h.<locals>.d2hr   r   r/   )r   r   Z
zeros_liker   r   Ú	threadingÚThreadr5   r&   r   r   )r,   rA   r?   r@   ÚdarrÚthr	   r	   r   Ú#test_invalid_context_error_with_d2hK   s    

z<TestMultiThreadCompiling.test_invalid_context_error_with_d2hc                 C   sl   dd„ }t  d¡}t |¡}t t j|j|jd¡}tj|||gd}| 	¡  | 
¡  t j | ¡ |¡ d S )Nc                 S   s   |   |¡ d S r+   )Zcopy_to_device)ÚdstÚsrcr	   r	   r   Úd2dX   s    zITestMultiThreadCompiling.test_invalid_context_error_with_d2d.<locals>.d2déd   )Údtyper/   )r   r   r   r   ÚzerosÚshaperK   rB   rC   r5   r&   r   r   r>   )r,   rI   r?   ÚcommonrD   rE   r	   r	   r   Ú#test_invalid_context_error_with_d2dW   s    

z<TestMultiThreadCompiling.test_invalid_context_error_with_d2dN)Ú__name__Ú
__module__Ú__qualname__ÚunittestZskipIfÚhas_concurrent_futuresr-   Úhas_mp_get_contextr=   rF   rO   r	   r	   r	   r   r*   1   s   

r*   Ú__main__)r#   rB   r2   Únumpyr   Znumbar   Znumba.cuda.testingr   r   r   rS   Úconcurrent.futuresr   ÚImportErrorrT   ÚhasattrrU   r    r)   r*   rP   Úmainr	   r	   r	   r   Ú<module>   s&   

1