annotate src/fftw-3.3.3/genfft/dag.ml @ 22:b07fe9e906dc

Portaudio: add missed file
author Chris Cannam
date Tue, 26 Mar 2013 12:14:11 +0000
parents 37bf6b4a2645
children
rev   line source
Chris@10 1 (*
Chris@10 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
Chris@10 3 * Copyright (c) 2003, 2007-11 Matteo Frigo
Chris@10 4 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
Chris@10 5 *
Chris@10 6 * This program is free software; you can redistribute it and/or modify
Chris@10 7 * it under the terms of the GNU General Public License as published by
Chris@10 8 * the Free Software Foundation; either version 2 of the License, or
Chris@10 9 * (at your option) any later version.
Chris@10 10 *
Chris@10 11 * This program is distributed in the hope that it will be useful,
Chris@10 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@10 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@10 14 * GNU General Public License for more details.
Chris@10 15 *
Chris@10 16 * You should have received a copy of the GNU General Public License
Chris@10 17 * along with this program; if not, write to the Free Software
Chris@10 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@10 19 *
Chris@10 20 *)
Chris@10 21
Chris@10 22 open Util
Chris@10 23
Chris@10 24 (* Here, we have functions to transform a sequence of assignments
Chris@10 25 (variable = expression) into a DAG (a directed, acyclic graph).
Chris@10 26 The nodes of the DAG are the assignments, and the edges indicate
Chris@10 27 dependencies. (The DAG is analyzed in the scheduler to find an
Chris@10 28 efficient ordering of the assignments.)
Chris@10 29
Chris@10 30 This file also contains utilities to manipulate the DAG in various
Chris@10 31 ways. *)
Chris@10 32
Chris@10 33 (********************************************
Chris@10 34 * Dag structure
Chris@10 35 ********************************************)
Chris@10 36 type color = RED | BLUE | BLACK | YELLOW
Chris@10 37
Chris@10 38 type dagnode =
Chris@10 39 { assigned: Variable.variable;
Chris@10 40 mutable expression: Expr.expr;
Chris@10 41 input_variables: Variable.variable list;
Chris@10 42 mutable successors: dagnode list;
Chris@10 43 mutable predecessors: dagnode list;
Chris@10 44 mutable label: int;
Chris@10 45 mutable color: color}
Chris@10 46
Chris@10 47 type dag = Dag of (dagnode list)
Chris@10 48
Chris@10 49 (* true if node uses v *)
Chris@10 50 let node_uses v node =
Chris@10 51 List.exists (Variable.same v) node.input_variables
Chris@10 52
Chris@10 53 (* true if assignment of v clobbers any input of node *)
Chris@10 54 let node_clobbers node v =
Chris@10 55 List.exists (Variable.same_location v) node.input_variables
Chris@10 56
Chris@10 57 (* true if nodeb depends on nodea *)
Chris@10 58 let depends_on nodea nodeb =
Chris@10 59 node_uses nodea.assigned nodeb or
Chris@10 60 node_clobbers nodea nodeb.assigned
Chris@10 61
Chris@10 62 (* transform an assignment list into a dag *)
Chris@10 63 let makedag alist =
Chris@10 64 let dag = List.map
Chris@10 65 (fun assignment ->
Chris@10 66 let (v, x) = assignment in
Chris@10 67 { assigned = v;
Chris@10 68 expression = x;
Chris@10 69 input_variables = Expr.find_vars x;
Chris@10 70 successors = [];
Chris@10 71 predecessors = [];
Chris@10 72 label = 0;
Chris@10 73 color = BLACK })
Chris@10 74 alist
Chris@10 75 in begin
Chris@10 76 for_list dag (fun i ->
Chris@10 77 for_list dag (fun j ->
Chris@10 78 if depends_on i j then begin
Chris@10 79 i.successors <- j :: i.successors;
Chris@10 80 j.predecessors <- i :: j.predecessors;
Chris@10 81 end));
Chris@10 82 Dag dag;
Chris@10 83 end
Chris@10 84
Chris@10 85 let map f (Dag dag) = Dag (List.map f dag)
Chris@10 86 let for_all (Dag dag) f =
Chris@10 87 (* type system loophole *)
Chris@10 88 let make_unit _ = () in
Chris@10 89 make_unit (List.map f dag)
Chris@10 90 let to_list (Dag dag) = dag
Chris@10 91
Chris@10 92 let find_node f (Dag dag) = Util.find_elem f dag
Chris@10 93
Chris@10 94 (* breadth-first search *)
Chris@10 95 let rec bfs (Dag dag) node init_label =
Chris@10 96 let _ = node.label <- init_label in
Chris@10 97 let rec loop = function
Chris@10 98 [] -> ()
Chris@10 99 | node :: rest ->
Chris@10 100 let neighbors = node.predecessors @ node.successors in
Chris@10 101 let m = min_list (List.map (fun node -> node.label) neighbors) in
Chris@10 102 if (node.label > m + 1) then begin
Chris@10 103 node.label <- m + 1;
Chris@10 104 loop (rest @ neighbors);
Chris@10 105 end else
Chris@10 106 loop rest
Chris@10 107 in let neighbors = node.predecessors @ node.successors in
Chris@10 108 loop neighbors
Chris@10 109