High-performance C++ multi-dimensional arrays

Viewed 11064

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?

9 Answers

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.

Related