17

I am looking for advice regarding high performance multi-dimensional array libraries/classes for C++. What I really need is:

  • the ability to dynamically allocate arrays with a size determined at run-time

  • the ability to access and modify single array values (fast)

  • to be able to use simple array arithmetic such as array1 = array2 + 2 * array3

  • a well-maintained library

I have come across various libraries, including:

  • Blitz++, which looks exactly what I need, but which doesn't seem very well maintained (latest stable release was 5 years ago)

  • Boost, which doesn't support array arithmetic, and appears to be a quite slow compared to say Blitz++.

  • Jonn Bowman's array.h which has no documentation.

Does anyone have any other suggestions or comments about the above options?

astrofrog
  • 32,883
  • 32
  • 90
  • 131
  • Thanks for the replies. Just to clarify, I am not interested in linear algebra, just array containers and simple arithmetic operations. – astrofrog Oct 18 '10 at 16:23

9 Answers9

7

Eigen is extremely well-maintained (right now, at least, there are new versions coming out every month) and supports the other operations you need.

Neil G
  • 32,138
  • 39
  • 156
  • 257
4

There is a broad and relatively recent survey, including benchmarks, here.

I believe that you can speed up Boost.UBlas by binding it to underlying numerical libraries like LAPACK or Intel MKL, but have not done that.

fwiw, the implementations that seem to come up most often as candidates are Boost.UBlas and MTL. It's my experience that wide adoption is more likely to foster ongoing support and development.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
3

Necomi seems to provide the features you would like.

It includes support for an arbitrary multi-dimensional numbers whose dimensions can be fixed at runtime, provides fast access to single elements, while also supporting arithmetic (among others) expressions.

Émilien Tlapale
  • 903
  • 5
  • 11
3
  • uBlas, a part of Boost. It offers full BLAS level 1-3, and hence lots of array arithmetic functions.
  • Armadillo also seems to be a C++ linear algebra library, which as far as I can see optionally uses LAPACK/Atlas (which of course makes it canonically fast).
  • The GNU Scientific Library offers full BLAS. I don't know how fast it is, or if it can use LAPACK/Atlas.
  • If you don't need anything more fancy than what you list, you can quite easily wrap for example Atlas' BLAS yourself. But you probably don't want to reinvent the wheel if you don't have to.
gspr
  • 11,144
  • 3
  • 41
  • 74
2

Also another shameless self-promotion,

https://github.com/dwwork/FortCpp/

I've posted my own personal solution to this problem up on GitHub. I'm not a C++ expert by any stretch, but I thought I'd at least throw it out there.

dwwork
  • 838
  • 5
  • 12
2

Maybe you would like to try my "Multi" library: https://gitlab.com/correaa/boost-multi

  • the ability to dynamically allocate arrays with a size determined at run-time
    multi::array<int, 2> A({m, n});
  • the ability to access and modify single array values (fast)
    A[i][j] += 42;

Generates machine code similar to A.base() + i*stride_1 + j*stride_2. https://gitlab.com/correaa/boost-multi#whats-up-with-the-multiple-bracket-notation

  • to be able to use simple array arithmetic such as array1 = array2 + 2 * array3

Well, not like that, I decided to keep array arithmetic separate from the data structure.

Having said that, the library is very compatible with STL algorithms (and it has an adaptor to use BLAS if your strides permit).

Oversimplifying some issues about access patterns, ...

template<class T, class X, class Y>
auto axpy(T alpha, X const& x, Y&& y) -> Y&& {
    assert( extensions(x) == extensions(y) );
    std::transform(
        x.elements().begin(), x.elements().end(),
        y.elements().begin(), 
        y.elements().begin(),
        [&](auto const& ex, auto& ey) {return alpha*x + ey;}
    );
    return std::forward<Y>(y);
}
...

auto array1 = axpy(2, array2, +array3);  // array1 = 2*array2 + array3;  // unary + will make a modifiable copy before it starts.
  • a well-maintained library

I will maintain it as long as I could, I also welcome contributors.

alfC
  • 14,261
  • 4
  • 67
  • 118
1

From a performance perspective, I have tried boost::MultiArray and Armadillo. Neither were fast, in that both had slow access time compared to arrays or vectors, and I was able to beat these packages in an operation such as x1(4:10) = x2(1:6) + x2(2:7) + x2(3:8) by using a simple hand-coded loop (with the help of my compiler's optimization, I'm sure). When you get into matrix multiplication, these packages might offer some benefit via LAPACK and BLAS but you can always use those interfaces on your own.

Ethereal
  • 2,604
  • 1
  • 20
  • 20
1

With the caveat that this is shameless self-promotion,

https://github.com/ndarray/ndarray

may be worth looking into.

While it doesn't provide optimized mathematical operators, it does provide an interface to Eigen for that. Where it really stands out is in providing interoperability with Python/NumPy through SWIG or Boost.Python.

jbosch
  • 971
  • 6
  • 8
1

Maybe library such as BLAS, a CBLAS exists, but don't remember where.

http://www.netlib.org/blas/

ykatchou
  • 3,667
  • 1
  • 22
  • 27