Take the 2-minute tour ×
Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. It's 100% free, no registration required.

Does anyone have recommendations on a usable, fast C++ matrix library?

What I mean by usable is the following:

  • Matrix objects have an intuitive interface (ex.: I can use rows and columns while indexing)
  • I can do anything with the matrix class that I can do with LAPACK and BLAS
  • Easy to learn and use API
  • Relatively painless to install in Linux (I use Ubuntu 11.04 right now)

To me, usability is more important than speed or memory usage right now, to avoid premature optimization. In writing the code, I could always use 1-D arrays (or STL vectors) and proper index or pointer arithmetic to emulate a matrix, but I'd prefer not to in order to avoid bugs. I'd also like to focus my mental effort on the actual problem I'm trying to solve and program into the problem domain, rather than use part of my finite attention to remember all of the little programming tricks I used to emulate matrices as arrays, and remember LAPACK commands, et cetera. Plus, the less code I have to write, and the more standardized it is, the better.

Dense versus sparse doesn't matter yet; some of the matrices I am dealing with will be sparse, but not all of them. However, if a particular package handles dense or sparse matrices well, it is worth mentioning.

Templating doesn't matter much to me either, since I'll be working with standard numeric types and don't need to store anything other than doubles, floats, or ints. It's nice, but not necessary for what I'd like to do.

share|improve this question
    
Is using CUDA an option? –  flipchart Dec 12 '11 at 5:20
    
It could be, later on. I'm not interested in CUDA right now because I'm building a library for an application where matrix multiplication is the least of my concerns. The bulk of the effort will be spent on calling a mixed-integer linear program solver, so using CUDA would be overkill. After I finish my thesis, I plan on looking at algorithms that are more linear algebra heavy and less optimization-centric. I definitely encourage you to post about CUDA libraries, though, if you have experience with them, because I'm sure that other people would be interested to know your thoughts. –  Geoff Oxberry Dec 12 '11 at 5:24
    
What about Intel MKL and IPP? –  Drazick Apr 14 at 22:49

10 Answers 10

up vote 86 down vote accepted

I've gathered the following from online research so far:

I've used Armadillo a little bit, and found the interface to be intuitive enough, and it was easy to locate binary packages for Ubuntu (and I'm assuming other Linux distros). I haven't compiled it from source, but my hope is that it wouldn't be too difficult. It meets most of my design criteria, and uses dense linear algebra. It can call LAPACK or MKL routines. There generally is no need to compile Armadillo, it is a purely template-based library: You just include the header and link to BLAS/LAPACK or MKL etc.

I've heard good things about Eigen, but haven't used it. It claims to be fast, uses templating, and supports dense linear algebra. It doesn't have LAPACK or BLAS as a dependency, but appears to be able to do everything that LAPACK can do (plus some things LAPACK can't). A lot of projects use Eigen, which is promising. It has a binary package for Ubuntu, but as a header-only library it's trivial to use elsewhere too.

The Matrix Template Library version 4 also looks promising, and uses templating. It supports both dense and sparse linear algebra, and can call UMFPACK as a sparse solver. The features are somewhat unclear from their website. It has a binary package for Ubuntu, downloadable from their web site.

