Java on GPU: Complete Method directly on GPUin plain Java - java

Firstly: Is it possible to use Java and let it (partly) run on or use GPUs?
And if it's possible, is it possible to use the normal Java syntax and not using special cuda or opencl syntax?
I want just take my coded java source and let it run with the smallest changes possible on GPUs.
I would greatly appreciate code samples.

Consider Aparapi http://aparapi.github.io/. It attempts to convert bytecode to OpenCL at runtime. So you can code for your GPU in pure Java.
Full disclosure, I am the Aparapi lead developer.

The Rootbeer GPU Compiler supports running arbitrary Java code on the GPU. It is more full featured than Aparapi. Rootbeer supports arbirary object graphs of any type.
It was just released open source on my github account:
https://github.com/pcpratts/rootbeer1

There are several Java bindings to CUDA and OpenCL (jcuda.org, jocl.org, something else also called jocl) but these are all just ways to get CUDA or OpenCL code running on the GPU via Java and require you to write your code specifically for that. I don't think there is an easy way to run an arbitrary multi-threaded Java program on the GPU with just minor changes to the code.
What does your Java program do that you want to run on the GPU?
You have to take into account that the architecture of a GPU is quite different than that of a CPU; cores on a GPU are not general-purpose cores that can do anything and work independently, as in an Intel x86 CPU. To really take advantage of the specific SIMD architecture of a GPU, your code has to be written with that architecture in mind.

TornadoVM is a plug-in to OpenJDK that allows programmers to automatically run Java programs on heterogeneous hardware. TornadoVM currently targets OpenCL-compatible devices and it runs on multi-core CPUs, GPUs (NVIDIA and AMD), Intel integrated GPUs, and Intel FPGAs.
Take a look here: https://github.com/beehive-lab/TornadoVM

Have a look on http://code.google.com/p/java-gpu/.
It compiles pure java code into kernels, with only using annotations.

Related

Is there any way to perform Image processing using java with GPU

I have got some code of image processing using matlab. For running matlab code we need to buy matlab license. But as per requirement of my project I have to use any open source software. So I thought I can convert the whole code into Java.
Also the present code is taking too much time for computing the result. For that I thought I can use GPU .
Can any one tell me a way to use java and GPUs for image processing or any other solution for my problem(I need to use any open source s/w and also parallelize the code).
Is it a good Idea to use RootBeer???
There is a Java binding for OpenCL called JOCL (OpenCL tasks can run on GPU).
I don't know much about RootBeer.
Whatever the solution you choose, you will need to download the appropriate OpenCL driver from your GPU manufacturer (Nvidia, AMD or Intel). Note that the AMD driver can also use your CPU to execute the OpenCL kernels in SIMD mode and will work on any CPU.

Heterogenous computing using OpenCl Vs java

Java and OpenCL both support Heterogeneous Computing; systems with multiple architecture working cooperatively in parallel(Task and data parallel ).
portability is the main goal in both the cases, and both have achieved this goal to a large extent. In fact still OpenCl cannot be run on FPGAs and DSPs, as the tools are not available. JVM can be developed for GPUs FPGA, ARM etc.
Both generate intermediate code.
Despite so many similarities why and when should one prefer OpenCL over Java for Heterogeneous Computing?
EDITED
Please be specific to my question: Despite so many similarities why and when should one prefer OpenCL over Java for Heterogeneous Computing? Why at all I chose OpenCL instead of Java?
If you say openCl has better performance then my immediate question will be why it is so(since both generate intermediate code that need to be converted into binaries for specific h/w).
OpenCL is normally used for processing large amounts of parallel operations(calculations etc). This is where OpenCL thrives and the throughput of your program is really increased by it.
Java is a general purpose language for writing applications running on various platforms/devices.
They have a different purpose as tools but you can use OpenCL from your Java program using JOCL. I also used a tool from AMD called Aparapi. because I wanted my application to carry out some calculations, a job that my ASUS AMD 6970 card seemed to get done faster than my 6 core AMD 1090T Phenom Black Edition.
Just for you to know what Aparapi is(taken from the google code site):
Aparapi allows Java developers to take advantage of the compute power
of GPU and APU devices by executing data parallel code fragments on
the GPU rather than being confined to the local CPU. It does this by
converting Java bytecode to OpenCL at runtime and executing on the
GPU, if for any reason Aparapi can't execute on the GPU it will
execute in a Java thread pool.
Despite so many similarities why and when should one prefer OpenCL
over Java for Heterogeneous Computing?
One would choose OpenCL for parallel processing tasks that would otherwise take a lot of time to be executed on the CPU. The disadvantage in this case would be the extra heat and power consumption costs. Bear also in mind that some tasks would execute faster on a CPU. It all depends on the application that most of the time would require trials on both types of chips in order to decide which one does the job best.
Why at all I chose OpenCL instead of Java?
One would choose OpenCL over Java for parallel processing oriented tasks. OpenCL is a tool built for this purpose. OpenCL is not something you would use for building enterprise or desktop applications. Java is a programming language for almost any domain running on many platforms. If one would know that using OpenCL would save them 24 hours of calculations over Java; of course Java would be sidelined and OpenCL would be the way to go or simply OpenCL could be used through a Java application.
If you say openCl has better performance then my immediate question
will be why it is so(since both generate intermediate code that need
to be converted into binaries for specific h/w).
It has better performance on specific applications since GPU have more cores than a CPU. Their frequency though is quite low so this means that they are better on parallel tasks rather than sequential. You have to bear in mind that OpenCL is used for basic calculations and not for building full flavor applications...at least as far as I know.

