Inlining: unroll1.hs

File unroll1.hs, 1.6 KB (added by claus, 10 years ago)

loop over bulk-array op, INLINE loop PEEL 1 UNROLL 4 activates array fusion RULES

Line 
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE MagicHash #-}
3{-# LANGUAGE BangPatterns #-}
4
5import Data.Array.Vector
6import Data.Bits
7import GHC.Prim
8import GHC.Base
9
10-- ~0.4s real with DOINLINE+enable-rewrite-rules (array fusion for unfolded loop bodies)
11-- ~1.1s real with DOINLINE+no-enable-rewrite-rules
12-- ~1.2s real without DOINLINE
13main = print $ loop 1 10000000 body (toU [1,2,3,4,5::Int])
14
15body i arr = mapU (42+) arr
16
17#ifdef DOINLINE
18-- simulating INLINE loop PEEL 1 UNROLL 4
19-- (assuming that PEEL 1 also triggers static argument transformation)
20{-# INLINE loop #-}
21loop :: Int -> Int -> (Int -> acc -> acc) -> acc -> acc
22loop !i !max !body !acc = loopW i acc
23  where
24  loopW !i !acc | i+4<=max  = loopW (i+4) (body (i+3) (body (i+2) (body (i+1) (body i acc))))
25  loopW !i !acc | i+3<=max  = loopW (i+3) (body (i+2) (body (i+1) (body i acc)))
26  loopW !i !acc | i+2<=max  = loopW (i+2) (body (i+1) (body i acc))
27  loopW !i !acc | i<=max    = loopW (i+1) (body i acc)
28                | otherwise = acc
29#else
30loop :: Int -> Int -> (Int -> acc -> acc) -> acc -> acc
31loop !i !max !body !acc | i<=max    = loop (i+1) max body (body i acc)
32                        | otherwise = acc
33#endif
34
35-- ISSUE: we would like to associate constants and variables into separate groups,
36--        to enable constant folding
37-- to simulate, we make use of the fact that the variable is under a known function..
38-- (this rules seems to work, but makes no difference to timing)
39{-# RULES
40-- "reassoc" forall a# b# c. ((I# a#) +# ((I# b#) +# c)) = ((I# a#) +# (I# b#)) +# c
41"reassoc" forall a b i c. a +# (b +# (indexIntArray# i c)) = (a +# b) +# (indexIntArray# i c)
42  #-}