annotate src/fftw-3.3.5/genfft/schedule.ml @ 148:b4bfdf10c4b3

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