Best approach for GPGPU/CUDA/OpenCL in Java?

JavaCudaGpgpuOpencl

Java Problem Overview


General-purpose computing on graphics processing units (GPGPU) is a very attractive concept to harness the power of the GPU for any kind of computing.

I'd love to use GPGPU for image processing, particles, and fast geometric operations.

Right now, it seems the two contenders in this space are CUDA and OpenCL. I'd like to know:

  • Is OpenCL usable yet from Java on Windows/Mac?
  • What are the libraries ways to interface to OpenCL/CUDA?
  • Is using JNA directly an option?
  • Am I forgetting something?

Any real-world experience/examples/war stories are appreciated.

Java Solutions


Solution 1 - Java

AFAIK, [JavaCL / OpenCL4Java][1] is the only OpenCL binding that is available on all platforms right now (including MacOS X, FreeBSD, Linux, Windows, Solaris, all in Intel 32, 64 bits and ppc variants, thanks to its use of [JNA][2]).

It has demos that actually run fine from Java Web Start at least on Mac and Windows (to avoid random crashes on Linux, please see [this wiki page][3], such as this [Particles Demo][4].

It also comes with a few utilities (GPGPU random number generation, basic parallel reduction, linear algebra) and a [Scala DSL][5].

Finally, it's the oldest bindings available (since june 2009) and [it has an active user community][6].

(Disclaimer: I'm [JavaCL][7]'s author :-))

[1]: https://github.com/nativelibs4java/JavaCL "JavaCL / OpenCL4Java" [2]: https://github.com/twall/jna/ "JNA" [3]: http://code.google.com/p/javacl/wiki/TroubleShootingJavaCLOnLinux "this wiki page" [4]: http://nativelibs4java.sourceforge.net/webstart/OpenCL/ParticlesDemo.jnlp "Particles Demo" [5]: http://ochafik.free.fr/blog/?p=207 "Scala DSL" [6]: http://groups.google.com/group/nativelibs4java "NativeLibs4Java's user group" [7]: http://code.google.co

Solution 2 - Java

You may also consider Aparapi. It allows you to write your code in Java and will attempt to convert bytecode to OpenCL at runtime.

Full disclosure. I am the Aparapi developer.

Solution 3 - Java

Well CUDA is a modification of C, to write CUDA kernel you have to code in C, and then compile to executable form with nvidia's CUDA compiler. Produced native code could then be linked with Java using JNI. So technically you can't write kernel code from Java. There is JCUDA http://www.jcuda.de/jcuda/JCuda.html, it provides you with cuda's apis for general memory/device menagement and some Java methods that are implemented in CUDA and JNI wrapped (FFT, some linear algebra methods.. etc etc..).

On the other hand OpenCL is just an API. OpenCL kernels are plain strings passed to the API so using OpenCL from Java you should be able to specify your own kernels. OpenCL binding for java can be found here http://www.jocl.org/.

Solution 4 - Java

I've been using JOCL and I'm very happy with it.

The main disadvantage of OpenCL over CUDA (at least for me) is the lack of available libraries (Thrust, CUDPP, etc). However CUDA can be easily ported to OpenCL, and by looking at how those libraries work (algorithms, strategies, etc) is actually very nice as you learn a lot with it.

Solution 5 - Java

I know it's late but take a look at this: https://github.com/pcpratts/rootbeer1

I have not worked with it but seems much easier to use than other solutions.

From the project page:

Rootbeer is more advanced than CUDA or OpenCL Java Language Bindings. With bindings the developer must serialize complex graphs of objects into arrays of primitive types. With Rootbeer this is done automatically. Also with language bindings, the developer must write the GPU kernel in CUDA or OpenCL. With Rootbeer a static analysis of the Java Bytecode is done (using Soot) and CUDA code is automatically generated.

Solution 6 - Java

I can also recommend JOCL by jogamp.org, works on Linux, Mac, and Windows. CONRAD, for example, uses heavily OpenCL in combination with JOCL.

Solution 7 - Java

If you want to do some image processing or geometric operations, you may want a linear algebra library with gpu support (with CUDA for instance). I would suggest you ND4J witch is the linear algrebra with CUDA GPU support on which DeepLearning4J is built. With that you don't have to deal with CUDA directly and have to low level code in c. Plus if you want to do more stuff with image with DL4J you will have access to specific image processing operations such as convolution.

Solution 8 - Java

You can take a look at the CUDA4J API

http://sett.com/gpgpu/the-cuda4j-api

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrederikView Question on Stackoverflow
Solution 1 - JavazOliveView Answer on Stackoverflow
Solution 2 - JavagfrostView Answer on Stackoverflow
Solution 3 - JavaIvanView Answer on Stackoverflow
Solution 4 - JavahalfwarpView Answer on Stackoverflow
Solution 5 - JavakarlView Answer on Stackoverflow
Solution 6 - JavaMichael DornerView Answer on Stackoverflow
Solution 7 - JavaGuillaume SurrocaView Answer on Stackoverflow
Solution 8 - Javauser1197918View Answer on Stackoverflow