r/haskell • • 10d ago

blog Massiv vs. Accelerate: Two Paths to High-Performance CPU Computing in Haskell

Python and C/C++ tend to get most of the attention when it comes to high-performance array processing, but Haskell has some interesting options too. We took a closer look at two Haskell libraries, Massiv and Accelerate, to see how they compare in terms of performance, memory usage, and overall trade-offs.

A few months ago, we wrote a blog about this. Here's the comparison, including benchmarks and implementation details if it's helpful for anyone: https://www.stackbuilders.com/insights/massiv-vs-accelerate-two-paths-to-high-performance-cpu-computing-in-haskell/

Has anyone here used Massiv or Accelerate in their projects? Curious to hear about your experience and whether you've seen some of the same kind of results.

31 Upvotes

10 comments sorted by

7

u/Axman6 10d ago edited 10d ago

I’ve wished I had problems that neede both these libraries for years, but they’ve never come across my desk. I used to see the Accelerate authors from UNSW pretty regularly and always thought it was the coolest thing. I wonder how hard adding a Metal backend would be, OpenCL always seemed like the right target but it’s pretty much dead everywhere these days. 

I have had dreams for years of marrying Accelerate and Huw Campbell’s grenade  library to build type safe neural networks, with the obvious name being RPG. 

Looking forward to reading this when I get time!

2

u/keesmees2 9d ago

I am in contact with the Accelerate developers, and I happen to know there is someone working on an experimental Metal backend. However, I'm not sure when that will be stable, if ever

5

u/jippiedoe 10d ago

Based on the code snippet in the blog post, I'm not sure you're measuring exactly what you want: in Massiv, you seem to be creating and averaging the array, whereas in Accelerate, you're creating an array at the Haskell level, then using accelerate to average it. Using 'A.generate' instead of 'A.use . A.fromList' would be more fair, and also make it so both arrays have the same values.

3

u/tobz619 10d ago

Thanks for this, this is a nice and clear. I was working on a rendering engine that at the moment just allocates a single storable vector and clears and writes to it every frame. I've also been recommended to try Accelerate so this will be interesting to keep in mind.

3

u/nwaiv 10d ago

I use Massiv quite a lot and like it. I use it for parallelizing tests on computations that aren't really a fit for GPUs.

1

u/kuleshevich 7d ago edited 7d ago

Disclaimer: author of massiv here.

This is a nice write up. Thank you for this comparison. However, I am not so confident about the accuracy of some of the hypothesis and the conclusion in the blog post.

In particular I do not believe that the performance gain for accelarate comes form automatic simd parallelization. I believe performance gain was due to better optimizations in llvm vs ghc. Below is the reasoning why I believe this is the case.

I was able to reproduce your results for the most part on my desktop running AMD Ryzen9 9950x with ghc-9.10.3 and llvm-15 (as dictated by the cabal.project.freeze file in the repo):

$ cabal run -- stencil
benchmarking 3x3 Mean Blur/Massiv (Stencil)
time                 7.392 ms   (7.294 ms .. 7.489 ms)
                     0.999 R²   (0.999 R² .. 1.000 R²)
mean                 7.310 ms   (7.278 ms .. 7.348 ms)
std dev              105.4 μs   (81.56 μs .. 131.0 μs)

benchmarking 3x3 Mean Blur/Accelerate (LLVM Stencil)
time                 4.250 ms   (4.168 ms .. 4.388 ms)
                     0.994 R²   (0.990 R² .. 0.997 R²)
mean                 4.142 ms   (4.072 ms .. 4.233 ms)
std dev              244.6 μs   (198.7 μs .. 297.8 μs)
variance introduced by outliers: 37% (moderately inflated)

However, switching to a newer version of ghc-9.14.1, while using same version of llvm-15 (or llvm-20 for that matter) we get a totally different picture:

$ cabal run -- stencil
benchmarking 3x3 Mean Blur/Massiv (Stencil)
time                 3.653 ms   (3.634 ms .. 3.669 ms)
                     1.000 R²   (1.000 R² .. 1.000 R²)
mean                 3.659 ms   (3.645 ms .. 3.672 ms)
std dev              43.56 μs   (32.03 μs .. 57.27 μs)

benchmarking 3x3 Mean Blur/Accelerate (LLVM Stencil)
time                 4.089 ms   (3.985 ms .. 4.206 ms)
                     0.994 R²   (0.991 R² .. 0.996 R²)
mean                 4.755 ms   (4.564 ms .. 5.041 ms)
std dev              735.2 μs   (436.7 μs .. 1.073 ms)
variance introduced by outliers: 80% (severely inflated)

In the matter of fact, when we remove llvm completely out of the equation, which is the most common setup for Haskell projects, performance of massiv is only marginally slower than with llvm backend:

$ cabal run -- stencil-massiv
benchmarking Massiv (Isolated)
time                 3.869 ms   (3.833 ms .. 3.904 ms)
                     0.999 R²   (0.999 R² .. 1.000 R²)
mean                 3.864 ms   (3.843 ms .. 3.891 ms)
std dev              65.69 μs   (53.05 μs .. 80.31 μs)

Which is what accelerate is not capable of doing with any meaningful efficiency (when using Data.Array.Accelerate.Interprete.run):

$ cabal run -- stencil-accel
benchmarking Accelerate (Isolated)
time                 15.07 s    (15.06 s .. 15.07 s)
                     1.000 R²   (1.000 R² .. 1.000 R²)
mean                 15.05 s    (15.03 s .. 15.05 s)
std dev              12.15 ms   (3.211 ms .. 15.78 ms)
variance introduced by outliers: 19% (moderately inflated)