PETSc, written by a team at Argonne National Laboratory, has access to sparse and dense linear solvers, so I'm presuming that it can function as a matrix library. It's written in C, but has C++ bindings, I think (and even if it didn't, calling C from C++ is no problem). The documentation is incredibly thorough. The package is a bit overkill for what I want to do now (matrix multiplication and indexing to set up mixed-integer linear programs), but could be useful as a matrix format for me in the future, or for other people who have different needs than I do.

Trilinos, written by a team at Sandia National Laboratory, provides object-oriented C++ interfaces for dense and sparse matrices through its Epetra component, and templated interfaces for dense and sparse matrices through its Tpetra component. It also has components that provide linear solver and eigensolver functionality. The documentation does not seem to be as polished or prominent as PETSc; Trilinos seems like the Sandia analog of PETSc. PETSc can call some of the Trilinos solvers. Binaries for Trilinos are available for Linux.

Blitz is a C++ object-oriented library that has Linux binaries. It doesn't seem to be actively maintained (2012-06-29: a new version has just appeared yesterday!), although the mailing list is active, so there is some community that uses it. It doesn't appear to do much in the way of numerical linear algebra beyond BLAS, and looks like a dense matrix library. It uses templates.

Boost::uBLAS is a C++ object-oriented library and part of the Boost project. It supports templating and dense numerical linear algebra. I've heard it's not particularly fast.

The Template Numerical Toolkit is a C++ object-oriented library developed by NIST. Its author, Roldan Pozo, seems to contribute patches occasionally, but it doesn't seem to be under active development any longer (last update was 2010). It focuses on dense linear algebra, and provides interfaces for some basic matrix decompositions and an eigenvalue solver.

Elemental, developed by Jack Poulson, is a distributed memory (parallel) dense linear algebra software package written in a style similar to FLAME. For a list of features and background on the project, see his documentation. FLAME itself has an associated library for sequential and shared-memory dense linear algebra, called libflame, which appears to be written in object-oriented C. Libflame looks a lot like LAPACK, but with better notation underlying the algorithms to make development of fast numerical linear algebra libraries more of a science and less of a black art.

There are other libraries that can be added to the list; if we're counting sparse linear algebra packages as "matrix libraries", the best free one I know of in C is SuiteSparse, which is programmed in object-oriented style. I've used SuiteSparse and found it fairly easy to pick up; it depends on BLAS and LAPACK for some of the algorithms that decompose sparse problems into lots of small, dense linear algebra subproblems. The lead author of the package, Tim Davis, is incredibly helpful and a great all-around guy.

The Harwell Subroutine Libraries are famous for their sparse linear algebra routines, and are free for academic users, though you have to go through this process of filling out a form and receiving an e-mail for each file that you want to download. Since the subroutines often have dependencies, using one solver might require downloading five or six files, and the process can get somewhat tedious, especially since the form approval is not instantaneous.

There are also other sparse linear algebra solvers, but as far as I can tell, MUMPS and other packages are focused mostly on the solution of linear systems, and solving linear systems is the least of my concerns right now. (Maybe later, I will need that functionality, and it could be useful for others.)

share|improve this answer
1  
I think you have confused Eigen with Elemental; I have not worked on Eigen, though I am very impressed with the project. Elemental mainly targets distributed-memory machines. –  Jack Poulson Dec 12 '11 at 6:57
2  
I guess my first question would be: Do you ever want to run anything in parallel? –  Matt Knepley Dec 12 '11 at 14:49
1  
I should mention Trilinos... though it doesn't get a lot of visibility here (yet), it is a viable alternative to PETSc, with a templated matrix package, an eigensolver, and a sparse matrix solver, it also has a package meant specifically for abstracting out the bookkeeping of an algorithm, though I don't know how well it works. –  Andrew Spott Dec 12 '11 at 22:53
1  
You should probably differentiate between header-only template libraries such as Eigen, Boost:uBLAS, and Blitz++ and traditional compiled libraries such as PETSc and Trilinos. –  Aron Ahmadia Dec 23 '11 at 21:53
6  
I would also add the following libraries to your answer: ViennaCL - OpenCL-based C++ header library which can interface into Eigen and MTL. PLASMA - a UTK-based redesign of the BLAS and LAPACK libraries featuring tile-based decompositions. MAGMA - another UTK project which focuses on improving LAPACK/BLAS performance. –  Aron Ahmadia Dec 23 '11 at 21:59

This document was written in March 2009 to help in the choice of a linear algebra library for a scientific library. It evaluates portability, high-level interface and licensing for several libraries, among them Eigen, GSL, Lapack++ MTL, PETSc, Trilinos and uBlas. It seems to be particularly fond of Flens and Seldon. (One of the requirements was that C++ templates and sparse matrices had to be supported.)

share|improve this answer

Of all the projects listed above, there are really only two heavy-weights that are extremely widely used (and for good reasons): PETSc and Trilinos. Both are professionally developed and have a large developer base. All the others are rather small projects compared to these two, and I would recommend going with them because (i) they will be supported for a long time to come and (ii) they are likely to already have all functionality you will ever need regarding linear algebra (and much more on top of that).

share|improve this answer
3  
PETSc doesn't feature much more than a slightly abstract interface to ScaLAPACK and PLAPACK. If Geoff is interested in convenient dense linear algebra, I think PETSc is overkill (I don't know enough about Trilinos's dense capabilities to comment) –  Aron Ahmadia Dec 24 '11 at 0:51
    
@AronAhmadia: At this point, I think the answer has morphed into more of a community wiki resource, which is why I've been adding packages at the request of commenters. –  Geoff Oxberry Dec 24 '11 at 19:59
2  
@WolfgangBangerth: I think it's important to mention design goals. If the goal is to write a piece of software that is long-lasting and meant to be used by others, and linear algebra is an integral part of that software, then PETSc and Trilinos are good choices. However, there are cases where more lightweight options are preferable and useful, because they have simpler APIs and can accomplish almost the same task in fewer lines of code. Finally, selection bias plays a role in the survival of codes. If no one uses the smaller codes, they won't last or build up a base like PETSc and Trilinos. –  Geoff Oxberry Dec 24 '11 at 20:07
1  
To add to what Geoff said, the vast majority of PETSc and Trilinos's functionality comes from wrappers around external libraries. To say that no one should use said external libraries directly seems strange to me. –  Jack Poulson Dec 29 '11 at 18:30
1  
@JackPoulson: I actually stand by my comment. Yes, PETSc wraps a lot of individual packages. But it does so with a uniform interface and so it is simpler to learn this once than to have to learn the peculiarities of each packages naming convention and style. In other words, while each individual project may equally well use the wrapped packages, there is a synergy that comes from using them through PETSc everytime. –  Wolfgang Bangerth Jan 2 '12 at 4:51

If you want

  • Matrix classes with an intuitive interface
  • All the LAPACK and BLAS features
  • Easy to learn and use API
  • Easy to install

Then I recommend you to have a look at my library FLENS. I designed it for exactly these kind of tasks. However, it requires a C++11 conform compiler (e.g. gcc 4.7 or clang).

FLENS gives you exactly the same performance as the underlying BLAS implementation. There are some (rather old) benchmarks showing this

The same can be said about FLENS-LAPACK it just gives you exactly the same performance as Netlib's LAPACK if the same BLAS implementation is used.

Regarding new benchmarks let me go into more into detail ...

Some time ago I asked Clint Whaley (the author of ATLAS) what he thinks about the benchmarks published on the Eigen site. He just confirmed my suspect that these benchmarks are most likely not reliable. In the meantime some other realized benchmarks the way Clint suggested. Details can be found on the ATLAS site and the Eigen mailing list. The benchmarks are not nicely presented in graphs but they show that ATLAS is always about 40% faster than Eigen. This contradicts the benchmarks from the Eigen site but confirms other benchmarks (e.g. those from blaze-lib).

Note that for dense numerical linear algebra the matrix-matrix products are most relevant. Personally I don't care whether Eigen or ATLAS is faster. If Eigen was faster than ATLAS then I would use Eigen as BLAS-backend.

Disclaimer: Yes, FLENS is my baby! That means I coded about 95% of it and every line of code was worth it :-)

