3
I4W^  ã               @   s<   d dl Z d dlZG dd„ dejjƒZG dd„ dejjƒZdS )é    Nc                   s|   e Zd ZdZd‡ fdd„	Zdd„ Zdd	„ Zdd
d„Z‡ fdd„Z‡ fdd„Z	e
dd„ ƒZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚStreama1  Wrapper around a CUDA stream.

    A CUDA stream is a linear sequence of execution that belongs to a specific
    device, independent from other streams.  See :ref:`cuda-semantics` for
    details.

    Arguments:
        device(torch.device or int, optional): a device on which to allocate
            the stream. If :attr:`device` is ``None`` (default) or a negative
            integer, this will use the current device.
        priority(int, optional): priority of the stream. Lower numbers
                                 represent higher priorities.
    Nr   c                s4   t jj|ƒ� tt| ƒj| fd|i|—ŽS Q R X d S )NÚpriority)ÚtorchÚcudaÚdeviceÚsuperr   Ú__new__)Úclsr   r   Úkwargs)Ú	__class__© ú3/tmp/pip-build-e66j97b2/torch/torch/cuda/streams.pyr      s    zStream.__new__c             C   s   |j | ƒ dS )aò  Makes all future work submitted to the stream wait for an event.

        Arguments:
            event (Event): an event to wait for.

        .. note:: This is a wrapper around ``cudaStreamWaitEvent()``: see `CUDA
           documentation`_ for more info.

           This function returns without waiting for :attr:`event`: only future
           operations are affected.

        .. _CUDA documentation:
           http://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__STREAM.html
        N)Úwait)ÚselfÚeventr   r   r   Ú
wait_event   s    zStream.wait_eventc             C   s   | j |jƒ ƒ dS )aŸ  Synchronizes with another stream.

        All future work submitted to this stream will wait until all kernels
        submitted to a given stream at the time of call complete.

        Arguments:
            stream (Stream): a stream to synchronize.

        .. note:: This function returns without waiting for currently enqueued
           kernels in :attr:`stream`: only future operations are affected.
        N)r   Úrecord_event)r   Ústreamr   r   r   Úwait_stream)   s    zStream.wait_streamc             C   s   |dkrt ƒ }|j| ƒ |S )zÍRecords an event.

        Arguments:
            event (Event, optional): event to record. If not given, a new one
                will be allocated.

        Returns:
            Recorded event.
        N)ÚEventÚrecord)r   r   r   r   r   r   7   s    

zStream.record_eventc                s   t t| ƒjƒ S )z”Checks if all the work submitted has been completed.

        Returns:
            A boolean indicating if all kernels in this stream are completed.)r   r   Úquery)r   )r   r   r   r   F   s    zStream.queryc                s   t t| ƒjƒ  dS )a.  Wait for all the kernels in this stream to complete.

        .. note:: This is a wrapper around ``cudaStreamSynchronize()``: see
           `CUDA documentation`_ for more info.

        .. _CUDA documentation:
           http://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__STREAM.html
        N)r   r   Úsynchronize)r   )r   r   r   r   M   s    	zStream.synchronizec             C   s   t j| jƒS )N)ÚctypesÚc_void_pÚcuda_stream)r   r   r   r   Ú_as_parameter_X   s    zStream._as_parameter_c                s   t |tƒrtt| ƒj|ƒS dS )NF)Ú
