Get Started
ClojureCUDA uses native Nvidia GPU drivers, and CUDA toolkit, so it is very important that you do not skip any part of this guide.
How to Get Started
- Walk through this guide, set up your development environment, and try the examples.
- Familiarize yourself with ClojureCUDA’s more detailed tutorials and API documentation.
Minimum requirements
- Java 8
- CUDA Toolkit 11.0+ (the CUDA version on your system has to match the one specified project.clj. Prefer 11.6)
- Linux or Windows. macOS doesn’t allow CUDA from version 11 and up. You can only use an old release of ClojureCUDA on macOS.
Usage
First use
or require
uncomplicate.clojurecuda.core
and/or uncomplicate.commons.core
and/or uncomplicate.clojurecuda.info
in your namespace, and you’ll be able to call appropriate functions from the ClojureCUDA library.
(require '[uncomplicate.clojurecuda.core :refer :all]
'[uncomplicate.commons.core :refer [info]]
'[uncomplicate.clojurecuda.info :refer :all])
Now you can work with CUDA devices, contexts, streams, memory etc.
Here we initialize cuda and get the info of all devices.
(init)
(map info (map device (range (device-count))))
If at least one device was found, you can continue with the following to verify that everything works on your system. This has been taken from an introductory article and is explained in much more detail there if you want to know what’s going on there:
(def my-nvidia-gpu (device 0))
(def ctx (context my-nvidia-gpu))
;; set the current context
(current-context! ctx)
;; allocate memory on the GPU
(def gpu-array (mem-alloc 1024))
;; allocate memory on the host
(def main-array (float-array (range 256)))
;; copy host memory to the device
(memcpy-host! main-array gpu-array)
(def kernel-source
"extern \"C\"
__global__ void increment (int n, float *a) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) {
a[i] = a[i] + 1.0f;
}
};")
(def hello-program (compile! (program kernel-source)))
(def hello-module (module hello-program))
(def increment (function hello-module "increment"))
(launch! increment (grid-1d 256) (parameters 256 gpu-array))
(def result (memcpy-host! gpu-array (float-array 256)))
(take 12 result)
Overview and Features
ClojureCUDA is a Clojure library for High Performance Computing with CUDA, which supports Nvidia’s GPUs. If you need to create programs for AMD, Intel, or even Nvidia’s GPUs, or Intel’s and AMD’s CPUs, you probably need ClojureCL, ClojureCUDA’s OpenCL based cousin.
If you need higher-level high performance functionality, such as matrix computations, try Neanderthal.
Installation
Install CUDA Toolkit
To use ClojureCUDA, you must have an Nvidia GPU, and install appropriate GPU drivers. If you need to create your own CUDA kernels (you most probably do), you also need CUDA Toolkit. You can download both the drivers and the toolkit as one bundle from Nvidia’s CUDA Toolkit page. Please note that ClojureCUDA requires a minimal CUDA version, which is currently 11.0
, and prefers the latest CUDA (currently 11.4
) so make sure that you have recently updated your drivers and the toolkit. If you use older drivers, some things might work, but some might not.
Add ClojureCUDA jar
The most straightforward way to include ClojureCUDA in your project is with Leiningen. Add the following dependency to your project.clj
:
If you use the latest CUDA (as of this writing, 11.6
) that’s all. If you must use CUDA 10.2
, or 9.2
, add an explicit
dependency to org.jcuda/jcuda
10.0
, or 0.9.2
to your project(s), or use an earlier version of ClojureCUDA. ClojuerCUDA might work with an earlier (matching) version of CUDA/JCuda, but it is not guaranteed.
ClojureCUDA currently works out of the box on Linux, Windows, and OS X. For other plaforms, contact us.
Where to go next
Hopefully this guide got you started and now you’d like to learn more. CUDA programming requires a lot of knowledge about the CUDA parallel computing model, devices and specifics of parallel computations. The best beginner’s guide, in my opinion, is the OpenCL in Action book. It is not based on CUDA, but OpenCL (which is an open standard similar to CUDA supported by ClojureCL. Most books for CUDA are not as good as that one, but there are plenty of them, so you’ll find the one that suits your tastes (but I don’t know which one to recommend for beginners). I expect to build a comprehensive base of articles and references for exploring this topic, so please check the All Guides page from time to time. Of course, you should also check the ClojureCUDA API for specific details, and feel free to take a glance at the source while you are there.
Tell Us What You Think!
Please take some time to tell us about your experience with the library and this site. Let us know what we should be explaining or is not clear enough. If you are willing to contribute improvements, even better!