Reputation: 34091
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?
Upvotes: 17
Views: 12573
Reputation: 16242
Maybe you would like to try my "Multi" library: https://gitlab.com/correaa/boost-multi
multi::array<int, 2> A({m, n});
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
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.
I will maintain it as long as I could, I also welcome contributors.
Upvotes: 2
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
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
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
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
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
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
Reputation: 11227
Upvotes: 3
Reputation: 3727
Maybe library such as BLAS, a CBLAS exists, but don't remember where.
Upvotes: 1