struct dnnl::memory

Overview

Memory object. More…

#include <dnnl.hpp>

struct memory: public dnnl::handle
{
    // typedefs

    typedef dnnl_dim_t dim;
    typedef std::vector<dim> dims;

    // enums

    enum data_type;
    enum format_kind;
    enum format_tag;
    enum sparse_encoding;

    // structs

    struct desc;

    // construction

    memory();
    memory(const desc& md, const engine& aengine, void* handle);
    memory(const desc& md, const engine& aengine, std::vector<void*> handles);
    memory(const desc& md, const engine& aengine);

    // methods

    template <typename T>
    static void validate_dims(
        const std::vector<T>& v,
        int min_size = 0
        );

    static size_t data_type_size(data_type adata_type);
    static dnnl_data_type_t convert_to_c(data_type adata_type);
    static dnnl_format_tag_t convert_to_c(format_tag format);
    desc get_desc() const;
    engine get_engine() const;
    void* get_data_handle(int index = 0) const;
    void set_data_handle(void* handle, int index = 0) const;

    template <typename T = void>
    T* map_data(int index = 0) const;

    void unmap_data(void* mapped_ptr, int index = 0) const;
    handle();
    handle();
    handle();
    handle();
};

Inherited Members

public:
    // methods

    handle<T, traits>& operator = (const handle<T, traits>&);
    handle<T, traits>& operator = (handle<T, traits>&&);
    void reset(T t, bool weak = false);
    T get(bool allow_empty = false) const;
    operator T () const;
    operator bool () const;
    bool operator == (const handle<T, traits>& other) const;
    bool operator != (const handle& other) const;

Detailed Documentation

Memory object.

A memory object encapsulates a handle to a memory buffer allocated on a specific engine, tensor dimensions, data type, and memory format, which is the way tensor indices map to offsets in linear memory space. Memory objects are passed to primitives during execution.

Typedefs

typedef dnnl_dim_t dim

Integer type for representing dimension sizes and indices.

typedef std::vector<dim> dims

Vector of dimensions.

Implementations are free to force a limit on the vector’s length.

Construction

memory()

Default constructor.

Constructs an empty memory object, which can be used to indicate absence of a parameter.

memory(const desc& md, const engine& aengine, void* handle)

Constructs a memory object.

Unless handle is equal to DNNL_MEMORY_NONE, the constructed memory object will have the underlying buffer set. In this case, the buffer will be initialized as if dnnl::memory::set_data_handle() had been called.

Parameters:

md

Memory descriptor.

aengine

Engine to store the data on.

handle

Handle of the memory buffer to use.

  • A pointer to the user-allocated buffer. In this case the library doesn’t own the buffer.

  • The DNNL_MEMORY_ALLOCATE special value. Instructs the library to allocate the buffer for the memory object. In this case the library owns the buffer.

  • DNNL_MEMORY_NONE to create dnnl::memory without an underlying buffer.

See also:

memory::set_data_handle()

memory(const desc& md, const engine& aengine, std::vector<void*> handles)

Constructs a memory object with multiple handles.

Unless handle is equal to DNNL_MEMORY_NONE, the constructed memory object will have the underlying buffer set. In this case, the buffer will be initialized as if dnnl::memory::set_data_handle() had been called.

Parameters:

md

Memory descriptor.

aengine

Engine to store the data on.

handles

Handles of the memory buffers to use. For each element of the handles vector the following applies:

  • A pointer to the user-allocated buffer. In this case the library doesn’t own the buffer.

  • The DNNL_MEMORY_ALLOCATE special value. Instructs the library to allocate the buffer for the memory object. In this case the library owns the buffer.

  • DNNL_MEMORY_NONE Instructs the library to skip allocation of the memory buffer.

See also:

memory::set_data_handle()

memory(const desc& md, const engine& aengine)

Constructs a memory object.

The underlying buffer(s) for the memory will be allocated by the library.

Parameters:

md

Memory descriptor.

aengine

Engine to store the data on.

Methods

template <typename T>
static void validate_dims(
    const std::vector<T>& v,
    int min_size = 0
    )

Helper function that validates that an std::vector of dimensions can be safely converted to the C API array dnnl_dims_t.

Throws if validation fails.

Parameters:

v

Vector of dimensions.

min_size

Minimum expected size of the vector.

static size_t data_type_size(data_type adata_type)

Returns size of data type in bytes.

Returns:

The number of bytes occupied by data type.

desc get_desc() const

Returns the associated memory descriptor.

engine get_engine() const

Returns the associated engine.

void* get_data_handle(int index = 0) const

Returns an underlying memory buffer that corresponds to the given index.

On the CPU engine, or when using USM, this is a pointer to the allocated memory.

void set_data_handle(void* handle, int index = 0) const

Sets an underlying memory buffer that corresponds to the given index.

Parameters:

handle

Memory buffer to use. On the CPU engine or when USM is used, the memory buffer is a pointer to the actual data. For OpenCL it is a cl_mem. It must have at least dnnl::memory::desc::get_size() bytes allocated.

index

Memory index to attach the buffer. Defaults to 0.

template <typename T = void>
T* map_data(int index = 0) const

Maps a memory object and returns a host-side pointer to a memory buffer with a copy of its contents.

The memory buffer corresponds to the given index.

Mapping enables read/write directly from/to the memory contents for engines that do not support direct memory access.

Mapping is an exclusive operation - a memory object cannot be used in other operations until it is unmapped via dnnl::memory::unmap_data() call.

Note

Any primitives working with the memory should be completed before the memory is mapped. Use dnnl::stream::wait() to synchronize the corresponding execution stream.

Note

The map_data and unmap_data functions are provided mainly for debug and testing purposes and their performance may be suboptimal.

Parameters:

T

Data type to return a pointer to.

index

Index of the buffer. Defaults to 0.

Returns:

Pointer to the mapped memory.

void unmap_data(void* mapped_ptr, int index = 0) const

Unmaps a memory object and writes back any changes made to the previously mapped memory buffer.

The memory buffer corresponds to the given index.

Note

The map_data and unmap_data functions are provided mainly for debug and testing purposes and their performance may be suboptimal.

Parameters:

mapped_ptr

A pointer previously returned by dnnl::memory::map_data().

index

Index of the buffer. Defaults to 0.

handle()

Constructs an empty handle object.

Warning

Uninitialized object cannot be used in most library calls and is equivalent to a null pointer. Any attempt to use its methods, or passing it to the other library function, will cause an exception to be thrown.

handle()

Copy constructor.

handle()

Move constructor.

handle()

Constructs a handle wrapper object from a C API handle.

Parameters:

t

The C API handle to wrap.

weak

A flag specifying whether to construct a weak wrapper; defaults to false.