Skip to content

Commit 8ebefe9

Browse files
author
Thomas Mahler
committed
add benchmark for mhs compile + execute
1 parent 1c648cd commit 8ebefe9

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ cabal.project.local~
2525
stack.yaml.lock
2626
.idea/
2727
out/
28-
*.iml
28+
*.iml
29+
out.comb

benchmark/Akk.mhs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Akk (main) where
2+
import Data.Function (fix)
3+
4+
ack :: Int -> Int -> Int
5+
ack = fix (\f n m ->
6+
if n == 0
7+
then m + 1
8+
else (if m == 0
9+
then f (n-1) 1
10+
else f (n-1) (f n (m-1))))
11+
12+
main = ack 3 9

benchmark/Fib.mhs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Fib (main) where
2+
import Data.Function (fix)
3+
4+
fib :: Int -> Int
5+
fib = fix (\f n ->
6+
if n <= 1
7+
then 1
8+
else f (n-1) + f (n - 2))
9+
10+
main = fib 37

benchmark/ReductionBenchmarks.hs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import HhiReducer
1313
import Control.Monad.Fix ( fix )
1414
import BenchmarkSources
1515
import MicroHsExp ( toMhsPrg )
16+
import qualified MicroHs.Main as MHS (main)
17+
import System.Environment (withArgs)
18+
import System.IO (readFile')
1619
import MhsEval
1720

1821
loadTestCase :: SourceCode -> IO CL
@@ -72,7 +75,15 @@ reducerTestLog expr = show $ transLinkLog primitives expr
7275
microHsTest :: MhsContext -> String -> IO ()
7376
microHsTest ctx prg = run ctx prg
7477

75-
78+
microHsCompile :: FilePath -> IO String
79+
microHsCompile fileName = do
80+
-- use microHs to compile file fileName to 'out.comb'
81+
withArgs [fileName] MHS.main
82+
-- return the program 'out.comb' as a string
83+
content <- readFile' "out.comb"
84+
--deleteFile "out.comb"
85+
return content
86+
7687
benchmarks :: IO ()
7788
benchmarks = do
7889
fac <- loadTestCase factorial
@@ -107,6 +118,11 @@ benchmarks = do
107118
mhsFibEta = toMhsPrg fibEta
108119
mhsAckEta = toMhsPrg akkEta
109120

121+
-- prepare MicroHs programs
122+
mhsFib <- microHsCompile "benchmark/Fib.mhs"
123+
mhsAkk <- microHsCompile "benchmark/Akk.mhs"
124+
mhsTak <- microHsCompile "benchmark/Tak.mhs"
125+
110126
print facEta
111127
print takEta
112128
print fibEta
@@ -130,19 +146,19 @@ benchmarks = do
130146
-- , bench "factorial Graph-Reduce-Lin" $ nf graphTest facBulkLinear
131147
-- , bench "factorial Graph-Reduce-Log" $ nf graphTest facBulkLog
132148
-- , bench "factorial HHI-Reduce" $ nf reducerTest fac
133-
bench "factorial HHI-Eta" $ nf reducerTest facEta
149+
-- bench "factorial HHI-Eta" $ nf reducerTest facEta
134150
-- , bench "factorial HHI-Bulk" $ nf reducerTest facBulk
135151
-- , bench "factorial HHI-Bulk-Log" $ nf reducerTestLog facBulk
136152
-- , bench "factorial HHI-Break-Bulk" $ nf reducerTest facBulkLinear
137153
-- , bench "factorial HHI-Break-Log" $ nf reducerTestLog facBulkLog
138-
, bench "factorial MicroHs" $ nfIO (microHsTest mhsContext mhsFacEta)
139-
, bench "factorial Native" $ nf fact 10
154+
--, bench "factorial MicroHs" $ nfIO (microHsTest mhsContext mhsFacEta)
155+
--, bench "factorial Native" $ nf fact 10
140156
-- bench "fibonacci Graph-Reduce" $ nf graphTest fib
141157
--, bench "fibonacci Graph-Reduce-Eta" $ nf graphTest fibEta
142158
--, bench "fibonacci Graph-Reduce-Lin" $ nf graphTest fibBulkLinear
143159
--, bench "fibonacci Graph-Reduce-Log" $ nf graphTest fibBulkLog
144160
--, bench "fibonacci HHI-Reduce" $ nf reducerTest fib
145-
, bench "fibonacci HHI-Eta" $ nf reducerTest fibEta
161+
bench "fibonacci HHI-Eta" $ nf reducerTest fibEta
146162
-- , bench "fibonacci HHi-Bulk" $ nf reducerTest fibBulk
147163
-- , bench "fibonacci HHI-Bulk-Log" $ nf reducerTestLog fibBulk
148164
-- , bench "fibonacci HHI-Break-Bulk" $ nf reducerTest fibBulkLinear
@@ -153,6 +169,7 @@ benchmarks = do
153169
--, bench "ackermann Graph-Reduce-Eta" $ nf graphTest akkEta
154170
-- , bench "ackermann Graph-Reduce-Lin" $ nf graphTest akkBulkLinear
155171
-- , bench "ackermann Graph-Reduce-Log" $ nf graphTest akkBulkLog
172+
, bench "fibonacci MHS Haskell" $ nfIO (microHsTest mhsContext mhsFib)
156173
, bench "ackermann HHI-Reduce" $ nf reducerTest akkEta
157174
-- , bench "ackermann HHI-Eta" $ nf reducerTest akkEta
158175
-- , bench "ackermann HHI-Bulk" $ nf reducerTest akkBulk
@@ -173,13 +190,15 @@ benchmarks = do
173190
-- , bench "tak Graph-Reduce-Lin" $ nf graphTest takBulkLinear
174191
-- , bench "tak Graph-Reduce-Log" $ nf graphTest takBulkLog
175192
-- , bench "tak HHI-Reduce" $ nf reducerTest tak
193+
, bench "ackermann MHS Haskell" $ nfIO (microHsTest mhsContext mhsAkk)
176194
, bench "tak HHI-Eta" $ nf reducerTest takEta
177195
-- , bench "tak HHI-Bulk" $ nf reducerTest takBulk
178196
-- , bench "tak HHI-Bulk-Log" $ nf reducerTestLog takBulk
179197
-- , bench "tak HHI-Break-Bulk" $ nf reducerTest takBulkLinear
180198
-- , bench "tak HHI-Break-Log" $ nf reducerTestLog takBulkLog
181199
, bench "tak MicroHs" $ nfIO (run mhsContext mhsTakEta)
182200
, bench "tak Native" $ nf tak1 (18,6,3)
201+
, bench "tak MHS Haskell" $ nfIO (microHsTest mhsContext mhsTak)
183202
]
184203
closeMhsContext mhsContext
185204
putStrLn "Benchmarks completed."
@@ -214,6 +233,9 @@ tak_18_6 :: Int -> Int
214233
tak_18_6 = takN 18 6
215234

216235
takN :: Int -> Int -> Int -> Int
217-
takN = fix (\f x y z -> (if y >= x then z else f (f (x-1) y z) (f (y-1) z x) (f (z-1) x y )))
236+
takN = fix (\f x y z ->
237+
if y >= x
238+
then z
239+
else f (f (x-1) y z) (f (y-1) z x) (f (z-1) x y ))
218240

219241
tak1 (x,y,z) = takN x y z

benchmark/Tak.mhs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Tak (main) where
2+
import Data.Function (fix)
3+
4+
takN :: Int -> Int -> Int -> Int
5+
takN = fix (\f x y z ->
6+
if y >= x
7+
then z
8+
else f (f (x-1) y z) (f (y-1) z x) (f (z-1) x y ))
9+
10+
main = takN 18 6 3

0 commit comments

Comments
 (0)