Chris@82: (* Chris@82: * Copyright (c) 1997-1999 Massachusetts Institute of Technology Chris@82: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@82: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@82: * Chris@82: * This program is free software; you can redistribute it and/or modify Chris@82: * it under the terms of the GNU General Public License as published by Chris@82: * the Free Software Foundation; either version 2 of the License, or Chris@82: * (at your option) any later version. Chris@82: * Chris@82: * This program is distributed in the hope that it will be useful, Chris@82: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@82: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@82: * GNU General Public License for more details. Chris@82: * Chris@82: * You should have received a copy of the GNU General Public License Chris@82: * along with this program; if not, write to the Free Software Chris@82: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@82: * Chris@82: *) Chris@82: Chris@82: (* This file contains the instruction scheduler, which finds an Chris@82: efficient ordering for a given list of instructions. Chris@82: Chris@82: The scheduler analyzes the DAG (directed acyclic graph) formed by Chris@82: the instruction dependencies, and recursively partitions it. The Chris@82: resulting schedule data structure expresses a "good" ordering Chris@82: and structure for the computation. Chris@82: Chris@82: The scheduler makes use of utilties in Dag and other packages to Chris@82: manipulate the Dag and the instruction list. *) Chris@82: Chris@82: open Dag Chris@82: (************************************************* Chris@82: * Dag scheduler Chris@82: *************************************************) Chris@82: let to_assignment node = (Expr.Assign (node.assigned, node.expression)) Chris@82: let makedag l = Dag.makedag Chris@82: (List.map (function Expr.Assign (v, x) -> (v, x)) l) Chris@82: Chris@82: let return x = x Chris@82: let has_color c n = (n.color = c) Chris@82: let set_color c n = (n.color <- c) Chris@82: let has_either_color c1 c2 n = (n.color = c1 || n.color = c2) Chris@82: Chris@82: let infinity = 100000 Chris@82: Chris@82: let cc dag inputs = Chris@82: begin Chris@82: Dag.for_all dag (fun node -> Chris@82: node.label <- infinity); Chris@82: Chris@82: (match inputs with Chris@82: a :: _ -> bfs dag a 0 Chris@82: | _ -> failwith "connected"); Chris@82: Chris@82: return Chris@82: ((List.map to_assignment (List.filter (fun n -> n.label < infinity) Chris@82: (Dag.to_list dag))), Chris@82: (List.map to_assignment (List.filter (fun n -> n.label == infinity) Chris@82: (Dag.to_list dag)))) Chris@82: end Chris@82: Chris@82: let rec connected_components alist = Chris@82: let dag = makedag alist in Chris@82: let inputs = Chris@82: List.filter (fun node -> Util.null node.predecessors) Chris@82: (Dag.to_list dag) in Chris@82: match cc dag inputs with Chris@82: (a, []) -> [a] Chris@82: | (a, b) -> a :: connected_components b Chris@82: Chris@82: let single_load node = Chris@82: match (node.input_variables, node.predecessors) with Chris@82: ([x], []) -> Chris@82: Variable.is_constant x || Chris@82: (!Magic.locations_are_special && Variable.is_locative x) Chris@82: | _ -> false Chris@82: Chris@82: let loads_locative node = Chris@82: match (node.input_variables, node.predecessors) with Chris@82: | ([x], []) -> Variable.is_locative x Chris@82: | _ -> false Chris@82: Chris@82: let partition alist = Chris@82: let dag = makedag alist in Chris@82: let dag' = Dag.to_list dag in Chris@82: let inputs = Chris@82: List.filter (fun node -> Util.null node.predecessors) dag' Chris@82: and outputs = Chris@82: List.filter (fun node -> Util.null node.successors) dag' Chris@82: and special_inputs = List.filter single_load dag' in Chris@82: begin Chris@82: Chris@82: let c = match !Magic.schedule_type with Chris@82: | 1 -> RED; (* all nodes in the input partition *) Chris@82: | -1 -> BLUE; (* all nodes in the output partition *) Chris@82: | _ -> BLACK; (* node color determined by bisection algorithm *) Chris@82: in Dag.for_all dag (fun node -> node.color <- c); Chris@82: Chris@82: Util.for_list inputs (set_color RED); Chris@82: Chris@82: (* Chris@82: The special inputs are those input nodes that load a single Chris@82: location or twiddle factor. Special inputs can end up either Chris@82: in the blue or in the red part. These inputs are special Chris@82: because they inherit a color from their neighbors: If a red Chris@82: node needs a special input, the special input becomes red, but Chris@82: if all successors of a special input are blue, the special Chris@82: input becomes blue. Outputs are always blue, whether they be Chris@82: special or not. Chris@82: Chris@82: Because of the processing of special inputs, however, the final Chris@82: partition might end up being composed only of blue nodes (which Chris@82: is incorrect). In this case we manually reset all inputs Chris@82: (whether special or not) to be red. Chris@82: *) Chris@82: Chris@82: Util.for_list special_inputs (set_color YELLOW); Chris@82: Chris@82: Util.for_list outputs (set_color BLUE); Chris@82: Chris@82: let rec loopi donep = Chris@82: match (List.filter Chris@82: (fun node -> (has_color BLACK node) && Chris@82: List.for_all (has_either_color RED YELLOW) node.predecessors) Chris@82: dag') with Chris@82: [] -> if (donep) then () else loopo true Chris@82: | i -> Chris@82: begin Chris@82: Util.for_list i (fun node -> Chris@82: begin Chris@82: set_color RED node; Chris@82: Util.for_list node.predecessors (set_color RED); Chris@82: end); Chris@82: loopo false; Chris@82: end Chris@82: Chris@82: and loopo donep = Chris@82: match (List.filter Chris@82: (fun node -> (has_either_color BLACK YELLOW node) && Chris@82: List.for_all (has_color BLUE) node.successors) Chris@82: dag') with Chris@82: [] -> if (donep) then () else loopi true Chris@82: | o -> Chris@82: begin Chris@82: Util.for_list o (set_color BLUE); Chris@82: loopi false; Chris@82: end Chris@82: Chris@82: in loopi false; Chris@82: Chris@82: (* fix the partition if it is incorrect *) Chris@82: if not (List.exists (has_color RED) dag') then Chris@82: Util.for_list inputs (set_color RED); Chris@82: Chris@82: return Chris@82: ((List.map to_assignment (List.filter (has_color RED) dag')), Chris@82: (List.map to_assignment (List.filter (has_color BLUE) dag'))) Chris@82: end Chris@82: Chris@82: type schedule = Chris@82: Done Chris@82: | Instr of Expr.assignment Chris@82: | Seq of (schedule * schedule) Chris@82: | Par of schedule list Chris@82: Chris@82: Chris@82: Chris@82: (* produce a sequential schedule determined by the user *) Chris@82: let rec sequentially = function Chris@82: [] -> Done Chris@82: | a :: b -> Seq (Instr a, sequentially b) Chris@82: Chris@82: let schedule = Chris@82: let rec schedule_alist = function Chris@82: | [] -> Done Chris@82: | [a] -> Instr a Chris@82: | alist -> match connected_components alist with Chris@82: | ([a]) -> schedule_connected a Chris@82: | l -> Par (List.map schedule_alist l) Chris@82: Chris@82: and schedule_connected alist = Chris@82: match partition alist with Chris@82: | (a, b) -> Seq (schedule_alist a, schedule_alist b) Chris@82: Chris@82: in fun x -> Chris@82: let () = Util.info "begin schedule" in Chris@82: let res = schedule_alist x in Chris@82: let () = Util.info "end schedule" in Chris@82: res Chris@82: Chris@82: Chris@82: (* partition a dag into two parts: Chris@82: Chris@82: 1) the set of loads from locatives and their successors, Chris@82: 2) all other nodes Chris@82: Chris@82: This step separates the ``body'' of the dag, which computes the Chris@82: actual fft, from the ``precomputations'' part, which computes e.g. Chris@82: twiddle factors. Chris@82: *) Chris@82: let partition_precomputations alist = Chris@82: let dag = makedag alist in Chris@82: let dag' = Dag.to_list dag in Chris@82: let loads = List.filter loads_locative dag' in Chris@82: begin Chris@82: Chris@82: Dag.for_all dag (set_color BLUE); Chris@82: Util.for_list loads (set_color RED); Chris@82: Chris@82: let rec loop () = Chris@82: match (List.filter Chris@82: (fun node -> (has_color RED node) && Chris@82: List.exists (has_color BLUE) node.successors) Chris@82: dag') with Chris@82: [] -> () Chris@82: | i -> Chris@82: begin Chris@82: Util.for_list i Chris@82: (fun node -> Chris@82: Util.for_list node.successors (set_color RED)); Chris@82: loop () Chris@82: end Chris@82: Chris@82: in loop (); Chris@82: Chris@82: return Chris@82: ((List.map to_assignment (List.filter (has_color BLUE) dag')), Chris@82: (List.map to_assignment (List.filter (has_color RED) dag'))) Chris@82: end Chris@82: Chris@82: let isolate_precomputations_and_schedule alist = Chris@82: let (a, b) = partition_precomputations alist in Chris@82: Seq (schedule a, schedule b) Chris@82: