170
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ Is using CUDA an option? $\endgroup$
    – flipchart
    Dec 12, 2011 at 5:20
  • 1
    $\begingroup$ 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. $\endgroup$ Dec 12, 2011 at 5:24
  • $\begingroup$ What about Intel MKL and IPP? $\endgroup$
    – Royi
    Apr 14, 2014 at 22:49

8 Answers 8

154
$\begingroup$

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.)

$\endgroup$
12
  • 4
    $\begingroup$ I guess my first question would be: Do you ever want to run anything in parallel? $\endgroup$ Dec 12, 2011 at 14:49
  • 1
    $\begingroup$ 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. $\endgroup$ Dec 12, 2011 at 22:53
  • 1
    $\begingroup$ Eigen seems great - a colleague of mine used it in a professional context, and it can get you up and running quickly enough without sacrificing performance. $\endgroup$
    – qdot
    Dec 18, 2011 at 23:57
  • 1
    $\begingroup$ 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. $\endgroup$ Dec 23, 2011 at 21:53
  • 7
    $\begingroup$ 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. $\endgroup$ Dec 23, 2011 at 21:59
24
$\begingroup$

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.)

$\endgroup$
0
19
$\begingroup$

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).

$\endgroup$
6
  • 4
    $\begingroup$ 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) $\endgroup$ Dec 24, 2011 at 0:51
  • $\begingroup$ @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. $\endgroup$ Dec 24, 2011 at 19:59
  • 3
    $\begingroup$ @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. $\endgroup$ Dec 24, 2011 at 20:07
  • 1
    $\begingroup$ 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. $\endgroup$ Dec 29, 2011 at 18:30
  • 1
    $\begingroup$ @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. $\endgroup$ Jan 2, 2012 at 4:51
11
$\begingroup$

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 :-)

$\endgroup$
1
3
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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.)

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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.

$\endgroup$

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