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-2020 Intel Corporation
6 
7 
8 #ifndef OPENCV_GAPI_GCPUBACKEND_HPP
9 #define OPENCV_GAPI_GCPUBACKEND_HPP
10 
11 #include <map>                // map
12 #include <unordered_map>      // unordered_map
13 #include <tuple>              // tuple
14 #include <ade/util/algorithm.hpp> // type_list_index
15 
16 #include <opencv2/gapi/garg.hpp>
17 #include <opencv2/gapi/gproto.hpp>
18 #include <opencv2/gapi/cpu/gcpukernel.hpp>
19 
20 #include "api/gorigin.hpp"
21 #include "backends/common/gbackend.hpp"
22 #include "compiler/gislandmodel.hpp"
23 
24 namespace cv { namespace gimpl {
25 
26 struct CPUUnit
27 {
namecv::gimpl::CPUUnit28     static const char *name() { return "HostKernel"; }
29     GCPUKernel k;
30 };
31 
32 class GCPUExecutable final: public GIslandExecutable
33 {
34     const ade::Graph &m_g;
35     GModel::ConstGraph m_gm;
36     const cv::GCompileArgs m_compileArgs;
37 
38     struct OperationInfo
39     {
40         ade::NodeHandle nh;
41         GMetaArgs expected_out_metas;
42     };
43 
44     // Execution script, currently absolutely naive
45     std::vector<OperationInfo> m_script;
46 
47     // TODO: Check that it is thread-safe
48     // Map of stateful kernel nodes to their kernels' states
49     std::unordered_map<ade::NodeHandle, GArg,
50                        ade::HandleHasher<ade::Node>> m_nodesToStates;
51 
52     // List of all resources in graph (both internal and external)
53     std::vector<ade::NodeHandle> m_dataNodes;
54 
55     // Actual data of all resources in graph (both internal and external)
56     Mag m_res;
57 
58     // Flag which identifies if new stream was started
59     bool m_newStreamStarted = false;
60 
61     GArg packArg(const GArg &arg);
62     void setupKernelStates();
63 
64 public:
65     GCPUExecutable(const ade::Graph                   &graph,
66                    const cv::GCompileArgs             &compileArgs,
67                    const std::vector<ade::NodeHandle> &nodes);
68 
canReshape() const69     virtual inline bool canReshape() const override { return false; }
reshape(ade::Graph &,const GCompileArgs &)70     virtual inline void reshape(ade::Graph&, const GCompileArgs&) override
71     {
72         // FIXME: CPU plugin is in fact reshapeable (as it was initially,
73         // even before outMeta() has been introduced), so this limitation
74         // should be dropped.
75         util::throw_error(std::logic_error("GCPUExecutable::reshape() should never be called"));
76     }
77 
78     virtual void handleNewStream() override;
79 
80     virtual void run(std::vector<InObj>  &&input_objs,
81                      std::vector<OutObj> &&output_objs) override;
82 };
83 
84 }}
85 
86 #endif // OPENCV_GAPI_GCPUBACKEND_HPP
87