CGRA-ME
OpScheduler.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * The software programs comprising "CGRA-ME" and the documentation provided
3  * with them are copyright by its authors and the University of Toronto. Only
4  * non-commercial, not-for-profit use of this software is permitted without ex-
5  * plicit permission. This software is provided "as is" with no warranties or
6  * guarantees of support. See the LICENCE for more details. You should have re-
7  * ceived a copy of the full licence along with this software. If not, see
8  * <http://cgra-me.ece.utoronto.ca/license/>.
9  ******************************************************************************/
10 
11 #ifndef INC_CGRA_OPSCHEDULER_H_
12 #define INC_CGRA_OPSCHEDULER_H_
13 
14 #include <CGRA/MRRG.h>
15 #include <CGRA/OpGraph.h>
16 
17 #include <iosfwd>
18 #include <map>
19 #include <memory>
20 #include <set>
21 #include <string>
22 #include <unordered_map>
23 #include <utility>
24 #include <vector>
25 #include <stack>
26 
27 #include <gurobi_c++.h>
28 
29 enum class SchedType {
30  ASAP,
31  ALAP,
32  SDCLIST,
33  SDCLRO // Live range optimization
34 };
35 
36 using OpCodeEdge = std::pair<OpGraphOpCode, OpGraphOpCode>;
37 
38 // Scheduler class to create a schedule for the opgrah
39 class OpScheduler {
40  public:
42  const OpGraph& opgraph,
43  const MRRG& mrrg,
44  std::string arch_name,
45  std::string supported_ops_file_name ="");
47 
48  // Sets the different operation constraints to the GRB model
49  int addOpConstr(GRBmodel* model, unsigned II, int max_step, bool ignore_backedge, int exit_id);
50 
51  // Caclulate the edge distance for the different edge types
52  int calcEdgeDist(OpGraphVal* val, OpGraphOp* sink_op, unsigned II, bool ignore_backedge);
53 
54  // Calculates the node prioity depending on the asap schedule
55  int calcNodePrio(OpGraphOp*, int);
56 
57  // Parses the network schedule file
58  void parseNetworkSchedule(std::string netwrok_latency_filename);
59 
60  // Checks if the operation has a backedge
61  bool isBackEdge(OpGraphOp*);
62 
63  // Schedules either asap or alap depending on the schedule type
64  int schedASLAP(unsigned II, unsigned max_cycles, SchedType algo);
65 
66  // Schedules either LIST or LRO depening on the schedule type
67  int schedSDCMod(unsigned II, SchedType algo);
68 
69  // Builds the last use table and sets the constriants for them
70  void buildLastUseTable(int& ext_node_count, int exit_id,
71  std::map<OpGraphOp*, int>& last_use);
72  int addLastUseConstr(GRBmodel* model , std::map<OpGraphOp*, int> last_use);
73 
75  if (lower_bound_op_code_edge.find({op1, op2}) == lower_bound_op_code_edge.end()) {
76  throw make_from_stream<cgrame_model_error>([&](auto&& s){
77  s << "Could not find edge of opcode" << op1 << " and opcode " << op2;
78  });
79  }
80  return lower_bound_op_code_edge[{op1, op2}];
81  }
82 
83  // Gets the schedule of ASAP, ALAP or LRO depending on the schedule type
84  std::unordered_map<const OpGraphOp*, int> getSchedule(SchedType type);
85 
86  // Not needed anymore
87  int calcLowerBound(OpGraphOp* op);
88  using opPair = std::pair<OpGraphOp*, OpGraphOp*>;
89  std::unordered_map<opPair , int, pair_hash> extended_sched_const;
90 
91  private:
92  const OpGraph& op_graph;
93  const MRRG& mrrg;
94  std::unordered_map<const OpGraphOp*, int> asap_schedule;
95  std::unordered_map<const OpGraphOp*, int> alap_schedule;
96  std::unordered_map<const OpGraphOp*, int> sdc_mod_schedule;
97  std::unordered_map<const OpGraphOp*, int> sdc_lro_schedule;
98  std::vector<const MRRGNode*> function_nodes;
99  std::unordered_map<const OpGraphOp*, int> schedule;
100  std::unordered_map<OpGraphOpCode, int> op_latency;
101  std::unordered_map<OpCodeEdge, int, pair_hash> lower_bound_op_code_edge;
102  std::unordered_map<OpCodeEdge, int, pair_hash> upper_bound_op_code_edge;
103 };
104 
105 #endif // INC_CGRA_OPSCHEDULER_H_
OpScheduler::getSchedule
std::unordered_map< const OpGraphOp *, int > getSchedule(SchedType type)
Definition: OpScheduler.cpp:643
OpGraph.h
MRRG
Definition: MRRG.h:216
SchedType
SchedType
Definition: OpScheduler.h:29
OpScheduler::asap_schedule
std::unordered_map< const OpGraphOp *, int > asap_schedule
Definition: OpScheduler.h:94
OpScheduler::parseNetworkSchedule
void parseNetworkSchedule(std::string netwrok_latency_filename)
Definition: OpScheduler.cpp:54
SchedType::SDCLRO
@ SDCLRO
OpScheduler::getLowerBoundEdge
int getLowerBoundEdge(OpGraphOpCode op1, OpGraphOpCode op2)
Definition: OpScheduler.h:74
OpScheduler::OpScheduler
OpScheduler(const OpGraph &opgraph, const MRRG &mrrg, std::string arch_name, std::string supported_ops_file_name="")
Definition: OpScheduler.cpp:30
OpCodeEdge
std::pair< OpGraphOpCode, OpGraphOpCode > OpCodeEdge
Definition: OpScheduler.h:36
OpScheduler::buildLastUseTable
void buildLastUseTable(int &ext_node_count, int exit_id, std::map< OpGraphOp *, int > &last_use)
Definition: OpScheduler.cpp:381
OpScheduler
Definition: OpScheduler.h:39
OpScheduler::~OpScheduler
~OpScheduler()
Definition: OpScheduler.h:46
OpScheduler::isBackEdge
bool isBackEdge(OpGraphOp *)
Definition: OpScheduler.cpp:81
OpGraphOpCode
signed char OpCode OpGraphOpCode
Definition: OpGraph.h:15
OpScheduler::alap_schedule
std::unordered_map< const OpGraphOp *, int > alap_schedule
Definition: OpScheduler.h:95
OpScheduler::sdc_lro_schedule
std::unordered_map< const OpGraphOp *, int > sdc_lro_schedule
Definition: OpScheduler.h:97
SchedType::SDCLIST
@ SDCLIST
SchedType::ALAP
@ ALAP
OpScheduler::addLastUseConstr
int addLastUseConstr(GRBmodel *model, std::map< OpGraphOp *, int > last_use)
Definition: OpScheduler.cpp:403
OpScheduler::mrrg
const MRRG & mrrg
Definition: OpScheduler.h:93
OpScheduler::schedASLAP
int schedASLAP(unsigned II, unsigned max_cycles, SchedType algo)
Definition: OpScheduler.cpp:252
SchedType::ASAP
@ ASAP
OpScheduler::calcLowerBound
int calcLowerBound(OpGraphOp *op)
Definition: OpScheduler.cpp:99
OpScheduler::schedSDCMod
int schedSDCMod(unsigned II, SchedType algo)
Definition: OpScheduler.cpp:430
OpScheduler::opPair
std::pair< OpGraphOp *, OpGraphOp * > opPair
Definition: OpScheduler.h:88
OpScheduler::op_graph
const OpGraph & op_graph
Definition: OpScheduler.h:92
OpGraphVal
Definition: OpGraph.h:178
OpGraphOp
Definition: OpGraph.h:131
OpScheduler::sdc_mod_schedule
std::unordered_map< const OpGraphOp *, int > sdc_mod_schedule
Definition: OpScheduler.h:96
OpScheduler::extended_sched_const
std::unordered_map< opPair, int, pair_hash > extended_sched_const
Definition: OpScheduler.h:89
OpScheduler::op_latency
std::unordered_map< OpGraphOpCode, int > op_latency
Definition: OpScheduler.h:100
OpScheduler::upper_bound_op_code_edge
std::unordered_map< OpCodeEdge, int, pair_hash > upper_bound_op_code_edge
Definition: OpScheduler.h:102
OpScheduler::function_nodes
std::vector< const MRRGNode * > function_nodes
Definition: OpScheduler.h:98
OpScheduler::schedule
std::unordered_map< const OpGraphOp *, int > schedule
Definition: OpScheduler.h:99
MRRG.h
OpScheduler::calcNodePrio
int calcNodePrio(OpGraphOp *, int)
Definition: OpScheduler.cpp:376
OpGraph
Definition: OpGraph.h:215
OpScheduler::calcEdgeDist
int calcEdgeDist(OpGraphVal *val, OpGraphOp *sink_op, unsigned II, bool ignore_backedge)
Definition: OpScheduler.cpp:117
OpScheduler::lower_bound_op_code_edge
std::unordered_map< OpCodeEdge, int, pair_hash > lower_bound_op_code_edge
Definition: OpScheduler.h:101
OpScheduler::addOpConstr
int addOpConstr(GRBmodel *model, unsigned II, int max_step, bool ignore_backedge, int exit_id)
Definition: OpScheduler.cpp:161