高性能 C++多维数组

发布于 2024-09-27 22:30:41 字数 697 浏览 0 评论 0原文

我正在寻找有关 C++ 高性能多维数组库/类的建议。我真正需要的是:

  • 动态分配数组的能力,其大小在运行时确定

  • 访问和修改的能力单个数组值(快速)

  • 能够使用简单的数组算术,例如array1 = array2 + 2 * array3< /p>

  • 维护良好的库

我遇到过各种库,包括:

  • Blitz++,它看起来正是我需要的,但看起来不太好维护良好(最新的稳定版本是 5 年前)

  • Boost,它不支持数组运算,并且与 Blitz++ 相比似乎相当慢。

  • Jonn Bowman 的 array.h 没有文档。

还有人对上述选项有任何其他建议或意见吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

凉宸 2024-10-04 22:30:41

Eigen 维护得非常好(至少现在,有新版本每个月都会发布)并支持您需要的其他操作。

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

何以笙箫默 2024-10-04 22:30:41

此处进行了一项广泛且相对较新的调查,包括基准。

我相信您可以通过将 Boost.UBlas 绑定到 LAPACK 或 Intel MKL 等底层数值库来加速 Boost.UBlas,但还没有这样做。

fwiw,最常出现的候选实现是 Boost.UBlas 和 MTL。根据我的经验,广泛采用更有可能促进持续的支持和发展。

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.

始终不够 2024-10-04 22:30:41
  • uBlas,Boost 的一部分。它提供完整的 BLAS 级别 1-3,因此提供大量数组算术函数。
  • Armadillo 似乎也是一个 C++ 线性代数库,据我所知,它可以选择使用 LAPACK/Atlas (这当然使其速度更快)。
  • GNU 科学库 提供完整的 BLAS。我不知道它有多快,或者是否可以使用LAPACK/Atlas。
  • 如果你不需要比你列出的内容更奇特的东西,你可以很容易地自己包装,例如 Atlas 的 BLAS。但如果没有必要,您可能不想重新发明轮子。
  • 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.
丶情人眼里出诗心の 2024-10-04 22:30:41

Necomi 似乎提供了您想要的功能。

它支持任意多维数字,其维度可以在运行时固定,提供对单个元素的快速访问,同时还支持算术(除其他外)表达式。

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.

烟花肆意 2024-10-04 22:30:41

还有另一个无耻的自我推销,

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

我已经在 GitHub 上发布了我个人对此问题的解决方案。
无论如何,我都不是 C++ 专家,但我想我至少应该把它扔掉。

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.

不醒的梦 2024-10-04 22:30:41

也许你想尝试我的“Multi”库: https://gitlab.com/correaa/boost- multi

  • 动态分配数组的能力,其大小在运行时确定
    multi::array<int, 2> A({m, n});
  • 访问和修改单个数组值的能力(快速)
    A[i][j] += 42;

生成类似于 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

嗯,不是那样的,我决定将数组算术与数据结构分开。

话虽如此,该库与 STL 算法非常兼容(如果您的步骤允许,它有一个使用 BLAS 的适配器)。

过度简化有关访问模式的一些问题,......

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.
  • 一个维护良好的库,

我将尽我所能维护它,我也欢迎贡献者。

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.

回首观望 2024-10-04 22:30:41

也许存在诸如 BLAS、CBLAS 之类的库,但不记得在哪里。

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

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

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

自在安然 2024-10-04 22:30:41

从性能角度来看,我尝试过 boost::MultiArray 和 Armadillo。两者都不是很快,因为与数组或向量相比,两者的访问时间都很慢,而且我能够在 x1(4:10) = x2(1:6) + x2(2:7) 等操作中击败这些包+ x2(3:8) 通过使用简单的手工编码循环(我确信在我的编译器优化的帮助下)。当您进行矩阵乘法时,这些软件包可能会通过 LAPACK 和 BLAS 提供一些好处,但您始终可以自己使用这些接口。

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.

疏忽 2024-10-04 22:30:41

需要注意的是,这是无耻的自我推销,

https://github.com/ndarray/ndarray

可能值得研究。

虽然它不提供优化的数学运算符,但它确实为此提供了 Eigen 接口。它真正突出的地方在于通过 SWIG 或 Boost.Python 提供与 Python/NumPy 的互操作性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文