Skip to content

Latest commit

 

History

History
546 lines (422 loc) · 21 KB

examples.rst

File metadata and controls

546 lines (422 loc) · 21 KB

Examples

Vector Addition

This example uses Numba to create on-device arrays and a vector addition kernel; it is a warmup for learning how to write GPU kernels using Numba. We'll begin with some required imports:

../../../numba/cuda/tests/doc_examples/test_vecadd.py

The following function is the kernel. Note that it is defined in terms of Python variables with unspecified types. When the kernel is launched, Numba will examine the types of the arguments that are passed at runtime and generate a CUDA kernel specialized for them.

Note that Numba kernels do not return values and must write any output into arrays passed in as parameters (this is similar to the requirement that CUDA C/C++ kernels have void return type). Here we pass in c for the results to be written into.

../../../numba/cuda/tests/doc_examples/test_vecadd.py

cuda.to_device() <numba.cuda.to_device> can be used create device-side copies of arrays. cuda.device_array_like() <numba.cuda.device_array_like> creates an uninitialized array of the same shape and type as an existing array. Here we transfer two vectors and create an empty vector to hold our results:

../../../numba/cuda/tests/doc_examples/test_vecadd.py

A call to forall() <numba.cuda.dispatcher.Dispatcher.forall> generates an appropriate launch configuration with a 1D grid (see cuda-kernel-invocation) for a given data size and is often the simplest way of launching a kernel:

../../../numba/cuda/tests/doc_examples/test_vecadd.py

This prints:

[0.73548323 1.32061059 0.12582968 ... 1.25925809 1.49335059 1.59315414]

One can also configure the grid manually using the subscripting syntax. The following example launches a grid with sufficient threads to operate on every vector element:

../../../numba/cuda/tests/doc_examples/test_vecadd.py

This also prints:

[0.73548323 1.32061059 0.12582968 ... 1.25925809 1.49335059 1.59315414]

1D Heat Equation

This example solves Laplace's equation in one dimension for a certain set of initial conditions and boundary conditions. A full discussion of Laplace's equation is out of scope for this documentation, but it will suffice to say that it describes how heat propagates through an object over time. It works by discretizing the problem in two ways:

  1. The domain is partitioned into a mesh of points that each have an individual temperature.
  2. Time is partitioned into discrete intervals that are advanced forward sequentially.

Then, the following assumption is applied: The temperature of a point after some interval has passed is some weighted average of the temperature of the points that are directly adjacent to it. Intuitively, if all the points in the domain are very hot and a single point in the middle is very cold, as time passes, the hot points will cause the cold one to heat up and the cold point will cause the surrounding hot pieces to cool slightly. Simply put, the heat spreads throughout the object.

We can implement this simulation using a Numba kernel. Let's start simple by assuming we have a one dimensional object which we'll represent with an array of values. The position of the element in the array is the position of a point within the object, and the value of the element represents the temperature.

../../../numba/cuda/tests/doc_examples/test_laplace.py

Some initial setup here. Let's make one point in the center of the object very hot.

../../../numba/cuda/tests/doc_examples/test_laplace.py

The initial state of the problem can be visualized as:

image

In our kernel each thread will be responsible for managing the temperature update for a single element in a loop over the desired number of timesteps. The kernel is below. Note the use of cooperative group synchronization and the use of two buffers swapped at each iteration to avoid race conditions. See numba.cuda.cg.this_grid() <numba.cuda.cg.this_grid> for details.

../../../numba/cuda/tests/doc_examples/test_laplace.py

Calling the kernel:

../../../numba/cuda/tests/doc_examples/test_laplace.py

Plotting the final data shows an arc that is highest where the object was hot initially and gradually sloping down to zero towards the edges where the temperature is fixed at zero. In the limit of infinite time, the arc will flatten out completely.

image

Shared Memory Reduction

Numba exposes many CUDA features, including shared memory <cuda-shared-memory>. To demonstrate shared memory, let's reimplement a famous CUDA solution for summing a vector which works by "folding" the data up using a successively smaller number of threads.

Note that this is a fairly naive implementation, and there are more efficient ways of implementing reductions using Numba - see cuda_montecarlo for an example.

../../../numba/cuda/tests/doc_examples/test_reduction.py

Let's create some one dimensional data that we'll use to demonstrate the kernel itself:

../../../numba/cuda/tests/doc_examples/test_reduction.py

Here is a version of the kernel implemented using Numba:

../../../numba/cuda/tests/doc_examples/test_reduction.py

We can run kernel and verify that the same result is obtained through summing data on the host as follows:

../../../numba/cuda/tests/doc_examples/test_reduction.py

This algorithm can be greatly improved upon by redesigning the inner loop to use sequential memory accesses, and even further by using strategies that keep more threads active and working, since in this example most threads quickly become idle.

Dividing Click Data into Sessions

A common problem in business analytics is that of grouping the activity of users of an online platform into sessions, called "sessionization". The idea is that users generally traverse through a website and perform various actions (clicking something, filling out a form, etc.) in discrete groups. Perhaps a customer spends some time shopping for an item in the morning and then again at night - often the business is interested in treating these periods as separate interactions with their service, and this creates the problem of programmatically splitting up activity in some agreed-upon way.

Here we'll illustrate how to write a Numba kernel to solve this problem. We'll start with data containing two fields: let user_id represent a unique ID corresponding to an individual customer, and let action_time be a time that some unknown action was taken on the service. Right now, we'll assume there's only one type of action, so all there is to know is when it happened.

Our goal will be to create a new column called session_id, which contains a label corresponding to a unique session. We'll define the boundary between sessions as when there has been at least one hour between clicks.

