# Applications
This feature is not supported on the Wolfram Cloud.

Because GPUs are SIMD machines, to exploit CUDA's potential you must pose the problem in an SIMD manner. Computation that can be partitioned in such a way that each thread can compute one element independently is ideal for the GPU.

Some algorithms either cannot be written in parallel, or cannot be used on CUDA (due to architecture constraints). In those cases, research is ongoing to introduce alternative methods to use the GPU to perform those computations.

In this section, some usage of CUDA programming inside the Wolfram Language* *is showcased. All the following examples use CUDAFunctionLoad, which allows you to load CUDA source, binaries, or libraries into the Wolfram Language.

CUDAFunctionLoad | load CUDA function into the Wolfram Language |

CUDAFunctionLoad allows you to load CUDA source, binaries, or libraries into the Wolfram Language.

## Image Processing

This section contains examples of CUDA applications that perform image processing operations. *CUDALink* contains a few built-in functions that perform image processing operations, such as CUDAImageConvolve, CUDABoxFilter, CUDAErosion, CUDADilation, CUDAOpening, CUDAClosing, CUDAImageAdd, etc.

### Image Binarize

Binarize takes an input image and outputs a binary image with pixels set to white if above a threshold and black otherwise.

If you have not done so already, import the *CUDALink* application.

In[1]:= |

This defines the input image. To reduce the memory footprint on the GPU, use to represent the image.

In[2]:= |

This loads CUDAFunction. The type is signified by in the parameter list.

This defines the input image and allocates memory for the output.

In[4]:= |

This calls the binarize function, using as the threshold value.

This displays the output image.

The result agrees with the Wolfram Language*.*

This unloads the memory allocated.

In[11]:= |

### Recursive Gaussian

Recursive Gaussian approximates performing a Gaussian filter on input data. It sacrifices some accuracy in favor of speed. This implements a Deriche-style recursive Gaussian filter.

This loads two functions: the and functions. The functions take an RGBA image, and the data is passed as or .

In[13]:= |

This specifies the value. Recall that the Gaussian is .

In[15]:= |

The following parameters are calculated based on the value.

In[16]:= |

The following defines the input image and allocates memory for the output.

In[17]:= |

This runs the functions. Perform a vertical Gaussian, transpose, perform another vertical Gaussian, and transpose again to get the result.

In[21]:= |

Memory allocated must be freed.

In[26]:= |

### Box Filter

The box filter is an optimized convolution when the kernel is a BoxMatrix. It is implemented here.

This loads the CUDA functions needed to perform the box filter.

In[28]:= |

This sets the input parameters.

In[30]:= |

In[34]:= |

In[35]:= |

This unloads the memory allocated.

In[38]:= |

### Image Adjust

This is an implementation of ImageAdjust in CUDA.

In[39]:= |

CUDAFunction is loaded from the source string, and a float is used for the constant values.

This wraps the CUDAFunction to make with similar syntax to ImageAdjust.

In[41]:= |

The function can be used similarly to ImageAdjust.

### Canny Edge Detection

Canny edge detection combines a dozen or so filters to find edges in an image. The Wolfram Language's EdgeDetect provides similar functionality. Here is the implementation.

This loads the CUDA functions needed to perform the edge detection.

In[45]:= |

This sets the input image along with its parameters.

In[57]:= |

Now add host and device tensors to the CUDA manager. These will hold both the input and the output.

In[60]:= |

Next, define device-only temporary memory that will be used in the computation. Since Canny edge detection involves many filters, you need quite a few of them.

In[62]:= |

This calls the Canny edge-detection functions.

In[68]:= |

This views the output as an image.

This unloads the memory allocated.

In[81]:= |

## Linear Algebra and List Processing

This section contains examples of CUDA applications that perform linear algebra operations. Most of the functions discussed can be performed using CUDATranspose or CUDADot.

### Matrix Transpose

Matrix transposition is essential in many algorithms. *CUDALink* provides a ready-made implementation in the form of CUDATranspose. Users may wish to implement their own, however.

