Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving performance of Unsigned/Signed and Fixed #162

Open
tomjaguarpaw opened this issue Jul 19, 2016 · 2 comments
Open

Improving performance of Unsigned/Signed and Fixed #162

tomjaguarpaw opened this issue Jul 19, 2016 · 2 comments

Comments

@tomjaguarpaw
Copy link

Slow simulation is my single biggest pain point with CLaSH at the moment.

Could we do some work on improving the performance of Unsigned/Signed and Fixed? The former is phenomenally (40x) slower than Integer, and the latter is 3x slower than the former. If we could get down to Word64 speeds (four thousand times faster than Fixed) that would be absolutely fantastic.

Some sample code. Here is src/Profile2.hs:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MagicHash #-}

import CLaSH.Prelude
import GHC.Word (Word64)
import System.Environment (getArgs)

seven :: Int
seven = 7

loop_Word64 :: Word64
loop_Word64 = go big 0
  where go n acc = if   n <= 0
                   then acc
                   else acc `seq` go (n - 1) (acc + n)
        big = 10 ^ seven

loop_Integer :: Integer
loop_Integer = go big 0
  where go n acc = if   n <= 0
                   then acc
                   else acc `seq` go (n - 1) (acc + n)
        big = 10 ^ seven

loop_Unsigned :: Unsigned 64
loop_Unsigned= go big 0
  where go n acc = if   n <= 0
                   then acc
                   else acc `seq` go (n - 1) (acc + n)
        big = 10 ^ seven

loop_Fixed :: Fixed Unsigned 64 0
loop_Fixed = go big 0
  where go n acc = if   n <= 0
                   then acc
                   else acc `seq` go (n - 1) (acc + n)
        big = 10 ^ seven

main = do
     arg <- fmap Prelude.head getArgs
     case arg of
          "Word64"   -> print loop_Word64
          "Integer"  -> print loop_Integer
          "Unsigned" -> print loop_Unsigned
          "Fixed"    -> print loop_Fixed

This is the output of running it

ghc -O2 -XDataKinds src/Profile2.hs && for i in Word64 Integer Unsigned
Fixed; do echo $i; time src/Profile2 $i; echo --; echo; done
Word64
50000005000000

real    0m0.004s
user    0m0.000s
sys     0m0.000s
--

Integer
50000005000000

real    0m0.156s
user    0m0.156s
sys     0m0.000s
--

Unsigned
50000005000000

real    0m6.364s
user    0m6.340s
sys     0m0.024s
--

Fixed
50000005000000.0

real    0m15.490s
user    0m15.460s
sys     0m0.036s
--
@tomjaguarpaw
Copy link
Author

Most of the slowdown relative to Integer comes from fromInteger_INLINE.

@christiaanb
Copy link
Member

I've started on improving simulation speed in the following branch of the clash-prelude: https://github.com/clash-lang/clash-prelude/tree/fast_sim

@christiaanb christiaanb added this to the 1.1 milestone Dec 18, 2018
@christiaanb christiaanb modified the milestones: 1.1, 1.2 Jan 16, 2020
@martijnbastiaan martijnbastiaan removed this from the 1.4 milestone Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants