[1] | 1 | /*------------------------------------------------------------\ |
---|
| 2 | | | |
---|
| 3 | | Tool : systemcass | |
---|
| 4 | | | |
---|
| 5 | | File : schedulers.cc | |
---|
| 6 | | | |
---|
| 7 | | Author : Buchmann Richard | |
---|
| 8 | | Nicolas Pouillon | |
---|
| 9 | | | |
---|
| 10 | | Date : 23_03_2007 | |
---|
| 11 | | | |
---|
| 12 | \------------------------------------------------------------*/ |
---|
| 13 | |
---|
| 14 | /* |
---|
| 15 | * This file is part of the Disydent Project |
---|
| 16 | * Copyright (C) Laboratoire LIP6 - Département ASIM |
---|
| 17 | * Universite Pierre et Marie Curie |
---|
| 18 | * |
---|
| 19 | * Home page : http://www-asim.lip6.fr/disydent |
---|
| 20 | * E-mail : mailto:richard.buchmann@lip6.fr |
---|
| 21 | * |
---|
| 22 | * This library is free software; you can redistribute it and/or modify it |
---|
| 23 | * under the terms of the GNU Library General Public License as published |
---|
| 24 | * by the Free Software Foundation; either version 2 of the License, or (at |
---|
| 25 | * your option) any later version. |
---|
| 26 | * |
---|
| 27 | * Disydent is distributed in the hope that it will be |
---|
| 28 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
| 30 | * Public License for more details. |
---|
| 31 | * |
---|
| 32 | * You should have received a copy of the GNU General Public License along |
---|
| 33 | * with the GNU C Library; see the file COPYING. If not, write to the Free |
---|
| 34 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 35 | */ |
---|
| 36 | |
---|
[52] | 37 | #include <cstdio> |
---|
| 38 | #include <cassert> |
---|
| 39 | #include <iostream> |
---|
| 40 | #include <algorithm> //std::sort |
---|
| 41 | |
---|
[27] | 42 | #include "sc_module.h" // method_process_t |
---|
| 43 | #include "gen_code.h" // gen_scheduling_code_for_dynamic_link & gen_scheduling_code_for_static_func |
---|
| 44 | #include "internal.h" // dump_all_graph |
---|
| 45 | #include "graph_cass.h" // makegraph |
---|
| 46 | #include "process_dependency.h" // MakeProcessDependencyList |
---|
| 47 | #include "signal_dependency.h" // MakeSignalDependencyGraph |
---|
| 48 | #include "mouchard_scheduling.h" // MakeMouchardScheduling |
---|
| 49 | #include "graph_signals.h" // makegraph |
---|
[1] | 50 | |
---|
[63] | 51 | #ifdef _OPENMP |
---|
[61] | 52 | #include <omp.h> |
---|
[60] | 53 | #endif |
---|
| 54 | |
---|
[27] | 55 | #ifdef HAVE_CONFIG_H |
---|
| 56 | #include "config.h" |
---|
| 57 | #endif |
---|
| 58 | |
---|
[1] | 59 | using namespace std; |
---|
| 60 | |
---|
| 61 | namespace sc_core { |
---|
| 62 | |
---|
[65] | 63 | // |
---|
[60] | 64 | // sort_functions splits and sorts instances_list into three functions lists : |
---|
[65] | 65 | method_process_list_t *transition_func_list; |
---|
| 66 | method_process_list_t *moore_func_list; |
---|
[63] | 67 | #ifdef _OPENMP |
---|
[65] | 68 | #pragma omp threadprivate (transition_func_list, moore_func_list) |
---|
[62] | 69 | #endif |
---|
[60] | 70 | method_process_list_t combinational_func_list; |
---|
[65] | 71 | |
---|
[1] | 72 | /* ***************************** */ |
---|
| 73 | /* Dumping functions (for debug) */ |
---|
| 74 | /* ***************************** */ |
---|
| 75 | |
---|
| 76 | template <typename T> |
---|
[52] | 77 | ostream & operator << (ostream & o, const vector<T*> & v) { |
---|
| 78 | typename vector<T *>::const_iterator i; |
---|
| 79 | for (i = v.begin(); i != v.end(); ++i) { |
---|
| 80 | o << **i << " "; |
---|
| 81 | } |
---|
| 82 | return o; |
---|
[1] | 83 | } |
---|
| 84 | |
---|
| 85 | /* ************ */ |
---|
| 86 | /* functions */ |
---|
| 87 | /****************/ |
---|
| 88 | |
---|
[52] | 89 | static bool sort_by_module_ptr (const method_process_t * a1, const method_process_t * a2) { |
---|
| 90 | assert(a1 != NULL); |
---|
| 91 | assert(a2 != NULL); |
---|
| 92 | return (a1->module < a2->module); |
---|
[1] | 93 | } |
---|
| 94 | |
---|
[52] | 95 | |
---|
| 96 | static bool sort_by_fct_ptr (const method_process_t * a1, const method_process_t * a2) { |
---|
[27] | 97 | assert(a1 != NULL); |
---|
| 98 | assert(a2 != NULL); |
---|
[1] | 99 | union { |
---|
| 100 | SC_ENTRY_FUNC func; |
---|
| 101 | unsigned long addr_l; |
---|
| 102 | unsigned long long addr_ll; |
---|
| 103 | } addr1, addr2; |
---|
[52] | 104 | |
---|
[1] | 105 | addr1.func = a1->func; |
---|
| 106 | addr2.func = a2->func; |
---|
[52] | 107 | |
---|
| 108 | if (a1->func == a2->func) { |
---|
| 109 | return sort_by_module_ptr(a1,a2); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | if (sizeof(SC_ENTRY_FUNC) == 4 ) { |
---|
[1] | 113 | return (addr1.addr_l < addr2.addr_l); |
---|
[52] | 114 | } |
---|
| 115 | else { |
---|
[1] | 116 | return (addr1.addr_ll < addr2.addr_ll); |
---|
| 117 | } |
---|
| 118 | } |
---|
[52] | 119 | |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | void sort_functions() { |
---|
| 123 | method_process_list_t::const_iterator m; |
---|
[63] | 124 | #ifdef _OPENMP |
---|
[60] | 125 | #pragma omp parallel |
---|
| 126 | #pragma omp critical |
---|
[62] | 127 | #endif |
---|
[60] | 128 | { |
---|
| 129 | transition_func_list = new method_process_list_t; |
---|
| 130 | moore_func_list = new method_process_list_t; |
---|
| 131 | for (m = method_process_list.begin(); m != method_process_list.end(); ++m) { |
---|
[63] | 132 | #ifdef _OPENMP |
---|
[60] | 133 | if ((*m)->omp_threadnum == omp_get_thread_num()) |
---|
| 134 | #endif |
---|
[65] | 135 | { |
---|
| 136 | if ((*m)->is_transition ()) |
---|
| 137 | transition_func_list->push_back(*m); |
---|
| 138 | else if ((*m)->is_genmoore ()) |
---|
| 139 | moore_func_list->push_back(*m); |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | #if 1 |
---|
| 143 | // Sort transition functions by method pointer (1) and by module pointer (2) |
---|
| 144 | std::sort (transition_func_list->begin(), transition_func_list->end(), sort_by_fct_ptr); |
---|
| 145 | // Sort generation functions by method pointer (1) and by module pointer (2) |
---|
| 146 | std::sort (moore_func_list->begin(), moore_func_list->end(), sort_by_fct_ptr); |
---|
| 147 | #endif |
---|
| 148 | #if 0 |
---|
| 149 | std::sort (transition_func_list->begin(), transition_func_list->end(), only_sort_by_module_ptr); |
---|
| 150 | std::sort (moore_func_list->begin(), moore_func_list->end(), only_sort_by_module_ptr); |
---|
| 151 | #endif |
---|
| 152 | } |
---|
[60] | 153 | |
---|
[65] | 154 | for (m = method_process_list.begin(); m != method_process_list.end(); ++m) { |
---|
| 155 | if ((*m)->is_combinational()) combinational_func_list.push_back(*m); |
---|
| 156 | } |
---|
[1] | 157 | } |
---|
| 158 | |
---|
| 159 | /* ****************** */ |
---|
| 160 | /* process dependency */ |
---|
| 161 | /* ****************** */ |
---|
| 162 | |
---|
[52] | 163 | static SignalDependencyGraph * MakeAcyclicSignalDependencyGraph() { |
---|
| 164 | if (dump_all_graph) { |
---|
| 165 | const PortDependencyGraph & port_graph = get_port_dependency_graph (); |
---|
| 166 | PortDependencyGraph2dot ("port_graph", port_graph); |
---|
| 167 | } |
---|
[1] | 168 | |
---|
[52] | 169 | SignalDependencyGraph * sig_graph = MakeSignalDependencyGraph(); |
---|
[1] | 170 | |
---|
[52] | 171 | if (dump_all_graph) { |
---|
| 172 | SignalDependencyGraph2dot ("signal_graph",*sig_graph); |
---|
| 173 | } |
---|
[1] | 174 | |
---|
[52] | 175 | if (!Check(*sig_graph)) { |
---|
| 176 | cerr << "The signal dependency graph is not valid.\n"; |
---|
| 177 | exit (29092004); |
---|
| 178 | } |
---|
[1] | 179 | |
---|
[52] | 180 | if (!Check(method_process_list, *sig_graph)) { |
---|
| 181 | cerr << "Sensitivity list is not valid.\n"; |
---|
| 182 | exit (30092004); |
---|
| 183 | } |
---|
[1] | 184 | |
---|
[52] | 185 | // There is a cycle in the signal dependency graph ? |
---|
| 186 | Graph * sig_knuth = makegraph(*sig_graph); |
---|
| 187 | strong_component_list_t * s = strong_component(sig_knuth); |
---|
[1] | 188 | |
---|
[52] | 189 | if (dump_all_graph) { |
---|
| 190 | SignalDependencyOrder2txt("signal_order",*s); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | if (has_cycle(*s)) { |
---|
| 194 | cerr << "Error : There is a cycle in the signal dependency graph.\n"; |
---|
| 195 | exit (24092004); |
---|
| 196 | } |
---|
[62] | 197 | delete s; |
---|
[52] | 198 | return sig_graph; |
---|
[1] | 199 | } |
---|
| 200 | |
---|
| 201 | |
---|
[52] | 202 | static ProcessDependencyList * MouchardScheduling() { |
---|
| 203 | SignalDependencyGraph * sig_graph = MakeAcyclicSignalDependencyGraph(); |
---|
| 204 | assert(sig_graph != NULL); |
---|
| 205 | // Create the process evaluation list |
---|
| 206 | ProcessDependencyList * process_list = MakeMouchardScheduling(*sig_graph); |
---|
| 207 | assert(process_list != NULL); |
---|
[1] | 208 | |
---|
[52] | 209 | if (dump_all_graph) { |
---|
| 210 | ProcessDependencyList2dot ("process_order", *process_list); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | return process_list; |
---|
[1] | 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
[52] | 217 | static ProcessDependencyList * BuchmannScheduling() { |
---|
| 218 | SignalDependencyGraph * sig_graph = MakeAcyclicSignalDependencyGraph(); |
---|
| 219 | // Create the process evaluation list |
---|
| 220 | ProcessDependencyList * process_list = MakeProcessDependencyList(*sig_graph); |
---|
[1] | 221 | |
---|
[52] | 222 | if (dump_all_graph) { |
---|
| 223 | ProcessDependencyList2dot("process_order", *process_list); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | return process_list; |
---|
[1] | 227 | } |
---|
| 228 | |
---|
[65] | 229 | string |
---|
| 230 | get_scheduling (int scheduling_method) |
---|
| 231 | { |
---|
| 232 | string base_name; |
---|
| 233 | /* marque les fonctions comme fonction de mealy ou non */ |
---|
| 234 | if (dump_funclist_info) |
---|
| 235 | cerr << "method process list : " << method_process_list << "\n"; |
---|
| 236 | sort_functions (); |
---|
[63] | 237 | #ifdef _OPENMP |
---|
[60] | 238 | #pragma omp parallel |
---|
| 239 | #pragma omp critical |
---|
[62] | 240 | #endif |
---|
[60] | 241 | { |
---|
| 242 | if (dump_funclist_info) { |
---|
[63] | 243 | #ifdef _OPENMP |
---|
[60] | 244 | cerr << "Thread " << omp_get_thread_num() << "\n"; |
---|
| 245 | #endif |
---|
[65] | 246 | cerr << " Transition functions : " << *transition_func_list << "\n"; |
---|
| 247 | cerr << " Moore generation functions : " << *moore_func_list << "\n"; |
---|
[63] | 248 | #ifdef _OPENMP |
---|
[60] | 249 | #pragma omp master |
---|
[62] | 250 | #endif |
---|
[65] | 251 | { |
---|
| 252 | if (!combinational_func_list.empty()) { |
---|
| 253 | cerr << "Mealy generation functions : " << |
---|
| 254 | combinational_func_list << "\n"; |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | } |
---|
[52] | 258 | |
---|
[65] | 259 | /* Schedule */ |
---|
| 260 | switch (scheduling_method) { |
---|
| 261 | case BUCHMANN_SCHEDULING : { |
---|
| 262 | // Generate the scheduled code, compile and link. |
---|
| 263 | // Buchmann's thesis explains this scheduling method. |
---|
| 264 | // Uses port dependancies like Dr. Mouchard. |
---|
| 265 | ProcessDependencyList* process_list = BuchmannScheduling (); |
---|
| 266 | if (dynamic_link_of_scheduling_code) |
---|
| 267 | base_name = gen_scheduling_code_for_dynamic_link (*transition_func_list, *moore_func_list,*process_list); |
---|
| 268 | else |
---|
| 269 | gen_scheduling_code_for_static_func (*transition_func_list, *moore_func_list, *process_list); |
---|
| 270 | break; |
---|
| 271 | } |
---|
| 272 | case MOUCHARD_SCHEDULING : { |
---|
| 273 | // Generate the scheduled code, compile and link. |
---|
| 274 | // Mouchard's thesis explains this scheduling method. |
---|
| 275 | // Uses port dependancies like Dr. Mouchard. |
---|
| 276 | // CAUTION : unlike FastSysC, this scheduling is totally static |
---|
| 277 | // and does not use an event-driven scheduler. |
---|
| 278 | ProcessDependencyList* process_list = MouchardScheduling (); |
---|
| 279 | if (dynamic_link_of_scheduling_code) |
---|
| 280 | base_name = gen_scheduling_code_for_dynamic_link(*transition_func_list, *moore_func_list,*process_list); |
---|
| 281 | else |
---|
| 282 | gen_scheduling_code_for_static_func (*transition_func_list, *moore_func_list, *process_list); |
---|
| 283 | break; |
---|
| 284 | } |
---|
| 285 | case CASS_SCHEDULING : { |
---|
| 286 | // Generate the scheduled code, compile and link |
---|
| 287 | // Hommais's thesis explains this scheduling method (like CASS strategy) |
---|
| 288 | // Doesn't use port dependancies |
---|
| 289 | strong_component_list_t *strong_list = NULL; |
---|
[63] | 290 | #ifdef _OPENMP |
---|
[60] | 291 | #pragma omp master |
---|
[62] | 292 | #endif |
---|
[65] | 293 | { |
---|
| 294 | Graph *g = makegraph (&combinational_func_list); |
---|
| 295 | if (dump_all_graph && g) |
---|
| 296 | graph2dot("module_graph", *g); |
---|
| 297 | strong_list = strong_component (g); |
---|
| 298 | } |
---|
| 299 | if (dynamic_link_of_scheduling_code) |
---|
| 300 | base_name = gen_scheduling_code_for_dynamic_link(*transition_func_list, *moore_func_list,strong_list); |
---|
| 301 | else |
---|
| 302 | gen_scheduling_code_for_quasistatic_func (*transition_func_list, *moore_func_list, strong_list); |
---|
[63] | 303 | #ifdef _OPENMP |
---|
[62] | 304 | #pragma omp master |
---|
| 305 | #endif |
---|
[65] | 306 | { |
---|
| 307 | delete strong_list; |
---|
| 308 | } |
---|
| 309 | break; |
---|
| 310 | } |
---|
| 311 | default : |
---|
| 312 | cerr << "Error : Unable to schedule SystemC process." |
---|
| 313 | "Please select a scheduling method.\n"; |
---|
| 314 | exit (35); |
---|
| 315 | } |
---|
| 316 | } |
---|
| 317 | return base_name; |
---|
[1] | 318 | } |
---|
| 319 | |
---|
[52] | 320 | |
---|
| 321 | bool run_schedule_editor (const char * schedule) { |
---|
| 322 | char buf[128]; |
---|
| 323 | const char * editor = getenv("EDITOR"); |
---|
| 324 | if (editor == NULL) { |
---|
| 325 | editor = "vim"; |
---|
| 326 | } |
---|
| 327 | sprintf (buf, "(cd '%s' ; %s '%s')", temporary_dir, editor, schedule); |
---|
| 328 | if (dump_stage) { |
---|
| 329 | cerr << "Executing : " << buf << endl; |
---|
| 330 | } |
---|
| 331 | return (system(buf) == 0); |
---|
[1] | 332 | } |
---|
| 333 | |
---|
| 334 | |
---|
| 335 | } // end of sc_core namespace |
---|
| 336 | |
---|
[52] | 337 | /* |
---|
| 338 | # Local Variables: |
---|
| 339 | # tab-width: 4; |
---|
| 340 | # c-basic-offset: 4; |
---|
| 341 | # c-file-offsets:((innamespace . 0)(inline-open . 0)); |
---|
| 342 | # indent-tabs-mode: nil; |
---|
| 343 | # End: |
---|
| 344 | # |
---|
| 345 | # vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 346 | */ |
---|
| 347 | |
---|