Java 中 GPGPU/CUDA/OpenCL 的最佳方法?
图形处理单元上的通用计算(GPGPU)是一个非常有吸引力的概念,可以利用适用于任何类型计算的 GPU。
我喜欢使用 GPGPU 进行图像处理、粒子和快速几何运算。
目前,该领域的两个竞争者似乎是 CUDA 和 OpenCL。我想知道:
- OpenCL 是否可以在 Windows/Mac 上通过 Java 使用?
- 连接 OpenCL/CUDA 的库方式有哪些?
- 直接使用 JNA 是一种选择吗?
- 我是不是忘记了什么?
任何现实世界的经验/例子/战争故事都值得赞赏。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我还可以推荐 jogamp.org 的 JOCL,它适用于 Linux、Mac 和 Windows。例如,CONRAD 大量使用 OpenCL 与 JOCL 的结合。
I can also recommend JOCL by jogamp.org, works on Linux, Mac, and Windows. CONRAD, for example, uses heavily OpenCL in combination with JOCL.
如果您想做一些图像处理或几何运算,您可能需要一个具有 GPU 支持的线性代数库(例如 CUDA)。我建议您使用 ND4J,它是支持 CUDA GPU 的线性代数,DeepLearning4J 是在其上构建的。这样你就不必直接处理 CUDA,而必须使用 c 语言编写低级代码。另外,如果您想使用 DL4J 对图像执行更多操作,您将可以访问特定的图像处理操作,例如卷积。
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.
您可以查看 CUDA4J API
http://sett.com/gpgpu/the-cuda4j- API
You can take a look at the CUDA4J API
http://sett.com/gpgpu/the-cuda4j-api
AFAIK,JavaCL / OpenCL4Java 是唯一可在所有平台上使用的 OpenCL 绑定现在(包括 MacOS X、FreeBSD、Linux、Windows、Solaris,全部采用 Intel 32 位、64 位和 ppc 变体,这要归功于它使用 JNA)。
它的演示至少在 Mac 和 Windows 上实际上可以从 Java Web Start 正常运行(为避免 Linux 上的随机崩溃,请参阅 此 Wiki 页面,例如这个 粒子演示。
它还附带一些实用程序(GPGPU 随机数生成、基本并行归约、线性代数)和 Scala DSL。
最后,它是最古老的可用绑定(自 2009 年 6 月起)和 它有一个活跃的用户社区。
(免责声明:我JavaCL 的作者:-))
AFAIK, JavaCL / OpenCL4Java 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).
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, such as this Particles Demo.
It also comes with a few utilities (GPGPU random number generation, basic parallel reduction, linear algebra) and a Scala DSL.
Finally, it's the oldest bindings available (since june 2009) and it has an active user community.
(Disclaimer: I'm JavaCL's author :-))
您也可以考虑 Aparapi。它允许您用 Java 编写代码,并尝试在运行时将字节码转换为 OpenCL。
全面披露。我是 Aparapi 开发者。
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.
CUDA 是 C 的修改版,要编写 CUDA 内核,您必须用 C 编写代码,然后使用 nvidia 的 CUDA 编译器编译为可执行形式。然后可以使用 JNI 将生成的本机代码与 Java 链接。因此从技术上讲,您无法从 Java 编写内核代码。有JCUDA http://www.jcuda.de/jcuda/JCuda.html,它为您提供了用于一般内存/设备管理的 cuda api 以及一些在 CUDA 和 JNI 包装中实现的 Java 方法(FFT、一些线性代数方法等)。
另一方面,OpenCL 只是一个 API。 OpenCL 内核是传递给 API 的纯字符串,因此使用 Java 中的 OpenCL,您应该能够指定自己的内核。 Java 的 OpenCL 绑定可以在这里找到 http://www.jocl.org/。
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/.
我一直在使用 JOCL,我对此非常满意。
OpenCL 相对于 CUDA 的主要缺点(至少对我来说)是缺乏可用的库(Thrust、CUDPP 等)。然而,CUDA 可以轻松移植到 OpenCL,并且通过查看这些库的工作原理(算法、策略等)实际上非常好,因为您可以从中学到很多东西。
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.
我知道已经晚了,但看看这个: https://github.com/pcpratts/rootbeer1
我还没有使用过它但似乎比其他解决方案更容易使用。
从项目页面来看:
Rootbeer 比 CUDA 或 OpenCL Java 语言绑定更先进。通过绑定,开发人员必须将复杂的对象图序列化为基本类型的数组。对于 Rootbeer,这是自动完成的。此外,通过语言绑定,开发人员必须使用 CUDA 或 OpenCL 编写 GPU 内核。使用 Rootbeer,可以完成 Java 字节码的静态分析(使用 Soot)并自动生成 CUDA 代码。
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.