Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / common / primitive.hpp
1 /*******************************************************************************
2 * Copyright 2016-2018 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 PRIMITIVE_HPP
18 #define PRIMITIVE_HPP
19
20 #include <assert.h>
21
22 #include "mkldnn.h"
23
24 #include "c_types_map.hpp"
25 #include "event.hpp"
26 #include "nstl.hpp"
27 #include "primitive_desc.hpp"
28
29 /** \brief A pure virtual primitive class
30  *
31  * Primitive contains links to its inputs & outputs, though it does not track
32  * their readiness on execution step.
33  *
34  * @remark @b Rational.
35  *   Dependencies are essential through-out the whole MKL-DNN library, so it
36  *   makes sense to include them on the very low level. On the other hand,
37  *   tracking them should be a task for corresponding essence, like scheduler,
38  *   stream or whatever. Primitive itself should know nothing about the
39  *   environment it is running in.
40  *
41  * @note
42  *   To make user experience better we should provide API which allows
43  *   achieving the best (or good enough) performance when creating primitives
44  *   in natural order: i.e. from bottom to top for forward pass and from top to
45  *   bottom for backward pass. Please consider restriction [1] in Level 0.
46  */
47 struct mkldnn_primitive: public mkldnn::impl::c_compatible {
48     typedef mkldnn::impl::nstl::vector<mkldnn::impl::primitive_at_t>
49         input_vector;
50     typedef mkldnn::impl::nstl::vector<const mkldnn::impl::primitive_t *>
51         output_vector;
52
53     mkldnn_primitive(const mkldnn::impl::primitive_desc_t *pd,
54             const input_vector &inputs, const output_vector &outputs)
55         : pd_(pd->clone())
56         , inputs_(inputs)
57         , outputs_(outputs)
58     {}
59     virtual ~mkldnn_primitive() { delete pd_; }
60
61     /** returns primitive's engine */
62     mkldnn::impl::engine_t *engine() const { return pd_->engine(); }
63     /** returns primitive's inputs */
64     const input_vector &inputs() const { return inputs_; }
65     /** returns primitive's outputs */
66     const output_vector &outputs() const { return outputs_; }
67     /** returns primitive's primitive desc */
68     const mkldnn::impl::primitive_desc_t *pd() const { return pd_; }
69     /** returns primitive's kind */
70     mkldnn::impl::primitive_kind_t kind() const { return pd_->kind(); }
71
72     /** executes primitive with resulting event @p e
73      *
74      * @p e (output)
75      *   a resulting event. It is primitive responsibility to set @p e state
76      *   after actual computations are done
77      *
78      * @remark @b Rational.
79      *   Suppose engine has a task pool and for some reasons submission failed.
80      *   In this case primitive will set @p e's state to event::error
81      */
82     virtual void execute(mkldnn::impl::event_t *e) const = 0;
83
84     /** returns data handle. Applicable for memory primitives only. */
85     virtual mkldnn::impl::status_t get_data_handle(void **handle) const {
86         UNUSED(handle);
87         assert(this->kind() == mkldnn::impl::primitive_kind::memory);
88         return mkldnn::impl::status::invalid_arguments;
89     }
90     /** sets data handle. Applicable for memory primitives only. */
91     virtual mkldnn::impl::status_t set_data_handle(void *handle) {
92         UNUSED(handle);
93         assert(this->kind() == mkldnn::impl::primitive_kind::memory);
94         return mkldnn::impl::status::invalid_arguments;
95     }
96
97 protected:
98     const mkldnn::impl::primitive_desc_t *pd_;
99     input_vector inputs_;
100     output_vector outputs_;
101
102 private:
103     mkldnn_primitive() = delete;
104     mkldnn_primitive(const mkldnn_primitive &) = delete;
105     mkldnn_primitive(mkldnn_primitive &&) = delete;
106     mkldnn_primitive &operator=(const mkldnn_primitive &) = delete;
107     mkldnn_primitive &operator=(mkldnn_primitive &&) = delete;
108 };
109
110 #endif
111
112 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s