../../../numba/cuda/tests/doc_examples/test_sessionize.py

Here is a solution using Numba:

../../../numba/cuda/tests/doc_examples/test_sessionize.py

Let's generate some data and try out the kernel:

../../../numba/cuda/tests/doc_examples/test_sessionize.py

As can be seen above, the kernel successfully divided the first three datapoints from the second three for the first user ID, and a similar pattern is seen throughout.

JIT Function CPU-GPU Compatibility

This example demonstrates how numba.jit can be used to jit compile a function for the CPU, while at the same time making it available for use inside CUDA kernels. This can be very useful for users that are migrating workflows from CPU to GPU as they can directly reuse potential business logic with fewer code changes.

Take the following example function:

../../../numba/cuda/tests/doc_examples/test_cpu_gpu_compat.py

The function business_logic can be run standalone in compiled form on the CPU:

../../../numba/cuda/tests/doc_examples/test_cpu_gpu_compat.py

It can also be directly reused threadwise inside a GPU kernel. For example one may generate some vectors to represent x, y, and z:

../../../numba/cuda/tests/doc_examples/test_cpu_gpu_compat.py

And a numba kernel referencing the decorated function:

../../../numba/cuda/tests/doc_examples/test_cpu_gpu_compat.py

This kernel can be invoked in the normal way:

../../../numba/cuda/tests/doc_examples/test_cpu_gpu_compat.py

Monte Carlo Integration

This example shows how to use Numba to approximate the value of a definite integral by rapidly generating random numbers on the GPU. A detailed description of the mathematical mechanics of Monte Carlo integration is out of the scope of the example, but it can briefly be described as an averaging process where the area under the curve is approximated by taking the average of many rectangles formed by its function values.

In addition, this example shows how to perform reductions in numba using the cuda.reduce() <numba.cuda.Reduce> API.

../../../numba/cuda/tests/doc_examples/test_montecarlo.py

Let's create a variable to control the number of samples drawn:

../../../numba/cuda/tests/doc_examples/test_montecarlo.py

The following kernel implements the main integration routine:

../../../numba/cuda/tests/doc_examples/test_montecarlo.py

This convenience function calls the kernel performs some preprocessing and post processing steps. Note the use of Numba's reduction API to take sum of the array and compute the final result:

../../../numba/cuda/tests/doc_examples/test_montecarlo.py

We can now use mc_integrate to compute the definite integral of this function between two limits:

../../../numba/cuda/tests/doc_examples/test_montecarlo.py

Matrix multiplication

First, import the modules needed for this example:

../../../numba/cuda/tests/doc_examples/test_matmul.py

Here is a naïve implementation of matrix multiplication using a CUDA kernel:

../../../numba/cuda/tests/doc_examples/test_matmul.py

An example usage of this function is as follows:

../../../numba/cuda/tests/doc_examples/test_matmul.py

This implementation is straightforward and intuitive but performs poorly, because the same matrix elements will be loaded multiple times from device memory, which is slow (some devices may have transparent data caches, but they may not be large enough to hold the entire inputs at once).

It will be faster if we use a blocked algorithm to reduce accesses to the device memory. CUDA provides a fast shared memory <cuda-shared-memory> for threads in a block to cooperatively compute on a task. The following implements a faster version of the square matrix multiplication using shared memory:

../../../numba/cuda/tests/doc_examples/test_matmul.py

Because the shared memory is a limited resource, the code preloads a small block at a time from the input arrays. Then, it calls ~numba.cuda.syncthreads to wait until all threads have finished preloading and before doing the computation on the shared memory. It synchronizes again after the computation to ensure all threads have finished with the data in shared memory before overwriting it in the next loop iteration.

An example usage of the fast_matmul function is as follows:

../../../numba/cuda/tests/doc_examples/test_matmul.py

This passes a CUDA memory check test <debugging-cuda-python-code>, which can help with debugging. Running the code above produces the following output:

$ python fast_matmul.py
[[ 6.  6.  6.  6.]
[22. 22. 22. 22.]
[38. 38. 38. 38.]
[54. 54. 54. 54.]]
[[ 6.  6.  6.  6.]
[22. 22. 22. 22.]
[38. 38. 38. 38.]
[54. 54. 54. 54.]]

Note

For high performance matrix multiplication in CUDA, see also the CuPy implementation.

The approach outlined here generalizes to non-square matrix multiplication as follows by adjusting the blockspergrid variable:

Again, here is an example usage:

../../../numba/cuda/tests/doc_examples/test_matmul.py

and the corresponding output:

$ python nonsquare_matmul.py
[[ 253.  253.  253.  253.  253.  253.  253.]
[ 782.  782.  782.  782.  782.  782.  782.]
[1311. 1311. 1311. 1311. 1311. 1311. 1311.]
[1840. 1840. 1840. 1840. 1840. 1840. 1840.]
[2369. 2369. 2369. 2369. 2369. 2369. 2369.]]
[[ 253.  253.  253.  253.  253.  253.  253.]
[ 782.  782.  782.  782.  782.  782.  782.]
[1311. 1311. 1311. 1311. 1311. 1311. 1311.]
[1840. 1840. 1840. 1840. 1840. 1840. 1840.]
[2369. 2369. 2369. 2369. 2369. 2369. 2369.]]

Calling a NumPy UFunc

UFuncs supported in the CUDA target (see cuda_numpy_support) can be called inside kernels, but the output array must be passed in as a positional argument. The following example demonstrates a call to np.sin inside a kernel following this pattern:

../../../numba/cuda/tests/doc_examples/test_ufunc.py