annotate src/fftw-3.3.3/genfft/dag.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 open Util
cannam@95 23
cannam@95 24 (* Here, we have functions to transform a sequence of assignments
cannam@95 25 (variable = expression) into a DAG (a directed, acyclic graph).
cannam@95 26 The nodes of the DAG are the assignments, and the edges indicate
cannam@95 27 dependencies. (The DAG is analyzed in the scheduler to find an
cannam@95 28 efficient ordering of the assignments.)
cannam@95 29
cannam@95 30 This file also contains utilities to manipulate the DAG in various
cannam@95 31 ways. *)
cannam@95 32
cannam@95 33 (********************************************
cannam@95 34 * Dag structure
cannam@95 35 ********************************************)
cannam@95 36 type color = RED | BLUE | BLACK | YELLOW
cannam@95 37
cannam@95 38 type dagnode =
cannam@95 39 { assigned: Variable.variable;
cannam@95 40 mutable expression: Expr.expr;
cannam@95 41 input_variables: Variable.variable list;
cannam@95 42 mutable successors: dagnode list;
cannam@95 43 mutable predecessors: dagnode list;
cannam@95 44 mutable label: int;
cannam@95 45 mutable color: color}
cannam@95 46
cannam@95 47 type dag = Dag of (dagnode list)
cannam@95 48
cannam@95 49 (* true if node uses v *)
cannam@95 50 let node_uses v node =
cannam@95 51 List.exists (Variable.same v) node.input_variables
cannam@95 52
cannam@95 53 (* true if assignment of v clobbers any input of node *)
cannam@95 54 let node_clobbers node v =
cannam@95 55 List.exists (Variable.same_location v) node.input_variables
cannam@95 56
cannam@95 57 (* true if nodeb depends on nodea *)
cannam@95 58 let depends_on nodea nodeb =
cannam@95 59 node_uses nodea.assigned nodeb or
cannam@95 60 node_clobbers nodea nodeb.assigned
cannam@95 61
cannam@95 62 (* transform an assignment list into a dag *)
cannam@95 63 let makedag alist =
cannam@95 64 let dag = List.map
cannam@95 65 (fun assignment ->
cannam@95 66 let (v, x) = assignment in
cannam@95 67 { assigned = v;
cannam@95 68 expression = x;
cannam@95 69 input_variables = Expr.find_vars x;
cannam@95 70 successors = [];
cannam@95 71 predecessors = [];
cannam@95 72 label = 0;
cannam@95 73 color = BLACK })
cannam@95 74 alist
cannam@95 75 in begin
cannam@95 76 for_list dag (fun i ->
cannam@95 77 for_list dag (fun j ->
cannam@95 78 if depends_on i j then begin
cannam@95 79 i.successors <- j :: i.successors;
cannam@95 80 j.predecessors <- i :: j.predecessors;
cannam@95 81 end));
cannam@95 82 Dag dag;
cannam@95 83 end
cannam@95 84
cannam@95 85 let map f (Dag dag) = Dag (List.map f dag)
cannam@95 86 let for_all (Dag dag) f =
cannam@95 87 (* type system loophole *)
cannam@95 88 let make_unit _ = () in
cannam@95 89 make_unit (List.map f dag)
cannam@95 90 let to_list (Dag dag) = dag
cannam@95 91
cannam@95 92 let find_node f (Dag dag) = Util.find_elem f dag
cannam@95 93
cannam@95 94 (* breadth-first search *)
cannam@95 95 let rec bfs (Dag dag) node init_label =
cannam@95 96 let _ = node.label <- init_label in
cannam@95 97 let rec loop = function
cannam@95 98 [] -> ()
cannam@95 99 | node :: rest ->
cannam@95 100 let neighbors = node.predecessors @ node.successors in
cannam@95 101 let m = min_list (List.map (fun node -> node.label) neighbors) in
cannam@95 102 if (node.label > m + 1) then begin
cannam@95 103 node.label <- m + 1;
cannam@95 104 loop (rest @ neighbors);
cannam@95 105 end else
cannam@95 106 loop rest
cannam@95 107 in let neighbors = node.predecessors @ node.successors in
cannam@95 108 loop neighbors
cannam@95 109