a
    –§»aœ  ã                   @   sx   d dl Zd dlmZ d dlmZ d dlmZmZm	Z	 G dd„ de
ƒZG dd„ de	ƒZG d	d
„ d
eƒZG dd„ deƒZdS )é    N)Úcuda)Údevicearray)ÚUFuncMechanismÚGenerializedUFuncÚGUFuncCallStepsc                   @   sL   e Zd ZdZdd„ Zedd„ ƒZejdd„ ƒZdd„ Zdd
d„Z	dd„ Z
dS )ÚCUDAUFuncDispatcherzD
    Invoke the CUDA ufunc specialization for the given inputs.
    c                 C   s   || _ d| _d S )Nr   )Ú	functionsÚ_maxblocksize)ÚselfZtypes_to_retty_kernels© r   úO/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/cuda/dispatcher.pyÚ__init__   s    zCUDAUFuncDispatcher.__init__c                 C   s   | j S ©N)r	   ©r
   r   r   r   Úmax_blocksize   s    z!CUDAUFuncDispatcher.max_blocksizec                 C   s
   || _ d S r   )Z_max_blocksize)r
   Zblkszr   r   r   r      s    c                 O   s   t  | j||¡S )a¦  
        *args: numpy arrays or DeviceArrayBase (created by cuda.to_device).
               Cannot mix the two types in one call.

        **kws:
            stream -- cuda stream; when defined, asynchronous mode is used.
            out    -- output array. Can be a numpy array or DeviceArrayBase
                      depending on the input arguments.  Type must match
                      the input arguments.
        )ÚCUDAUFuncMechanismÚcallr   )r
   ÚargsÚkwsr   r   r   Ú__call__   s    zCUDAUFuncDispatcher.__call__r   c                 C   sè   t t| j ¡ ƒd ƒdks"J dƒ‚|jdks4J dƒ‚|jd }g }|dkrTtdƒ‚n|dkrd|d S |pnt ¡ }| 	¡ �X t
 |¡rŠ|}nt ||¡}|  |||¡}tjd|jd�}|j||d	� W d   ƒ n1 sÖ0    Y  |d S )
Nr   é   zmust be a binary ufuncé   zmust use 1d arrayzReduction on an empty array.)r   )Údtype©Ústream)ÚlenÚlistr   ÚkeysÚndimÚshapeÚ	TypeErrorr   r   Zauto_synchronizer   Úis_cuda_ndarrayÚ	to_deviceÚ_CUDAUFuncDispatcher__reduceÚnpÚarrayr   Úcopy_to_host)r
   Úargr   ÚnÚgpu_memsÚmemÚoutÚbufr   r   r   Úreduce'   s"    "



,zCUDAUFuncDispatcher.reducec           
      C   s¼   |j d }|d dkrd| |d ¡\}}| |¡ | |¡ |  |||¡}| |¡ | ||||d�S | |d ¡\}}	| |¡ | |	¡ | ||	||d� |d dkr´|  |||¡S |S d S )Nr   r   r   )r+   r   )r   ÚsplitÚappendr#   )
r
   r*   r)   r   r(   ZfatcutZthincutr+   ÚleftÚrightr   r   r   Z__reduceD   s    





zCUDAUFuncDispatcher.__reduceN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   Úsetterr   r-   r#   r   r   r   r   r   	   s   


r   c                   @   sJ   e Zd ZdgZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )Ú_CUDAGUFuncCallStepsÚ_streamc                 C   s
   t  |¡S r   ©r   Zis_cuda_array©r
   Úobjr   r   r   Úis_device_arraya   s    z$_CUDAGUFuncCallSteps.is_device_arrayc                 C   s   t  |¡r|S t |¡S r   ©r   r!   r   Zas_cuda_arrayr;   r   r   r   Úas_device_arrayd   s    
