a
    –§»a”'  ã                   @   sÜ   d dl mZ d dlmZ d dlZd dlZd dlZddlm	Z	m
Z
 ddlmZmZmZ ddlmZ dd	lmZmZ daed
d„ ƒZdd„ ZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ dejƒZG dd„ deƒZdS )é    )Úcontextmanager)ÚreduceNé   )ÚFakeCUDAArrayÚFakeWithinKernelCUDAArray)ÚDim3ÚFakeCUDAModuleÚswapped_cuda_moduleé   )Únormalize_kernel_dimensions)Úwrap_argÚArgHintc                 c   s.   t du sJ dƒ‚| a zdV  W da nda 0 dS )z*
    Push the current kernel context.
    Nz'conrrent simulated kernel not supported©Ú_kernel_context)Úmod© r   úU/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/cuda/simulator/kernel.pyÚ_push_kernel_context   s
    r   c                   C   s   t S )zT
    Get the current kernel context. This is usually done by a device function.
    r   r   r   r   r   Ú_get_kernel_context$   s    r   c                   @   s   e Zd ZdZdd„ ZdS )ÚFakeOverloadzE
    Used only to provide the max_cooperative_grid_blocks method
    c                 C   s   dS )Nr   r   )ÚselfZblockdimr   r   r   Úmax_cooperative_grid_blocks/   s    z(FakeOverload.max_cooperative_grid_blocksN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   +   s   r   c                   @   s   e Zd Zdd„ ZdS )ÚFakeOverloadDictc                 C   s   t ƒ S ©N)r   )r   Úkeyr   r   r   Ú__getitem__6   s    zFakeOverloadDict.__getitem__N)r   r   r   r   r   r   r   r   r   5   s   r   c                   @   sT   e Zd ZdZdg fdd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zddd„Z	e
dd„ ƒZdS )ÚFakeCUDAKernelz(
    Wraps a @cuda.jit-ed function.
    Fc                 C   s8   || _ || _|| _t|ƒ| _d | _d | _d| _d| _d S )Nr   )	ÚfnÚ_deviceZ	_fastmathÚlistÚ
extensionsÚgrid_dimÚ	block_dimÚstreamÚdynshared_size)r   r!   ZdeviceZfastmathr$   r   r   r   Ú__init__A   s    
zFakeCUDAKernel.__init__c           	   	      s  ˆj r>tˆjtƒ ƒ� ˆj|Ž W  d   ƒ S 1 s40    Y  tˆjˆjƒ\}}t||ˆjƒ}t	|ƒ�  g ‰‡‡fdd„‰ ‡ fdd„|D ƒ}tˆj|ƒ�@ t
j|Ž D ]$}tˆj||ƒ}|j|g|¢R Ž  q¤W d   ƒ n1 sÞ0    Y  ˆD ]
}|ƒ  qìW d   ƒ n1 �s0    Y  d S )Nc                    sŠ   t ‡ fdd„ˆjd | fƒ\}} t| tjƒrD| jdkrDt| ƒ ˆ ¡}n0t| tƒrZ|  ˆ ¡}nt| tj	ƒrpt
| ƒ}n| }t|t
ƒr†t|ƒS |S )Nc                    s   |j | dˆ dœŽS )Nr   )r'   Úretr)Zprepare_args)Zty_valÚ	extension)r*   r   r   Ú<lambda>`   s   ýz;FakeCUDAKernel.__call__.<locals>.fake_arg.<locals>.<lambda>r   )r   r$   Ú
isinstanceÚnpZndarrayÚndimr   Z	to_devicer   Úvoidr   r   )ÚargÚ_Úret)r*   r   r   r   Úfake_arg]   s    
ú	


z)FakeCUDAKernel.__call__.<locals>.fake_argc                    s   g | ]}ˆ |ƒ‘qS r   r   )Ú.0r1   )r4   r   r   Ú
<listcomp>t   ó    z+FakeCUDAKernel.__call__.<locals>.<listcomp>)r"   r	   r!   r   r   r%   r&   r   r(   r   r.   ÚndindexÚBlockManagerÚrun)	r   Úargsr%   r&   Zfake_cuda_moduleZ	fake_argsÚ
grid_pointZbmÚwbr   )r4   r*   r   r   Ú__call__M   s&    (ÿÿ
2zFakeCUDAKernel.__call__c                 C   s2   t |d d… Ž \| _| _t|ƒdkr.|d | _| S )Nr
   é   é   )r   r%   r&   Úlenr(   )r   Úconfigurationr   r   r   r   ~   s
    ÿ

zFakeCUDAKernel.__getitem__c                 C   s   d S r   r   ©r   r   r   r   Úbind‡   s    zFakeCUDAKernel.bindc                 G   s   | S r   r   )r   r;   r   r   r   Ú
specializeŠ   s    zFakeCUDAKernel.specializer   c                 C   s$   |dk rt d| ƒ‚| |d||f S )Nr   z0Can't create ForAll with negative task count: %sr   )Ú
ValueError)r   ZntasksZtpbr'   Z	sharedmemr   r   r   Úforall�   s
    ÿzFakeCUDAKernel.forallc                 C   s   t ƒ S r   )r   rC   r   r   r   Ú	overloads“   s    zFakeCUDAKernel.overloadsN)r   r   r   )r   r   r   r   r)   r>   r   rD   rE   rG   ÚpropertyrH   r   r   r   r   r    <   s   1	
