annotate src/fftw-3.3.8/genfft/algsimp.ml @ 82:d0c2a83c1364

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