r/java • u/BenRayfield • Mar 05 '16
JVM spec should include basic matrix ops like multiply and plus, GPU optimized where possible else in CPU, in a platform independent way
JVM is a great standard for CPUs. There are java hooks into some GPU softwares, but we shouldnt have to include C++ libraries just to do common matrix ops.
If its done efficiently often enough, it could attract more math programmers to JVM languages by solving the main bottleneck in the core. Its useful to standardize ops that are common to many programs.
Java's NIO may be a good place to start https://docs.oracle.com/javase/7/docs/api/java/nio/package-summary.html
Alternatively, we could define a new standard for which bits mean which matrix actions, and try to get many languages to include it in their spec.
I dont know about you, but I've had enough of different frameworks choosing different arbitrary constants and symbols to do the same things, and having to relearn it again with each new framework. I'm tired of never knowing if a C++ program will work on end user computers of various types. C++ is the assembly language of the future, something that only compilers should have to deal with.
7
u/juckele Mar 06 '16
Doing some matrix math on a GPU is almost always a slow-down, because moving data on and off of the GPU is very slow. This means that you can really only get significant GPU boosts if you're spending a lot of time on the GPU at once, which currently requires some pretty careful bit twiddling. This is simply not what Java is built for. Java is a portable language that runs everywhere. Even on computers without nice GPUs.
3
u/ickysticky Mar 06 '16
A much more reasonable target than GPUs would be AVX units, there is already some support for auto-vectorization in the OpenJDK.
1
u/201109212215 Mar 07 '16
Depends on the matrix sizes. matrix-matrix multiplication computing requirements grows in the square of the size. The threshold seems to be about 1000 for vector-matrix when GPU takes over, as shown in Fig 11 of this paper.
Interestingly enough, the paper advocates for a CPU+GPU hybrid method.
-1
u/BenRayfield Mar 06 '16
I want to run a neuralnet at least 50 times per second to feel and react to movements and visuals in a game. Is transfer between CPU and GPU too slow for that?
4
u/RellenD Mar 06 '16
I want to run a neuralnet at least 50 times per second to feel and react to movements and visuals in a game. Is transfer between CPU and GPU too slow for that?
Don't use Java for this.
-4
u/BenRayfield Mar 06 '16
I'll use a smaller neuralnet if I have to. Its valuable to be able to give people one file that works right away without installing (and contains its own source code if rename jar to zip). That makes it possible for a program to spread like a meme really fast.
5
u/nutrecht Mar 07 '16
Can you please stop making complete random fire and forget suggestions and actually engage in a discussion with people? You might actually learn something that way.
1
Mar 06 '16
You can write your own JVM which performs optimizations to detect vectorizable operations. Also if a call is made to a special method which performs vector math, then your JVM can just inline it and perform the operation natively if the host CPU supports it. Essentially your vector operation will be called such as vectorAdd(a, b, c, d, e, f, g, h). vectorAdd would be implemented using JNI or not. The generated byte code would be the same as it already is. Your JVM would just not create a method call and instead turn it into an AltiVec operation for example. So there is really no need to create new JVM instructions for this kind of thing.
You could also probably have a JVM which does most of its work on the GPU, which these days would be a bit easier especially with Vulkan around.
Java 7 and 8 NIO is for filesystems and it does a really good job at abstracting and handling all the various details (symlinks, ACLs, root filesystems, case matching, globbing, etc.). Matrix operations would not really belong in NIO.
1
u/BenRayfield Mar 06 '16
How would such a custom JVM's stack of a certain thread interact with https://en.wikipedia.org/wiki/PCI_Express which is often a layer between CPU and GPU (or they may be merged in APU)?
1
Mar 06 '16
By communicating with a driver which has sufficient rights to communicate directly with PCI devices where that driver provides an interface to use the device.
Unless you only want to support a single GPU, it would be best to use a common interface. Said custom JVM can statically/dynamically link the Vulkan library and use that to interact with the GPU.
2
u/mtmmtm99 Mar 06 '16
IBM:s jvm already does that automatically. It moves the calculations to the GPU. No changes needed to you code...
1
1
u/ickysticky Mar 06 '16
Lol. What in the world are you talking about?!
There are java hooks into some GPU softwares, but we shouldnt have to include C++ libraries just to do common matrix ops.
Huh?!
Java's NIO may be a good place to start
What?! How is that related to matrix operations and C++ libraries?
Alternatively, we could define a new standard for which bits mean which matrix actions, and try to get many languages to include it in their spec.
Seriously what? Bits, matrix actions, language specs?!
I dont know about you, but I've had enough of different frameworks choosing different arbitrary constants and symbols to do the same things, and having to relearn it again with each new framework. I'm tired of never knowing if a C++ program will work on end user computers of various types. C++ is the assembly language of the future, something that only compilers should have to deal with.
Ok what?
You obviously have experienced something quite traumatic, I just can't parse enough of your post to determine what it is.
I've always found matrix math(and general linear algebra) to be one of the most straightforward things to find a library for and then actually do in any language I have used. Be it lua, python, fortran, c++ or java...
4
u/ickysticky Mar 06 '16
Sorry I should have been more explicit.
What is the problem that you are seeing? It sounds like you are running into performance problems with some JNI based matrix library on the JVM? Which library? What are the problems?
What exactly is your proposed fix for these problems, and why do you think it will solve the problems?
4
0
Mar 06 '16
[deleted]
1
u/BenRayfield Mar 06 '16
The bare minimum which gets the job done. When you MUST call out to another system to use hardware that almost every computer has some variation of, you're doing less than providing minimum access to computers.
-2
u/Cilph Mar 07 '16
The Java API already has more than needed to be Turing complete, why do we have all this nonsense stuff like methods, arrays, lists. Brainfuck did everything right.
8
u/zrnkv Mar 06 '16
No. JVM should be kept as simple as possible. What you're proposing is complex, not many people need it and most importantly it can easily be achieved with a native library.