Imported Upstream version 1.12.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 #include "util/TracingCtx.h"
32
33 namespace onert
34 {
35 namespace exec
36 {
37
38 class DataflowExecutor : public ExecutorBase
39 {
40
41 protected:
42   virtual void notify(uint32_t finished_job_id);
43   bool noWaitingJobs();
44
45 public:
46   /**
47    * @brief Constructs a DataflowExecutor object
48    *
49    * @param lowered_graph LoweredGraph object
50    * @param tensor_builders Tensor builders that are currently used
51    * @param code_map OpSequence and its code map
52    */
53   DataflowExecutor(std::unique_ptr<compiler::LoweredGraph> lowered_graph,
54                    const compiler::TensorRegistries &tensor_regs, compiler::CodeMap &&code_map,
55                    const util::TracingCtx *tracing_ctx);
56
57   void executeImpl() override;
58
59 protected:
60   int64_t calculateRank(const std::vector<ir::OperationIndex> &operations);
61   void emplaceToReadyJobs(const uint32_t &id);
62
63 protected:
64   compiler::CodeMap _code_map;
65   /**
66    * @brief A vector of finished jobs for current execution
67    *        After a run it has all the jobs of this execution for the next run
68    */
69   std::vector<std::unique_ptr<Job>> _finished_jobs;
70   /**
71    * @brief A vector of waiting jobs for current execution
72    *        All the jobs are moved from #_finished_jobs to it when start a run
73    */
74   std::vector<std::unique_ptr<Job>> _waiting_jobs;
75   /**
76    * @brief Jobs' output info
77    *        Used for notifying after finishing a job
78    */
79   std::vector<std::list<uint32_t>> _output_info;
80   std::vector<uint32_t> _initial_input_info;
81   std::vector<uint32_t> _input_info;
82   /**
83    * @brief A collection of jobs that are ready for execution
84    *        Jobs in it are ready to be scheduled.
85    *        Ordered by priority from `_indexed_ranks`
86    */
87   std::multimap<int64_t, std::unique_ptr<Job>, std::greater<int64_t>> _ready_jobs;
88
89   /// @brief Which job runs which op and function.
90   std::unordered_map<uint32_t, ir::OpSequenceIndex> _job_to_op_seq;
91 };
92
93 } // namespace exec
94 } // namespace onert
95
96 #endif // __ONERT_EXEC_DATAFLOW_EXECUTOR_H__