2018-09-27 02:50:39 +08:00
|
|
|
// This file is part of OpenCV project.
|
|
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2018 Intel Corporation
|
|
|
|
|
|
|
|
|
2018-09-28 23:42:09 +08:00
|
|
|
#include "precomp.hpp"
|
|
|
|
|
2018-09-27 02:50:39 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include <ade/util/checked_cast.hpp>
|
|
|
|
|
|
|
|
#include "api/gbackend_priv.hpp" // GBackend::Priv().compile()
|
|
|
|
#include "compiler/gmodel.hpp"
|
|
|
|
#include "compiler/gislandmodel.hpp"
|
2019-11-27 23:21:00 +08:00
|
|
|
#include "compiler/gmodel.hpp"
|
|
|
|
|
2018-09-27 02:50:39 +08:00
|
|
|
#include "logger.hpp" // GAPI_LOG
|
|
|
|
|
|
|
|
namespace cv { namespace gimpl {
|
|
|
|
|
|
|
|
GIsland::GIsland(const gapi::GBackend &bknd,
|
|
|
|
ade::NodeHandle op,
|
|
|
|
util::optional<std::string> &&user_tag)
|
|
|
|
: m_backend(bknd)
|
|
|
|
, m_user_tag(std::move(user_tag))
|
|
|
|
{
|
|
|
|
m_all.insert(op);
|
|
|
|
m_in_ops.insert(op);
|
|
|
|
m_out_ops.insert(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
// _ because of gcc4.8 wanings on ARM
|
|
|
|
GIsland::GIsland(const gapi::GBackend &_bknd,
|
|
|
|
node_set &&_all,
|
|
|
|
node_set &&_in_ops,
|
|
|
|
node_set &&_out_ops,
|
|
|
|
util::optional<std::string> &&_user_tag)
|
|
|
|
: m_backend(_bknd)
|
|
|
|
, m_all(std::move(_all))
|
|
|
|
, m_in_ops(std::move(_in_ops))
|
|
|
|
, m_out_ops(std::move(_out_ops))
|
|
|
|
, m_user_tag(std::move(_user_tag))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const GIsland::node_set& GIsland::contents() const
|
|
|
|
{
|
|
|
|
return m_all;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GIsland::node_set& GIsland::in_ops() const
|
|
|
|
{
|
|
|
|
return m_in_ops;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GIsland::node_set& GIsland::out_ops() const
|
|
|
|
{
|
|
|
|
return m_out_ops;
|
|
|
|
}
|
|
|
|
|
|
|
|
gapi::GBackend GIsland::backend() const
|
|
|
|
{
|
|
|
|
return m_backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GIsland::is_user_specified() const
|
|
|
|
{
|
|
|
|
return m_user_tag.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GIsland::debug() const
|
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
stream << name() << " {{\n input ops: ";
|
|
|
|
for (const auto& nh : m_in_ops) stream << nh << "; ";
|
|
|
|
stream << "\n output ops: ";
|
|
|
|
for (const auto& nh : m_out_ops) stream << nh << "; ";
|
|
|
|
stream << "\n contents: ";
|
|
|
|
for (const auto& nh : m_all) stream << nh << "; ";
|
|
|
|
stream << "\n}}" << std::endl;
|
|
|
|
GAPI_LOG_INFO(NULL, stream.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
GIsland::node_set GIsland::consumers(const ade::Graph &g,
|
|
|
|
const ade::NodeHandle &slot_nh) const
|
|
|
|
{
|
|
|
|
GIslandModel::ConstGraph gim(g);
|
|
|
|
auto data_nh = gim.metadata(slot_nh).get<DataSlot>().original_data_node;
|
|
|
|
GIsland::node_set result;
|
|
|
|
for (const auto& in_op : m_in_ops)
|
|
|
|
{
|
|
|
|
auto it = std::find(in_op->inNodes().begin(),
|
|
|
|
in_op->inNodes().end(),
|
|
|
|
data_nh);
|
|
|
|
if (it != in_op->inNodes().end())
|
|
|
|
result.insert(in_op);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ade::NodeHandle GIsland::producer(const ade::Graph &g,
|
|
|
|
const ade::NodeHandle &slot_nh) const
|
|
|
|
{
|
|
|
|
GIslandModel::ConstGraph gim(g);
|
|
|
|
auto data_nh = gim.metadata(slot_nh).get<DataSlot>().original_data_node;
|
|
|
|
for (const auto& out_op : m_out_ops)
|
|
|
|
{
|
|
|
|
auto it = std::find(out_op->outNodes().begin(),
|
|
|
|
out_op->outNodes().end(),
|
|
|
|
data_nh);
|
|
|
|
if (it != out_op->outNodes().end())
|
|
|
|
return out_op;
|
|
|
|
}
|
|
|
|
// Consistency: A GIsland requested for producer() of slot_nh should
|
|
|
|
// always had the appropriate GModel node handle in its m_out_ops vector.
|
|
|
|
GAPI_Assert(false);
|
|
|
|
return ade::NodeHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GIsland::name() const
|
|
|
|
{
|
|
|
|
if (is_user_specified())
|
|
|
|
return m_user_tag.value();
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "island_#" << std::hex << static_cast<const void*>(this);
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GIslandModel::generateInitial(GIslandModel::Graph &g,
|
|
|
|
const ade::Graph &src_graph)
|
|
|
|
{
|
|
|
|
const GModel::ConstGraph src_g(src_graph);
|
|
|
|
|
|
|
|
// Initially GIslandModel is a 1:1 projection from GModel:
|
|
|
|
// 1) Every GModel::OP becomes a separate GIslandModel::FusedIsland;
|
|
|
|
// 2) Every GModel::DATA becomes GIslandModel::DataSlot;
|
|
|
|
// 3) Single-operation FusedIslands are connected with DataSlots in the
|
|
|
|
// same way as OPs and DATA (edges with the same metadata)
|
|
|
|
|
|
|
|
using node_set = std::unordered_set
|
|
|
|
< ade::NodeHandle
|
|
|
|
, ade::HandleHasher<ade::Node>
|
|
|
|
>;
|
|
|
|
using node_map = std::unordered_map
|
|
|
|
< ade::NodeHandle
|
|
|
|
, ade::NodeHandle
|
|
|
|
, ade::HandleHasher<ade::Node>
|
|
|
|
>;
|
|
|
|
|
|
|
|
node_set all_operations;
|
|
|
|
node_map data_to_slot;
|
|
|
|
|
|
|
|
// First, list all operations and build create DataSlots in <g>
|
|
|
|
for (auto src_nh : src_g.nodes())
|
|
|
|
{
|
|
|
|
switch (src_g.metadata(src_nh).get<NodeType>().t)
|
|
|
|
{
|
|
|
|
case NodeType::OP: all_operations.insert(src_nh); break;
|
|
|
|
case NodeType::DATA: data_to_slot[src_nh] = mkSlotNode(g, src_nh); break;
|
|
|
|
default: GAPI_Assert(false); break;
|
|
|
|
}
|
|
|
|
} // for (src_g.nodes)
|
|
|
|
|
|
|
|
// Now put single-op islands and connect it with DataSlots
|
|
|
|
for (auto src_op_nh : all_operations)
|
|
|
|
{
|
|
|
|
auto nh = mkIslandNode(g, src_g.metadata(src_op_nh).get<Op>().backend, src_op_nh, src_graph);
|
|
|
|
for (auto in_edge : src_op_nh->inEdges())
|
|
|
|
{
|
|
|
|
auto src_data_nh = in_edge->srcNode();
|
|
|
|
auto isl_slot_nh = data_to_slot.at(src_data_nh);
|
|
|
|
g.link(isl_slot_nh, nh); // no other data stored yet
|
|
|
|
}
|
|
|
|
for (auto out_edge : src_op_nh->outEdges())
|
|
|
|
{
|
|
|
|
auto dst_data_nh = out_edge->dstNode();
|
|
|
|
auto isl_slot_nh = data_to_slot.at(dst_data_nh);
|
|
|
|
g.link(nh, isl_slot_nh);
|
|
|
|
}
|
|
|
|
} // for(all_operations)
|
|
|
|
}
|
|
|
|
|
|
|
|
ade::NodeHandle GIslandModel::mkSlotNode(Graph &g, const ade::NodeHandle &data_nh)
|
|
|
|
{
|
|
|
|
auto nh = g.createNode();
|
|
|
|
g.metadata(nh).set(DataSlot{data_nh});
|
|
|
|
g.metadata(nh).set(NodeKind{NodeKind::SLOT});
|
|
|
|
return nh;
|
|
|
|
}
|
|
|
|
|
|
|
|
ade::NodeHandle GIslandModel::mkIslandNode(Graph &g, const gapi::GBackend& bknd, const ade::NodeHandle &op_nh, const ade::Graph &orig_g)
|
|
|
|
{
|
|
|
|
const GModel::ConstGraph src_g(orig_g);
|
|
|
|
util::optional<std::string> user_tag;
|
|
|
|
if (src_g.metadata(op_nh).contains<Island>())
|
|
|
|
{
|
|
|
|
user_tag = util::make_optional(src_g.metadata(op_nh).get<Island>().island);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto nh = g.createNode();
|
|
|
|
std::shared_ptr<GIsland> island(new GIsland(bknd, op_nh, std::move(user_tag)));
|
|
|
|
g.metadata(nh).set(FusedIsland{std::move(island)});
|
|
|
|
g.metadata(nh).set(NodeKind{NodeKind::ISLAND});
|
|
|
|
return nh;
|
|
|
|
}
|
|
|
|
|
|
|
|
ade::NodeHandle GIslandModel::mkIslandNode(Graph &g, std::shared_ptr<GIsland>&& isl)
|
|
|
|
{
|
|
|
|
ade::NodeHandle nh = g.createNode();
|
|
|
|
g.metadata(nh).set(cv::gimpl::NodeKind{cv::gimpl::NodeKind::ISLAND});
|
|
|
|
g.metadata(nh).set<cv::gimpl::FusedIsland>({std::move(isl)});
|
|
|
|
return nh;
|
|
|
|
}
|
|
|
|
|
Merge pull request #15216 from dmatveev:dm/ng-0010-g-api-streaming-api
* G-API-NG/Streaming: Introduced a Streaming API
Now a GComputation can be compiled in a special "streaming" way
and then "played" on a video stream.
Currently only VideoCapture is supported as an input source.
* G-API-NG/Streaming: added threading & real streaming
* G-API-NG/Streaming: Added tests & docs on Copy kernel
- Added very simple pipeline tests, not all data types are covered yet
(in fact, only GMat is tested now);
- Started testing non-OCV backends in the streaming mode;
- Added required fixes to Fluid backend, likely it works OK now;
- Added required fixes to OCL backend, and now it is likely broken
- Also added a UMat-based (OCL) version of Copy kernel
* G-API-NG/Streaming: Added own concurrent queue class
- Used only if TBB is not available
* G-API-NG/Streaming: Fixing various issues
- Added missing header to CMakeLists.txt
- Fixed various CI issues and warnings
* G-API-NG/Streaming: Fixed a compile-time GScalar queue deadlock
- GStreamingExecutor blindly created island's input queues for
compile-time (value-initialized) GScalars which didn't have any
producers, making island actor threads wait there forever
* G-API-NG/Streaming: Dropped own version of Copy kernel
One was added into master already
* G-API-NG/Streaming: Addressed GArray<T> review comments
- Added tests on mov()
- Removed unnecessary changes in garray.hpp
* G-API-NG/Streaming: Added Doxygen comments to new public APIs
Also fixed some other comments in the code
* G-API-NG/Streaming: Removed debug info, added some comments & renamed vars
* G-API-NG/Streaming: Fixed own-vs-cv abstraction leak
- Now every island is triggered with own:: (instead of cv::)
data objects as inputs;
- Changes in Fluid backend required to support cv::Mat/Scalar were
reverted;
* G-API-NG/Streaming: use holds_alternative<> instead of index/index_of test
- Also fixed regression test comments
- Also added metadata check comments for GStreamingCompiled
* G-API-NG/Streaming: Made start()/stop() more robust
- Fixed various possible deadlocks
- Unified the shutdown code
- Added more tests covering different corner cases on start/stop
* G-API-NG/Streaming: Finally fixed Windows crashes
In fact the problem hasn't been Windows-only.
Island thread popped data from queues without preserving the Cmd
objects and without taking the ownership over data acquired so when
islands started to process the data, this data may be already freed.
Linux version worked only by occasion.
* G-API-NG/Streaming: Fixed (I hope so) Windows warnings
* G-API-NG/Streaming: fixed typos in internal comments
- Also added some more explanation on Streaming/OpenCL status
* G-API-NG/Streaming: Added more unit tests on streaming
- Various start()/stop()/setSource() call flow combinations
* G-API-NG/Streaming: Added tests on own concurrent bounded queue
* G-API-NG/Streaming: Added more tests on various data types, + more
- Vector/Scalar passed as input;
- Vector/Scalar passed in-between islands;
- Some more assertions;
- Also fixed a deadlock problem when inputs are mixed (1 constant, 1 stream)
* G-API-NG/Streaming: Added tests on output data types handling
- Vector
- Scalar
* G-API-NG/Streaming: Fixed test issues with IE + Windows warnings
* G-API-NG/Streaming: Decoupled G-API from videoio
- Now the core G-API doesn't use a cv::VideoCapture directly,
it comes in via an abstract interface;
- Polished a little bit the setSource()/start()/stop() semantics,
now setSource() is mandatory before ANY call to start().
* G-API-NG/Streaming: Fix STANDALONE build (errors brought by render)
2019-10-19 00:29:58 +08:00
|
|
|
ade::NodeHandle GIslandModel::mkEmitNode(Graph &g, std::size_t in_idx)
|
|
|
|
{
|
|
|
|
ade::NodeHandle nh = g.createNode();
|
|
|
|
g.metadata(nh).set(cv::gimpl::NodeKind{cv::gimpl::NodeKind::EMIT});
|
|
|
|
g.metadata(nh).set(cv::gimpl::Emitter{in_idx, {}});
|
|
|
|
return nh;
|
|
|
|
}
|
|
|
|
|
|
|
|
ade::NodeHandle GIslandModel::mkSinkNode(Graph &g, std::size_t out_idx)
|
|
|
|
{
|
|
|
|
ade::NodeHandle nh = g.createNode();
|
|
|
|
g.metadata(nh).set(cv::gimpl::NodeKind{cv::gimpl::NodeKind::SINK});
|
|
|
|
g.metadata(nh).set(cv::gimpl::Sink{out_idx});
|
|
|
|
return nh;
|
|
|
|
}
|
|
|
|
|
2018-09-27 02:50:39 +08:00
|
|
|
void GIslandModel::syncIslandTags(Graph &g, ade::Graph &orig_g)
|
|
|
|
{
|
|
|
|
GModel::Graph gm(orig_g);
|
|
|
|
for (auto nh : g.nodes())
|
|
|
|
{
|
|
|
|
if (NodeKind::ISLAND == g.metadata(nh).get<NodeKind>().k)
|
|
|
|
{
|
|
|
|
auto island = g.metadata(nh).get<FusedIsland>().object;
|
|
|
|
auto isl_tag = island->name();
|
|
|
|
for (const auto& orig_nh_inside : island->contents())
|
|
|
|
{
|
|
|
|
gm.metadata(orig_nh_inside).set(Island{isl_tag});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GIslandModel::compileIslands(Graph &g, const ade::Graph &orig_g, const GCompileArgs &args)
|
|
|
|
{
|
|
|
|
GModel::ConstGraph gm(orig_g);
|
|
|
|
|
|
|
|
auto original_sorted = gm.metadata().get<ade::passes::TopologicalSortData>();
|
|
|
|
for (auto nh : g.nodes())
|
|
|
|
{
|
|
|
|
if (NodeKind::ISLAND == g.metadata(nh).get<NodeKind>().k)
|
|
|
|
{
|
2019-11-27 23:21:00 +08:00
|
|
|
auto nodes_to_data = [&](const ade::NodeHandle& dnh)
|
|
|
|
{
|
|
|
|
GAPI_Assert(g.metadata(dnh).get<NodeKind>().k == NodeKind::SLOT);
|
|
|
|
const auto& orig_data_nh = g.metadata(dnh).get<DataSlot>().original_data_node;
|
|
|
|
const auto& data = gm.metadata(orig_data_nh).get<Data>();
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<cv::gimpl::Data> ins_data;
|
|
|
|
ade::util::transform(nh->inNodes(), std::back_inserter(ins_data), nodes_to_data);
|
|
|
|
|
|
|
|
std::vector<cv::gimpl::Data> outs_data;
|
|
|
|
ade::util::transform(nh->outNodes(), std::back_inserter(outs_data), nodes_to_data);
|
|
|
|
|
2018-09-27 02:50:39 +08:00
|
|
|
auto island_obj = g.metadata(nh).get<FusedIsland>().object;
|
|
|
|
auto island_ops = island_obj->contents();
|
|
|
|
|
|
|
|
std::vector<ade::NodeHandle> topo_sorted_list;
|
|
|
|
ade::util::copy_if(original_sorted.nodes(),
|
|
|
|
std::back_inserter(topo_sorted_list),
|
|
|
|
[&](ade::NodeHandle sorted_nh) {
|
|
|
|
return ade::util::contains(island_ops, sorted_nh);
|
|
|
|
});
|
|
|
|
|
|
|
|
auto island_exe = island_obj->backend().priv()
|
2019-11-27 23:21:00 +08:00
|
|
|
.compile(orig_g, args, topo_sorted_list, ins_data, outs_data);
|
2018-09-27 02:50:39 +08:00
|
|
|
GAPI_Assert(nullptr != island_exe);
|
2019-11-27 23:21:00 +08:00
|
|
|
|
2018-09-27 02:50:39 +08:00
|
|
|
g.metadata(nh).set(IslandExec{std::move(island_exe)});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ade::NodeHandle GIslandModel::producerOf(const ConstGraph &g, ade::NodeHandle &data_nh)
|
|
|
|
{
|
|
|
|
for (auto nh : g.nodes())
|
|
|
|
{
|
|
|
|
// find a data slot...
|
|
|
|
if (NodeKind::SLOT == g.metadata(nh).get<NodeKind>().k)
|
|
|
|
{
|
|
|
|
// which is associated with the given data object...
|
|
|
|
if (data_nh == g.metadata(nh).get<DataSlot>().original_data_node)
|
|
|
|
{
|
|
|
|
// which probably has a produrer...
|
|
|
|
if (0u != nh->inNodes().size())
|
|
|
|
{
|
|
|
|
// ...then the answer is that producer
|
|
|
|
return nh->inNodes().front();
|
|
|
|
}
|
|
|
|
else return ade::NodeHandle(); // input data object?
|
|
|
|
// return empty to break the cycle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No appropriate data slot found - probably, the object has been
|
|
|
|
// optimized out during fusion
|
|
|
|
return ade::NodeHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cv
|
|
|
|
} // namespace gimpl
|