#ifndef COMMON_MEMORY_HPP
#define COMMON_MEMORY_HPP
#include <assert.h>
#include <memory>
#include "oneapi/dnnl/dnnl.h"
#include "c_types_map.hpp"
#include "memory_desc_wrapper.hpp"
#include "memory_storage.hpp"
#include "nstl.hpp"
#include "utils.hpp"
namespace dnnl {
namespace impl {
struct exec_ctx_t;
enum memory_flags_t {
alloc = 0x1,
use_runtime_ptr = 0x2,
prefer_device_usm = 0x4
};
} }
struct dnnl_memory : public dnnl::impl::c_compatible {
dnnl_memory(dnnl::impl::engine_t *engine,
const dnnl::impl::memory_desc_t *md,
const std::vector<unsigned> &flags,
const std::vector<void *> &handles);
dnnl_memory(dnnl::impl::engine_t *engine,
const dnnl::impl::memory_desc_t *md, unsigned flags, void *handle)
: dnnl_memory(engine, md, std::vector<unsigned> {flags},
std::vector<void *> {handle}) {}
dnnl_memory(dnnl::impl::engine_t *engine,
const dnnl::impl::memory_desc_t *md,
std::unique_ptr<dnnl::impl::memory_storage_t> &&memory_storage);
dnnl_memory(dnnl::impl::engine_t *engine,
const dnnl::impl::memory_desc_t *md,
std::vector<std::unique_ptr<dnnl::impl::memory_storage_t>>
&&memory_storage);
dnnl::impl::engine_t *engine() const { return engine_; }
const dnnl::impl::memory_desc_t *md() const { return &md_; }
dnnl::impl::memory_storage_t *memory_storage(int index = 0) const {
if (index >= (int)memory_storages_.size()) return nullptr;
return memory_storages_[index].get();
}
dnnl::impl::memory_storage_t *memory_storage_clean(
const dnnl::impl::exec_ctx_t &ctx,
dnnl::impl::status_t &status) const {
status = zero_pad(ctx);
return memory_storage(0);
}
dnnl::impl::status_t get_data_handle(void **handle, int index = 0) const {
auto ms = memory_storage(index);
if (!ms) return dnnl::impl::status::invalid_arguments;
return ms->get_data_handle(handle);
}
dnnl::impl::status_t set_data_handle(void *handle, int index = 0) const;
dnnl::impl::status_t zero_pad(const dnnl::impl::exec_ctx_t &ctx) const;
dnnl::impl::status_t reset_memory_storage(
std::unique_ptr<dnnl::impl::memory_storage_t> &&memory_storage);
size_t get_num_handles() const { return memory_storages_.size(); }
void retain() { counter_++; }
void release() {
if (--counter_ == 0) { delete this; }
}
protected:
virtual ~dnnl_memory() = default;
dnnl::impl::engine_t *engine_;
const dnnl::impl::memory_desc_t md_;
private:
dnnl_memory() = delete;
DNNL_DISALLOW_COPY_AND_ASSIGN(dnnl_memory);
std::vector<std::unique_ptr<dnnl::impl::memory_storage_t>> memory_storages_;
std::atomic<int> counter_;
};
namespace dnnl {
namespace impl {
struct memory_deleter_t {
void operator()(memory_t *m) const { m->release(); }
};
} }
#endif