r    c                       sT   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	dd„ Z
‡  ZS )ÚBlockThreadzG
    Manages the execution of a function for a single CUDA thread.
    c                    s„   t t| ƒj|d� t ¡ | _d| _|| _t|Ž | _	t|Ž | _
d | _d| _d| _t| jjŽ }| j
j|j| j
j|j| j
j    | _d S )N)ÚtargetFT)ÚsuperrJ   r)   Ú	threadingÚEventÚsyncthreads_eventÚsyncthreads_blockedÚ_managerr   ÚblockIdxÚ	threadIdxÚ	exceptionÚdaemonÚabortÚ
_block_dimÚxÚyÚzZ	thread_id)r   ÚfÚmanagerrR   rS   ZblockDim©Ú	__class__r   r   r)   ž   s    


ÿÿzBlockThread.__init__c              
      sž   zt t| ƒ ¡  W n† ty˜ } zndt| jƒ }dt| jƒ }t|ƒdkrXd||f }nd|||f }t 	¡ d }t
|ƒ|ƒ|f| _W Y d }~n
d }~0 0 d S )Nztid=%szctaid=%sÚ z%s %sz	%s %s: %sr
   )rL   rJ   r:   Ú	Exceptionr#   rS   rR   ÚstrÚsysÚexc_infoÚtyperT   )r   ÚeÚtidZctaidÚmsgÚtbr]   r   r   r:   ­   s    zBlockThread.runc                 C   s:   | j rtdƒ‚d| _| j ¡  | j ¡  | j r6tdƒ‚d S )Nz"abort flag set on syncthreads callTz#abort flag set on syncthreads clear)rV   ÚRuntimeErrorrP   rO   ÚwaitÚclearrC   r   r   r   Úsyncthreads¼   s    

zBlockThread.syncthreadsc                 C   sD   | j j| j j| j jf}|| jj|< |  ¡  t | jj¡}|  ¡  |S r   )	rS   rX   rY   rZ   rQ   Úblock_staterl   r.   Zcount_nonzero)r   ÚvalueÚidxÚcountr   r   r   Úsyncthreads_countÈ   s    zBlockThread.syncthreads_countc                 C   sL   | j j| j j| j jf}|| jj|< |  ¡  t | jj¡}|  ¡  |rHdS dS ©Nr   r   )	rS   rX   rY   rZ   rQ   rm   rl   r.   Úall©r   rn   ro   Útestr   r   r   Úsyncthreads_andÐ   s    zBlockThread.syncthreads_andc                 C   sL   | j j| j j| j jf}|| jj|< |  ¡  t | jj¡}|  ¡  |rHdS dS rr   )	rS   rX   rY   rZ   rQ   rm   rl   r.   Úanyrt   r   r   r   Úsyncthreads_orØ   s    zBlockThread.syncthreads_orc                 C   s   d| j | jf S )NzThread <<<%s, %s>>>)rR   rS   rC   r   r   r   Ú__str__à   s    zBlockThread.__str__)r   r   r   r   r)   r:   rl   rq   rv   rx   ry   Ú__classcell__r   r   r]   r   rJ   š   s   rJ   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r9   aç  
    Manages the execution of a thread block.

    When run() is called, all threads are started. Each thread executes until it
    hits syncthreads(), at which point it sets its own syncthreads_blocked to
    True so that the BlockManager knows it is blocked. It then waits on its
    syncthreads_event.

    The BlockManager polls threads to determine if they are blocked in
    syncthreads(). If it finds a blocked thread, it adds it to the set of
    blocked threads. When all threads are blocked, it unblocks all the threads.
    The thread are unblocked by setting their syncthreads_blocked back to False
    and setting their syncthreads_event.

    The polling continues until no threads are alive, when execution is
    complete.
    c                 C   s(   || _ || _|| _tj|tjd�| _d S )N)Zdtype)Z	_grid_dimrW   Ú_fr.   ÚzerosZbool_rm   )r   r[   r%   r&   r   r   r   r)   ö   s    zBlockManager.__init__c           
         s  t ƒ }t ƒ }t ƒ }tjˆjŽ D ]<}‡ ‡fdd„}t|ˆ||ƒ}| ¡  | |¡ | |¡ q|rô|D ]R}|jrz| |¡ qd|jrd|D ]}	d|	_	d|	_|	j
  ¡  q„|jd  |jd ¡‚qd||krà|D ]}d|_|j
  ¡  qÄt ƒ }t dd„ |D ƒƒ}q\|D ] }|jrø|jd  |jd ¡‚qød S )	Nc                      s   ˆj ˆ Ž  d S r   )r{   r   ©r;   r   r   r   rK     s    z BlockManager.run.<locals>.targetTFr   r   c                 S   s   g | ]}|  ¡ r|‘qS r   )Úis_alive)r5   Útr   r   r   r6     r7   z$BlockManager.run.<locals>.<listcomp>)Úsetr.   r8   rW   rJ   ÚstartÚaddrP   rT   rV   rO   Úwith_traceback)
r   r<   r;   ÚthreadsZlivethreadsZblockedthreadsZblock_pointrK   r   Zt_otherr   r}   r   r:   ü   s8    
zBlockManager.runN)r   r   r   r   r)   r:   r   r   r   r   r9   ä   s   r9   )Ú
contextlibr   Ú	functoolsr   rb   rM   Únumpyr.   Zcudadrv.devicearrayr   r   Z	kernelapir   r   r	   Úerrorsr   r;   r   r   r   r   r   r   Údictr   Úobjectr    ÚThreadrJ   r9   r   r   r   r   Ú<module>   s"   

^J