From a122f0f24802113a0f250f3b71e2b90ed9763bad Mon Sep 17 00:00:00 2001 From: Dmitry Matveev Date: Tue, 30 Aug 2022 23:50:34 +0300 Subject: [PATCH] G-API: Introduce GAbstractStreamingExecutor Now GStreamingExecutor is its subclass; others to come --- modules/gapi/CMakeLists.txt | 1 + modules/gapi/src/compiler/gstreaming.cpp | 4 +- modules/gapi/src/compiler/gstreaming_priv.hpp | 8 +-- .../executor/gabstractstreamingexecutor.cpp | 23 +++++++++ .../executor/gabstractstreamingexecutor.hpp | 49 +++++++++++++++++++ .../gapi/src/executor/gstreamingexecutor.cpp | 10 +--- .../gapi/src/executor/gstreamingexecutor.hpp | 29 ++++------- 7 files changed, 91 insertions(+), 33 deletions(-) create mode 100644 modules/gapi/src/executor/gabstractstreamingexecutor.cpp create mode 100644 modules/gapi/src/executor/gabstractstreamingexecutor.hpp diff --git a/modules/gapi/CMakeLists.txt b/modules/gapi/CMakeLists.txt index 962256cc88..edf4e588e0 100644 --- a/modules/gapi/CMakeLists.txt +++ b/modules/gapi/CMakeLists.txt @@ -114,6 +114,7 @@ set(gapi_srcs # Executor src/executor/gabstractexecutor.cpp + src/executor/gabstractstreamingexecutor.cpp src/executor/gexecutor.cpp src/executor/gtbbexecutor.cpp src/executor/gstreamingexecutor.cpp diff --git a/modules/gapi/src/compiler/gstreaming.cpp b/modules/gapi/src/compiler/gstreaming.cpp index e45e770427..c0bc0b1d1b 100644 --- a/modules/gapi/src/compiler/gstreaming.cpp +++ b/modules/gapi/src/compiler/gstreaming.cpp @@ -19,14 +19,14 @@ // GStreamingCompiled private implementation /////////////////////////////////// void cv::GStreamingCompiled::Priv::setup(const GMetaArgs &_metaArgs, const GMetaArgs &_outMetas, - std::unique_ptr &&_pE) + std::unique_ptr &&_pE) { m_metas = _metaArgs; m_outMetas = _outMetas; m_exec = std::move(_pE); } -void cv::GStreamingCompiled::Priv::setup(std::unique_ptr &&_pE) +void cv::GStreamingCompiled::Priv::setup(std::unique_ptr &&_pE) { m_exec = std::move(_pE); } diff --git a/modules/gapi/src/compiler/gstreaming_priv.hpp b/modules/gapi/src/compiler/gstreaming_priv.hpp index 1b559ba310..0fd5fc7b7f 100644 --- a/modules/gapi/src/compiler/gstreaming_priv.hpp +++ b/modules/gapi/src/compiler/gstreaming_priv.hpp @@ -9,7 +9,7 @@ #define OPENCV_GAPI_GSTREAMING_COMPILED_PRIV_HPP #include // unique_ptr -#include "executor/gstreamingexecutor.hpp" +#include "executor/gabstractstreamingexecutor.hpp" namespace cv { @@ -26,7 +26,7 @@ class GAPI_EXPORTS GStreamingCompiled::Priv { GMetaArgs m_metas; // passed by user GMetaArgs m_outMetas; // inferred by compiler - std::unique_ptr m_exec; + std::unique_ptr m_exec; // NB: Used by python wrapper to clarify input/output types GTypesInfo m_out_info; @@ -35,8 +35,8 @@ class GAPI_EXPORTS GStreamingCompiled::Priv public: void setup(const GMetaArgs &metaArgs, const GMetaArgs &outMetas, - std::unique_ptr &&pE); - void setup(std::unique_ptr &&pE); + std::unique_ptr &&pE); + void setup(std::unique_ptr &&pE); bool isEmpty() const; const GMetaArgs& metas() const; diff --git a/modules/gapi/src/executor/gabstractstreamingexecutor.cpp b/modules/gapi/src/executor/gabstractstreamingexecutor.cpp new file mode 100644 index 0000000000..07579a9a6f --- /dev/null +++ b/modules/gapi/src/executor/gabstractstreamingexecutor.cpp @@ -0,0 +1,23 @@ +// 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) 2022 Intel Corporation + +#include "precomp.hpp" + +#include + +#include "executor/gabstractstreamingexecutor.hpp" + +cv::gimpl::GAbstractStreamingExecutor::GAbstractStreamingExecutor(std::unique_ptr &&g_model, + const GCompileArgs &comp_args) + : m_orig_graph(std::move(g_model)) + , m_island_graph(GModel::Graph(*m_orig_graph).metadata() + .get().model) + , m_comp_args(comp_args) + , m_gim(*m_island_graph) + , m_desync(GModel::Graph(*m_orig_graph).metadata() + .contains()) +{ +} diff --git a/modules/gapi/src/executor/gabstractstreamingexecutor.hpp b/modules/gapi/src/executor/gabstractstreamingexecutor.hpp new file mode 100644 index 0000000000..952bbd01db --- /dev/null +++ b/modules/gapi/src/executor/gabstractstreamingexecutor.hpp @@ -0,0 +1,49 @@ +// 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) 2022 Intel Corporation + +#ifndef OPENCV_GAPI_GABSTRACT_STREAMING_EXECUTOR_HPP +#define OPENCV_GAPI_GABSTRACT_STREAMING_EXECUTOR_HPP + +#include // unique_ptr, shared_ptr + +#include + +#include "backends/common/gbackend.hpp" + +namespace cv { +namespace gimpl { + +class GAbstractStreamingExecutor +{ +protected: + std::unique_ptr m_orig_graph; + std::shared_ptr m_island_graph; + cv::GCompileArgs m_comp_args; + + cv::gimpl::GIslandModel::Graph m_gim; // FIXME: make const? + const bool m_desync; + +public: + explicit GAbstractStreamingExecutor(std::unique_ptr &&g_model, + const cv::GCompileArgs &comp_args); + virtual ~GAbstractStreamingExecutor() = default; + virtual void setSource(GRunArgs &&args) = 0; + virtual void start() = 0; + virtual bool pull(cv::GRunArgsP &&outs) = 0; + virtual bool pull(cv::GOptRunArgsP &&outs) = 0; + + using PyPullResult = std::tuple>; + virtual PyPullResult pull() = 0; + + virtual bool try_pull(cv::GRunArgsP &&outs) = 0; + virtual void stop() = 0; + virtual bool running() const = 0; +}; + +} // namespace gimpl +} // namespace cv + +#endif // OPENCV_GAPI_GABSTRACT_STREAMING_EXECUTOR_HPP diff --git a/modules/gapi/src/executor/gstreamingexecutor.cpp b/modules/gapi/src/executor/gstreamingexecutor.cpp index 557e5ceee4..66aad90697 100644 --- a/modules/gapi/src/executor/gstreamingexecutor.cpp +++ b/modules/gapi/src/executor/gstreamingexecutor.cpp @@ -1288,13 +1288,7 @@ public: // proper graph reshape and islands recompilation cv::gimpl::GStreamingExecutor::GStreamingExecutor(std::unique_ptr &&g_model, const GCompileArgs &comp_args) - : m_orig_graph(std::move(g_model)) - , m_island_graph(GModel::Graph(*m_orig_graph).metadata() - .get().model) - , m_comp_args(comp_args) - , m_gim(*m_island_graph) - , m_desync(GModel::Graph(*m_orig_graph).metadata() - .contains()) + : GAbstractStreamingExecutor(std::move(g_model), comp_args) { GModel::Graph gm(*m_orig_graph); // NB: Right now GIslandModel is acyclic, and all the below code assumes that. @@ -1862,7 +1856,7 @@ bool cv::gimpl::GStreamingExecutor::pull(cv::GOptRunArgsP &&outs) GAPI_Assert(false && "Unreachable code"); } -std::tuple> cv::gimpl::GStreamingExecutor::pull() +cv::gimpl::GAbstractStreamingExecutor::PyPullResult cv::gimpl::GStreamingExecutor::pull() { using RunArgs = cv::util::variant; bool is_over = false; diff --git a/modules/gapi/src/executor/gstreamingexecutor.hpp b/modules/gapi/src/executor/gstreamingexecutor.hpp index da27f6a646..cb63d4b7f0 100644 --- a/modules/gapi/src/executor/gstreamingexecutor.hpp +++ b/modules/gapi/src/executor/gstreamingexecutor.hpp @@ -12,7 +12,6 @@ // on concurrent_bounded_queue #endif -#include // unique_ptr, shared_ptr #include // thread #include #include @@ -26,9 +25,7 @@ template using QueueClass = cv::gapi::own::concurrent_bounded_queue< #endif // TBB #include "executor/last_value.hpp" -#include - -#include "backends/common/gbackend.hpp" +#include "executor/gabstractstreamingexecutor.hpp" namespace cv { namespace gimpl { @@ -104,7 +101,7 @@ public: // FIXME: Currently all GExecutor comments apply also // to this one. Please document it separately in the future. -class GStreamingExecutor final +class GStreamingExecutor final: public GAbstractStreamingExecutor { protected: // GStreamingExecutor is a state machine described as follows @@ -131,15 +128,9 @@ protected: RUNNING, } state = State::STOPPED; - std::unique_ptr m_orig_graph; - std::shared_ptr m_island_graph; - cv::GCompileArgs m_comp_args; cv::GMetaArgs m_last_metas; util::optional m_reshapable; - cv::gimpl::GIslandModel::Graph m_gim; // FIXME: make const? - const bool m_desync; - // FIXME: Naive executor details are here for now // but then it should be moved to another place struct OpDesc @@ -202,14 +193,14 @@ public: explicit GStreamingExecutor(std::unique_ptr &&g_model, const cv::GCompileArgs &comp_args); ~GStreamingExecutor(); - void setSource(GRunArgs &&args); - void start(); - bool pull(cv::GRunArgsP &&outs); - bool pull(cv::GOptRunArgsP &&outs); - std::tuple> pull(); - bool try_pull(cv::GRunArgsP &&outs); - void stop(); - bool running() const; + void setSource(GRunArgs &&args) override; + void start() override; + bool pull(cv::GRunArgsP &&outs) override; + bool pull(cv::GOptRunArgsP &&outs) override; + PyPullResult pull() override; + bool try_pull(cv::GRunArgsP &&outs) override; + void stop() override; + bool running() const override; }; } // namespace gimpl