a
    –§»ak  ã                   @   s²  d Z ddlZddlZddlZddlZddlmZ ddlZddl	Z	ddl	m
Z
 ddlmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ zeedƒdƒZW n eyÜ   dd„ ZY n0 dd„ Zdd„ Z dd„ Z!G dd„ de
j"ƒZ#G dd„ de#ƒZ$edd„ ƒZ%G dd„ de#ƒZ&G dd„ de'ƒZ(G dd „ d e#ej)ƒZ*G d!d"„ d"e#ej)ƒZ+d4d#d$„Z,d5d%d&„Z-d'd(„ Z.d)d*„ Z/d+Z0d,d-„ Z1d6d0d1„Z2d2d3„ Z3dS )7zÑ
A CUDA ND Array is recognized by checking the __cuda_memory__ attribute
on the object.  If it exists and evaluate to True, it must define shape,
strides, dtype and size attributes similar to a NumPy ndarray.
é    N)Úc_void_p)Ú_devicearray)Údevices)Údriver)ÚtypesÚconfig)Úto_fixed_tuple)Ú
dummyarray)Únumpy_support)ÚNumbaPerformanceWarning)ÚwarnÚ	lru_cachec                 C   s   | S ©N© )Úfuncr   r   úX/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/cuda/cudadrv/devicearray.pyr      s    c                 C   s   t | ddƒS )z$Check if an object is a CUDA ndarrayÚ__cuda_ndarray__F)Úgetattr©Úobjr   r   r   Úis_cuda_ndarray"   s    r   c                    sB   t ˆ ƒ ‡ fdd„}|dtƒ |dtƒ |dtjƒ |dtƒ dS )z,Verify the CUDA ndarray interface for an objc                    s6   t ˆ | ƒst| ƒ‚ttˆ | ƒ|ƒs2td| |f ƒ‚d S )Nz%s must be of type %s)ÚhasattrÚAttributeErrorÚ
isinstancer   )ÚattrÚtypr   r   r   Úrequires_attr+   s    
z4verify_cuda_ndarray_interface.<locals>.requires_attrÚshapeÚstridesÚdtypeÚsizeN)Úrequire_cuda_ndarrayÚtupleÚnpr   Úint)r   r   r   r   r   Úverify_cuda_ndarray_interface'   s    

r%   c                 C   s   t | ƒstdƒ‚dS )z9Raises ValueError is is_cuda_ndarray(obj) evaluates Falsezrequire an cuda ndarray objectN)r   Ú
ValueErrorr   r   r   r   r!   7   s    r!   c                   @   sÆ   e Zd ZdZdZdZd%dd„Zedd„ ƒZd&d	d
„Z	edd„ ƒZ
d'dd„Zdd„ Zedd„ ƒZedd„ ƒZejd(dd„ƒZejd)dd„ƒZd*dd„Zdd„ Zdd„ Zd+dd „Zd!d"„ Zed#d$„ ƒZdS ),ÚDeviceNDArrayBasez$A on GPU NDArray representation
    Tr   Nc                 C   s
  t |tƒr|f}t |tƒr |f}t |¡}t|ƒ| _t|ƒ| jkrJtdƒ‚tj 	d|||j
¡| _t|ƒ| _t|ƒ| _|| _tt tj| jd¡ƒ| _| jdkrÜ|du rÎt | j| j| jj
¡| _t ¡  | j¡}qút |¡| _ntjt ¡ tdƒdd�}d| _|| _|| _dS )a5  
        Args
        ----

        shape
            array shape.
        strides
            array strides.
        dtype
            data type as np.dtype coercible object.
        stream
            cuda stream.
        gpu_data
            user provided device memory for the ndarray data buffer
        zstrides not match ndimr   é   N)ÚcontextÚpointerr    )r   r$   r#   r   ÚlenÚndimr&   r	   ÚArrayZ	from_descÚitemsizeÚ_dummyr"   r   r   Ú	functoolsÚreduceÚoperatorÚmulr    Ú_driverZmemory_size_from_infoÚ
