annotate src/fftw-3.3.3/genfft/schedule.ml @ 168:ceec0dd9ec9c

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 07 Feb 2020 11:51:13 +0000
parents 89f5e221ed7b
children
rev   line source
cannam@95 1 (*
cannam@95 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
cannam@95 3 * Copyright (c) 2003, 2007-11 Matteo Frigo
cannam@95 4 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
cannam@95 5 *
cannam@95 6 * This program is free software; you can redistribute it and/or modify
cannam@95 7 * it under the terms of the GNU General Public License as published by
cannam@95 8 * the Free Software Foundation; either version 2 of the License, or
cannam@95 9 * (at your option) any later version.
cannam@95 10 *
cannam@95 11 * This program is distributed in the hope that it will be useful,
cannam@95 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@95 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@95 14 * GNU General Public License for more details.
cannam@95 15 *
cannam@95 16 * You should have received a copy of the GNU General Public License
cannam@95 17 * along with this program; if not, write to the Free Software
cannam@95 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cannam@95 19 *
cannam@95 20 *)
cannam@95 21
cannam@95 22 (* This file contains the instruction scheduler, which finds an
cannam@95 23 efficient ordering for a given list of instructions.
cannam@95 24
cannam@95 25 The scheduler analyzes the DAG (directed acyclic graph) formed by
cannam@95 26 the instruction dependencies, and recursively partitions it. The
cannam@95 27 resulting schedule data structure expresses a "good" ordering
cannam@95 28 and structure for the computation.
cannam@95 29
cannam@95 30 The scheduler makes use of utilties in Dag and other packages to
cannam@95 31 manipulate the Dag and the instruction list. *)
cannam@95 32
cannam@95 33 open Dag
cannam@95 34 (*************************************************
cannam@95 35 * Dag scheduler
cannam@95 36 *************************************************)
cannam@95 37 let to_assignment node = (Expr.Assign (node.assigned, node.expression))
cannam@95 38 let makedag l = Dag.makedag
cannam@95 39 (List.map (function Expr.Assign (v, x) -> (v, x)) l)
cannam@95 40
cannam@95 41 let return x = x
cannam@95 42 let has_color c n = (n.color = c)
cannam@95 43 let set_color c n = (n.color <- c)
cannam@95 44 let has_either_color c1 c2 n = (n.color = c1 || n.color = c2)
cannam@95 45
cannam@95 46 let infinity = 100000
cannam@95 47
cannam@95 48 let cc dag inputs =
cannam@95 49 begin
cannam@95 50 Dag.for_all dag (fun node ->
cannam@95 51 node.label <- infinity);
cannam@95 52
cannam@95 53 (match inputs with
cannam@95 54 a :: _ -> bfs dag a 0
cannam@95 55 | _ -> failwith "connected");
cannam@95 56
cannam@95 57 return
cannam@95 58 ((List.map to_assignment (List.filter (fun n -> n.label < infinity)
cannam@95 59 (Dag.to_list dag))),
cannam@95 60 (List.map to_assignment (List.filter (fun n -> n.label == infinity)
cannam@95 61 (Dag.to_list dag))))
cannam@95 62 end
cannam@95 63
cannam@95 64 let rec connected_components alist =
cannam@95 65 let dag = makedag alist in
cannam@95 66 let inputs =
cannam@95 67 List.filter (fun node -> Util.null node.predecessors)
cannam@95 68 (Dag.to_list dag) in
cannam@95 69 match cc dag inputs with
cannam@95 70 (a, []) -> [a]
cannam@95 71 | (a, b) -> a :: connected_components b
cannam@95 72
cannam@95 73 let single_load node =
cannam@95 74 match (node.input_variables, node.predecessors) with
cannam@95 75 ([x], []) ->
cannam@95 76 Variable.is_constant x ||
cannam@95 77 (!Magic.locations_are_special && Variable.is_locative x)
cannam@95 78 | _ -> false
cannam@95 79
cannam@95 80 let loads_locative node =
cannam@95 81 match (node.input_variables, node.predecessors) with
cannam@95 82 | ([x], []) -> Variable.is_locative x
cannam@95 83 | _ -> false
cannam@95 84
cannam@95 85 let partition alist =
cannam@95 86 let dag = makedag alist in
cannam@95 87 let dag' = Dag.to_list dag in
cannam@95 88 let inputs =
cannam@95 89 List.filter (fun node -> Util.null node.predecessors) dag'
cannam@95 90 and outputs =
cannam@95 91 List.filter (fun node -> Util.null node.successors) dag'
cannam@95 92 and special_inputs = List.filter single_load dag' in
cannam@95 93 begin
cannam@95 94
cannam@95 95 let c = match !Magic.schedule_type with
cannam@95 96 | 1 -> RED; (* all nodes in the input partition *)
cannam@95 97 | -1 -> BLUE; (* all nodes in the output partition *)
cannam@95 98 | _ -> BLACK; (* node color determined by bisection algorithm *)
cannam@95 99 in Dag.for_all dag (fun node -> node.color <- c);
cannam@95 100
cannam@95 101 Util.for_list inputs (set_color RED);
cannam@95 102
cannam@95 103 (*
cannam@95 104 The special inputs are those input nodes that load a single
cannam@95 105 location or twiddle factor. Special inputs can end up either
cannam@95 106 in the blue or in the red part. These inputs are special
cannam@95 107 because they inherit a color from their neighbors: If a red
cannam@95 108 node needs a special input, the special input becomes red, but
cannam@95 109 if all successors of a special input are blue, the special
cannam@95 110 input becomes blue. Outputs are always blue, whether they be
cannam@95 111 special or not.
cannam@95 112
cannam@95 113 Because of the processing of special inputs, however, the final
cannam@95 114 partition might end up being composed only of blue nodes (which
cannam@95 115 is incorrect). In this case we manually reset all inputs
cannam@95 116 (whether special or not) to be red.
cannam@95 117 *)
cannam@95 118
cannam@95 119 Util.for_list special_inputs (set_color YELLOW);
cannam@95 120
cannam@95 121 Util.for_list outputs (set_color BLUE);
cannam@95 122
cannam@95 123 let rec loopi donep =
cannam@95 124 match (List.filter
cannam@95 125 (fun node -> (has_color BLACK node) &&
cannam@95 126 List.for_all (has_either_color RED YELLOW) node.predecessors)
cannam@95 127 dag') with
cannam@95 128 [] -> if (donep) then () else loopo true
cannam@95 129 | i ->
cannam@95 130 begin
cannam@95 131 Util.for_list i (fun node ->
cannam@95 132 begin
cannam@95 133 set_color RED node;
cannam@95 134 Util.for_list node.predecessors (set_color RED);
cannam@95 135 end);
cannam@95 136 loopo false;
cannam@95 137 end
cannam@95 138
cannam@95 139 and loopo donep =
cannam@95 140 match (List.filter
cannam@95 141 (fun node -> (has_either_color BLACK YELLOW node) &&
cannam@95 142 List.for_all (has_color BLUE) node.successors)
cannam@95 143 dag') with
cannam@95 144 [] -> if (donep) then () else loopi true
cannam@95 145 | o ->
cannam@95 146 begin
cannam@95 147 Util.for_list o (set_color BLUE);
cannam@95 148 loopi false;
cannam@95 149 end
cannam@95 150
cannam@95 151 in loopi false;
cannam@95 152
cannam@95 153 (* fix the partition if it is incorrect *)
cannam@95 154 if not (List.exists (has_color RED) dag') then
cannam@95 155 Util.for_list inputs (set_color RED);
cannam@95 156
cannam@95 157 return
cannam@95 158 ((List.map to_assignment (List.filter (has_color RED) dag')),
cannam@95 159 (List.map to_assignment (List.filter (has_color BLUE) dag')))
cannam@95 160 end
cannam@95 161
cannam@95 162 type schedule =
cannam@95 163 Done
cannam@95 164 | Instr of Expr.assignment
cannam@95 165 | Seq of (schedule * schedule)
cannam@95 166 | Par of schedule list
cannam@95 167
cannam@95 168
cannam@95 169
cannam@95 170 (* produce a sequential schedule determined by the user *)
cannam@95 171 let rec sequentially = function
cannam@95 172 [] -> Done
cannam@95 173 | a :: b -> Seq (Instr a, sequentially b)
cannam@95 174
cannam@95 175 let schedule =
cannam@95 176 let rec schedule_alist = function
cannam@95 177 | [] -> Done
cannam@95 178 | [a] -> Instr a
cannam@95 179 | alist -> match connected_components alist with
cannam@95 180 | ([a]) -> schedule_connected a
cannam@95 181 | l -> Par (List.map schedule_alist l)
cannam@95 182
cannam@95 183 and schedule_connected alist =
cannam@95 184 match partition alist with
cannam@95 185 | (a, b) -> Seq (schedule_alist a, schedule_alist b)
cannam@95 186
cannam@95 187 in fun x ->
cannam@95 188 let () = Util.info "begin schedule" in
cannam@95 189 let res = schedule_alist x in
cannam@95 190 let () = Util.info "end schedule" in
cannam@95 191 res
cannam@95 192
cannam@95 193
cannam@95 194 (* partition a dag into two parts:
cannam@95 195
cannam@95 196 1) the set of loads from locatives and their successors,
cannam@95 197 2) all other nodes
cannam@95 198
cannam@95 199 This step separates the ``body'' of the dag, which computes the
cannam@95 200 actual fft, from the ``precomputations'' part, which computes e.g.
cannam@95 201 twiddle factors.
cannam@95 202 *)
cannam@95 203 let partition_precomputations alist =
cannam@95 204 let dag = makedag alist in
cannam@95 205 let dag' = Dag.to_list dag in
cannam@95 206 let loads = List.filter loads_locative dag' in
cannam@95 207 begin
cannam@95 208
cannam@95 209 Dag.for_all dag (set_color BLUE);
cannam@95 210 Util.for_list loads (set_color RED);
cannam@95 211
cannam@95 212 let rec loop () =
cannam@95 213 match (List.filter
cannam@95 214 (fun node -> (has_color RED node) &&
cannam@95 215 List.exists (has_color BLUE) node.successors)
cannam@95 216 dag') with
cannam@95 217 [] -> ()
cannam@95 218 | i ->
cannam@95 219 begin
cannam@95 220 Util.for_list i
cannam@95 221 (fun node ->
cannam@95 222 Util.for_list node.successors (set_color RED));
cannam@95 223 loop ()
cannam@95 224 end
cannam@95 225
cannam@95 226 in loop ();
cannam@95 227
cannam@95 228 return
cannam@95 229 ((List.map to_assignment (List.filter (has_color BLUE) dag')),
cannam@95 230 (List.map to_assignment (List.filter (has_color RED) dag')))
cannam@95 231 end
cannam@95 232
cannam@95 233 let isolate_precomputations_and_schedule alist =
cannam@95 234 let (a, b) = partition_precomputations alist in
cannam@95 235 Seq (schedule a, schedule b)
cannam@95 236