This loads the CUDAFunction for matrix transposition and defines a new function that takes a real-valued matrix and outputs its transpose.

In[82]:= |

The result agrees with the Wolfram Language.

### Matrix-Vector Multiplication

Matrix-vector multiplication is a common operation in linear algebra, finite element analysis, etc. This loads CUDAFunction, implementing matrix-vector multiplication.

In[86]:= |

This sets the input matrix and vector.

In[87]:= |

This invokes the above defined function, displaying the result using MatrixForm.

The result agrees with the Wolfram Language.

### Matrix-Matrix Multiplication

Matrix-matrix multiplication is a pivotal function in many algorithms. This loads CUDAFunction from a source file, setting the block dimension to .

In[91]:= |

This sets input values and allocates memory for the output.

In[93]:= |

This performs the computation.

In[96]:= |

This displays the result using MatrixForm.

The result agrees with the Wolfram Language.

This unloads the output buffer.

In[99]:= |

### Dot Product

The dot product of two vectors is a common operation in linear algebra. This implements a function that takes a set of vectors and gives the dot product of each.

This loads the function into the Wolfram Language.

In[102]:= |

This calls the function, returning the result.

The results agree with the Wolfram Language's output.

### Convex Hull

Convex hull is hard to make parallel on the GPU, so in this example the hybrid approach to GPU programming is taken: do computation that makes sense on the GPU, and the rest is done on the CPU.

This convex hull implementation is a textbook implementation of Andrew's algorithm and is designed to be simple, not efficient.

Define the predicate in the Wolfram Language.

In[107]:= |

This defines a function that splits the point set to be either above or below a given line connecting the extreme points.

In[108]:= |

This loads the above function as a CUDA function.

In[109]:= |

Define a Wolfram Language function that takes two split point sets and finds the convex hull for them.

In[110]:= |

This calls the above function. Note that the list is sorted with CUDASort before being processed by .

In[111]:= |

To test, create 20,000 uniformly distributed random points.

In[112]:= |

In[114]:= |

This visualizes the result. Lines are drawn between the hull points.

The above algorithm handles the extreme case where all or most points lie on the hull. This generates uniformly distributed points on the unit disk.

In[116]:= |

This computes the hull points.

In[118]:= |

The above is a prime example of combining Wolfram Language and CUDA programming to make an algorithm partially parallel that would otherwise be written in only serial code.

## Random Number Generation

Many algorithms, ranging from ray tracing to PDE solving, require random numbers as input. This is, however, a difficult problem on many core systems, where each core has the same state as any other core. To avoid that, parallel random number generators usually use entropy values such as the time of day to seed the random number generators, but those calls are not available in CUDA.

The following section gives three classes of algorithms to generate random numbers. The first is uniform random number generators (where pseudorandom number generators and quasi-random number generators are showcased). The second is random number generators that exploit the uniform distribution of a hashing function to generate random numbers. The final is normal random number generators.

### Pseudorandom Number Generators

Pseudorandom number generators are deterministic algorithms that generate numbers that appear to be random. They rely on the seed value to generate further random numbers.

In this section, a simple linear congruential random number generator (the Park–Miller algorithm) is shown, along with a more complex Mersenne Twister.

#### Park–Miller

The Park–Miller random number generator is defined by the following recurrence equation:

It can be implemented easily in the Wolfram Language.

In[120]:= |

Here, common values for and are used. Using NestList, you can generate a list of 1000 numbers and plot them.

Here is the timing to generate 1 million numbers.

An alternative is to use the Method option in SeedRandom; this can be used in the same manner as before.

Compared to the Wolfram Language implementation, this is around 300 times faster.

The CUDA implementation is similar to the one written in the Wolfram Language*. *The implementation is distributed along with *CUDALink*, and the location is shown below.

This loads the CUDA function into the Wolfram Language.

This allocates the output memory.

This calls CUDAFunction.

If you measure the timing, you notice that it is twice as fast as the Wolfram Language's built-in method, and 600 times faster than pure Wolfram Language implementation.

The timing against a Compile is similar to that of CUDA. This generates C code from a Compile statement with no integer overflow detection.

In[132]:= |

This finds the timing. Notice that there is little difference.

If you ignore the time it takes for memory allocation (and you can rightly do so in this case, since generated random numbers are usually reused on the GPU), you notice a 10× speed improvement.

#### Mersenne Twister

Mersenne Twister utilizes shift registers to generate random numbers. Because the implementation is simple, it maps well to the GPU. The following file contains the implementation.

This loads the function from the file.

In[138]:= |

Here, the Mersenne Twister input parameters are defined. The twister requires seed values. Those values can be computed offline and stored in a file, or they can be generated by the Wolfram Language. The latter is shown here.

In[139]:= |

This allocates the output memory. Since the output will be overwritten, there is no need to load memory from the Wolfram Language onto the GPU.

This invokes CUDAFunction with parameters.

The output can be plotted to show it is random.

A Wolfram Language function can be written that takes the number of random numbers to be generated as input, performs the required allocations and setting of parameters, and returns the random output memory.

In[149]:= |

This generates a plot of the first 10,000 elements.

The following measures the time it takes for the random number generator to generate 100 million numbers.

This is on par with the Wolfram Language's random number generator timings.

Considering that random numbers are seeds to other problems, a user may get performance increase in the overall algorithm even if the Wolfram Language's timings are superior to the CUDA implementation.

### Quasi-Random Number Generators

This section describes quasi-random number generators. Unlike pseudorandom number generators, these sequences are nonuniform and have underlying structures that are sometimes useful in numerical methods. For instance, these sequences typically provide faster convergence in multidimensional Monte Carlo integration.

#### Halton Sequence

The Halton sequence generates quasi-random numbers that are uniform on the unit interval. While the code works with arbitrary dimensions, only the van der Corput sequence is discussed, which works on 1D space. This is adequate for comparison.

The resulting numbers of the Halton (or van der Corput) sequence are deterministic but have low discrepancy over the unit interval. Because they fill the space uniformly in some applications, such as Monte Carlo integration, they are preferred to pseudorandom number generators.

The one-dimensional Halton (or van der Corput) value in base :

The sequence of length is then written as:

Given a number in base representation , the van der Corput sequence mirrors the number across the decimal point, so that its sequence value is .

In the Wolfram Language, you can find the sequence using IntegerDigits.

In[153]:= |

Setting the base to , you can calculate the first 1000 elements in the sequence.

In[154]:= |

This plots the result; notice how it fills the space uniformly.

A property of low-discrepancy sequences is that the next elements in the sequence know where the previous elements are positioned. This can be shown with Manipulate.

For the CUDA implementation, you have to implement your own version of IntegerDigits, but this is not difficult. First, load the implementation source code.

This loads CUDAFunction.

This allocates memory for the output. Here, only 1024 random numbers are generated.

This runs the function for dimension 1.

#### Sobol Sequence

The Sobol sequence is also a low-discrepancy sequence. It is implemented in the following CUDA file.

This loads the function, using as the block dimension.

Here, the input parameters are loaded. The direction vectors needed by the Sobol sequence are precomputed and stored in a file.

In[164]:= |

This executes the Sobol function, passing parameters.

In[169]:= |

This plots the first 10,000 values in the sequences. Note that the space is filled evenly with points (a property of quasi-random number generators).

When complete, the memory must be unloaded.

In[171]:= |

#### Niederreiter Sequence

The Niederreiter sequence is another commonly used low-discrepancy sequence. It is implemented in the following CUDA file.

This loads the seed data for the random number generator.

In[173]:= |

This loads CUDAFunction.

This allocates the memory needed for computation and sets the proper parameters. Here, the sequence for dimension 3 is generated, and the resolution is set to .

In[175]:= |

In[181]:= |

This gets the output from the GPU.

In[182]:= |

This plots the first 10,000 values in the sequences. Note that the space is filled evenly, like the Sobol sequence.

When complete, the memory must be unloaded.

In[184]:= |

### Hashing Random Number Generators

