annotate fft/fftw/fftw-3.3.4/genfft/schedule.ml @ 40:223f770b5341 kissfft-double tip

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