alloc_sizer   Úget_contextZmemallocZdevice_memory_sizeZMemoryPointerr   Úgpu_dataÚstream)Úselfr   r   r   r8   r7   r   r   r   Ú__init__C   s8    



ÿ


ÿ
ÿzDeviceNDArrayBase.__init__c                 C   s`   | j jd ur| j j}nd}t| jƒt| ƒr.d nt| jƒ|df| jj| jdkrVt	| jƒnd ddœS )Nr   Fé   )r   r   ÚdataZtypestrr8   Úversion)
Údevice_ctypes_pointerÚvaluer"   r   Úis_contiguousr   r   Ústrr8   r$   )r9   Úptrr   r   r   Ú__cuda_array_interface__r   s    
úz*DeviceNDArrayBase.__cuda_array_interface__c                 C   s   t   | ¡}||_|S )zBind a CUDA stream to this object so that all subsequent operation
        on this array defaults to the given stream.
        )Úcopyr8   )r9   r8   Úcloner   r   r   Úbind‚   s    
zDeviceNDArrayBase.bindc                 C   s   |   ¡ S r   ©Ú	transpose©r9   r   r   r   ÚTŠ   s    zDeviceNDArrayBase.Tc                 C   s|   |rt |ƒt t| jƒƒkr| S | jdkr6d}t|ƒ‚nB|d urdt|ƒtt| jƒƒkrdtd|f ƒ‚nddlm} || ƒS d S )Né   z2transposing a non-2D DeviceNDArray isn't supportedzinvalid axes list %rr   rG   )r"   Úranger,   ÚNotImplementedErrorÚsetr&   Znumba.cuda.kernels.transposerH   )r9   ZaxesÚmsgrH   r   r   r   rH   Ž   s    

zDeviceNDArrayBase.transposec                 C   s   |s
| j S |S r   ©r8   )r9   r8   r   r   r   Ú_default_streamš   s    z!DeviceNDArrayBase._default_streamc                 C   sR   d| j v }| jd r|sd}n| jd r2|s2d}nd}t | j¡}t || j|¡S )ún
        Magic attribute expected by Numba to get the numba type that
        represents this object.
        r   ÚC_CONTIGUOUSÚCÚF_CONTIGUOUSÚFÚA)r   Úflagsr
   Ú
from_dtyper   r   r-   r,   )r9   Ú	broadcastZlayoutr   r   r   r   Ú_numba_type_�   s    
zDeviceNDArrayBase._numba_type_c                 C   s   | j du rtdƒS | j jS dS )z:Returns the ctypes pointer to the GPU data buffer
        Nr   )r7   r   r>   rI   r   r   r   r>   ¼   s    
z'DeviceNDArrayBase.device_ctypes_pointerc                 C   s®   |j dkrdS t| ƒ |  |¡}t| ƒt|ƒ }}t |¡rdt|ƒ t||ƒ tj| || j|d� nFt	j
||jd rxdndd|jd  d	�}t||ƒ tj| || j|d� dS )
zŸCopy `ary` to `self`.

        If `ary` is a CUDA memory, perform a device-to-device transfer.
        Otherwise, perform a a host-to-device transfer.
        r   NrP   rS   rT   rV   TZ	WRITEABLE)ÚorderÚsubokrD   )r    Úsentry_contiguousrQ   Ú
array_corer4   Úis_device_memoryÚcheck_array_compatibilityZdevice_to_devicer5   r#   ÚarrayrX   Zhost_to_device)r9   Úaryr8   Z	self_coreZary_corer   r   r   Úcopy_to_deviceÅ   s&    




ü
ÿz DeviceNDArrayBase.copy_to_devicec                 C   sÐ   t dd„ | jD ƒƒr(d}t| | j¡ƒ‚| jdks:J dƒ‚|  |¡}|du r`tj| jtjd�}nt	| |ƒ |}| jdkrŒt
j|| | j|d� |du rÌ| jdkr´tj| j| j|d	�}ntj| j| j| j|d
�}|S )a^  Copy ``self`` to ``ary`` or create a new Numpy ndarray
        if ``ary`` is ``None``.

        If a CUDA ``stream`` is given, then the transfer will be made
        asynchronously as part as the given stream.  Otherwise, the transfer is
        synchronous: the function returns after the copy is finished.

        Always returns the host array.

        Example::

            import numpy as np
            from numba import cuda

            arr = np.arange(1000)
            d_arr = cuda.to_device(arr)

            my_kernel[100, 100](d_arr)

            result_array = d_arr.copy_to_host()
        c                 s   s   | ]}|d k V  qdS )r   Nr   )Ú.0Úsr   r   r   Ú	<genexpr>û   ó    z1DeviceNDArrayBase.copy_to_host.<locals>.<genexpr>z2D->H copy not implemented for negative strides: {}r   zNegative memory sizeN©r   r   rP   )r   r   Úbuffer)r   r   r   rj   )Úanyr   rM   Úformatr5   rQ   r#   ÚemptyÚbytera   r4   Údevice_to_hostr    Úndarrayr   r   )r9   rc   r8   rO   Úhostaryr   r   r   Úcopy_to_hostä   s,    


