astrofrog
astrofrog

Reputation: 34091

High-performance C++ multi-dimensional arrays

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

I have come across various libraries, including:

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

Upvotes: 17

Views: 12573

Answers (9)

alfC
alfC

Reputation: 16242

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.

Upvotes: 2

&#201;milien Tlapale
&#201;milien Tlapale

Reputation: 903

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.

Upvotes: 3

dwwork
dwwork

Reputation: 858

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.

Upvotes: 2

jbosch
jbosch

Reputation: 991

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.

Upvotes: 1

Ethereal
Ethereal

Reputation: 2824

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.

Upvotes: 1

Neil G
Neil G

Reputation: 33202

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

Upvotes: 7

Steve Townsend
Steve Townsend

Reputation: 54138

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.

Upvotes: 4

gspr
gspr

Reputation: 11227

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

Upvotes: 3

ykatchou
ykatchou

Reputation: 3727

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

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

Upvotes: 1

Related Questions