Jacket by Example

From Jacket Wiki

Jump to: navigation, search
Back to Jacket Basics, Forward to GFOR Usage

To start using Jacket with MATLAB, you need to add path to the <jacket_root>/engine directory in your Jacket installation. In this page, a few short examples are given to help you get acquainted with Jacket.

Please refer to the note about timing Jacket code before getting started.

Contents

Something Familiar: Multiplying Two Matrices

Let us get started by using Jacket for GPU matrix multiplication.

addpath <jacket_root>/engine
X = gdouble( magic( 3 ) );
Y = gones( 3, 'double' );
A = X * Y
 
A =
 
    15    15    15
    15    15    15
    15    15    15

Note that the only difference between this code and CPU-based MATLAB code is the introduction of the GDOUBLE casting functions. GONES creates a matrix of ones on the GPU.

Approximating Pi

In this section, we provide a quick example to approximate Pi on the GPU using Jacket. This example is included in the <jacket_root>/examples directory:

addpath <jacket_root>/engine
NSET = 1000000;
X = grand( 1, NSET );
Y = grand( 1, NSET );
distance_from_zero = sqrt( X.*X + Y.*Y );
inside_circle = (distance_from_zero <= 1);
pi = 4 * sum(inside_circle) / NSET
 
pi =
 
    3.1421

GRAND creates psuedo random numbers on the GPU and is equivalent in functionality with RAND. This example also demonstrates Jacket's ability to work with logical indices.

Running Jacket Examples

Now, let us have a look at one of the examples packaged with Jacket. In this section the steps required to run the fdtd_example will be discussed. First you will need to addpath to the fdtd_example directory which is located in <jacket_root>/examples/fdtd_example

This particular example uses the new Graphics Library (available from Jacket 1.7). You can see the demo by running the following code:

addpath <jacket_root>/engine
addpath <jacket_root>/examples/fdtd_example
fdtd_gpu

You should be able to see a small demo which looks like the following image.

Fdtd screenshot.png

You can run other examples by replacing fdtd_example with name_of_example in the appropriate places!

Typical Workflow

The recommended workflow to Jacketize MATLAB code is shown in the following figure:

Workflow.png

The first step is to profile the code using the MATLAB Profiler. The results of the profiler can help determine where the program is spending most of its time. The next step is to cast the input variables of slow code segments to GPU data types provided by Jacket (e.g. GDOUBLE) as shown in the above example. The subsequent MATLAB operations that are performed on the GPU matrices will automatically be executed on the GPU. For most data parallel computations this is all that is necessary to get performance boost from the GPU. However, there are several factors that may affect Jacket's performance and if the desired speedup is not achieved, additional analysis and some modification of the original M-code may help optimize performance. The following case studies are useful examples depicting two real Jacketization workflows.

For more great examples and case studies from a Jacket customer and power user, visit Torben's Corner.

Read more tips on converting your MATLAB code to Jacket.

Continue to GFOR Usage
Personal tools