简体   繁体   English

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

[英]Fast Java matrix library suitable for JOGL + generic matrix math?

I'm interested in writing an OpenGL app in JOGL 2, using shaders instead of the fixed-function pipeline.我有兴趣在 JOGL 2 中使用着色器而不是固定功能管道编写 OpenGL 应用程序。 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.我需要在 CPU 端做一些 4x4 双精度矩阵数学运算,以替换固定的 function 管道的推送/弹出/转换业务。 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.我已经将 JBLAS 用于机器学习(并且由于我已经将 JNI 用于 JOGL,因此依赖另一个本机库的缺点很小)),但我不确定它是否是 GL- 的最佳选择相关矩阵。 Thoughts?想法?

Do you only need to manipulate 4x4 matrices?您只需要操作 4x4 矩阵吗? 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的部分原因是为了解决这个问题并激励其他开发人员针对小型矩阵进行优化。 EJML is the fastest for small matrices, but it is possible to do better. EJML 对于小矩阵来说是最快的,但也有可能做得更好。

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: 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;
}

Note I have not tried to compile this code, it is just an example.注意我没有尝试编译这段代码,它只是一个例子。

These benchmarks might help you choose something that meets your performance needs.这些基准可以帮助您选择满足您的性能需求的东西。

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

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

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.要使用 OpenGL 在屏幕上显示某些内容,您将需要通常的透视投影矩阵,并且可能需要一些东西来计算对象的仿射变换。 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.但是第一个只是你可以通过 copypasta 获得的几个 LOC,而后者是微不足道的,因为Java已经有它们,所以我认为你已经准备好 go 与你所拥有的。

You probably want to use different libraries for the machine learning and the OpenGL.您可能希望为机器学习和 OpenGL 使用不同的库

OpenGL will benefit significantly from use of small, fast, optimised matrices that are special-cased for 2D, 3D and 4D vectors. OpenGL 将显着受益于使用专门针对 2D、3D 和 4D 向量的小型、快速、优化矩阵。 These are typically included in your OpenGL library or game engine, for example LWJGL includes Matrix4f and friends.这些通常包含在您的 OpenGL 库或游戏引擎中,例如 LWJGL 包括Matrix4f和朋友。 There are various other graphics-related features that these libraries will also provide, eg 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.平行柯尔特这样的东西是合适的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM