Giter VIP home page Giter VIP logo

Comments (5)

Magalame avatar Magalame commented on June 4, 2024

Interestingly, this fuses:

testRewrite3 :: Matrix -> Double 
testRewrite3 (Matrix r c v) = U.sum . flip (\(r,c,v) j -> U.generate r (\i -> v `U.unsafeIndex` (j + i * c))) 0 $ (r,c,v)

from dh-core.

Magalame avatar Magalame commented on June 4, 2024

It seems that pattern matching is the cause?
This also fuses:

testRewrite4 :: Matrix -> Double 
testRewrite4 m = U.sum . flip (\m1 j -> U.generate (rows m1) (\i -> (_vector m1) `U.unsafeIndex` (j + i * (cols m1)))) 0 $ m

from dh-core.

Magalame avatar Magalame commented on June 4, 2024

Bottom line:

column :: Matrix -> Int -> Vector Double
column (Matrix r c v) j= U.generate r (\i -> v `U.unsafeIndex` (j + i * c))
{-# INLINE column #-}

column2 :: Matrix -> Int -> Vector Double
column2 m j = U.generate (rows m) (\i -> (_vector m) `U.unsafeIndex` (j + i * (cols m)))
{-# INLINE column2 #-}

testRewrite0 :: Matrix -> Double
testRewrite0 a = (U.sum . flip column2 0) a

testRewrite1 :: Matrix -> Double
testRewrite1 a = (U.sum . flip column 0) a

testRewrite0 fuses, testRewrite1 doesn't.

However, the fusion is depending on the INLINE and I can't get M.column2 to get inlined across modules (moving the definition of column2 from the main file to Matrix.hs)

from dh-core.

Magalame avatar Magalame commented on June 4, 2024

The results are actually a bit disappointing, I'll keep digging

from dh-core.

Magalame avatar Magalame commented on June 4, 2024

Back to it! Turns out GHC makes wonders with rewrite rules/inlining. So throwing in for example:

{-# INLINE [1] row #-}
{-# RULES
      "row/fuse"    forall c v r i.  row (M.Matrix r c v) i = U.slice (c*i) c v
  #-}

works amazing.

It gives us some nice stuff:

Say I have matrices A,B, and C = A*B.
Then now I can take the sum of a row of C, without allocating the whole matrix, and a ~10x speedup on my machine.

I would expect to hit a point of diminishing return though, so that's to be explored

from dh-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.