a
    ‘º”h®  ã                   @   sJ  U d dl Z d dlZd dlmZmZ d dlZd dlmZ g d¢Ze	e
 ed< e
edœdd„Zeejd	ƒseejd
ƒrxJ ‚eejdƒrˆJ ‚eejdƒr˜J ‚eejdƒr¨J ‚eejdƒr¸J ‚ed	ƒejjd	< ed
ƒejjd
< edƒejjd< edƒejjd< edƒejjd< edƒejjd< eddœdd„Zeddœdd„ZG dd„ dƒZdS )é    N)ÚCallableÚOptional)ÚStorage)Úgds_register_bufferÚgds_deregister_bufferÚGdsFileÚ__all__)ÚnameÚreturnc                    s   ‡ fdd„}|S )Nc                     s   t dˆ › dƒ‚d S )Nz	torch._C.z" is not supported on this platform)ÚRuntimeError)ÚargsÚkwargs©r	   © ú</var/www/auris/lib/python3.9/site-packages/torch/cuda/gds.pyÚfn   s    z_dummy_fn.<locals>.fnr   )r	   r   r   r   r   Ú	_dummy_fn   s    r   Ú_gds_register_bufferÚ_gds_deregister_bufferÚ_gds_register_handleÚ_gds_deregister_handleÚ_gds_load_storageÚ_gds_save_storage)Úsr
   c                 C   s   t j | ¡ dS )a:  Registers a storage on a CUDA device as a cufile buffer.

    Example::

        >>> # xdoctest: +SKIP("gds filesystem requirements")
        >>> src = torch.randn(1024, device="cuda")
        >>> s = src.untyped_storage()
        >>> gds_register_buffer(s)

    Args:
        s (Storage): Buffer to register.
    N)ÚtorchÚ_Cr   ©r   r   r   r   r   &   s    r   c                 C   s   t j | ¡ dS )aw  Deregisters a previously registered storage on a CUDA device as a cufile buffer.

    Example::

        >>> # xdoctest: +SKIP("gds filesystem requirements")
        >>> src = torch.randn(1024, device="cuda")
        >>> s = src.untyped_storage()
        >>> gds_register_buffer(s)
        >>> gds_deregister_buffer(s)

    Args:
        s (Storage): Buffer to register.
    N)r   r   r   r   r   r   r   r   6   s    r   c                   @   sr   e Zd ZdZeedœdd„Zddœdd„Zddœd	d
„Zddœdd„Z	de
eddœdd„Zde
eddœdd„ZdS )r   a“  Wrapper around cuFile.

    cuFile is a file-like interface to the GPUDirect Storage (GDS) API.

    See the `cufile docs <https://docs.nvidia.com/gpudirect-storage/api-reference-guide/index.html#cufile-io-api>`_
    for more details.

    Args:
        filename (str): Name of the file to open.
        flags (int): Flags to pass to ``os.open`` when opening the file. ``os.O_DIRECT`` will
            be added automatically.

    Example::

        >>> # xdoctest: +SKIP("gds filesystem requirements")
        >>> src1 = torch.randn(1024, device="cuda")
        >>> src2 = torch.randn(2, 1024, device="cuda")
        >>> file = torch.cuda.gds.GdsFile(f, os.O_CREAT | os.O_RDWR)
        >>> file.save_storage(src1.untyped_storage(), offset=0)
        >>> file.save_storage(src2.untyped_storage(), offset=src1.nbytes)
        >>> dest1 = torch.empty(1024, device="cuda")
        >>> dest2 = torch.empty(2, 1024, device="cuda")
        >>> file.load_storage(dest1.untyped_storage(), offset=0)
        >>> file.load_storage(dest2.untyped_storage(), offset=src1.nbytes)
        >>> torch.equal(src1, dest1)
        True
        >>> torch.equal(src2, dest2)
        True

    )ÚfilenameÚflagsc                 C   sD   t jdkrtdƒ‚|| _|| _t ||tjB ¡| _d | _	|  
¡  d S )NÚwin32z*GdsFile is not supported on this platform.)ÚsysÚplatformr   r   r   ÚosÚopenÚO_DIRECTÚfdÚhandleÚregister_handle)Úselfr   r   r   r   r   Ú__init__g   s    
zGdsFile.__init__N)r
   c                 C   s"   | j d ur|  ¡  t | j¡ d S )N)r&   Úderegister_handler"   Úcloser%   ©r(   r   r   r   Ú__del__p   s    
zGdsFile.__del__c                 C   s&   | j du sJ dƒ‚tj | j¡| _ dS )zpRegisters file descriptor to cuFile Driver.

        This is a wrapper around ``cuFileHandleRegister``.
        Nz4Cannot register a handle that is already registered.)r&   r   r   r   r%   r,   r   r   r   r'   u   s
    ÿþzGdsFile.register_handlec                 C   s*   | j dusJ dƒ‚tj | j ¡ d| _ dS )zvDeregisters file descriptor from cuFile Driver.

        This is a wrapper around ``cuFileHandleDeregister``.
        Nz2Cannot deregister a handle that is not registered.)r&   r   r   r   r,   r   r   r   r*      s    ÿþzGdsFile.deregister_handler   )ÚstorageÚoffsetr
   c                 C   s(   | j dusJ dƒ‚tj | j ||¡ dS )am  Loads data from the file into the storage.

        This is a wrapper around ``cuFileRead``. ``storage.nbytes()`` of data
        will be loaded from the file at ``offset`` into the storage.

        Args:
            storage (Storage): Storage to load data into.
            offset (int, optional): Offset into the file to start loading from. (Default: 0)
        Nz4Cannot load data from a file that is not registered.)r&   r   r   r   ©r(   r.   r/   r   r   r   Úload_storageŠ   s
    ÿþzGdsFile.load_storagec                 C   s(   | j dusJ dƒ‚tj | j ||¡ dS )aU  Saves data from the storage into the file.

        This is a wrapper around ``cuFileWrite``. All bytes of the storage
        will be written to the file at ``offset``.

        Args:
            storage (Storage): Storage to save data from.
            offset (int, optional): Offset into the file to start saving to. (Default: 0)
        Nz2Cannot save data to a file that is not registered.)r&   r   r   r   r0   r   r   r   Úsave_storage™   s
    ÿþzGdsFile.save_storage)r   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚintr)   r-   r'   r*   r   r1   r2   r   r   r   r   r   G   s   	
r   )r"   r    Útypingr   r   r   Ztorch.typesr   r   Úlistr7   Ú__annotations__r   Úhasattrr   Ú__dict__r   r   r   r   r   r   r   Ú<module>   s*   