z$_CUDAGUFuncCallSteps.as_device_arrayc                 C   s   t j|| jd�S ©Nr   )r   r"   r9   )r
   Úhostaryr   r   r   r"   n   s    z_CUDAGUFuncCallSteps.to_devicec                 C   s   |j || jd�}|S r@   )r&   r9   )r
   ÚdevaryrA   r+   r   r   r   Úto_hostq   s    z_CUDAGUFuncCallSteps.to_hostc                 C   s   t j||| jd�S ©N)r   r   r   )r   Údevice_arrayr9   )r
   r   r   r   r   r   rE   u   s    z!_CUDAGUFuncCallSteps.device_arrayc                 C   s   | j  dd¡| _d S )Nr   r   )ÚkwargsÚgetr9   r   r   r   r   Úprepare_inputsx   s    z#_CUDAGUFuncCallSteps.prepare_inputsc                 C   s   |j || jd�|Ž  d S r@   )Úforallr9   )r
   ÚkernelZnelemr   r   r   r   Úlaunch_kernel{   s    z"_CUDAGUFuncCallSteps.launch_kernelN)r2   r3   r4   Ú	__slots__r=   r?   r"   rC   rE   rH   rK   r   r   r   r   r8   \   s   ÿ
r8   c                   @   s(   e Zd Zedd„ ƒZdd„ Zdd„ ZdS )ÚCUDAGenerializedUFuncc                 C   s   t S r   )r8   r   r   r   r   Ú_call_steps€   s    z!CUDAGenerializedUFunc._call_stepsc                 C   s   t j|d|j|jd�S ©N)r   ©r   Ústridesr   Úgpu_data)r   ÚDeviceNDArrayr   rR   )r
   Úaryr   r   r   r   Ú_broadcast_scalar_input„   s
    ýz-CUDAGenerializedUFunc._broadcast_scalar_inputc                 C   s6   t |ƒt |jƒ }d| |j }tj|||j|jd�S rO   )r   r   rQ   r   rS   r   rR   )r
   rT   ZnewshapeZnewaxZ
newstridesr   r   r   Ú_broadcast_add_axisŠ   s    ýz)CUDAGenerializedUFunc._broadcast_add_axisN)r2   r3   r4   r6   rN   rU   rV   r   r   r   r   rM      s   
rM   c                   @   sL   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ ZdS )r   z%
    Provide CUDA specialization
    r   c                 C   s   |j ||d�|Ž  d S r@   )rI   )r
   ÚfuncÚcountr   r   r   r   r   Úlaunchš   s    zCUDAUFuncMechanism.launchc                 C   s
   t  |¡S r   r:   r;   r   r   r   r=   �   s    z"CUDAUFuncMechanism.is_device_arrayc                 C   s   t  |¡r|S t |¡S r   r>   r;   r   r   r   r?       s    
z"CUDAUFuncMechanism.as_device_arrayc                 C   s   t j||d�S r@   )r   r"   )r
   rA   r   r   r   r   r"   ª   s    zCUDAUFuncMechanism.to_devicec                 C   s   |j |d�S r@   )r&   )r
   rB   r   r   r   r   rC   ­   s    zCUDAUFuncMechanism.to_hostc                 C   s   t j|||d�S rD   )r   rE   )r
   r   r   r   r   r   r   rE   °   s    zCUDAUFuncMechanism.device_arrayc                    sj   ‡ ‡fdd„t tˆƒƒD ƒ}tˆƒtˆ jƒ }dg| tˆ jƒ }|D ]}d||< qFtjˆ|ˆ jˆ jd�S )Nc                    s,   g | ]$}|ˆ j ks$ˆ j| ˆ| kr|‘qS r   )r   r   )Ú.0Úax©rT   r   r   r   Ú
<listcomp>´   s   
þz7CUDAUFuncMechanism.broadcast_device.<locals>.<listcomp>r   rP   )	Úranger   r   r   rQ   r   rS   r   rR   )r
   rT   r   Z
ax_differsZ
missingdimrQ   r[   r   r\   r   Úbroadcast_device³   s    
ýz#CUDAUFuncMechanism.broadcast_deviceN)r2   r3   r4   r5   ZDEFAULT_STREAMrY   r=   r?   r"   rC   rE   r_   r   r   r   r   r   ”   s   
r   )Únumpyr$   Znumbar   Znumba.cuda.cudadrvr   Znumba.np.ufunc.deviceufuncr   r   r   Úobjectr   r8   rM   r   r   r   r   r   Ú<module>   s   S#