Path: blob/master/modules/gapi/src/compiler/gmodel.hpp
16337 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.3//4// Copyright (C) 2018 Intel Corporation567#ifndef OPENCV_GAPI_GMODEL_HPP8#define OPENCV_GAPI_GMODEL_HPP910#include <memory> // shared_ptr11#include <unordered_map>12#include <functional> // std::function1314#include <ade/graph.hpp>15#include <ade/typed_graph.hpp>16#include <ade/passes/topological_sort.hpp>1718// /!\ ATTENTION:19//20// No API includes like GMat, GNode, GCall here!21// This part of the system is API-unaware by its design.22//2324#include "opencv2/gapi/garg.hpp"25#include "opencv2/gapi/gkernel.hpp"26#include "api/gapi_priv.hpp" // GShape27#include "api/gproto_priv.hpp" // origin_of28#include "backends/common/gbackend.hpp"2930#include "compiler/gobjref.hpp"31#include "compiler/gislandmodel.hpp"3233namespace cv { namespace gimpl {3435// TODO: Document all metadata types3637struct NodeType38{39static const char *name() { return "NodeType"; }40enum { OP, DATA } t;41};4243struct Input44{45static const char *name() { return "Input"; }46std::size_t port;47};4849struct Output50{51static const char *name() { return "Output"; }52std::size_t port;53};5455struct Op56{57static const char *name() { return "Op"; }58cv::GKernel k;59std::vector<GArg> args; // TODO: Introduce a new type for internal args?60std::vector<RcDesc> outs; // TODO: Introduce a new type for resource references6162cv::gapi::GBackend backend;63util::any opaque;64};6566struct Data67{68static const char *name() { return "Data"; }6970// FIXME: This is a _pure_ duplication of RcDesc now! (except storage)71GShape shape; // FIXME: Probably to be replaced by GMetaArg?72int rc;73GMetaArg meta;74HostCtor ctor; // T-specific helper to deal with unknown types in our code75// FIXME: Why rc+shape+meta is not represented as RcDesc here?7677enum class Storage78{79INTERNAL, // data object is not listed in GComputation protocol80INPUT, // data object is listed in GComputation protocol as Input81OUTPUT, // data object is listed in GComputation protocol as Output82CONST, // data object is constant83};84Storage storage;85};8687struct ConstValue88{89static const char *name() { return "ConstValue"; }90GRunArg arg;91};9293// This metadata is valid for both DATA and OP kinds of nodes94// FIXME: Rename to IslandTag95struct Island96{97static const char *name() { return "Island"; }98std::string island; // can be set by user, otherwise is set by fusion99};100101struct Protocol102{103static const char *name() { return "Protocol"; }104// TODO: Replace the whole thing with a "Protocol" object105std::vector<RcDesc> inputs;106std::vector<RcDesc> outputs;107108std::vector<ade::NodeHandle> in_nhs;109std::vector<ade::NodeHandle> out_nhs;110};111112struct OutputMeta113{114static const char *name() { return "OutputMeta"; }115GMetaArgs outMeta;116};117118struct Journal119{120static const char *name() { return "Journal"; }121std::vector<std::string> messages;122};123124// The mapping between user-side GMat/GScalar/... objects125// and its appropriate nodes. Can be stored in graph optionally126// (NOT used by any compiler or backends, introspection purposes127// only)128struct Layout129{130static const char *name() { return "Layout"; }131GOriginMap<ade::NodeHandle> object_nodes;132};133134// Unique data object counter (per-type)135class DataObjectCounter136{137public:138static const char* name() { return "DataObjectCounter"; }139int GetNewId(GShape shape) { return m_next_data_id[shape]++; }140private:141std::unordered_map<cv::GShape, int> m_next_data_id;142};143144// A projected graph of Islands (generated from graph of Operations)145struct IslandModel146{147static const char* name() { return "IslandModel"; }148std::shared_ptr<ade::Graph> model;149};150151// List of backends selected for current graph execution152struct ActiveBackends153{154static const char *name() { return "ActiveBackends"; }155std::unordered_set<cv::gapi::GBackend> backends;156};157158namespace GModel159{160using Graph = ade::TypedGraph161< NodeType162, Input163, Output164, Op165, Data166, ConstValue167, Island168, Protocol169, OutputMeta170, Journal171, ade::passes::TopologicalSortData172, DataObjectCounter173, Layout174, IslandModel175, ActiveBackends176>;177178// FIXME: How to define it based on GModel???179using ConstGraph = ade::ConstTypedGraph180< NodeType181, Input182, Output183, Op184, Data185, ConstValue186, Island187, Protocol188, OutputMeta189, Journal190, ade::passes::TopologicalSortData191, DataObjectCounter192, Layout193, IslandModel194, ActiveBackends195>;196197// User should initialize graph before using it198// GAPI_EXPORTS for tests199GAPI_EXPORTS void init (Graph& g);200201ade::NodeHandle mkOpNode(Graph &g, const GKernel &k, const std::vector<GArg>& args, const std::string &island);202203// FIXME: change it to take GMeta instead of GShape?204ade::NodeHandle mkDataNode(Graph &g, const GOrigin& origin);205206// Adds a string message to a node. Any node can be subject of log, messages then207// appear in the dumped .dot file.x208void log(Graph &g, ade::NodeHandle op, std::string &&message, ade::NodeHandle updater = ade::NodeHandle());209void log(Graph &g, ade::EdgeHandle op, std::string &&message, ade::NodeHandle updater = ade::NodeHandle());210211void linkIn (Graph &g, ade::NodeHandle op, ade::NodeHandle obj, std::size_t in_port);212void linkOut (Graph &g, ade::NodeHandle op, ade::NodeHandle obj, std::size_t out_port);213214// FIXME: Align this GModel API properly, it is a mess now215namespace detail216{217// FIXME: GAPI_EXPORTS only because of tests!!!218GAPI_EXPORTS ade::NodeHandle dataNodeOf(const ConstGraph& g, const GOrigin &origin);219}220template<typename T> inline ade::NodeHandle dataNodeOf(const ConstGraph& g, T &&t)221{222return detail::dataNodeOf(g, cv::gimpl::proto::origin_of(GProtoArg{t}));223}224225void linkIn (Graph &g, ade::NodeHandle op, ade::NodeHandle obj, std::size_t in_port);226void linkOut (Graph &g, ade::NodeHandle op, ade::NodeHandle obj, std::size_t out_port);227228void redirectReaders(Graph &g, ade::NodeHandle from, ade::NodeHandle to);229void redirectWriter (Graph &g, ade::NodeHandle from, ade::NodeHandle to);230231std::vector<ade::NodeHandle> orderedInputs (Graph &g, ade::NodeHandle nh);232std::vector<ade::NodeHandle> orderedOutputs(Graph &g, ade::NodeHandle nh);233234// Returns input meta array for given op node235// Array is sparse, as metadata for non-gapi input objects is empty236// TODO:237// Cover with tests!!238GMetaArgs collectInputMeta(GModel::ConstGraph cg, ade::NodeHandle node);239GMetaArgs collectOutputMeta(GModel::ConstGraph cg, ade::NodeHandle node);240241ade::EdgeHandle getInEdgeByPort(const GModel::ConstGraph& cg, const ade::NodeHandle& nh, std::size_t in_port);242243// Returns true if the given backend participates in the execution244bool isActive(const GModel::Graph &cg, const cv::gapi::GBackend &backend);245} // namespace GModel246247248}} // namespace cv::gimpl249250#endif // OPENCV_GAPI_GMODEL_HPP251252253