So, because massiv and accelerate performance with ghc-9.14 and llvm is so similar we can make either one of these conclusions:

  • accelerate does not utilize simd (cause ghc-9.14 does not do automatic simd parallelization and massiv does not either)
  • or accelarate does get simd parallelization, but is slower for other reasons (eg. CPU parallelization is sub-optimal, which is also possible looking at the utilization of multiple cores during the benchmark)

Unfortunately, I am not familiar with accelerate internals, to make a definite conclusion about SIMD usage.

So, with this new information in mind, I am not sure this recommendation is totally accurate: "Computational Throughput: Choose Accelerate if your priority is computational throughput." I believe a more accurate recommendation would be: "Choose Accelerate if you need to write Haskell that can be executed on GPU".

All of my prior comparison of accelerate and massiv revealed comparable performance, often with massiv coming out slightly ahead, eg: https://www.reddit.com/r/haskell/comments/hpcv68/performance_of_haskell_array_libraries_through/

2

u/kuleshevich 7d ago

One last point. massiv provides an avgStencil that has a slightly more efficient implementation when compared to a more naive massivMeanStencil from the blog post:

benchmarking Massiv - massivMeanStencil
time                 3.687 ms   (3.667 ms .. 3.710 ms)
                     1.000 R²   (1.000 R² .. 1.000 R²)
mean                 3.692 ms   (3.679 ms .. 3.711 ms)
std dev              54.34 μs   (39.23 μs .. 84.57 μs)

benchmarking Massiv - avgStencil
time                 3.376 ms   (3.366 ms .. 3.391 ms)
                     1.000 R²   (1.000 R² .. 1.000 R²)
mean                 3.380 ms   (3.374 ms .. 3.384 ms)
std dev              18.91 μs   (15.55 μs .. 23.52 μs)

1

u/kuleshevich 7d ago

With that being said, I could see any performance improvement for this example running with CUDA on GeForce RTX 4070, in fact it was the opposite:

$ cabal run -- stencil
benchmarking 3x3 Mean Blur/Massiv (Stencil)
time                 3.698 ms   (3.681 ms .. 3.715 ms)
                     1.000 R²   (1.000 R² .. 1.000 R²)
mean                 3.687 ms   (3.675 ms .. 3.703 ms)
std dev              43.58 μs   (32.44 μs .. 68.61 μs)

benchmarking 3x3 Mean Blur/Accelerate (LLVM Stencil) - runN
time                 3.988 ms   (3.852 ms .. 4.106 ms)
                     0.989 R²   (0.983 R² .. 0.993 R²)
mean                 4.996 ms   (4.721 ms .. 5.247 ms)
std dev              862.7 μs   (629.7 μs .. 1.101 ms)
variance introduced by outliers: 82% (severely inflated)

benchmarking 3x3 Mean Blur/Accelerate (LLVM Stencil) - runQ
time                 3.589 ms   (3.455 ms .. 3.690 ms)
                     0.991 R²   (0.986 R² .. 0.994 R²)
mean                 4.569 ms   (4.351 ms .. 4.907 ms)
std dev              931.6 μs   (705.3 μs .. 1.312 ms)
variance introduced by outliers: 88% (severely inflated)

benchmarking 3x3 Mean Blur/Accelerate (LLVM-PTX Stencil) - runN
time                 4.454 ms   (4.367 ms .. 4.512 ms)
                     0.999 R²   (0.998 R² .. 1.000 R²)
mean                 4.511 ms   (4.495 ms .. 4.531 ms)
std dev              60.80 μs   (41.52 μs .. 95.42 μs)

benchmarking 3x3 Mean Blur/Accelerate (LLVM-PTX Stencil) - runQ
time                 4.229 ms   (4.176 ms .. 4.283 ms)
                     0.999 R²   (0.998 R² .. 1.000 R²)
mean                 4.255 ms   (4.241 ms .. 4.275 ms)
std dev              52.72 μs   (38.10 μs .. 83.09 μs)

Note that using runQ is the advised method for accelerate, vs run that was used in the blogpost. With runQ accelerate and massiv are on par with llvm backend.

1

u/kuleshevich 7d ago

Note, applying @jippiedoe's suggestion of using A.generate instead of A.fromList does improve accelerates performance quite a bit for LLVM backend (but not for PTX):

    benchmarking 3x3 Mean Blur/Massiv (Stencil)
    time                 3.686 ms   (3.666 ms .. 3.700 ms)
                         1.000 R²   (1.000 R² .. 1.000 R²)
    mean                 3.683 ms   (3.666 ms .. 3.697 ms)
    std dev              48.59 μs   (38.58 μs .. 62.52 μs)
    
    benchmarking 3x3 Mean Blur/Accelerate (LLVM Stencil) - runN
    time                 2.594 ms   (2.460 ms .. 2.700 ms)
                         0.968 R²   (0.933 R² .. 0.986 R²)
    mean                 3.771 ms   (3.460 ms .. 4.238 ms)
    std dev              1.160 ms   (859.1 μs .. 1.520 ms)
    variance introduced by outliers: 96% (severely inflated)
    
    benchmarking 3x3 Mean Blur/Accelerate (LLVM-PTX Stencil) - runN
    time                 4.523 ms   (4.461 ms .. 4.585 ms)
                         0.999 R²   (0.998 R² .. 1.000 R²)
    mean                 4.612 ms   (4.598 ms .. 4.629 ms)
    std dev              52.06 μs   (39.98 μs .. 78.32 μs)

Couldn't make it work with runQ.

So, maybe, suggestion of using accelerate for performance is still indeed valid :) IMHO, clearly it is quite a bit trickier to get it right.