annotate fft/fftw/fftw-3.3.4/genfft/algsimp.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
Chris@19 23 open Util
Chris@19 24 open Expr
Chris@19 25
Chris@19 26 let node_insert x = Assoctable.insert Expr.hash x
Chris@19 27 let node_lookup x = Assoctable.lookup Expr.hash (==) x
Chris@19 28
Chris@19 29 (*************************************************************
Chris@19 30 * Algebraic simplifier/elimination of common subexpressions
Chris@19 31 *************************************************************)
Chris@19 32 module AlgSimp : sig
Chris@19 33 val algsimp : expr list -> expr list
Chris@19 34 end = struct
Chris@19 35
Chris@19 36 open Monads.StateMonad
Chris@19 37 open Monads.MemoMonad
Chris@19 38 open Assoctable
Chris@19 39
Chris@19 40 let fetchSimp =
Chris@19 41 fetchState >>= fun (s, _) -> returnM s
Chris@19 42 let storeSimp s =
Chris@19 43 fetchState >>= (fun (_, c) -> storeState (s, c))
Chris@19 44 let lookupSimpM key =
Chris@19 45 fetchSimp >>= fun table ->
Chris@19 46 returnM (node_lookup key table)
Chris@19 47 let insertSimpM key value =
Chris@19 48 fetchSimp >>= fun table ->
Chris@19 49 storeSimp (node_insert key value table)
Chris@19 50
Chris@19 51 let subset a b =
Chris@19 52 List.for_all (fun x -> List.exists (fun y -> x == y) b) a
Chris@19 53
Chris@19 54 let structurallyEqualCSE a b =
Chris@19 55 match (a, b) with
Chris@19 56 | (Num a, Num b) -> Number.equal a b
Chris@19 57 | (NaN a, NaN b) -> a == b
Chris@19 58 | (Load a, Load b) -> Variable.same a b
Chris@19 59 | (Times (a, a'), Times (b, b')) ->
Chris@19 60 ((a == b) && (a' == b')) or
Chris@19 61 ((a == b') && (a' == b))
Chris@19 62 | (CTimes (a, a'), CTimes (b, b')) ->
Chris@19 63 ((a == b) && (a' == b')) or
Chris@19 64 ((a == b') && (a' == b))
Chris@19 65 | (CTimesJ (a, a'), CTimesJ (b, b')) -> ((a == b) && (a' == b'))
Chris@19 66 | (Plus a, Plus b) -> subset a b && subset b a
Chris@19 67 | (Uminus a, Uminus b) -> (a == b)
Chris@19 68 | _ -> false
Chris@19 69
Chris@19 70 let hashCSE x =
Chris@19 71 if (!Magic.randomized_cse) then
Chris@19 72 Oracle.hash x
Chris@19 73 else
Chris@19 74 Expr.hash x
Chris@19 75
Chris@19 76 let equalCSE a b =
Chris@19 77 if (!Magic.randomized_cse) then
Chris@19 78 (structurallyEqualCSE a b || Oracle.likely_equal a b)
Chris@19 79 else
Chris@19 80 structurallyEqualCSE a b
Chris@19 81
Chris@19 82 let fetchCSE =
Chris@19 83 fetchState >>= fun (_, c) -> returnM c
Chris@19 84 let storeCSE c =
Chris@19 85 fetchState >>= (fun (s, _) -> storeState (s, c))
Chris@19 86 let lookupCSEM key =
Chris@19 87 fetchCSE >>= fun table ->
Chris@19 88 returnM (Assoctable.lookup hashCSE equalCSE key table)
Chris@19 89 let insertCSEM key value =
Chris@19 90 fetchCSE >>= fun table ->
Chris@19 91 storeCSE (Assoctable.insert hashCSE key value table)
Chris@19 92
Chris@19 93 (* memoize both x and Uminus x (unless x is already negated) *)
Chris@19 94 let identityM x =
Chris@19 95 let memo x = memoizing lookupCSEM insertCSEM returnM x in
Chris@19 96 match x with
Chris@19 97 Uminus _ -> memo x
Chris@19 98 | _ -> memo x >>= fun x' -> memo (Uminus x') >> returnM x'
Chris@19 99
Chris@19 100 let makeNode = identityM
Chris@19 101
Chris@19 102 (* simplifiers for various kinds of nodes *)
Chris@19 103 let rec snumM = function
Chris@19 104 n when Number.is_zero n ->
Chris@19 105 makeNode (Num (Number.zero))
Chris@19 106 | n when Number.negative n ->
Chris@19 107 makeNode (Num (Number.negate n)) >>= suminusM
Chris@19 108 | n -> makeNode (Num n)
Chris@19 109
Chris@19 110 and suminusM = function
Chris@19 111 Uminus x -> makeNode x
Chris@19 112 | Num a when (Number.is_zero a) -> snumM Number.zero
Chris@19 113 | a -> makeNode (Uminus a)
Chris@19 114
Chris@19 115 and stimesM = function
Chris@19 116 | (Uminus a, b) -> stimesM (a, b) >>= suminusM
Chris@19 117 | (a, Uminus b) -> stimesM (a, b) >>= suminusM
Chris@19 118 | (NaN I, CTimes (a, b)) -> stimesM (NaN I, b) >>=
Chris@19 119 fun ib -> sctimesM (a, ib)
Chris@19 120 | (NaN I, CTimesJ (a, b)) -> stimesM (NaN I, b) >>=
Chris@19 121 fun ib -> sctimesjM (a, ib)
Chris@19 122 | (Num a, Num b) -> snumM (Number.mul a b)
Chris@19 123 | (Num a, Times (Num b, c)) ->
Chris@19 124 snumM (Number.mul a b) >>= fun x -> stimesM (x, c)
Chris@19 125 | (Num a, b) when Number.is_zero a -> snumM Number.zero
Chris@19 126 | (Num a, b) when Number.is_one a -> makeNode b
Chris@19 127 | (Num a, b) when Number.is_mone a -> suminusM b
Chris@19 128 | (a, b) when is_known_constant b && not (is_known_constant a) ->
Chris@19 129 stimesM (b, a)
Chris@19 130 | (a, b) -> makeNode (Times (a, b))
Chris@19 131
Chris@19 132 and sctimesM = function
Chris@19 133 | (Uminus a, b) -> sctimesM (a, b) >>= suminusM
Chris@19 134 | (a, Uminus b) -> sctimesM (a, b) >>= suminusM
Chris@19 135 | (a, b) -> makeNode (CTimes (a, b))
Chris@19 136
Chris@19 137 and sctimesjM = function
Chris@19 138 | (Uminus a, b) -> sctimesjM (a, b) >>= suminusM
Chris@19 139 | (a, Uminus b) -> sctimesjM (a, b) >>= suminusM
Chris@19 140 | (a, b) -> makeNode (CTimesJ (a, b))
Chris@19 141
Chris@19 142 and reduce_sumM x = match x with
Chris@19 143 [] -> returnM []
Chris@19 144 | [Num a] ->
Chris@19 145 if (Number.is_zero a) then
Chris@19 146 returnM []
Chris@19 147 else returnM x
Chris@19 148 | [Uminus (Num a)] ->
Chris@19 149 if (Number.is_zero a) then
Chris@19 150 returnM []
Chris@19 151 else returnM x
Chris@19 152 | (Num a) :: (Num b) :: s ->
Chris@19 153 snumM (Number.add a b) >>= fun x ->
Chris@19 154 reduce_sumM (x :: s)
Chris@19 155 | (Num a) :: (Uminus (Num b)) :: s ->
Chris@19 156 snumM (Number.sub a b) >>= fun x ->
Chris@19 157 reduce_sumM (x :: s)
Chris@19 158 | (Uminus (Num a)) :: (Num b) :: s ->
Chris@19 159 snumM (Number.sub b a) >>= fun x ->
Chris@19 160 reduce_sumM (x :: s)
Chris@19 161 | (Uminus (Num a)) :: (Uminus (Num b)) :: s ->
Chris@19 162 snumM (Number.add a b) >>=
Chris@19 163 suminusM >>= fun x ->
Chris@19 164 reduce_sumM (x :: s)
Chris@19 165 | ((Num _) as a) :: b :: s -> reduce_sumM (b :: a :: s)
Chris@19 166 | ((Uminus (Num _)) as a) :: b :: s -> reduce_sumM (b :: a :: s)
Chris@19 167 | a :: s ->
Chris@19 168 reduce_sumM s >>= fun s' -> returnM (a :: s')
Chris@19 169
Chris@19 170 and collectible1 = function
Chris@19 171 | NaN _ -> false
Chris@19 172 | Uminus x -> collectible1 x
Chris@19 173 | _ -> true
Chris@19 174 and collectible (a, b) = collectible1 a
Chris@19 175
Chris@19 176 (* collect common factors: ax + bx -> (a+b)x *)
Chris@19 177 and collectM which x =
Chris@19 178 let rec findCoeffM which = function
Chris@19 179 | Times (a, b) when collectible (which (a, b)) -> returnM (which (a, b))
Chris@19 180 | Uminus x ->
Chris@19 181 findCoeffM which x >>= fun (coeff, b) ->
Chris@19 182 suminusM coeff >>= fun mcoeff ->
Chris@19 183 returnM (mcoeff, b)
Chris@19 184 | x -> snumM Number.one >>= fun one -> returnM (one, x)
Chris@19 185 and separateM xpr = function
Chris@19 186 [] -> returnM ([], [])
Chris@19 187 | a :: b ->
Chris@19 188 separateM xpr b >>= fun (w, wo) ->
Chris@19 189 (* try first factor *)
Chris@19 190 findCoeffM (fun (a, b) -> (a, b)) a >>= fun (c, x) ->
Chris@19 191 if (xpr == x) && collectible (c, x) then returnM (c :: w, wo)
Chris@19 192 else
Chris@19 193 (* try second factor *)
Chris@19 194 findCoeffM (fun (a, b) -> (b, a)) a >>= fun (c, x) ->
Chris@19 195 if (xpr == x) && collectible (c, x) then returnM (c :: w, wo)
Chris@19 196 else returnM (w, a :: wo)
Chris@19 197 in match x with
Chris@19 198 [] -> returnM x
Chris@19 199 | [a] -> returnM x
Chris@19 200 | a :: b ->
Chris@19 201 findCoeffM which a >>= fun (_, xpr) ->
Chris@19 202 separateM xpr x >>= fun (w, wo) ->
Chris@19 203 collectM which wo >>= fun wo' ->
Chris@19 204 splusM w >>= fun w' ->
Chris@19 205 stimesM (w', xpr) >>= fun t' ->
Chris@19 206 returnM (t':: wo')
Chris@19 207
Chris@19 208 and mangleSumM x = returnM x
Chris@19 209 >>= reduce_sumM
Chris@19 210 >>= collectM (fun (a, b) -> (a, b))
Chris@19 211 >>= collectM (fun (a, b) -> (b, a))
Chris@19 212 >>= reduce_sumM
Chris@19 213 >>= deepCollectM !Magic.deep_collect_depth
Chris@19 214 >>= reduce_sumM
Chris@19 215
Chris@19 216 and reorder_uminus = function (* push all Uminuses to the end *)
Chris@19 217 [] -> []
Chris@19 218 | ((Uminus _) as a' :: b) -> (reorder_uminus b) @ [a']
Chris@19 219 | (a :: b) -> a :: (reorder_uminus b)
Chris@19 220
Chris@19 221 and canonicalizeM = function
Chris@19 222 [] -> snumM Number.zero
Chris@19 223 | [a] -> makeNode a (* one term *)
Chris@19 224 | a -> generateFusedMultAddM (reorder_uminus a)
Chris@19 225
Chris@19 226 and generateFusedMultAddM =
Chris@19 227 let rec is_multiplication = function
Chris@19 228 | Times (Num a, b) -> true
Chris@19 229 | Uminus (Times (Num a, b)) -> true
Chris@19 230 | _ -> false
Chris@19 231 and separate = function
Chris@19 232 [] -> ([], [], Number.zero)
Chris@19 233 | (Times (Num a, b)) as this :: c ->
Chris@19 234 let (x, y, max) = separate c in
Chris@19 235 let newmax = if (Number.greater a max) then a else max in
Chris@19 236 (this :: x, y, newmax)
Chris@19 237 | (Uminus (Times (Num a, b))) as this :: c ->
Chris@19 238 let (x, y, max) = separate c in
Chris@19 239 let newmax = if (Number.greater a max) then a else max in
Chris@19 240 (this :: x, y, newmax)
Chris@19 241 | this :: c ->
Chris@19 242 let (x, y, max) = separate c in
Chris@19 243 (x, this :: y, max)
Chris@19 244 in fun l ->
Chris@19 245 if !Magic.enable_fma && count is_multiplication l >= 2 then
Chris@19 246 let (w, wo, max) = separate l in
Chris@19 247 snumM (Number.div Number.one max) >>= fun invmax' ->
Chris@19 248 snumM max >>= fun max' ->
Chris@19 249 mapM (fun x -> stimesM (invmax', x)) w >>= splusM >>= fun pw' ->
Chris@19 250 stimesM (max', pw') >>= fun mw' ->
Chris@19 251 splusM (wo @ [mw'])
Chris@19 252 else
Chris@19 253 makeNode (Plus l)
Chris@19 254
Chris@19 255
Chris@19 256 and negative = function
Chris@19 257 Uminus _ -> true
Chris@19 258 | _ -> false
Chris@19 259
Chris@19 260 (*
Chris@19 261 * simplify patterns of the form
Chris@19 262 *
Chris@19 263 * ((c_1 * a + ...) + ...) + (c_2 * a + ...)
Chris@19 264 *
Chris@19 265 * The pattern includes arbitrary coefficients and minus signs.
Chris@19 266 * A common case of this pattern is the butterfly
Chris@19 267 * (a + b) + (a - b)
Chris@19 268 * (a + b) - (a - b)
Chris@19 269 *)
Chris@19 270 (* this whole procedure needs much more thought *)
Chris@19 271 and deepCollectM maxdepth l =
Chris@19 272 let rec findTerms depth x = match x with
Chris@19 273 | Uminus x -> findTerms depth x
Chris@19 274 | Times (Num _, b) -> (findTerms (depth - 1) b)
Chris@19 275 | Plus l when depth > 0 ->
Chris@19 276 x :: List.flatten (List.map (findTerms (depth - 1)) l)
Chris@19 277 | x -> [x]
Chris@19 278 and duplicates = function
Chris@19 279 [] -> []
Chris@19 280 | a :: b -> if List.memq a b then a :: duplicates b
Chris@19 281 else duplicates b
Chris@19 282
Chris@19 283 in let rec splitDuplicates depth d x =
Chris@19 284 if (List.memq x d) then
Chris@19 285 snumM (Number.zero) >>= fun zero ->
Chris@19 286 returnM (zero, x)
Chris@19 287 else match x with
Chris@19 288 | Times (a, b) ->
Chris@19 289 splitDuplicates (depth - 1) d a >>= fun (a', xa) ->
Chris@19 290 splitDuplicates (depth - 1) d b >>= fun (b', xb) ->
Chris@19 291 stimesM (a', b') >>= fun ab ->
Chris@19 292 stimesM (a, xb) >>= fun xb' ->
Chris@19 293 stimesM (xa, b) >>= fun xa' ->
Chris@19 294 stimesM (xa, xb) >>= fun xab ->
Chris@19 295 splusM [xa'; xb'; xab] >>= fun x ->
Chris@19 296 returnM (ab, x)
Chris@19 297 | Uminus a ->
Chris@19 298 splitDuplicates depth d a >>= fun (x, y) ->
Chris@19 299 suminusM x >>= fun ux ->
Chris@19 300 suminusM y >>= fun uy ->
Chris@19 301 returnM (ux, uy)
Chris@19 302 | Plus l when depth > 0 ->
Chris@19 303 mapM (splitDuplicates (depth - 1) d) l >>= fun ld ->
Chris@19 304 let (l', d') = List.split ld in
Chris@19 305 splusM l' >>= fun p ->
Chris@19 306 splusM d' >>= fun d'' ->
Chris@19 307 returnM (p, d'')
Chris@19 308 | x ->
Chris@19 309 snumM (Number.zero) >>= fun zero' ->
Chris@19 310 returnM (x, zero')
Chris@19 311
Chris@19 312 in let l' = List.flatten (List.map (findTerms maxdepth) l)
Chris@19 313 in match duplicates l' with
Chris@19 314 | [] -> returnM l
Chris@19 315 | d ->
Chris@19 316 mapM (splitDuplicates maxdepth d) l >>= fun ld ->
Chris@19 317 let (l', d') = List.split ld in
Chris@19 318 splusM l' >>= fun l'' ->
Chris@19 319 let rec flattenPlusM = function
Chris@19 320 | Plus l -> returnM l
Chris@19 321 | Uminus x ->
Chris@19 322 flattenPlusM x >>= mapM suminusM
Chris@19 323 | x -> returnM [x]
Chris@19 324 in
Chris@19 325 mapM flattenPlusM d' >>= fun d'' ->
Chris@19 326 splusM (List.flatten d'') >>= fun d''' ->
Chris@19 327 mangleSumM [l''; d''']
Chris@19 328
Chris@19 329 and splusM l =
Chris@19 330 let fma_heuristics x =
Chris@19 331 if !Magic.enable_fma then
Chris@19 332 match x with
Chris@19 333 | [Uminus (Times _); Times _] -> Some false
Chris@19 334 | [Times _; Uminus (Times _)] -> Some false
Chris@19 335 | [Uminus (_); Times _] -> Some true
Chris@19 336 | [Times _; Uminus (Plus _)] -> Some true
Chris@19 337 | [_; Uminus (Times _)] -> Some false
Chris@19 338 | [Uminus (Times _); _] -> Some false
Chris@19 339 | _ -> None
Chris@19 340 else
Chris@19 341 None
Chris@19 342 in
Chris@19 343 mangleSumM l >>= fun l' ->
Chris@19 344 (* no terms are negative. Don't do anything *)
Chris@19 345 if not (List.exists negative l') then
Chris@19 346 canonicalizeM l'
Chris@19 347 (* all terms are negative. Negate them all and collect the minus sign *)
Chris@19 348 else if List.for_all negative l' then
Chris@19 349 mapM suminusM l' >>= splusM >>= suminusM
Chris@19 350 else match fma_heuristics l' with
Chris@19 351 | Some true -> mapM suminusM l' >>= splusM >>= suminusM
Chris@19 352 | Some false -> canonicalizeM l'
Chris@19 353 | None ->
Chris@19 354 (* Ask the Oracle for the canonical form *)
Chris@19 355 if (not !Magic.randomized_cse) &&
Chris@19 356 Oracle.should_flip_sign (Plus l') then
Chris@19 357 mapM suminusM l' >>= splusM >>= suminusM
Chris@19 358 else
Chris@19 359 canonicalizeM l'
Chris@19 360
Chris@19 361 (* monadic style algebraic simplifier for the dag *)
Chris@19 362 let rec algsimpM x =
Chris@19 363 memoizing lookupSimpM insertSimpM
Chris@19 364 (function
Chris@19 365 | Num a -> snumM a
Chris@19 366 | NaN _ as x -> makeNode x
Chris@19 367 | Plus a ->
Chris@19 368 mapM algsimpM a >>= splusM
Chris@19 369 | Times (a, b) ->
Chris@19 370 (algsimpM a >>= fun a' ->
Chris@19 371 algsimpM b >>= fun b' ->
Chris@19 372 stimesM (a', b'))
Chris@19 373 | CTimes (a, b) ->
Chris@19 374 (algsimpM a >>= fun a' ->
Chris@19 375 algsimpM b >>= fun b' ->
Chris@19 376 sctimesM (a', b'))
Chris@19 377 | CTimesJ (a, b) ->
Chris@19 378 (algsimpM a >>= fun a' ->
Chris@19 379 algsimpM b >>= fun b' ->
Chris@19 380 sctimesjM (a', b'))
Chris@19 381 | Uminus a ->
Chris@19 382 algsimpM a >>= suminusM
Chris@19 383 | Store (v, a) ->
Chris@19 384 algsimpM a >>= fun a' ->
Chris@19 385 makeNode (Store (v, a'))
Chris@19 386 | Load _ as x -> makeNode x)
Chris@19 387 x
Chris@19 388
Chris@19 389 let initialTable = (empty, empty)
Chris@19 390 let simp_roots = mapM algsimpM
Chris@19 391 let algsimp = runM initialTable simp_roots
Chris@19 392 end
Chris@19 393
Chris@19 394 (*************************************************************
Chris@19 395 * Network transposition algorithm
Chris@19 396 *************************************************************)
Chris@19 397 module Transpose = struct
Chris@19 398 open Monads.StateMonad
Chris@19 399 open Monads.MemoMonad
Chris@19 400 open Littlesimp
Chris@19 401
Chris@19 402 let fetchDuals = fetchState
Chris@19 403 let storeDuals = storeState
Chris@19 404
Chris@19 405 let lookupDualsM key =
Chris@19 406 fetchDuals >>= fun table ->
Chris@19 407 returnM (node_lookup key table)
Chris@19 408
Chris@19 409 let insertDualsM key value =
Chris@19 410 fetchDuals >>= fun table ->
Chris@19 411 storeDuals (node_insert key value table)
Chris@19 412
Chris@19 413 let rec visit visited vtable parent_table = function
Chris@19 414 [] -> (visited, parent_table)
Chris@19 415 | node :: rest ->
Chris@19 416 match node_lookup node vtable with
Chris@19 417 | Some _ -> visit visited vtable parent_table rest
Chris@19 418 | None ->
Chris@19 419 let children = match node with
Chris@19 420 | Store (v, n) -> [n]
Chris@19 421 | Plus l -> l
Chris@19 422 | Times (a, b) -> [a; b]
Chris@19 423 | CTimes (a, b) -> [a; b]
Chris@19 424 | CTimesJ (a, b) -> [a; b]
Chris@19 425 | Uminus x -> [x]
Chris@19 426 | _ -> []
Chris@19 427 in let rec loop t = function
Chris@19 428 [] -> t
Chris@19 429 | a :: rest ->
Chris@19 430 (match node_lookup a t with
Chris@19 431 None -> loop (node_insert a [node] t) rest
Chris@19 432 | Some c -> loop (node_insert a (node :: c) t) rest)
Chris@19 433 in
Chris@19 434 (visit
Chris@19 435 (node :: visited)
Chris@19 436 (node_insert node () vtable)
Chris@19 437 (loop parent_table children)
Chris@19 438 (children @ rest))
Chris@19 439
Chris@19 440 let make_transposer parent_table =
Chris@19 441 let rec termM node candidate_parent =
Chris@19 442 match candidate_parent with
Chris@19 443 | Store (_, n) when n == node ->
Chris@19 444 dualM candidate_parent >>= fun x' -> returnM [x']
Chris@19 445 | Plus (l) when List.memq node l ->
Chris@19 446 dualM candidate_parent >>= fun x' -> returnM [x']
Chris@19 447 | Times (a, b) when b == node ->
Chris@19 448 dualM candidate_parent >>= fun x' ->
Chris@19 449 returnM [makeTimes (a, x')]
Chris@19 450 | CTimes (a, b) when b == node ->
Chris@19 451 dualM candidate_parent >>= fun x' ->
Chris@19 452 returnM [CTimes (a, x')]
Chris@19 453 | CTimesJ (a, b) when b == node ->
Chris@19 454 dualM candidate_parent >>= fun x' ->
Chris@19 455 returnM [CTimesJ (a, x')]
Chris@19 456 | Uminus n when n == node ->
Chris@19 457 dualM candidate_parent >>= fun x' ->
Chris@19 458 returnM [makeUminus x']
Chris@19 459 | _ -> returnM []
Chris@19 460
Chris@19 461 and dualExpressionM this_node =
Chris@19 462 mapM (termM this_node)
Chris@19 463 (match node_lookup this_node parent_table with
Chris@19 464 | Some a -> a
Chris@19 465 | None -> failwith "bug in dualExpressionM"
Chris@19 466 ) >>= fun l ->
Chris@19 467 returnM (makePlus (List.flatten l))
Chris@19 468
Chris@19 469 and dualM this_node =
Chris@19 470 memoizing lookupDualsM insertDualsM
Chris@19 471 (function
Chris@19 472 | Load v as x ->
Chris@19 473 if (Variable.is_constant v) then
Chris@19 474 returnM (Load v)
Chris@19 475 else
Chris@19 476 (dualExpressionM x >>= fun d ->
Chris@19 477 returnM (Store (v, d)))
Chris@19 478 | Store (v, x) -> returnM (Load v)
Chris@19 479 | x -> dualExpressionM x)
Chris@19 480 this_node
Chris@19 481
Chris@19 482 in dualM
Chris@19 483
Chris@19 484 let is_store = function
Chris@19 485 | Store _ -> true
Chris@19 486 | _ -> false
Chris@19 487
Chris@19 488 let transpose dag =
Chris@19 489 let _ = Util.info "begin transpose" in
Chris@19 490 let (all_nodes, parent_table) =
Chris@19 491 visit [] Assoctable.empty Assoctable.empty dag in
Chris@19 492 let transposerM = make_transposer parent_table in
Chris@19 493 let mapTransposerM = mapM transposerM in
Chris@19 494 let duals = runM Assoctable.empty mapTransposerM all_nodes in
Chris@19 495 let roots = List.filter is_store duals in
Chris@19 496 let _ = Util.info "end transpose" in
Chris@19 497 roots
Chris@19 498 end
Chris@19 499
Chris@19 500
Chris@19 501 (*************************************************************
Chris@19 502 * Various dag statistics
Chris@19 503 *************************************************************)
Chris@19 504 module Stats : sig
Chris@19 505 type complexity
Chris@19 506 val complexity : Expr.expr list -> complexity
Chris@19 507 val same_complexity : complexity -> complexity -> bool
Chris@19 508 val leq_complexity : complexity -> complexity -> bool
Chris@19 509 val to_string : complexity -> string
Chris@19 510 end = struct
Chris@19 511 type complexity = int * int * int * int * int * int
Chris@19 512 let rec visit visited vtable = function
Chris@19 513 [] -> visited
Chris@19 514 | node :: rest ->
Chris@19 515 match node_lookup node vtable with
Chris@19 516 Some _ -> visit visited vtable rest
Chris@19 517 | None ->
Chris@19 518 let children = match node with
Chris@19 519 Store (v, n) -> [n]
Chris@19 520 | Plus l -> l
Chris@19 521 | Times (a, b) -> [a; b]
Chris@19 522 | Uminus x -> [x]
Chris@19 523 | _ -> []
Chris@19 524 in visit (node :: visited)
Chris@19 525 (node_insert node () vtable)
Chris@19 526 (children @ rest)
Chris@19 527
Chris@19 528 let complexity dag =
Chris@19 529 let rec loop (load, store, plus, times, uminus, num) = function
Chris@19 530 [] -> (load, store, plus, times, uminus, num)
Chris@19 531 | node :: rest ->
Chris@19 532 loop
Chris@19 533 (match node with
Chris@19 534 | Load _ -> (load + 1, store, plus, times, uminus, num)
Chris@19 535 | Store _ -> (load, store + 1, plus, times, uminus, num)
Chris@19 536 | Plus x -> (load, store, plus + (List.length x - 1), times, uminus, num)
Chris@19 537 | Times _ -> (load, store, plus, times + 1, uminus, num)
Chris@19 538 | Uminus _ -> (load, store, plus, times, uminus + 1, num)
Chris@19 539 | Num _ -> (load, store, plus, times, uminus, num + 1)
Chris@19 540 | CTimes _ -> (load, store, plus, times, uminus, num)
Chris@19 541 | CTimesJ _ -> (load, store, plus, times, uminus, num)
Chris@19 542 | NaN _ -> (load, store, plus, times, uminus, num))
Chris@19 543 rest
Chris@19 544 in let (l, s, p, t, u, n) =
Chris@19 545 loop (0, 0, 0, 0, 0, 0) (visit [] Assoctable.empty dag)
Chris@19 546 in (l, s, p, t, u, n)
Chris@19 547
Chris@19 548 let weight (l, s, p, t, u, n) =
Chris@19 549 l + s + 10 * p + 20 * t + u + n
Chris@19 550
Chris@19 551 let same_complexity a b = weight a = weight b
Chris@19 552 let leq_complexity a b = weight a <= weight b
Chris@19 553
Chris@19 554 let to_string (l, s, p, t, u, n) =
Chris@19 555 Printf.sprintf "ld=%d st=%d add=%d mul=%d uminus=%d num=%d\n"
Chris@19 556 l s p t u n
Chris@19 557
Chris@19 558 end
Chris@19 559
Chris@19 560 (* simplify the dag *)
Chris@19 561 let algsimp v =
Chris@19 562 let rec simplification_loop v =
Chris@19 563 let () = Util.info "simplification step" in
Chris@19 564 let complexity = Stats.complexity v in
Chris@19 565 let () = Util.info ("complexity = " ^ (Stats.to_string complexity)) in
Chris@19 566 let v = (AlgSimp.algsimp @@ Transpose.transpose @@
Chris@19 567 AlgSimp.algsimp @@ Transpose.transpose) v in
Chris@19 568 let complexity' = Stats.complexity v in
Chris@19 569 let () = Util.info ("complexity = " ^ (Stats.to_string complexity')) in
Chris@19 570 if (Stats.leq_complexity complexity' complexity) then
Chris@19 571 let () = Util.info "end algsimp" in
Chris@19 572 v
Chris@19 573 else
Chris@19 574 simplification_loop v
Chris@19 575
Chris@19 576 in
Chris@19 577 let () = Util.info "begin algsimp" in
Chris@19 578 let v = AlgSimp.algsimp v in
Chris@19 579 if !Magic.network_transposition then simplification_loop v else v
Chris@19 580