Random number generators that depend on hashing generate random numbers of lesser quality, but they generate them fast. For many applications, they are more than adequate.

#### Tiny Encryption Algorithm Hashing

The Tiny Encryption Algorithm (TEA) is a very simple hashing algorithm implemented in the following file.

Load CUDAFunction.

This allocates memory for the output.

This calls CUDAFunction.

This deletes allocated memory.

In[190]:= |

#### MD5 Hashing

Other general hashing methods can be used for random number generators. Here is an implementation of the MD5 algorithm—a well-known hashing algorithm.

This loads CUDAFunction from the source.

This calls CUDAFunction.

This deletes allocated memory.

In[196]:= |

### Normal Random Numbers

The following algorithms generate normally distributed random numbers.

#### Inverse Cumulative Normal Distribution

The following implements a way to generate normally distributed random numbers.

This loads CUDAFunction.

Allocate memory for 100,000 random numbers.

In[199]:= |

This calls CUDAFunction.

This gets the memory into the Wolfram Language.

In[202]:= |

This plots the result, using Histogram.

In[204]:= |

#### Box–Muller

Box–Muller is a method of generating normally distributed numbers, given a set of uniformly distributed random numbers. The CUDA implementation is found in the following file.

This loads CUDAFunction.

This sets the input arguments.

In[207]:= |

Use the Mersenne Twister (defined two sections ago) to generate a list of uniformly distributed random numbers.

Transform the list of uniform random numbers to normally distributed random numbers.

You can see the bell curve when using Histogram.

This deletes allocated memory.

In[215]:= |

### Applications of Random Number Generators

Random numbers have applications in many areas. Here two main applications are presented: Monte Carlo integration (by approximating and an arbitrary function) and simulating Brownian motion.

#### Approximating π

The value of can be approximated using Monte Carlo integration. First, generate uniformly random numbers in the unit square. Then the number of points inside the first quadrant of the unit circle is counted. The result is then divided by the number of points. This will give .

This implements reduction, counting the number of points in a unit circle.

This loads CUDAFunction.

In[218]:= |

Generate the random numbers using the Mersenne Twister algorithms discussed previously.

This allocates the output memory.

This performs the computation.

The result agrees with the Wolfram Language.

The timing is considerably faster.

Compared to the Wolfram Language.

#### Monte Carlo Integration

Monte Carlo integration finds its way into many areas. Here, Sqrt[x] from 0 to 1 is integrated.

In[228]:= |

Use the Sobol quasi-random number generator for random numbers.

In[230]:= |

Then use the number of random number generators as the length.

This allocates memory for the output.

This checks whether the first few elements make sense.

You now need to sum the output. This can be done using CUDAFold.

The result agrees with NIntegrate.

In[244]:= |

#### Brownian Motion

This allocates memory for the simulation.

In[246]:= |

In[247]:= |

The values of the pseudorandom sequence are normally distributed.

In[249]:= |

## Code Generation

Since *CUDALink* is integrated in the Wolfram Language, you can use Wolfram Language features like SymbolicC to generate CUDA kernel code. If you have not done so already, import *CUDALink*.

In[251]:= |

For this example, you need the SymbolicC package.

In[252]:= |

This defines some common Wolfram Language constructs, translating them to their SymbolicC representation.

In[253]:= |

To test, pass in a Wolfram Language statement and get the SymbolicC output.

To convert to a C string, use the ToCCodeString method.

The above allows you to write a function that takes a Wolfram Language function (pure or not) and would generate the appropriate CUDA kernel source.

In[273]:= |

Passing a pure function to returns the kernel code.

This defines a function that, given a Wolfram Language function and an input list, generates the CUDA kernel code, loads the code as a CUDAFunction, runs the CUDAFunction, and returns the result.

In[277]:= |

You can test with a pure function that adds 2 to each element in an input list.

Any construct translated by is supported by . Here, each element is squared.

This performs Monte Carlo integration.

Functions can be defined and passed into . Here, a color negation function is defined.

In[282]:= |

Invoke the color negation function.

The above is a simple example, but can be used as a seed for more complicated ones.