CUDA and Mathematica

Written by on August 20, 2016 in Mathematica, Programming with 1 Comment

Recently I purchased a high-end desktop computer for my image processing project. Since many computations tend to take several hours to execute, I wanted to accelerate the calculations to the extent possible by adding a GPU. I chose NIVIDA’s GeForce GTX 1080 processor-based card.

Although I will be using C++ for my work, because Mathematica 11 came out around the same time I bought my computer, I wanted to explore Mathematica’s support for CUDA. Programming in  Mathematica is a lot easier and more compact compared to C++.

Mathematica introduced support for OpenCL and CUDA in version 8.  To use CUDA functions, you have to import CUDALink package.

You can check if your system has a CUDA-capable device by calling the function CUDAQ[]. It returns True if a CUDA device is available.

You can then call CUDAInformation[] to get more details of the CUDA hardware. See the figure below

Basic CUDA Check

Basic CUDA Check

The first thing I wanted to check was how much of a performance improvement the CUDA functions brought in. I performed a simple calculation involving the traditional Fold and Map operations, and then performed the same calculation using the CUDA counterparts – CUDAFold and CUDAMap. You can see the execution timings below.

Basic Operations

Basic Operations

CUDA shows a significant speed up.

Next, I performed a sort operation on a float array, first using the regular Sort and then using CUDASort.



Here too, you can see a significant speed improvement.

As a final check, I wanted to see the performance impact on image processing operations. For this, I computed the convolution operation on a sample image, first with CUDAImageConvolve and the with ImageConvolve.

Image Convolution

Image Convolution

There is approximately a 25% performance hit when CUDA is not used. I am surprised that the difference is small (compared to the earlier two operations).

Well, that is it for the CUDA experiment this time. Mathematica has many CUDA specific functions, and you can also add your own.

Go check it out!

Tags: ,


If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

1 Reader Comment

Trackback URL Comments RSS Feed

  1. Ramwolf says:

    The speed improvement should be even greater when transferring the matrices to GPU memory before performing the operations.

Leave a Reply

Your email address will not be published. Required fields are marked *