o
    ۾i-                     @   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y7   dZY nw 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rae  dS 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}|| |D ]	}tj|| q/W d    d S 1 sDw   Y  d S )Nc                 S   s   | d  d7  < d S )Nr       xr	   r	   ]/home/ubuntu/.local/lib/python3.10/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>   s    z.check_concurrent_compiling.<locals>.<listcomp>r   r   r      )max_workers)	r   jitranger   r   r   maptestingassert_equal)r   arraysexpectedearyr	   r   r   check_concurrent_compiling   s   

"r"   c              
   C   sD   zt   W n   t }| ddd|g Y d S | 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|	  w |d ur1t
|| |jdd d S )Nspawntargetargsr   ztest failed in child process)r   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   r1   )r   r   
zeros_liker   r   	threadingThreadr8   r(   r   r   )r.   rD   rB   rC   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-   )copy_to_device)dstsrcr	   r	   r   d2dX   s   zITestMultiThreadCompiling.test_invalid_context_error_with_d2d.<locals>.d2dd   )dtyper1   )r   r   r   r   zerosshaperP   rF   rG   r8   r(   r   r   rA   )r.   rN   rB   commonrH   rI   r	   r	   r   #test_invalid_context_error_with_d2dW   s   

z<TestMultiThreadCompiling.test_invalid_context_error_with_d2dN)__name__
__module____qualname__unittestskipIfhas_concurrent_futuresr/   has_mp_get_contextr@   rJ   rT   r	   r	   r	   r   r,   1   s    

r,   __main__)r%   rF   r5   numpyr   numbar   numba.cuda.testingr   r   r   rX   concurrent.futuresr   ImportErrorrZ   hasattrr[   r"   r+   r,   rU   mainr	   r	   r	   r   <module>   s,    
1