annotate src/fftw-3.3.8/genfft/algsimp.ml @ 167:bd3cc4d1df30

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