开发者

Which haskell array implementation to use? AKA what are the pros and cons of each

开发者 https://www.devze.com 2023-02-15 01:06 出处:网络
What do I need? [an unordered list] VERY easy parallelization support for map, filter etc. ability to perform array based computations efficiently, like A=B+C, sort of like matlab arrays.

What do I need? [an unordered list]

  • VERY easy parallelization
  • support for map, filter etc.
  • ability to perform array based computations efficiently, like A=B+C, sort of like matlab arrays.
  • Generation of SIMD code. I guess this is out of the question in the near future for anything, but hey, I can ask :)
  • support for matrices should be there at a minimum, higher dimensions are lower priority right now.
  • abi开发者_运维技巧lity to get a pointer to it and create one from a C pointer.
  • Support from other libraries. IE, bindings to popular C math packages, i/o to disk or images if the arrays are 2D

What do I see?

  • Array package in haskell-platform. It's the blessed one and can do parallel
  • Data.Vector. Has loop fusion, but not in platform, so its maturity is unknown to me.
  • repa package, contributed by the DPH team, but doesn't work well with any stable ghc today.
  • Lots of variation in the level of support for array implementations. For instance, there doesn't seem to be an easy way to dump a 2D vector to a image file. IOW, the haskell community apparently hasn't settled on an array implementation.

So please, help me choose.

EDIT A=B+C refers to element wise addition, and not list concatenation


Correct, the community hasn't settled on a good array implementation. I think it would be a good Haskell Prime submission to put forward the Vector API and remove Data.Array.

Vector is very mature! It has:

  • VERY easy parallelization
  • support for map, filter etc.
  • performs array based computations efficiently, like A=B+C (but I'm not in tune with how matlab does it)
  • vector creation from a pointer via Vector.Storable

It does not:

  • have enough support from other libraries. IE, bindings to popular C math packages
  • support matrices, but you can have vectors of vectors. If you build some vector-based matrix operations then perhaps you could upload to hackage as vector-matrix.
  • Generate SIMD code.

NOTE: You can turn bytestrings into vectors of whatever, so if you have an image as a bytestring then, via Vector.Storable, you might be able to do what you want with the image as a vector.


(I am not allowed to comment)

rpg: Does hmatrix accept Data.Vector? It has a Data.Packed.Vector but are they the same?

Yes. The last version of hmatrix uses by default Data.Vector.Storable for 1D vectors (previously it was optional). The dependency on vector is not shown in Hackage, probably because it is in a configuration flag.

For LAPACK compatibility matrices are not Vector or Vector t, but they can be easily converted (e.g.: Data.Vector.fromList . toRows).


If you want bindings to popular C libraries, the best options are probably hmatrix and blas. Blas is just a binding to a BLAS library, whereas hmatrix provides some higher-level operations. There are also many libraries built upon hmatrix offering further functionality. If you're doing any sort of matrix work, that's what I would start with.

The vector package is also a good choice; it's stable and provides excellent performance. The Data.Vector.Storable types are represented as C arrays, so it's trivial to interface from them to other C libraries. The biggest drawback is that there's no matrix support, so you'd have to do that yourself.

As for exporting to an image format, most haskell image libraries seem to use ByteStrings. You could either convert to a ByteString, or bind to a C library that does what you want. If you find a Haskell library that does what you want, it should be easy enough to convert hmatrix data to the proper format.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号