annotate src/fftw-3.3.5/genfft/algsimp.ml @ 84:08ae793730bd

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