Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / exec / DataflowExecutor.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __ONERT_EXEC_DATAFLOW_EXECUTOR_H__
18 #define __ONERT_EXEC_DATAFLOW_EXECUTOR_H__
19
20 #include <list>
21 #include <map>
22 #include <unordered_map>
23
24 #include "exec/FunctionSequence.h"
25 #include "Job.h"
26 #include "ir/OperandIndexSequence.h"
27 #include "ir/Index.h"
28 #include <memory>
29 #include "exec/ExecutorBase.h"
30 #include "compiler/CodeMap.h"
31
32 namespace onert
33 {
34 namespace exec
35 {
36
37 class DataflowExecutor : public ExecutorBase
38 {
39
40 protected:
41   virtual void notify(uint32_t finished_job_id);
42   bool noWaitingJobs();
43
44 public:
45   /**
46    * @brief Constructs a DataflowExecutor object
47    *
48    * @param lowered_graph LoweredGraph object
49    * @param tensor_builders Tensor builders that are currently used
50    * @param code_map OpSequence and its code map
51    */
52   DataflowExecutor(std::unique_ptr<compiler::LoweredGraph> lowered_graph,
53                    const std::vector<std::shared_ptr<backend::ITensor>> &input_tensors,
54                    const std::vector<std::shared_ptr<backend::ITensor>> &output_tensors,
55                    const compiler::TensorRegistries &tensor_regs,
56                    backend::TensorManagerSet &&tensor_mgrs, compiler::CodeMap &&code_map);
57
58   void executeImpl() override;
59
60 protected:
61   int64_t calculateRank(const std::vector<ir::OperationIndex> &operations);
62   void emplaceToReadyJobs(const uint32_t &id);
63
64 protected:
65   compiler::CodeMap _code_map;
66   /**
67    * @brief A vector of finished jobs for current execution
68    *        After a run it has all the jobs of this execution for the next run
69    */
70   std::vector<std::unique_ptr<Job>> _finished_jobs;
71   /**
72    * @brief A vector of waiting jobs for current execution
73    *        All the jobs are moved from #_finished_jobs to it when start a run
74    */
75   std::vector<std::unique_ptr<Job>> _waiting_jobs;
76   /**
77    * @brief Jobs' output info
78    *        Used for notifying after finishing a job
79    */
80   std::vector<std::list<uint32_t>> _output_info;
81   std::vector<uint32_t> _initial_input_info;
82   std::vector<uint32_t> _input_info;
83   /**
84    * @brief A collection of jobs that are ready for execution
85    *        Jobs in it are ready to be scheduled.
86    *        Ordered by priority from `_indexed_ranks`
87    */
88   std::multimap<int64_t, std::unique_ptr<Job>, std::greater<int64_t>> _ready_jobs;
89
90   /// @brief Which job runs which op and function.
91   std::unordered_map<uint32_t, ir::OpSequenceIndex> _job_to_op_seq;
92 };
93
94 } // namespace exec
95 } // namespace onert
96
97 #endif // __ONERT_EXEC_DATAFLOW_EXECUTOR_H__