-
Notifications
You must be signed in to change notification settings - Fork 5
/
Main.hs
297 lines (240 loc) · 7.81 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
{-# LANGUAGE FlexibleContexts, BangPatterns #-}
module Main (main) where
import Data.Int
import Data.List
import Data.Word
import qualified Data.IntMap as IM
import Data.Array
import Control.Monad.ST
import Control.Monad.Memo
import Control.Monad.Memo.Class
import Control.Monad.Memo.Vector.Unsafe
import Control.Monad.Memo.Vector.Expandable
import Criterion.Main
-- Fibonacci numbers
--------------------
{-# INLINE fibm #-}
fibm :: (Eq k, Num k, Num v, MonadMemo k v m) => k -> m v
fibm 0 = return 0
fibm 1 = return 1
fibm n = do
f1 <- memo fibm (n - 1)
f2 <- memo fibm (n - 2)
return (f1+f2)
fibM :: Int -> Word
fibM = startEvalMemo . fibm
fibIM :: Int -> Word
fibIM n = evalMemoState (fibm n) IM.empty
fibIOA :: Int -> IO Word
fibIOA n = evalArrayMemo (fibm n) (0,n)
fibIOUA :: Int -> IO Word
fibIOUA n = evalUArrayMemo (fibm n) (0,n)
fibSTA :: Int -> Word
fibSTA n = runST $ evalArrayMemo (fibm n) (0,n)
fibSTUA :: Int -> Word
fibSTUA n = runST $ evalUArrayMemo (fibm n) (0,n)
fibIOV :: Int -> IO Word
fibIOV n = evalVectorMemo (fibm n) (n+1)
fibIOUV :: Int -> IO Word
fibIOUV n = evalUVectorMemo (fibm n) (n+1)
fibSTV :: Int -> Word
fibSTV n = runST $ evalVectorMemo (fibm n) (n+1)
fibSTUV :: Int -> Word
fibSTUV n = runST $ evalUVectorMemo (fibm n) (n+1)
fibIOVU :: Int -> IO Word
fibIOVU n = unsafeEvalVectorMemo (fibm n) (n+1)
fibIOUVU :: Int -> IO Word
fibIOUVU n = unsafeEvalUVectorMemo (fibm n) (n+1)
fibSTVU :: Int -> Word
fibSTVU n = runST $ unsafeEvalVectorMemo (fibm n) (n+1)
fibSTUVU :: Int -> Word
fibSTUVU n = runST $ unsafeEvalUVectorMemo (fibm n) (n+1)
fibIOVE :: Int -> IO Word
fibIOVE n = startEvalVectorMemo (fibm n)
fibIOUVE :: Int -> IO Word
fibIOUVE n = startEvalUVectorMemo (fibm n)
fibSTVE :: Int -> Word
fibSTVE n = runST $ startEvalVectorMemo (fibm n)
fibSTUVE :: Int -> Word
fibSTUVE n = runST $ startEvalUVectorMemo (fibm n)
-- 0-1 Knapsack problem
-----------------------
{-# INLINE knap #-}
knap :: MonadMemo (Int, Int) Int m => [Int] -> [Int] -> Int -> m Int
knap ws vs = m (l-1)
where
l = length ws
wa = listArray (0,l-1) ws
va = listArray (0,l-1) vs
{-# INLINE m #-}
m 0 _ = return 0
m !i !w
| wa ! i > w = for2 memo m (i-1) w
| otherwise = do
!m1 <- for2 memo m (i-1) w
!m2 <- for2 memo m (i-1) (w - wa ! i)
return (m1 `max` (m2 + va ! i))
knapM :: [Int] -> [Int] -> Int -> Int
knapM ws vs w = startEvalMemo (knap ws vs w)
knapSTA :: [Int] -> [Int] -> Int -> Int
knapSTA ws vs w = runST $ evalArrayMemo (knap ws vs w) ((0,0), ((length ws),w))
knapSTUA :: [Int] -> [Int] -> Int -> Int
knapSTUA ws vs w = runST $ evalUArrayMemo (knap ws vs w) ((0,0), ((length ws),w))
knapIOA :: [Int] -> [Int] -> Int -> IO Int
knapIOA ws vs w = evalArrayMemo (knap ws vs w) ((0,0), ((length ws),w))
knapIOUA :: [Int] -> [Int] -> Int -> IO Int
knapIOUA ws vs w = evalUArrayMemo (knap ws vs w) ((0,0), ((length ws),w))
-- Longest common subsequence
-----------------------------
{-# INLINE lcsm2 #-}
lcsm2 :: MonadMemo (Int,Int) Int m => [Int] -> [Int] -> m Int
lcsm2 as bs = lcs la lb
where
la = length as
lb = length bs
aa = listArray (1,la) as
ba = listArray (1,lb) bs
{-# INLINE lcs #-}
lcs 0 _ = return 0
lcs _ 0 = return 0
lcs ia ib
| (aa!ia) == (ba!ib) = succ `liftM` for2 memo lcs (ia-1) (ib-1)
| otherwise = do
!l1 <- for2 memo lcs (ia-1) ib
!l2 <- for2 memo lcs ia (ib-1)
return (l1 `max` l2)
lcsM :: [Int] -> [Int] -> Int
lcsM as bs = startEvalMemo (lcsm2 as bs)
lcsSTA :: [Int] -> [Int] -> Int
lcsSTA as bs = runST $ evalArrayMemo (lcsm2 as bs) ((0,0), (length as, length bs))
lcsSTUA :: [Int] -> [Int] -> Int
lcsSTUA as bs = runST $ evalUArrayMemo (lcsm2 as bs) ((0,0), (length as, length bs))
{-# INLINE lcsm #-}
lcsm :: MonadMemo Int Int m => [Int] -> [Int] -> m Int
lcsm as bs = lcs la lb
where
la = genericLength as
lb = genericLength bs
aa = listArray (1,la) as
ba = listArray (1,lb) bs
{-# INLINE lcs #-}
lcs 0 _ = return 0
lcs _ 0 = return 0
lcs ia ib
| (aa!ia) == (ba!ib) = succ `liftM` mlcs (ia-1) (ib-1)
| otherwise = do
l1 <- mlcs (ia-1) ib
l2 <- mlcs ia (ib-1)
return (l1 `max` l2)
mlcs ai bi =
memo (\abi -> uncurry lcs $! abi `quotRem` lb) (ai*lb + bi)
lcsIM :: [Int] -> [Int] -> Int
lcsIM as bs = evalMemoState (lcsm as bs) IM.empty
lcsSTUV :: [Int] -> [Int] -> Int
lcsSTUV as bs = runST $ evalUVectorMemo (lcsm as bs) ((length as + 1) * (length bs + 1))
lcsSTUVE :: [Int] -> [Int] -> Int
lcsSTUVE as bs = runST $ startEvalUVectorMemo (lcsm as bs)
-- | Hofstadter Female and Male sequences
-- Mutually recursive memoized functions
gof :: (MonadTrans t, MonadCache Int Int m, MonadCache Int Int (t m)) => Int -> t m Int
gof 0 = return 1
gof i = do
fs <- memol0 gof (i-1)
ms <- memol1 gom fs
return (i - ms)
gom :: (MonadTrans t, MonadCache Int Int m, MonadCache Int Int (t m)) => Int -> t m Int
gom 0 = return 0
gom i = do
ms <- memol1 gom (i-1)
fs <- memol0 gof ms
return (i - fs)
fM :: Int -> Int
fM = startEvalMemo . startEvalMemoT . gof
fSTA :: Int -> Int
fSTA n = runST $ (`evalArrayMemo`(0,n)) . (`evalArrayMemo`(0,n)) . gof $ n
fSTAU :: Int -> Int
fSTAU n = runST $ (`evalUArrayMemo`(0,n)) . (`evalUArrayMemo`(0,n)) . gof $ n
fSTV :: Int -> Int
fSTV n = runST $ (`evalVectorMemo`(n+1)) . (`evalVectorMemo`(n+1)) . gof $ n
fSTVU :: Int -> Int
fSTVU n = runST $ (`evalUVectorMemo`(n+1)) . (`evalUVectorMemo`(n+1)) . gof $ n
fSTVUU :: Int -> Int
fSTVUU n = runST $ (`unsafeEvalUVectorMemo`(n+1)) . (`unsafeEvalUVectorMemo`(n+1)) . gof $ n
main :: IO ()
main = defaultMainWith defaultConfig [
bgroup "fib" [
bgroup "pure" [
bench "Map" $ whnf fibM n
, bench "IntMap" $ whnf fibIM n
]
, bgroup "ST" [
bench "Array" $ whnf fibSTA n
, bench "UArray" $ whnf fibSTUA n
, bench "Vector" $ whnf fibSTV n
, bench "UVector" $ whnf fibSTUV n
, bench "Vector unsafe" $ whnf fibSTVU n
, bench "UVector unsafe" $ whnf fibSTUVU n
, bench "Vector exp" $ whnf fibSTVE n
, bench "UVector exp" $ whnf fibSTUVE n
]
, bgroup "IO" [
bench "Array" $ whnfIO (fibIOA n)
, bench "UArray" $ whnfIO (fibIOUA n)
, bench "Vector" $ whnfIO (fibIOV n)
, bench "UVector" $ whnfIO (fibIOUV n)
, bench "Vector unsafe" $ whnfIO (fibIOVU n)
, bench "UVector unsafe" $ whnfIO (fibIOUVU n)
, bench "Vector exp" $ whnfIO (fibIOVE n)
, bench "UVector exp" $ whnfIO (fibIOUVE n)
]
]
, bgroup "knapsack" [
bgroup "pure" [
bench "Map" $ whnf (knapM ws vs) w
]
, bgroup "ST" [
bench "Array" $ whnf (knapSTA ws vs) w
, bench "UArray" $ whnf (knapSTUA ws vs) w
]
, bgroup "IO" [
bench "Array" $ whnfIO (knapIOA ws vs w)
, bench "UArray" $ whnfIO (knapIOUA ws vs w)
]
]
, bgroup "LCS" [
bgroup "pure" [
bench "Map" $ whnf (lcsM as) bs
, bench "IntMap" $ whnf (lcsIM as) bs
]
, bgroup "ST" [
bench "Array" $ whnf (lcsSTA as) bs
, bench "UArray" $ whnf (lcsSTUA as) bs
, bench "UVector exp" $ whnf (lcsSTUVE as) bs
, bench "UVector" $ whnf (lcsSTUV as) bs
]
]
, bgroup "Hofstadter" [
bgroup "pure" [
bench "Map" $ whnf fM fn
]
, bgroup "ST" [
bench "Array" $ whnf fSTA fn
, bench "UArray" $ whnf fSTAU fn
, bench "Vector" $ whnf fSTV fn
, bench "UVector" $ whnf fSTVU fn
, bench "UVector unsafe" $ whnf fSTVUU fn
]
]
]
where
-- fib arg
n = 100000
-- knapsac args
ws = [1..200]
vs = [1..200]
w = 800
-- LCS args
as = [1..400]
bs = [100,102..800]
-- Hofstadter
fn = 100000