How to speed up Java applications?

I need to use Java for a desktop application. I heard that there are many tools that compile java natively, is this true? does these tools compile java program into machine code?
THank you!
Since the (Sun/Oracle) Java VM has a good JIT (just-in-time) compiler, you don't have to compile your Java program to machine code yourself. The compiler will do that on the fly when it's necessary.
So: Speed up your Java programs just as every other program:
reduce algorithmic complexity
exploit parallelism
compute at the right moment
find and remove bottlenecks
...
Since Java is a garbage collected language, there is one important point to more speed: reduce allocations! Reducing allocations will help you at least twice: The allocation itself isn't done and the garbage collector will have to do less work (which will save time).
I agree with the others that compiling to machine code does not make much sense: mind that C free/malloc have same or higher costs than Java new/garbage collection.
The NetBeans IDE comes with a built-in Profiler; so you could profile your application in that IDE to find bottlenecks.
are you coding the app or it's someone's else?
It looks you're trying to run an java app that is slow. Try increasing the memory when running it. You can change the shell script specifying these params:
java -Xms64m -Xmx512m
I need to use Java for a desktop application. I heard that there are
many tools that compile java natively, is this true? does these tools
compile java program into machine code?
Such programs do exist, but may come with tradeoffs when using some of the more dynamic capabilities of the Java platform like you may lose the ability to load new classes at runtime. The JVM may have a slow start up, but it's plenty fast enough once it gets going.
That said, one solution that I didn't see anyone mention here is to replace code written in Swing with SWT. The SWT toolkit uses native code underneath.

Can I compile Java to native code?

Is there any way to compile from Java to standalone (or library) machine code without requiring a JVM?
There used to be a tool called GCJ that was part of GCC, but it's been removed. Now, all the links in the GCC site re-direct to their non-GCJ equivalents.
NB: the comments all refered to my original answer saying you can compile Java to native code with GCJ.
Yes!
Oracle has been working on the GraalVm, which supports Native Images. Check here: https://www.graalvm.org/
Native Image
The native image feature with the GraalVM SDK helps improve the startup time of Java applications and gives them a smaller footprint. Effectively, it's converting bytecode that runs on the JVM (on any platform) to native code for a specific OS/platform — which is where the performance comes from. It's using aggressive ahead-of-time (AOT) optimizations to achieve good performance.
See more:
Summary
https://www.graalvm.org/docs/getting-started/#native-images
Demos: Native images for faster startup
https://www.graalvm.org/docs/examples/native-list-dir/
Detailed: 'Ahead-of-time Compilation'
https://www.graalvm.org/docs/reference-manual/aot-compilation/
The Micronaut platform uses GraalVM to make native microservices:
https://guides.micronaut.io/latest/micronaut-creating-first-graal-app.html
Excelsior JET is a commercial Java to native code compiler. However, it was discontinued in May 2019.
Yes, the JIT in the JVM does exactly that for you.
In fact it can produce faster code than compiling the code in advance as it can generate code optimised for the specific platform based on how the code is used at runtime.
The JVM is always involved even if a very high percentage is compiled to native code as you could load and run byte code dynamically.
Another possibility would be RoboVM.
However, it only seems to work on Linux, iOS and Mac OS X.
As of today, the project still seems somewhat alive contrary to some posts online claiming the project to be dead.

What is the lightest Java Virtual Machine ever? (except KVM)

I'd like to install Java on a Psion 5MX with GNU/Linux 16MB RAM machine, will it be possible?
Unfortunatly, "KVM must be installed by the manufacturer".
What I want is to be able to is to write basic Java code, test and sketch simple algorithms.
Important notes: I don't need the graphic part since I'll only use the console (no graphic server) but this will be inside a Debian.
There is a List of Java virtual machines on Wikipedia, with a number of them open source and targeting embed devices. Like Mika which seems quite small and portable.
Please give us more details about the machine, besides the OS and the memory. Also give us details on what you hope to accomplish with Java on such an underpowered machine (Java has some big overhead in embedded places as compared to writing native code).
The answer is maybe. I've seen Java running on a machine with 4MB of RAM, but it was not running Linux (in fact there was no OS at all). Link: Java on the Nintendo DS
The Squawk virtual machine is designed for small devices. It's probably got sub-par performance though since most of it is in java.
Probably a little late but I'd also suggest Squawk. It runs on sun spot devices which have 160k of membory (or something in that area). 160mhz CPU. The whole JVM is open source so you can try to re-write it for your own devices :)

Categories

Resources