Installing CUDA and OpenCL

Installing CUDA and OpenCL requires two main components: A toolkit and a graphics driver. The toolkit provides the compiler (nvcc) and support libraries needed to compile and link programs. The graphics driver provides the low level functionality needed to make the hardware work for a specific video card.

CUDA and OpenCL typically needs the latest and greatest drivers in order to work. Installing these drivers is obviously very operating system specific, and also depends on each video card vendor.

CUDA is, of course, specific to nVidia based hardware. OpenCL is supported by a range of hardware manufacturers.

Ubuntu

The trick on Ubuntu is to leverage what Ubuntu provides, so that future kernel upgrades don't brick your machine.

Ubuntu provides an open source driver for nVidia hardware. However, to use CUDA you need the proprietary nVidia drivers. This can be readily installed using System>Administration>Additional Drivers.

After installing the new drivers, you need to reboot the machine.

The proprietary driver installed will not be the latest development driver, but rather an older, tested and stable driver. For example, on Ubuntu 22.04.1 LTS this would be the 525 driver. The CUDA runtime requires that the driver version be a minimum version number depending on the CUDA Toolkit. If you have the latest and greatest CUDA Toolkit, you also need the latest and greatest driver. If you do not, the program will fail to run.

DO NOT manually install a device driver unless you must, as doing so will cause you endless trouble and could brick your installation.

Ubuntu 22.04.1 provides an CUDA 11.5 using
apt-get install nvidia-cuda-toolkit
I recommend you just use this version of CUDA for this class. It is by far the least headaches. Note that installing CUDA also installs OpenCL.

This command should work for CUDA

nvcc -o $@ $^ -lcuda

and for OpenCL

g++ -Wall -o $@ $^ -lOpenCL

OSX

In order to use CUDA or OpenCL on the Mac, you will need to have a GPU that supports it. Some versions of the Mac use AMD hardware, and supports OpenCL but not CUDA. The instructions for CUDA on OSX provides step by step instruction to determine if you can use CUDA. If you have AMD/ATI hardware try OpenCL instead.

Windows

CUDA and OpenCL on Windows are generally provided by the hardware vendors. For CUDA start with CUDA downloads.