1 | /******************************************************************************* |
2 | * Copyright 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_PRIMITIVE_IFACE_HPP |
18 | #define COMMON_PRIMITIVE_IFACE_HPP |
19 | |
20 | #include <assert.h> |
21 | |
22 | #include "oneapi/dnnl/dnnl.h" |
23 | |
24 | #include "c_types_map.hpp" |
25 | #include "cache_blob.hpp" |
26 | #include "primitive_exec_types.hpp" |
27 | #include "resource.hpp" |
28 | #include "scratchpad.hpp" |
29 | |
30 | namespace dnnl { |
31 | namespace impl { |
32 | status_t primitive_execute( |
33 | const primitive_iface_t *primitive_iface, exec_ctx_t &ctx); |
34 | } |
35 | } // namespace dnnl |
36 | |
37 | // dnnl_primitive is a user facing entity that has an alias primitive_iface_t |
38 | // for internal use. |
39 | // The primitive_iface_t is responsible for holding: |
40 | // 1. impl::primitive_t - a primitive implementation that can be |
41 | // stored in the primitive cache. Other data members are NOT stored in |
42 | // the cache |
43 | // 2. scratchpad_t - a memory for scratchpad |
44 | // 3. primitive_desc_iface_t - an alias for dnnl_primitive_desc and is |
45 | // a user facing primitive descriptor (the one a user should create prior |
46 | // creating a primitive) |
47 | // 4. resource_mapper_t - a resource mapper that provides a mapping between |
48 | // impl::primitive_t and its resource |
49 | // |
50 | // Note: primitive_desc_iface_t and impl::primitive_t share the same |
51 | // impl::primitive_desc_t |
52 | struct dnnl_primitive : public dnnl::impl::c_compatible { |
53 | dnnl_primitive(const std::shared_ptr<dnnl::impl::primitive_t> &primitive, |
54 | dnnl::impl::engine_t *engine); |
55 | |
56 | // This is a ctor for reorder |
57 | dnnl_primitive(const std::shared_ptr<dnnl::impl::primitive_t> &primitive, |
58 | dnnl::impl::engine_t *engine, dnnl::impl::engine_t *src_engine, |
59 | dnnl::impl::engine_t *dst_engine); |
60 | |
61 | dnnl::impl::status_t init(); |
62 | dnnl::impl::engine_t *engine() const; |
63 | const primitive_desc_iface_t *pd() const; |
64 | dnnl::impl::status_t get_cache_blob_size(size_t *size) const; |
65 | dnnl::impl::status_t get_cache_blob( |
66 | dnnl::impl::cache_blob_t cache_blob) const; |
67 | dnnl::impl::status_t execute(dnnl::impl::exec_ctx_t &ctx) const; |
68 | |
69 | void retain() { counter_++; } |
70 | |
71 | void release() { |
72 | if (--counter_ == 0) { delete this; } |
73 | } |
74 | |
75 | protected: |
76 | ~dnnl_primitive(); |
77 | |
78 | private: |
79 | std::atomic<int> counter_; |
80 | std::shared_ptr<dnnl::impl::primitive_t> primitive_; |
81 | std::unique_ptr<dnnl::impl::scratchpad_t> scratchpad_; |
82 | std::unique_ptr<primitive_desc_iface_t> pd_; |
83 | dnnl::impl::resource_mapper_t resource_mapper_; |
84 | |
85 | dnnl_primitive() = delete; |
86 | DNNL_DISALLOW_COPY_AND_ASSIGN(dnnl_primitive); |
87 | }; |
88 | |
89 | #endif |
90 | |
91 | // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s |
92 | |