-
Notifications
You must be signed in to change notification settings - Fork 0
Optimizations #9
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f554a8c
Make `cross` a bit more generic
neclitoris 6d3fe15
Benchmark initialization against `Data.Vector.Storable`
neclitoris 620df61
Add naive matmul implementation
neclitoris b87c753
Add means of efficient iteration over indices
neclitoris 5eefeed
Make iteration actually efficient
neclitoris 3b67c53
Style
neclitoris 7e46c4f
Optimize more
neclitoris b5e85f4
Optimize `fromEnum` for indices
neclitoris 013c3a0
Bench against `massiv`
neclitoris 934a794
Optimize `succ` (did not help)
neclitoris 3241389
Remove useless conversions from `Index`
neclitoris ca6d070
Fix `EnumImpl`
neclitoris f8a2633
Optimize array construction further
neclitoris c05281a
add iota benchmarks because I said so
neclitoris dcc52ef
Avoid bounds checking in initialization
neclitoris 91114f7
Finally fix performance
neclitoris 298f911
Add pragmas, fix style and warnings
neclitoris 65d4dde
Fix tests failing
neclitoris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,60 @@ | ||
| {-# LANGUAGE PartialTypeSignatures #-} | ||
| {-# LANGUAGE PatternSynonyms #-} | ||
| {-# LANGUAGE RequiredTypeArguments #-} | ||
|
|
||
| import MLambda.Matrix | ||
| import MLambda.NDArr | ||
| import MLambda.TypeLits (KnownNat) | ||
| import MLambda.TypeLits (KnownNat, natVal) | ||
|
|
||
| import Data.Massiv.Array (Array, Comp (..), Ix2, pattern Sz2) | ||
| import Data.Massiv.Array.Manifest (S) | ||
| import Data.Massiv.Array.Mutable (freeze, makeMArrayS) | ||
| import Data.Primitive.PrimVar | ||
| import Data.Random.Normal (normalIO) | ||
| import GHC.TypeLits (type (<=)) | ||
| import Data.Vector.Storable qualified as Storable | ||
| import Foreign.Storable | ||
| import GHC.TypeLits (type (*), type (<=)) | ||
| import System.Random (mkStdGen, setStdGen) | ||
| import Test.Tasty.Bench (bench, bgroup, defaultMain, env, nf, nfIO) | ||
| import Test.Tasty (localOption) | ||
| import Test.Tasty.Bench | ||
| (TimeMode (..), bench, bgroup, defaultMain, env, nf, nfIO) | ||
|
|
||
| type M = 1000 | ||
| type K = 1000 | ||
| type N = 1000 | ||
| type M = 100 | ||
| type K = 100 | ||
| type N = 100 | ||
|
|
||
| setup :: IO (a -> b -> (a, b)) | ||
| setup = (,) <$ setStdGen (mkStdGen 0) | ||
|
|
||
| mkNd :: forall m n -> (KnownNat m, KnownNat n, 1 <= m, 1 <= n) => IO (NDArr [m, n] Double) | ||
| mkNd m n = fromIndexM @[m, n] (const normalIO) | ||
| mkNd :: forall m n -> (KnownNat m, KnownNat n, 1 <= m, 1 <= n, Storable a) | ||
| => IO a -> IO (NDArr [m, n] a) | ||
| mkNd m n gen = fromIndexM @'[m, n] $ const gen | ||
|
|
||
| mkVec :: forall m n -> (KnownNat m, KnownNat n, Storable a) | ||
| => IO a -> IO (Storable.Vector a) | ||
| mkVec m n gen = Storable.replicateM (natVal n * natVal m) $ gen | ||
|
|
||
| mkMassiv :: forall m n -> (KnownNat m, KnownNat n, Storable a) => IO a -> IO (Array S Ix2 a) | ||
| mkMassiv m n gen = freeze Seq =<< makeMArrayS (Sz2 (natVal n) (natVal m)) (const gen) | ||
|
|
||
| main :: IO () | ||
| main = defaultMain | ||
| [ bench "random init" $ nfIO $ mkNd M N | ||
| , env (setup <*> mkNd M K <*> mkNd K N) \input -> | ||
| bgroup "matmul" | ||
| [ bench "Massiv" $ nf (uncurry crossMassiv) input | ||
| , bench "OpenBLAS" $ nf (uncurry cross) input | ||
| main = do | ||
| var <- newPrimVar 0 | ||
| defaultMain $ localOption WallTime <$> | ||
| [ bgroup "primvar iota init" | ||
| [ bench "NDArr" $ nfIO $ mkNd M N (fetchAddInt var 1) | ||
| , bench "Storable.Vector" $ nfIO $ mkVec M N (fetchAddInt var 1) | ||
| , bench "massiv" $ nfIO $ mkMassiv M N (fetchAddInt var 1) | ||
| ] | ||
| , bgroup "random init" | ||
| [ bench "NDArr" $ nfIO $ mkNd @Double M N normalIO | ||
| , bench "Storable.Vector" $ nfIO $ mkVec @Double M N normalIO | ||
| , bench "massiv" $ nfIO $ mkMassiv @Double M N normalIO | ||
| ] | ||
| , env (setup <*> mkNd @Double M K normalIO <*> mkNd @Double K N normalIO) \input -> | ||
| bgroup "matmul" | ||
| [ bench "Massiv" $ nf (uncurry crossMassiv) input | ||
| , bench "OpenBLAS" $ nf (uncurry cross) input | ||
| , bench "Naive" $ nf (uncurry crossNaive) input | ||
| ] | ||
| ] | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.