annotate src/fftw-3.3.3/genfft/algsimp.ml @ 148:b4bfdf10c4b3

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