ÿ
ÿÿzDeviceNDArrayBase.copy_to_hostc                 c   s¼   |   |¡}| jdkrtdƒ‚| jd | jjkr6tdƒ‚tt t	| j
ƒ| ¡ƒ}| j}| jj}t|ƒD ]R}|| }t|| | j
ƒ}|| f}	| j || || ¡}
t|	|| j||
d�V  qddS )zžSplit the array into equal partition of the `section` size.
        If the array cannot be equally divided, the last section will be
        smaller.
        r(   zonly support 1d arrayr   zonly support unit stride)r   r8   r7   N)rQ   r,   r&   r   r   r.   r$   ÚmathÚceilÚfloatr    rL   Úminr7   ÚviewÚDeviceNDArray)r9   Úsectionr8   Znsectr   r.   ÚiÚbeginÚendr   r7   r   r   r   Úsplit  s     


ÿzDeviceNDArrayBase.splitc                 C   s   | j S )zEReturns a device memory object that is used as the argument.
        )r7   rI   r   r   r   Úas_cuda_arg(  s    zDeviceNDArrayBase.as_cuda_argc                 C   s0   t  ¡  | j¡}t| j| j| jd�}t||d�S )zÌ
        Returns a *IpcArrayHandle* object that is safe to serialize and transfer
        to another process to share the local allocation.

        Note: this feature is only available on Linux.
        )r   r   r   )Ú
ipc_handleÚ
array_desc)	r   r6   Úget_ipc_handler7   Údictr   r   r   ÚIpcArrayHandle)r9   ZipchÚdescr   r   r   r�   -  s    z DeviceNDArrayBase.get_ipc_handlec                 C   s2   | j j|d�\}}t|j|j| j|  |¡| jd�S )a(  
        Remove axes of size one from the array shape.

        Parameters
        ----------
        axis : None or int or tuple of ints, optional
            Subset of dimensions to remove. A `ValueError` is raised if an axis
            with size greater than one is selected. If `None`, all axes with
            size one are removed.
        stream : cuda stream or 0, optional
            Default stream for the returned view of the array.

        Returns
        -------
        DeviceNDArray
            Squeezed view into the array.

        )Úaxis©r   r   r   r8   r7   )r/   Úsqueezerx   r   r   r   rQ   r7   )r9   r…   r8   Z	new_dummyÚ_r   r   r   r‡   8  s    ûzDeviceNDArrayBase.squeezec                 C   sŒ   t  |¡}t| jƒ}t| jƒ}| jj|jkrv|  ¡ s<tdƒ‚t|d | jj |jƒ\|d< }|dkrltdƒ‚|j|d< t	|||| j
