Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/gapi/src/compiler/gmodel.hpp
16337 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
//
5
// Copyright (C) 2018 Intel Corporation
6
7
8
#ifndef OPENCV_GAPI_GMODEL_HPP
9
#define OPENCV_GAPI_GMODEL_HPP
10
11
#include <memory> // shared_ptr
12
#include <unordered_map>
13
#include <functional> // std::function
14
15
#include <ade/graph.hpp>
16
#include <ade/typed_graph.hpp>
17
#include <ade/passes/topological_sort.hpp>
18
19
// /!\ ATTENTION:
20
//
21
// No API includes like GMat, GNode, GCall here!
22
// This part of the system is API-unaware by its design.
23
//
24
25
#include "opencv2/gapi/garg.hpp"
26
#include "opencv2/gapi/gkernel.hpp"
27
#include "api/gapi_priv.hpp" // GShape
28
#include "api/gproto_priv.hpp" // origin_of
29
#include "backends/common/gbackend.hpp"
30
31
#include "compiler/gobjref.hpp"
32
#include "compiler/gislandmodel.hpp"
33
34
namespace cv { namespace gimpl {
35
36
// TODO: Document all metadata types
37
38
struct NodeType
39
{
40
static const char *name() { return "NodeType"; }
41
enum { OP, DATA } t;
42
};
43
44
struct Input
45
{
46
static const char *name() { return "Input"; }
47
std::size_t port;
48
};
49
50
struct Output
51
{
52
static const char *name() { return "Output"; }
53
std::size_t port;
54
};
55
56
struct Op
57
{
58
static const char *name() { return "Op"; }
59
cv::GKernel k;
60
std::vector<GArg> args; // TODO: Introduce a new type for internal args?
61
std::vector<RcDesc> outs; // TODO: Introduce a new type for resource references
62
63
cv::gapi::GBackend backend;
64
util::any opaque;
65
};
66
67
struct Data
68
{
69
static const char *name() { return "Data"; }
70
71
// FIXME: This is a _pure_ duplication of RcDesc now! (except storage)
72
GShape shape; // FIXME: Probably to be replaced by GMetaArg?
73
int rc;
74
GMetaArg meta;
75
HostCtor ctor; // T-specific helper to deal with unknown types in our code
76
// FIXME: Why rc+shape+meta is not represented as RcDesc here?
77
78
enum class Storage
79
{
80
INTERNAL, // data object is not listed in GComputation protocol
81
INPUT, // data object is listed in GComputation protocol as Input
82
OUTPUT, // data object is listed in GComputation protocol as Output
83
CONST, // data object is constant
84
};
85
Storage storage;
86
};
87
88
struct ConstValue
89
{
90
static const char *name() { return "ConstValue"; }
91
GRunArg arg;
92
};
93
94
// This metadata is valid for both DATA and OP kinds of nodes
95
// FIXME: Rename to IslandTag
96
struct Island
97
{
98
static const char *name() { return "Island"; }
99
std::string island; // can be set by user, otherwise is set by fusion
100
};
101
102
struct Protocol
103
{
104
static const char *name() { return "Protocol"; }
105
// TODO: Replace the whole thing with a "Protocol" object
106
std::vector<RcDesc> inputs;
107
std::vector<RcDesc> outputs;
108
109
std::vector<ade::NodeHandle> in_nhs;
110
std::vector<ade::NodeHandle> out_nhs;
111
};
112
113
struct OutputMeta
114
{
115
static const char *name() { return "OutputMeta"; }
116
GMetaArgs outMeta;
117
};
118
119
struct Journal
120
{
121
static const char *name() { return "Journal"; }
122
std::vector<std::string> messages;
123
};
124
125
// The mapping between user-side GMat/GScalar/... objects
126
// and its appropriate nodes. Can be stored in graph optionally
127
// (NOT used by any compiler or backends, introspection purposes
128
// only)
129
struct Layout
130
{
131
static const char *name() { return "Layout"; }
132
GOriginMap<ade::NodeHandle> object_nodes;
133
};
134
135
// Unique data object counter (per-type)
136
class DataObjectCounter
137
{
138
public:
139
static const char* name() { return "DataObjectCounter"; }
140
int GetNewId(GShape shape) { return m_next_data_id[shape]++; }
141
private:
142
std::unordered_map<cv::GShape, int> m_next_data_id;
143
};
144
145
// A projected graph of Islands (generated from graph of Operations)
146
struct IslandModel
147
{
148
static const char* name() { return "IslandModel"; }
149
std::shared_ptr<ade::Graph> model;
150
};
151
152
// List of backends selected for current graph execution
153
struct ActiveBackends
154
{
155
static const char *name() { return "ActiveBackends"; }
156
std::unordered_set<cv::gapi::GBackend> backends;
157
};
158
159
namespace GModel
160
{
161
using Graph = ade::TypedGraph
162
< NodeType
163
, Input
164
, Output
165
, Op
166
, Data
167
, ConstValue
168
, Island
169
, Protocol
170
, OutputMeta
171
, Journal
172
, ade::passes::TopologicalSortData
173
, DataObjectCounter
174
, Layout
175
, IslandModel
176
, ActiveBackends
177
>;
178
179
// FIXME: How to define it based on GModel???
180
using ConstGraph = ade::ConstTypedGraph
181
< NodeType
182
, Input
183
, Output
184
, Op
185
, Data
186
, ConstValue
187
, Island
188
, Protocol
189
, OutputMeta
190
, Journal
191
, ade::passes::TopologicalSortData
192
, DataObjectCounter
193
, Layout
194
, IslandModel
195
, ActiveBackends
196
>;
197
198
// User should initialize graph before using it
199
// GAPI_EXPORTS for tests
200
GAPI_EXPORTS void init (Graph& g);
201
202
ade::NodeHandle mkOpNode(Graph &g, const GKernel &k, const std::vector<GArg>& args, const std::string &island);
203
204
// FIXME: change it to take GMeta instead of GShape?
205
ade::NodeHandle mkDataNode(Graph &g, const GOrigin& origin);
206
207
// Adds a string message to a node. Any node can be subject of log, messages then
208
// appear in the dumped .dot file.x
209
void log(Graph &g, ade::NodeHandle op, std::string &&message, ade::NodeHandle updater = ade::NodeHandle());
210
void log(Graph &g, ade::EdgeHandle op, std::string &&message, ade::NodeHandle updater = ade::NodeHandle());
211
212
void linkIn (Graph &g, ade::NodeHandle op, ade::NodeHandle obj, std::size_t in_port);
213
void linkOut (Graph &g, ade::NodeHandle op, ade::NodeHandle obj, std::size_t out_port);
214
215
// FIXME: Align this GModel API properly, it is a mess now
216
namespace detail
217
{
218
// FIXME: GAPI_EXPORTS only because of tests!!!
219
GAPI_EXPORTS ade::NodeHandle dataNodeOf(const ConstGraph& g, const GOrigin &origin);
220
}
221
template<typename T> inline ade::NodeHandle dataNodeOf(const ConstGraph& g, T &&t)
222
{
223
return detail::dataNodeOf(g, cv::gimpl::proto::origin_of(GProtoArg{t}));
224
}
225
226
void linkIn (Graph &g, ade::NodeHandle op, ade::NodeHandle obj, std::size_t in_port);
227
void linkOut (Graph &g, ade::NodeHandle op, ade::NodeHandle obj, std::size_t out_port);
228
229
void redirectReaders(Graph &g, ade::NodeHandle from, ade::NodeHandle to);
230
void redirectWriter (Graph &g, ade::NodeHandle from, ade::NodeHandle to);
231
232
std::vector<ade::NodeHandle> orderedInputs (Graph &g, ade::NodeHandle nh);
233
std::vector<ade::NodeHandle> orderedOutputs(Graph &g, ade::NodeHandle nh);
234
235
// Returns input meta array for given op node
236
// Array is sparse, as metadata for non-gapi input objects is empty
237
// TODO:
238
// Cover with tests!!
239
GMetaArgs collectInputMeta(GModel::ConstGraph cg, ade::NodeHandle node);
240
GMetaArgs collectOutputMeta(GModel::ConstGraph cg, ade::NodeHandle node);
241
242
ade::EdgeHandle getInEdgeByPort(const GModel::ConstGraph& cg, const ade::NodeHandle& nh, std::size_t in_port);
243
244
// Returns true if the given backend participates in the execution
245
bool isActive(const GModel::Graph &cg, const cv::gapi::GBackend &backend);
246
} // namespace GModel
247
248
249
}} // namespace cv::gimpl
250
251
#endif // OPENCV_GAPI_GMODEL_HPP
252
253