CUDA Functions

CUDALink is a built-in Mathematica package that provides a simple and powerful interface for using CUDA within Mathematica's streamlined work flow.

CUDALink provides you with carefully tuned linear algebra, discrete Fourier transforms, and image processing algorithms. You can also write your own CUDALink modules with minimal effort. Using CUDALink from within Mathematica gives you access to Mathematica's features, including visualization, import/export, and programming capabilities.

In this section, the built-in CUDALink functions are discussed, and a handful of applications are also given.

List Processing

CUDALink list processing functions are designed to mimic the existing Mathematica functions, and, while less general than Mathematica's implementation, they do provide the most commonly used functions. CUDALink implements the following list processing functions.

CUDAMapmap a function to each element of an input list
CUDAFoldgiven an initial value and a function , this returns
CUDAFoldListgiven an initial value and a function , this returns
CUDASortsort a given list
CUDATotalfind the total value of a given list

CUDALink's list processing functions.

The above functions can be used as any Mathematica functions. To use the functions above, first load the CUDALink application.

In[1]:=
Click for copyable input

Once loaded, the above functions can be used. This maps the function Cos to a random list.

In[2]:=
Click for copyable input
Out[2]=

Computation can be strung together. Here you can find the total of the above list using CUDAFold (called reduction in the GPU programming field).

In[3]:=
Click for copyable input
Out[3]=

Applications

In many cases, the above list operators are pivotal to many algorithms. Here, a few are discussed.

Line of Sight

Given a height map, the line of sight problem finds all points on the height map visible from a single point. It does so by first transforming the height map to an angular map, and then performing a Max fold on the angular map. The results from the list can then be easily used to determine if a point is visible or not.

This generates a sample height map.

In[4]:=
Click for copyable input
Out[4]=

This computes the angular map. Here you use the first point in the height map as a reference angle.

In[5]:=
Click for copyable input
Out[5]=

The list is computed using CUDAFoldList.

In[6]:=
Click for copyable input
Out[6]=

An angular is marked as visible if .

In[7]:=
Click for copyable input
Out[7]=

This displays all points visible from the reference point.

In[8]:=
Click for copyable input
Out[8]=

Random Walk

Random walk is a common tool used in many applications, such as the analysis of Brownian motion in physics. This shows a random walk in one dimension using CUDAFoldList.

In[9]:=
Click for copyable input
Out[9]=

Choosing discrete random numbers, the walk can be performed on a lattice.

In[10]:=
Click for copyable input
Out[10]=

Histogram

Histograms are commonly used in many applications to place elements in bins. Here, you can use CUDASort to simplify the histogram calculation. This sorts the input image.

In[11]:=
Click for copyable input

Once sorted, given a value to count the number of its occurrences, you need to scan the sorted list until its value changes. To find the first element, you have to count the number of elements until the element is not equal to the first.

In[12]:=
Click for copyable input
Out[13]=

This resulting histogram is plotted using ListLinePlot.

In[14]:=
Click for copyable input
Out[14]=

Dot Product

This finds the dot product of two vectors.

In[15]:=
Click for copyable input
Out[15]=

Image Processing

The CUDALink Image Processing module can be classified into three categories. The first is convolution, which is optimized for CUDA. The second is morphology, which contains abilities such as erosion, dilation, opening, and closing. Finally, there are the binary operators. These are the image multiplication, division, subtraction, and addition operators. All operations work on either images or lists.

CUDAImageConvolveconvolve the kernel with the specified kernel
CUDABoxFilterconvolve the kernel with the BoxMatrix kernel
CUDAErosionperform morphological erosion
CUDADilationperform morphological dilation
CUDAOpeningperform morphological opening
CUDAClosingperform morphological closing
CUDAClampclamp the values between a range
CUDAColorNegateinvert the values of input
CUDAImageAddadd two inputs
CUDAImageSubtractsubtract two inputs
CUDAImageMultiplymultiply two inputs
CUDAImageDividedivide two inputs

