haskell-numerics / hmatrix

Linear algebra and numerical computation
381 stars 104 forks source link

Topic/mutable mk csr #322

Closed HuwCampbell closed 4 years ago

HuwCampbell commented 4 years ago

Here's an alternative which permits streaming. It's not super polished yet or exposed; but if you agree that it's worthwhile I'll polish it up.

Fixes #220 Fixes #320

HuwCampbell commented 4 years ago

I did some benchmarking, it looks like this version can be 5 to 10 times faster than the original version. Small medium and large are 100, 10k, and 1M entries in a diagonal matrix.

benchmarking mkCSR/small
time                 4.283 μs   (4.125 μs .. 4.556 μs)
                     0.990 R²   (0.979 R² .. 1.000 R²)
mean                 4.175 μs   (4.137 μs .. 4.315 μs)
std dev              212.5 ns   (62.74 ns .. 464.8 ns)
variance introduced by outliers: 64% (severely inflated)

benchmarking mkCSR/medium
time                 584.9 μs   (574.8 μs .. 594.7 μs)
                     0.999 R²   (0.998 R² .. 0.999 R²)
mean                 589.6 μs   (584.6 μs .. 593.5 μs)
std dev              14.87 μs   (12.46 μs .. 18.52 μs)
variance introduced by outliers: 16% (moderately inflated)

benchmarking mkCSR/large
time                 164.4 ms   (141.0 ms .. 191.3 ms)
                     0.985 R²   (0.968 R² .. 1.000 R²)
mean                 177.0 ms   (170.1 ms .. 183.1 ms)
std dev              9.480 ms   (7.195 ms .. 12.86 ms)
variance introduced by outliers: 13% (moderately inflated)

benchmarking original/small
time                 23.86 μs   (23.51 μs .. 24.18 μs)
                     0.999 R²   (0.998 R² .. 1.000 R²)
mean                 23.79 μs   (23.62 μs .. 24.02 μs)
std dev              613.5 ns   (482.6 ns .. 783.5 ns)
variance introduced by outliers: 26% (moderately inflated)

benchmarking original/medium
time                 10.34 ms   (9.243 ms .. 11.33 ms)
                     0.934 R²   (0.893 R² .. 0.965 R²)
mean                 10.18 ms   (9.335 ms .. 11.11 ms)
std dev              2.369 ms   (1.794 ms .. 3.439 ms)
variance introduced by outliers: 86% (severely inflated)

benchmarking original/large
time                 1.131 s    (1.103 s .. 1.165 s)
                     1.000 R²   (1.000 R² .. 1.000 R²)
mean                 1.222 s    (1.181 s .. 1.269 s)
std dev              54.45 ms   (21.30 ms .. 69.86 ms)
variance introduced by outliers: 19% (moderately inflated)
idontgetoutmuch commented 4 years ago

Hi @HuwCampbell, Does this still need a review? Is there anything else that I can help on?

HuwCampbell commented 4 years ago

Yep, a review would be good. I think it's ready.

idontgetoutmuch commented 4 years ago

@HuwCampbell how are you running those benchmarks?

HuwCampbell commented 4 years ago

I just made up a little benchmark suite. IIRC, I had to add bangs to the constructor for CSR for it to actually measure the old version.

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
module Main where

import Criterion
import Criterion.Main

import Numeric.LinearAlgebra.Devel
import qualified Data.Vector.Storable as V

main :: IO ()
main = do
  let
    compile n = fmap (\i -> ((i, i), 10)) [0 .. n]
    small     = compile 100
    medium    = compile 10000
    large     = compile 1000000

  defaultMain [
      bgroup "mkCSR" [
        bench "small"  $ whnf mkCSR small
      , bench "medium" $ whnf mkCSR medium
      , bench "large"  $ whnf mkCSR large
      ]
  ]
idontgetoutmuch commented 4 years ago

I made a few minor suggestions

HuwCampbell commented 4 years ago

I made a few minor suggestions

Nothing's shown up, you might need to complete the review for them to show.

idontgetoutmuch commented 4 years ago

Ah sorry I created a PR: https://github.com/HuwCampbell/hmatrix/pull/1

idontgetoutmuch commented 4 years ago

@HuwCampbell these are just suggestions - the rest looks good to me - thanks very much - feel free to merge

idontgetoutmuch commented 4 years ago

@HuwCampbell if you make those changes then we can merge - thanks very much 😄

HuwCampbell commented 4 years ago

Done done. Cheers