Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/gapi/src/compiler/gislandmodel.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_GISLANDMODEL_HPP
9
#define OPENCV_GAPI_GISLANDMODEL_HPP
10
11
#include <unordered_set>
12
#include <memory> // shared_ptr
13
14
#include <ade/graph.hpp>
15
#include <ade/typed_graph.hpp>
16
#include <ade/passes/topological_sort.hpp>
17
18
#include "opencv2/gapi/util/optional.hpp"
19
#include "opencv2/gapi/gkernel.hpp"
20
21
#include "compiler/gobjref.hpp"
22
23
namespace cv { namespace gimpl {
24
25
26
// FIXME: GAPI_EXPORTS only because of tests!
27
class GAPI_EXPORTS GIsland
28
{
29
public:
30
using node_set = std::unordered_set
31
< ade::NodeHandle
32
, ade::HandleHasher<ade::Node>
33
>;
34
35
// Initial constructor (constructs a single-op Island)
36
GIsland(const gapi::GBackend &bknd,
37
ade::NodeHandle op,
38
util::optional<std::string>&& user_tag);
39
40
// Merged constructor
41
GIsland(const gapi::GBackend &bknd,
42
node_set &&all,
43
node_set &&in_ops,
44
node_set &&out_ops,
45
util::optional<std::string>&& user_tag);
46
47
const node_set& contents() const;
48
const node_set& in_ops() const;
49
const node_set& out_ops() const;
50
51
std::string name() const;
52
gapi::GBackend backend() const;
53
54
/**
55
* Returns all GModel operation node handles which are _reading_
56
* from a GModel data object associated (wrapped in) the given
57
* Slot object.
58
*
59
* @param g an ade::Graph with GIslandModel information inside
60
* @param slot_nh Slot object node handle of interest
61
* @return a set of GModel operation node handles
62
*/
63
node_set consumers(const ade::Graph &g,
64
const ade::NodeHandle &slot_nh) const;
65
66
/**
67
* Returns a GModel operation node handle which is _writing_
68
* to a GModel data object associated (wrapped in) the given
69
* Slot object.
70
*
71
* @param g an ade::Graph with GIslandModel information inside
72
* @param slot_nh Slot object node handle of interest
73
* @return a node handle of original GModel
74
*/
75
ade::NodeHandle producer(const ade::Graph &g,
76
const ade::NodeHandle &slot_nh) const;
77
78
void debug() const;
79
bool is_user_specified() const;
80
81
protected:
82
gapi::GBackend m_backend; // backend which handles this Island execution
83
84
node_set m_all; // everything (data + operations) within an island
85
node_set m_in_ops; // operations island begins with
86
node_set m_out_ops; // operations island ends with
87
88
// has island name IF specified by user. Empty for internal (inferred) islands
89
util::optional<std::string> m_user_tag;
90
};
91
92
93
94
// GIslandExecutable - a backend-specific thing which executes
95
// contents of an Island
96
// * Is instantiated by the last step of the Islands fusion procedure;
97
// * Is orchestrated by a GExecutor instance.
98
//
99
class GIslandExecutable
100
{
101
public:
102
using InObj = std::pair<RcDesc, cv::GRunArg>;
103
using OutObj = std::pair<RcDesc, cv::GRunArgP>;
104
105
// FIXME: now run() requires full input vector to be available.
106
// actually, parts of subgraph may execute even if there's no all data
107
// slots in place.
108
// TODO: Add partial execution capabilities
109
virtual void run(std::vector<InObj> &&input_objs,
110
std::vector<OutObj> &&output_objs) = 0;
111
112
virtual ~GIslandExecutable() = default;
113
};
114
115
116
117
// Couldn't reuse NodeType here - FIXME unify (move meta to a shared place)
118
struct NodeKind
119
{
120
static const char *name() { return "NodeKind"; }
121
enum { ISLAND, SLOT} k;
122
};
123
124
// FIXME: Rename to Island (as soon as current GModel::Island is renamed
125
// to IslandTag).
126
struct FusedIsland
127
{
128
static const char *name() { return "FusedIsland"; }
129
std::shared_ptr<GIsland> object;
130
};
131
132
struct DataSlot
133
{
134
static const char *name() { return "DataSlot"; }
135
ade::NodeHandle original_data_node; // direct link to GModel
136
};
137
138
struct IslandExec
139
{
140
static const char *name() { return "IslandExecutable"; }
141
std::shared_ptr<GIslandExecutable> object;
142
};
143
144
namespace GIslandModel
145
{
146
using Graph = ade::TypedGraph
147
< NodeKind
148
, FusedIsland
149
, DataSlot
150
, IslandExec
151
, ade::passes::TopologicalSortData
152
>;
153
154
// FIXME: derive from TypedGraph
155
using ConstGraph = ade::ConstTypedGraph
156
< NodeKind
157
, FusedIsland
158
, DataSlot
159
, IslandExec
160
, ade::passes::TopologicalSortData
161
>;
162
163
// Top-level function
164
void generateInitial(Graph &g, const ade::Graph &src_g);
165
// "Building blocks"
166
ade::NodeHandle mkSlotNode(Graph &g, const ade::NodeHandle &data_nh);
167
ade::NodeHandle mkIslandNode(Graph &g, const gapi::GBackend &bknd, const ade::NodeHandle &op_nh, const ade::Graph &orig_g);
168
ade::NodeHandle mkIslandNode(Graph &g, std::shared_ptr<GIsland>&& isl);
169
170
// GIslandModel API
171
void syncIslandTags(Graph &g, ade::Graph &orig_g);
172
void compileIslands(Graph &g, const ade::Graph &orig_g, const GCompileArgs &args);
173
174
// Debug routines
175
// producerOf - returns an Island handle which produces given data object
176
// from the original model (! don't mix with DataSlot)
177
// FIXME: GAPI_EXPORTS because of tests only!
178
ade::NodeHandle GAPI_EXPORTS producerOf(const ConstGraph &g, ade::NodeHandle &data_nh);
179
180
} // namespace GIslandModel
181
182
}} // namespace cv::gimpl
183
184
#endif // OPENCV_GAPI_GISLANDMODEL_HPP
185
186