3

I'm writing a graphics library in Java, primarily for use in game development. It needs basic vector and matrix objects for use in 3D calculations, and ideally those objects would employ SIMD operations. Although Java doesn't provide such operations directly, it is possible to hint to the JVM to use them in the context of large arrays. Hence...

Can the JVM vectorize operations on vector-like objects? If so, how can I ensure that this happens?

To clarify: I need operations on small, statically sized objects, not variable-length arrays. E.g., matrices that are strictly 3x3 or 4x4, vectors that are strictly of length 4, etc. Vectorization of large, variable-length arrays has already been discussed.

Some example candidates for vectorization:

public class Vector4f
{
    public int x, y, z, w;

    public void madd(Vector4f multiplicand, Vector4f addend)
    {
        this.x = this.x * multiplicand.x + addend.x;
        this.y = this.y * multiplicand.y + addend.y;
        this.z = this.z * multiplicand.z + addend.z;
        this.w = this.w * multiplicand.w + addend.w;
    }

    public float dot(Vector4f other)
    {
        return this.x * other.x
             + this.y * other.y
             + this.z * other.z
             + this.w * other.w;
    }
}

public class Matrix44f
{
    public float m00, m01, m02, m03;
    public float m10, m11, m12, m13;
    public float m20, m21, m22, m23;
    public float m30, m31, m32, m33;

    public void multiply(Matrix44f other) { ... }
    public void transform(Vector4f other) { ... }
    public void andSoOnAndSoForth() { ... }
}
Community
  • 1
  • 1
Philip Guin
  • 1,452
  • 15
  • 21
  • 1
    You could always resort to JNI if you need that performance guarantee. Rather than pondering over if java is going to use some low level optimization technique, why don't you benchmark and find out if its within your performance requirements. Implementing SIMD in native code doesn't seem too difficult. Also, please note that in order to achieve successful widespread deployment thats **relies** on SIMD, you require **all** JVM/JITs to have SIMD capabilities which I highly doubt is the case. – initramfs Aug 02 '14 at 08:11
  • Generally, in 3D game development, vectors and matrices are abundant in performance-sensitive code. It's not a matter of specific requirements, but general performance of any application the library is used in. Also, my understanding is that the overhead of individual JNI calls would outweigh the benefits in the case of small, atomic operations like these. – Philip Guin Aug 02 '14 at 08:24
  • If its out of shear curiosity I have nothing for you. But what happens after you find out if java supports or does not support SIMD optimizations? Is that going to make a impact on your developmental path? With JNI I was suggesting to replace only the parts where batches of data have to be processed in one go (since thats where SIMD excels anyway). Small atomic operations can be left in java bytecode. If your operations are too scattered to batch up you're either looking at rewriting most of the engine in native code or sucking it up and hope the JVM can do its thing. – initramfs Aug 02 '14 at 08:31
  • Well, it's certainly more than curiosity, as I'm writing the library for use in an actual game I intend to distribute, and hopefully more games to follow. And no, it won't fundamentally change my development path, but it will affect performance of an engine I'm developing/researching (so yes, actually, I suppose there is a measurable use case.) Batching is certainly a possibility (e.g. every game entity's rotation is a 3x3 matrix allocated from a native pool), though you could only really batch holistic things like acceleration without tremendous effort. – Philip Guin Aug 02 '14 at 08:48
  • If SSE operations require batches of data to perform well, then you would definitely have a point, though. (I'm not aware if this is the case. I assumed that the mythical 4x speed up was only dependent on availability of data to shove into the registers without interruption.) – Philip Guin Aug 02 '14 at 08:54

0 Answers0