| jd�S )zeReturns a new object by reinterpretting the dtype without making a
        copy of the data.
        zHTo change to a dtype of a different size, the array must be C-contiguouséÿÿÿÿr   zuWhen changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array.r†   )r#   r   Úlistr   r   r.   Úis_c_contiguousr&   Údivmodrx   r8   r7   )r9   r   r   r   Úremr   r   r   rw   T  s0    


ÿþÿ
ûzDeviceNDArrayBase.viewc                 C   s   | j j| j S r   )r   r.   r    rI   r   r   r   Únbytesy  s    zDeviceNDArrayBase.nbytes)r   N)r   )N)r   )Nr   )r   )Nr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z__cuda_memory__r   r:   ÚpropertyrC   rF   rJ   rH   rQ   r[   r>   r   Úrequire_contextrd   rr   r}   r~   r�   r‡   rw   rŽ   r   r   r   r   r'   =   s4   
/





.

%r'   c                       s:   e Zd ZdZd
‡ fdd„	Zedd„ ƒZedd	„ ƒZ‡  ZS )ÚDeviceRecordz
    An on-GPU record type
    r   Nc                    s$   d}d}t t| ƒ |||||¡ d S ©Nr   )Úsuperr•   r:   )r9   r   r8   r7   r   r   ©Ú	__class__r   r   r:   …  s
    ÿzDeviceRecord.__init__c                 C   s   t | jjƒS ©zÿ
        For `numpy.ndarray` compatibility. Ideally this would return a
        `np.core.multiarray.flagsobj`, but that needs to be constructed
        with an existing `numpy.ndarray` (as the C- and F- contiguous flags
        aren't writeable).
        ©r‚   r/   rX   rI   r   r   r   rX   ‹  s    zDeviceRecord.flagsc                 C   s   t  | j¡S )rR   )r
   rY   r   rI   r   r   r   r[   •  s    zDeviceRecord._numba_type_)r   N)	r�   r�   r‘   r’   r:   r“   rX   r[   Ú__classcell__r   r   r˜   r   r•   �  s   
	r•   c                    s>   ddl m‰  ˆdkr&ˆ jdd„ ƒ}|S ˆ j‡ ‡fdd„ƒ}|S )zÙ
    A separate method so we don't need to compile code every assignment (!).

    :param ndim: We need to have static array sizes for cuda.local.array, so
        bake in the number of dimensions into the kernel
    r   )Úcudac                 S   s   |d | d< d S r–   r   )ÚlhsÚrhsr   r   r   Úkernelª  s    z_assign_kernel.<locals>.kernelc                    sÐ   ˆ   d¡}d}t| jƒD ]}|| j| 9 }q||kr8d S ˆ jjdˆftjd�}tˆd ddƒD ]L}|| j|  |d|f< || j|  |j| dk |d|f< || j|  }q^|t|d ˆƒ | t|d ˆƒ< d S )Nr(   rK   ri   r‰   r   )	ZgridrL   r,   r   Úlocalrb   r   Úint64r   )rž   rŸ   ÚlocationÚ
n_elementsrz   Úidx©r�   r,   r   r   r    ¯  s    
þ$)Únumbar�   Zjit)r,   r    r   r¦   r   Ú_assign_kernelž  s    
r¨   c                   @   s”   e Zd ZdZdd„ Zedd„ ƒZdd„ Zdd	d
„Zdd„ Z	dd„ Z
d dd„Zejdd„ ƒZd!dd„Zd"dd„Zejdd„ ƒZd#dd„Zd$dd„ZdS )%rx   z
    An on-GPU array type
    c                 C   s   | j jS )zA
        Return true if the array is Fortran-contiguous.
        )r/   Zis_f_contigrI   r   r   r   Úis_f_contiguousÎ  s    zDeviceNDArray.is_f_contiguousc                 C   s   t | jjƒS rš   r›   rI   r   r   r   rX   Ô  s    zDeviceNDArray.flagsc                 C   s   | j jS )z;
        Return true if the array is C-contiguous.
        )r/   Zis_c_contigrI   r   r   r   r‹   Þ  s    zDeviceNDArray.is_c_contiguousNc                 C   s"   |r|   ¡  |¡S |   ¡  ¡ S dS )zE
        :return: an `numpy.ndarray`, so copies to the host.
        N)rr   Ú	__array__)r9   r   r   r   r   rª   ä  s    zDeviceNDArray.__array__c                 C   s
   | j d S )Nr   )r   rI   r   r   r   Ú__len__í  s    zDeviceNDArray.__len__c                 O   s˜   t |ƒdkr&t|d ttfƒr&|d }t| ƒ}|| jkrP|| j| j| j| jd�S | j	j
|i |¤Ž\}}|| j	jgkrŒ||j|j| j| jd�S tdƒ‚dS )z¶
        Reshape the array without changing its contents, similarly to
        :meth:`numpy.ndarray.reshape`. Example::

            d_arr = d_arr.reshape(20, 50, order='F')
        r(   r   )r   r   r   r7   úoperation requires copyingN)r+   r   r"   rŠ   Útyper   r   r   r7   r/   ÚreshapeÚextentrM   )r9   ZnewshapeÚkwsÚclsÚnewarrÚextentsr   r   r   r®   ð  s    

ÿ
ÿzDeviceNDArray.reshaperT   r   c                 C   sX   |   |¡}t| ƒ}| jj|d�\}}|| jjgkrL||j|j| j| j|d�S t	dƒ‚dS )zr
        Flatten the array without changing its contents, similar to
        :meth:`numpy.ndarray.ravel`.
        )r\   ©r   r   r   r7   r8   r¬   N)
rQ   r­   r/   Úravelr¯   r   r   r   r7   rM   )r9   r\   r8   r±   r²   r³   r   r   r   rµ     s    

þzDeviceNDArray.ravelc                 C   s
   |   |¡S r   ©Ú_do_getitem)r9   Úitemr   r   r   Ú__getitem__  s    zDeviceNDArray.__getitem__c                 C   s   |   ||¡S )z0Do `__getitem__(item)` with CUDA stream
        r¶   )r9   r¸   r8   r   r   r   Úgetitem  s    zDeviceNDArray.getitemc                 C   s¾   |   |¡}| j |¡}t| ¡ ƒ}t| ƒ}t|ƒdkr”| jj|d Ž }|j	szt
jd| jd�}tj||| jj|d� |d S ||j|j| j||d�S n&| jj|jŽ }||j|j| j||d�S d S )Nr(   r   ©r   )ÚdstÚsrcr    r8   r´   )rQ   r/   r¹   rŠ   Ziter_contiguous_extentr­   r+   r7   rw   Zis_arrayr#   rm   r   r4   ro   r.   r   r   r¯   )r9   r¸   r8   Úarrr³   r±   Únewdatarq   r   r   r   r·   "  s(    
þ
ÿ
ÿzDeviceNDArray._do_getitemc                 C   s   |   ||¡S r   ©Ú_do_setitem)r9   Úkeyr?   r   r   r   Ú__setitem__:  s    zDeviceNDArray.__setitem__c                 C   s   | j |||d�S )z6Do `__setitem__(key, value)` with CUDA stream
        rP   rÀ   )r9   rÂ   r?   r8   r   r   r   Úsetitem>  s    zDeviceNDArray.setitemc                 C   s\  |   |¡}| }|r$t ¡ }| ¡ }| j |¡}| jj|jŽ }t	|t
jƒrTd}d}	n|j}|j}	t| ƒ||	| j||d�}
t||dd�\}}|j|
jkrªtd|j|
jf ƒ‚tj|
jtjd�}|j||
j|j d …< |j|Ž }tt|
j|jƒƒD ].\}\}}|dkrî||krîtd|||f ƒ‚qît tj|
jd¡}t|
jƒj||d	�|
|ƒ |�rX| ¡  d S )
Nr   r´   T)r8   Úuser_explicitz$Can't assign %s-D array to %s-D selfr»   r(   zCCan't copy sequence with size %d to array axis %d with dimension %drP   ) rQ   r   r6   Zget_default_streamr/   r¹   r7   rw   r¯   r   r	   ÚElementr   r   r­   r   Úauto_devicer,   r&   r#   Zonesr¢   r®   Ú	enumerateÚzipr0   r1   r2   r3   r¨   ÚforallZsynchronize)r9   rÂ   r?   r8   ZsynchronousÚctxr¾   r¿   r   r   rž   rŸ   rˆ   Z	rhs_shaperz   ÚlÚrr¤   r   r   r   rÁ   C  sJ    
û	þ
ÿzDeviceNDArray._do_setitem)N)rT   r   )r   )r   )r   )r   )r�   r�   r‘   r’   r©   r“   rX   r‹   rª   r«   r®   rµ   r   r”   r¹   rº   r·   rÃ   rÄ   rÁ   r   r   r   r   rx   Ê  s"   
	
	





rx   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )rƒ   a"  
    An IPC array handle that can be serialized and transfer to another process
    in the same machine for share a GPU allocation.

    On the destination process, use the *.open()* method to creates a new
    *DeviceNDArray* object that shares the allocation from the original process.
    To release the resources, call the *.close()* method.  After that, the
    destination can no longer use the shared array object.  (Note: the
    underlying weakref to the resource is now dead.)

    This object implements the context-manager interface that calls the
    *.open()* and *.close()* method automatically::

        with the_ipc_array_handle as ipc_array:
            # use ipc_array here as a normal gpu array object
            some_code(ipc_array)
        # ipc_array is dead at this point
    c                 C   s   || _ || _d S r   )Ú_array_descÚ_ipc_handle)r9   r   r€   r   r   r   r:   Ž  s    zIpcArrayHandle.__init__c                 C   s$   | j  t ¡ ¡}tf d|i| j¤ŽS )z˜
        Returns a new *DeviceNDArray* that shares the allocation from the
        original process.  Must not be used on the original process.
        r7   )rÏ   Úopenr   r6   rx   rÎ   )r9   Zdptrr   r   r   rÐ   ’  s    zIpcArrayHandle.openc                 C   s   | j  ¡  dS )z5
        Closes the IPC handle to the array.
        N)rÏ   ÚcloserI   r   r   r   rÑ   š  s    zIpcArrayHandle.closec                 C   s   |   ¡ S r   )rÐ   rI   r   r   r   Ú	__enter__   s    zIpcArrayHandle.__enter__c                 C   s   |   ¡  d S r   )rÑ   )r9   r­   r?   Ú	tracebackr   r   r   Ú__exit__£  s    zIpcArrayHandle.__exit__N)	r�   r�   r‘   r’   r:   rÐ   rÑ   rÒ   rÔ   r   r   r   r   rƒ   {  s   rƒ   c                   @   s   e Zd ZdZddd„ZdS )ÚMappedNDArrayz4
    A host array that uses CUDA mapped memory.
    r   c                 C   s   || _ || _d S r   ©r7   r8   ©r9   r7   r8   r   r   r   Údevice_setup¬  s    zMappedNDArray.device_setupN)r   ©r�   r�   r‘   r’   rØ   r   r   r   r   rÕ   §  s   rÕ   c                   @   s   e Zd ZdZddd„ZdS )ÚManagedNDArrayz5
    A host array that uses CUDA managed memory.
    r   c                 C   s   || _ || _d S r   rÖ   r×   r   r   r   rØ   ¶  s    zManagedNDArray.device_setupN)r   rÙ   r   r   r   r   rÚ   ±  s   rÚ   c                 C   s   t | j| j| j||d�S )z/Create a DeviceNDArray object that is like ary.©r8   r7   )rx   r   r   r   )rc   r8   r7   r   r   r   Úfrom_array_like»  s    ÿrÜ   c                 C   s   t | j||d�S )z.Create a DeviceRecord object that is like rec.rÛ   )r•   r   )Zrecr8   r7   r   r   r   Úfrom_record_likeÁ  s    rÝ   c                 C   s@   | j s