share|improve this answer
    
Hi Michael, welcome to scicomp. Your URL isn't working for me, is there something wrong with your server? –  Aron Ahmadia Sep 29 '12 at 12:02
    
Thanks for the hint. The URL is correct but it seems we are having a problem at the math department with the file server. Just on time for the weekend ... –  Michael Lehn Sep 29 '12 at 12:12
1  
Ok, I just created new documentation from the current git repo and copied it on another server: apfel.mathematik.uni-ulm.de/~lehn/FLENS/index.html –  Michael Lehn Sep 29 '12 at 15:03

I have been using GMM++ for some time and I'm happy with that.

share|improve this answer

Basically the same question popped up on SO:

What are the most widely used C++ vector/matrix math/linear algebra libraries, and their cost and benefit tradeoffs?

(It adds some value to Geoff's answer.)

share|improve this answer

What would be the best matrix library for dealing with small size matrices, that are commonly used, for instance, when assembling finite element matrices, for performing tensor operations and so on.

I'm already using PETSc for the solution of the large sparse linear systems arising in my application, but I'm currently using my own simple library to deal with these matrices/vectors...I'm considering moving to a faster, library like the ones mentioned above.

What would be the best choice to couple with PETSc? Eigen? Armadillo? BOOST::uBlas? MTL4? I use some things from BOOST, so, first I thought about using BOOST::uBlas, but there isn't much documentation, examples, and so on...

share|improve this answer
2  
I strongly recommend against using uBlas, as it is known to be about an order of magnitude slower than optimized BLAS routines. I think that Eigen shines most for very tiny matrices due to clever template tricks. –  Jack Poulson Jan 6 '12 at 16:49
1  
This is probably better for a separate question. I know that Eigen, MTL4, and Armadillo all have tricks for solving small size systems, but I think you're asking for how to assemble matrices, and for that, I don't know. I typically assemble matrices element-by-element. I'm not sure if any of the libraries mentioned are good for coupling with PETSc; I'm not a PETSc user yet. I second Jack's comment about Boost::uBlas, though, because it is slow. –  Geoff Oxberry Jan 6 '12 at 23:15

Armadillo, Boost and others now included as part of Ceemple, a rapid JIT based C++ technical computing environment. Available (free) from http://www.ceemple.com.

share|improve this answer

Surprised nobody has mentioned TooN so far. Been happily using it for almost 3 years now.

It's very similar to Eigen although not as comprehensive. However I think it has the nicer syntax in some ways.

It also comes with classes that help modeling common transformations that are frequently encountered in Graphics and Vision, based on Lie Groups (Special Euclidean/Orthogonal in 2 and 3 dimensions, etc) and associated Lie Algebras.

share|improve this answer

HASEM Matrix C++ Library is what you really need http://sourceforge.net/projects/hasem/

share|improve this answer
1  
"HASEM is almost able to solve any linear algebra problems"! –  David Ketcheson Aug 11 '12 at 8:57
    
Why is it what anyone needs? –  mabraham Apr 7 at 22:44

protected by Geoff Oxberry Apr 11 at 10:02

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.