简体   繁体   中英

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

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.

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

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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