| S g }| j D ]}| |dkr(dntdƒ¡ q| t|ƒ S )aG  
    Extract the repeated core of a broadcast array.

    Broadcast arrays are by definition non-contiguous due to repeated
    dimensions, i.e., dimensions with stride 0. In order to ascertain memory
    contiguity and copy the underlying data from such arrays, we must create
    a view without the repeated dimensions.

    r   N)r   ÚappendÚslicer"   )rc   Z
core_indexÚstrider   r   r   r_   Æ  s    

r_   c                 C   sR   | j j}tt| jƒt| jƒƒD ].\}}|dkr|dkr||krD dS ||9 }qdS )zÓ
    Returns True iff `ary` is C-style contiguous while ignoring
    broadcasted and 1-sized dimensions.
    As opposed to array_core(), it does not call require_context(),
    which can be quite expensive.
    r(   r   FT)r   r.   rÉ   Úreversedr   r   )rc   r    r   rà   r   r   r   r@   Ø  s    
r@   z™Array contains non-contiguous buffer and cannot be transferred as a single memory region. Please ensure contiguous buffer with numpy .ascontiguousarray()c                 C   s(   t | ƒ}|jd s$|jd s$ttƒ‚d S )NrS   rU   )r_   rX   r&   Úerrmsg_contiguous_buffer)rc   Úcorer   r   r   r^   î  s    r^   TFc                 C   s¸   t  | ¡r| dfS t| dƒr,tj | ¡dfS t| tjƒrFt	| |d�}n$tj
| ddd�} t| ƒ t| |d�}|r¬tjrž|sžt| tƒsžt| tjƒržd}tt|ƒƒ |j| |d� |dfS dS )zº
    Create a DeviceRecord or DeviceArray like obj and optionally copy data from
    host to device. If obj already represents device memory, it is returned and
    no copy is made.
    FrC   rP   T)rD   r]   zGHost array used in CUDA kernel will incur copy overhead to/from device.N)r4   r`   r   r§   r�   Zas_cuda_arrayr   r#   ÚvoidrÝ   rb   r^   rÜ   r   ZCUDA_WARN_ON_IMPLICIT_COPYrx   rp   r   r   rd   )r   r8   rD   rÅ   ZdevobjrO   r   r   r   rÇ   ô  s2    

ýÿþ
ýrÇ   c                 C   sv   |   ¡ |  ¡  }}| j|jkr2td| j|jf ƒ‚|j|jkrRtd| j|jf ƒ‚|j|jkrrtd| j|jf ƒ‚d S )Nzincompatible dtype: %s vs. %szincompatible shape: %s vs. %szincompatible strides: %s vs. %s)r‡   r   Ú	TypeErrorr   r&   r   )Zary1Zary2Zary1sqZary2sqr   r   r   ra     s    
ÿ
ÿ
ÿra   )r   N)r   N)r   TF)4r’   rs   r0   r2   rD   Úctypesr   Únumpyr#   r§   r   Znumba.cuda.cudadrvr   r   r4   Z
numba.corer   r   Znumba.np.unsafe.ndarrayr   Z
numba.miscr	   Znumba.npr
   Znumba.core.errorsr   Úwarningsr   r   r   r   r   r%   r!   ZDeviceArrayr'   r•   r¨   rx   Úobjectrƒ   rp   rÕ   rÚ   rÜ   rÝ   r_   r@   râ   r^   rÇ   ra   r   r   r   r   Ú<module>   sT     F
+ 2,




'