1/*******************************************************************************
2* Copyright 2018-2022 Intel Corporation
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*******************************************************************************/
16
17#ifndef COMMON_MEMORY_HPP
18#define COMMON_MEMORY_HPP
19
20#include <assert.h>
21#include <memory>
22
23#include "oneapi/dnnl/dnnl.h"
24
25#include "c_types_map.hpp"
26#include "memory_desc_wrapper.hpp"
27#include "memory_storage.hpp"
28#include "nstl.hpp"
29#include "utils.hpp"
30
31namespace dnnl {
32namespace impl {
33
34struct exec_ctx_t;
35
36enum memory_flags_t { alloc = 0x1, use_runtime_ptr = 0x2 };
37} // namespace impl
38} // namespace dnnl
39
40struct dnnl_memory : public dnnl::impl::c_compatible {
41 /** XXX: Parameter flags must contain either alloc or use_runtime_ptr from
42 * memory_flags_t. */
43 dnnl_memory(dnnl::impl::engine_t *engine,
44 const dnnl::impl::memory_desc_t *md, unsigned flags, void *handle);
45 dnnl_memory(dnnl::impl::engine_t *engine,
46 const dnnl::impl::memory_desc_t *md,
47 std::unique_ptr<dnnl::impl::memory_storage_t> &&memory_storage);
48 virtual ~dnnl_memory() = default;
49
50 /** returns memory's engine */
51 dnnl::impl::engine_t *engine() const { return engine_; }
52 /** returns memory's description */
53 const dnnl::impl::memory_desc_t *md() const { return &md_; }
54 /** returns the underlying memory storage */
55 dnnl::impl::memory_storage_t *memory_storage() const {
56 return memory_storage_.get();
57 }
58 /** returns the underlying memory storage */
59 dnnl::impl::memory_storage_t *memory_storage_clean(
60 const dnnl::impl::exec_ctx_t &ctx,
61 dnnl::impl::status_t &status) const {
62 status = zero_pad(ctx);
63 return memory_storage_.get();
64 }
65 /** returns the underlying memory storage */
66 dnnl::impl::memory_storage_t *memory_storage_clean(
67 const dnnl::impl::exec_ctx_t &ctx) const {
68 zero_pad(ctx);
69 return memory_storage_.get();
70 }
71 /** returns data handle */
72 dnnl::impl::status_t get_data_handle(void **handle) const {
73 return memory_storage()->get_data_handle(handle);
74 }
75
76 /** sets data handle */
77 dnnl::impl::status_t set_data_handle(void *handle);
78
79 /** zeros padding */
80 dnnl::impl::status_t zero_pad(const dnnl::impl::exec_ctx_t &ctx) const;
81
82 dnnl::impl::status_t reset_memory_storage(
83 std::unique_ptr<dnnl::impl::memory_storage_t> &&memory_storage);
84
85protected:
86 dnnl::impl::engine_t *engine_;
87 const dnnl::impl::memory_desc_t md_;
88
89private:
90 dnnl_memory() = delete;
91 DNNL_DISALLOW_COPY_AND_ASSIGN(dnnl_memory);
92
93 std::unique_ptr<dnnl::impl::memory_storage_t> memory_storage_;
94};
95
96#endif
97