CUDALink Image Base Operations.

To use any of these functions (and if not already done), include the CUDALink application.

In[1]:=
Click for copyable input

CUDALink's image processing functions, like Mathematica's, accept images as input. Here you can find the gradient of an input image.

In[2]:=
Click for copyable input
Out[2]=

Since the CUDA image processing functions behave like Mathematica functions, you can combine them with existing Mathematica functions. Here, you can apply CUDAImageMultiply to all combinations of a set of images.

In[3]:=
Click for copyable input
Out[3]=

The CUDA image processing functions work with Mathematica's dynamic evaluators, such as Manipulate, Dynamic, and Animate. Here, you can use Animate to create an animation of how an image behaves as it is convolved with different GaussianMatrix radius sizes.

In[4]:=
Click for copyable input
Out[4]=

Applications

Creating New Image Processing Operators

CUDALink's image processing operators are building blocks to more complicated operators. Here, you can define the operator, which is similar to the Darker operator in Mathematica.

In[5]:=
Click for copyable input

The function can then be used.

In[6]:=
Click for copyable input
Out[6]=

Input Smoothing

Many algorithms require the input to be smoothed before processing. This defines a random input list.

In[7]:=
Click for copyable input

This plots the results, showing the input to be very noisy.

In[8]:=
Click for copyable input
Out[8]=

You can use the fact that the image processing functions also operate on lists to smooth out the input list.

In[9]:=
Click for copyable input
Out[9]=

Geographical Data Processing

Since all image processing functions are also list processing functions, you can process any data that can be represented by a Mathematica list. In this example, you can use CUDAClamp to process geographic elevation data by clamping values in the elevation map.

This loads the data from the Wolfram servers.

In[10]:=
Click for copyable input

This creates an interface that allows the user to vary the clamp parameters.

In[11]:=
Click for copyable input
Out[11]=

Acquired Image Processing

The following example requires a web camera. CurrentImage returns an error if no camera is detected.

In[12]:=
Click for copyable input
Out[12]=

This creates an interface where the user can process input images from the web camera in real time.

In[13]:=
Click for copyable input
Out[13]=

Linear Algebra and Fourier Transforms

CUDADotgive product of vectors and matrices
CUDATransposetranpose input matrix
CUDAArgMaxListgive the index with maximum absolute element
CUDAArgMinListgive the index with minimum absolute element
CUDAFourierfind the Fourier transform
CUDAInverseFourierfind the inverse Fourier transform

Linear algebra and Fourier transform operations using CUDA.

If not done so already, import the CUDALink application.

In[1]:=
Click for copyable input

This multiplies two integer matrices together.

In[2]:=
Click for copyable input
Out[2]=

This shows the result in MatrixForm.

In[3]:=
Click for copyable input
Out[3]//MatrixForm=

Applications

Linear algebra and Fourier analysis have many applications that are beyond the scope of this tutorial. Here are two simple examples of the kind of operations made possible by these CUDALink features.

Spectrum

This loads the two-dimensional dataset.

In[4]:=
Click for copyable input

Find the logarithmic power spectrum.

In[5]:=
Click for copyable input
Out[5]=

Image Transformation

This transposes an input image.

In[6]:=
Click for copyable input
Out[6]=

Examples

Along with these useful functions, CUDALink bundles many examples that showcase the capabilities of programming with CUDALink. The source of these examples is bundled with Mathematica.

CUDAFluidDynamicscompute and render a fluid dynamics simulation
CUDAVolumetricRenderrender volumetric data

Example applications of CUDALink.

Fluid Dynamics

This approximates the solution of the Navier-Stokes equations on a torus.

In[7]:=
Click for copyable input
Out[7]=

Volumetric Render

This reads in the dataset.

In[8]:=
Click for copyable input

This renders the data by ray tracing the voxels.

In[9]:=
Click for copyable input
Out[9]=
New to Mathematica? Find your learning path »
Have a question? Ask support »