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