适用于 JOGL 的快速 Java 矩阵库通用矩阵数学?

发布于 2024-11-15 01:54:47 字数 219 浏览 1 评论 0原文

我有兴趣在 JOGL 2 中编写 OpenGL 应用程序,使用着色器而不是固定功能管道。我需要在 CPU 端执行大量 4x4 双精度矩阵数学,以替换固定函数管道的推送/弹出/转换业务。同一个应用程序还将包含一些需要对大型矩阵进行操作的机器学习代码。我已经研究过 JBLAS 的机器学习内容(并且由于我已经将 JNI 用于 JOGL,因此依赖另一个本机库的缺点最小)),但我不确定它是否是 GL 的最佳选择 -相关矩阵。想法?

I'm interested in writing an OpenGL app in JOGL 2, using shaders instead of the fixed-function pipeline. I'll need to do a fair bit of 4x4 double-precision matrix math CPU-side, to replace the fixed function pipeline's push/pop/transform business. The same app is also going to include some machine learning code that will require operations on large matrices. I've looked at JBLAS for machine learning stuff (and since I'm already using JNI for JOGL, there're minimal downsides to depending on another native library)), but I'm not sure if it's the best choice for GL-related matrices. Thoughts?

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

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

发布评论

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

评论(4

时光磨忆 2024-11-22 01:54:47

您只需要操作 4x4 矩阵吗?大多数通用线性代数库都针对大型矩阵进行了高度优化,而很少关注较小的矩阵。我编写 EJML 的部分原因是为了解决这个问题并激励其他开发人员针对小矩阵进行优化。对于小型矩阵,EJML 是最快的,但还可以做得更好。

如果您确实需要大量性能,我不会使用任何常见的嫌疑人,而是推出您自己的高度专业化的代码。应该可以比通用库好几倍。

2x2 矩阵的简单示例:

public class Matrix2x2 {
  double a11,a12,a21,a22;
}

public static void mult( Matrix2x2 a , Matrix2x2 b , Matrix2x2 c ) {
  c.a11 = a.a11*b.a11 + a.12*b.a21;
  c.a12 = a.a11*b.a12 + a.12*b.a22;
  c.a21 = a.a21*b.a11 + a.22*b.a21;
  c.a22 = a.a21*b.a12 + a.22*b.a22;
}

注意我没有尝试编译此代码,它只是一个示例。

Do you only need to manipulate 4x4 matrices? Most general purpose linear algebra libraries have been highly optimized for large matrices with little attention put into smaller ones. Part of the reason I wrote EJML was to address this issue and to motivate other developers to optimize for small matrices. EJML is the fastest for small matrices, but it is possible to do better.

If you really need a lot of performance I would not use any of the usual suspects and instead roll your own highly specialized code. It should be possible to beat general purpose libraries by several times.

Simple example for 2x2 matrix:

public class Matrix2x2 {
  double a11,a12,a21,a22;
}

public static void mult( Matrix2x2 a , Matrix2x2 b , Matrix2x2 c ) {
  c.a11 = a.a11*b.a11 + a.12*b.a21;
  c.a12 = a.a11*b.a12 + a.12*b.a22;
  c.a21 = a.a21*b.a11 + a.22*b.a21;
  c.a22 = a.a21*b.a12 + a.22*b.a22;
}

Note I have not tried to compile this code, it is just an example.

不离久伴 2024-11-22 01:54:47

这些基准可能会帮助您选择满足您的性能需求的产品。

http://lessthanoptimal.github.io/Java-Matrix-Benchmark/

These benchmarks might help you choose something that meets your performance needs.

http://lessthanoptimal.github.io/Java-Matrix-Benchmark/

爱冒险 2024-11-22 01:54:47

一方面,查看 JBLAS 的 API 文档,我认为它不是处理 OpenGL 矩阵的“最佳选择”,因为它缺少一些基本功能:

要使用 OpenGL 在屏幕上显示某些内容,您需要通常的透视投影矩阵,并且可能需要用于计算对象上的仿射变换的东西。但第一个只是你可以通过 copypasta 获得的一些 LOC,而后者是微不足道的,因为 Java 已经有了它们 已加入,所以我认为您已准备好使用现有的资源。

For one thing, looking at the API documentation of JBLAS I think it's not the "best choice" for dealing with OpenGL matrices because it misses some fundamental functionality:

To get something on screen with OpenGL you'll need the usual perspective projection matrices and possibly something to compute affine transformations on your objects. But the first is only a few LOC you can get via copypasta and the latter is trivial because Java already has them on board, so I think you're ready to go with what you have.

玩心态 2024-11-22 01:54:47

您可能想要为机器学习和 OpenGL 使用不同的库

OpenGL 将从使用专门针对 2D、3D 和 4D 向量的小型、快速、优化矩阵中获益匪浅。这些通常包含在您的 OpenGL 库或游戏引擎中,例如 LWJGL 包含 Matrix4f 和朋友们。这些库还将提供各种其他与图形相关的功能,例如您可能需要四元数进行旋转。

另一方面,机器学习算法需要针对并行计算优化大型矩阵。像 Parallel Colt 这样的东西就比较合适。

You probably want to use different libraries for the machine learning and the OpenGL.

OpenGL will benefit significantly from use of small, fast, optimised matrices that are special-cased for 2D, 3D and 4D vectors. These are typically included in your OpenGL library or game engine, for example LWJGL includes Matrix4f and friends. There are various other graphics-related features that these libraries will also provide, e.g. you may want quaternions for rotations.

The machine learning algorithms on the other hand will want large matrices optimised for parallel computation. Something like Parallel Colt would be appropriate.

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