isinstancer   r   Ú__eq__)r   Úo)r   r   r   r   \   s    
zStream.__eq__c             C   s   t | j| jfƒS )N)Úhashr   r   )r   r   r   r   Ú__hash__a   s    zStream.__hash__c             C   s   dj | j| jƒS )Nz1<torch.cuda.Stream device={0} cuda_stream={1:#x}>)Úformatr   r   )r   r   r   r   Ú__repr__d   s    zStream.__repr__)Nr   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   Úpropertyr   r   r!   r#   Ú__classcell__r   r   )r   r   r      s   
r   c                   s’   e Zd ZdZd‡ fdd„	Ze‡ fdd„ƒZd‡ fdd	„	Zd‡ fd
d„	Z‡ fdd„Z	‡ fdd„Z
‡ fdd„Z‡ fdd„Zedd„ ƒZdd„ Z‡  ZS )r   a“  Wrapper around a CUDA event.

    CUDA events are synchronization markers that can be used to monitor the
    device's progress, to accurately measure timing, and to synchronize CUDA
    streams.

    The underlying CUDA events are lazily initialized when the event is first
    recorded or exported to another process. After creation, only streams on the
    same device may record the event. However, streams on any device can wait on
    the event.

    Arguments:
        enable_timing (bool, optional): indicates if the event should measure time
            (default: ``False``)
        blocking (bool, optional): if ``True``, :meth:`wait` will be blocking (default: ``False``)
        interprocess (bool): if ``True``, the event can be shared between processes
            (default: ``False``)

       .. _CUDA documentation:
       https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__EVENT.html
    Fc                s   t t| ƒj| |||d�S )N)Úenable_timingÚblockingÚinterprocess)r   r   r   )r	   r*   r+   r,   )r   r   r   r   €   s    
zEvent.__new__c                s   t t| ƒj||ƒS )z<Reconstruct an event from an IPC handle on the given device.)r   r   Úfrom_ipc_handle)r	   r   Úhandle)r   r   r   r-   …   s    zEvent.from_ipc_handleNc                s&   |dkrt jjƒ }tt| ƒj|ƒ dS )z¨Records the event in a given stream.

        Uses ``torch.cuda.current_stream()`` if no stream is specified. The
        stream's device must match the event's device.N)r   r   Úcurrent_streamr   r   r   )r   r   )r   r   r   r   Š   s    
zEvent.recordc                s&   |dkrt jjƒ }tt| ƒj|ƒ dS )z˜Makes all future work submitted to the given stream wait for this
        event.

        Use ``torch.cuda.current_stream()`` if no stream is specified.N)r   r   r/   r   r   r   )r   r   )r   r   r   r   “   s    
z
Event.waitc                s   t t| ƒjƒ S )z¼Checks if all work currently captured by event has completed.

        Returns:
            A boolean indicating if all work currently captured by event has
            completed.
        )r   r   r   )r   )r   r   r   r   œ   s    zEvent.queryc                s   t t| ƒj|ƒS )z}Returns the time elapsed in milliseconds after the event was
        recorded and before the end_event was recorded.
        )r   r   Úelapsed_time)r   Z	end_event)r   r   r   r0   ¥   s    zEvent.elapsed_timec                s   t t| ƒjƒ  dS )a¼  Waits for the event to complete.

        Waits until the completion of all work currently captured in this event.
        This prevents the CPU thread from proceeding until the event completes.

         .. note:: This is a wrapper around ``cudaEventSynchronize()``: see `CUDA
           documentation`_ for more info.

        .. _CUDA documentation:
           https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__EVENT.html
        N)r   r   r   )r   )r   r   r   r   «   s    zEvent.synchronizec                s   t t| ƒjƒ S )ziReturns an IPC handle of this event. If not recorded yet, the event
        will use the current device. )r   r   Ú
ipc_handle)r   )r   r   r   r1   ¹   s    zEvent.ipc_handlec             C   s   t j| jƒS )N)r   r   Ú
cuda_event)r   r   r   r   r   ¾   s    zEvent._as_parameter_c             C   s   | j rdj| jjƒS dS d S )Nz<torch.cuda.Event {0:#x}>z <torch.cuda.Event uninitialized>)r2   r"   r   Úvalue)r   r   r   r   r#   Â   s    zEvent.__repr__)FFF)N)N)r$   r%   r&   r'   r   Úclassmethodr-   r   r   r   r0   r   r1   r(   r   r#   r)   r   r   )r   r   r   i   s   			r   )r   r   Z_CZ_CudaStreamBaser   Z_CudaEventBaser   r   r   r   r   Ú<module>   s   d