MATLAB GPU Programming

From TAMUQ Research Computing User Documentation Wiki
Jump to navigation Jump to search


What is GPU processing?

  1. Using Graphics processing unit to accelerate performance of your applications.
  2. Offloading compute intensive tasks of your application/program to the GPU and rest of the code runs on CPU.
  3. GPU has hundreds of smaller cores which are designed to achieve parallel performance.

CPU vs GPU

CPU GPU
General purpose computation Specialized for Graphics
Optimized sequential code Optimized data parallel applications
May contain 2,4 or 8+ cores Many cores (several hundreds or thousands)
Large low latency memory High bandwidth, high-latency memory

General purpose computing on GPU

Native purpose of GPU is to render 2-D or 3-D images for different applications. By offloading intensive graphics processing tasks to GPU, we can spare more time for CPU to work on compute intensive tasks. GPU is placed directly on motherboard or on the graphics board which is attached via PCI to main board. When we talk about General Purpose Computation on GPU (GPGPU), we use GPU for other tasks than rendering the images only. Most of the compute intensive tasks are offloaded to GPU for greater performance of application. To achieve this task, we use different drives and libraries to access GPU. Not all GPU's can be used for GPU Processing. In the next section we will discuss which applications are suitable to run on GPU.

Ideal applications for GPGPU

  1. Data parallel applications.
  2. High arithmetic intensity.
  3. Limited communication between tasks.

Profiling MATLAB code

  1. To improve performance of your code, its very help to figure out the portion of code which is consuming most of the time.
  2. Profiling helps you determine where your code spend its time. With this information you can identify bottlenecks and significantly improve performance of your code by introducing more efficient programming techniques.
  3. Mathworks has published a very good tutorial on how to get started with Profiling. MATLAB Code profiling
  4. Once you are done profiling your code and have identified the bottlenecks, the next task is to figure out how to improve the performance.
  5. Using GPU with MATLAB is one of many methods which can help in accelerating your MATLAB code.

Setting up environment for MATLAB GPU

  1. If you want to use MATLAB with GPU support on your desktop or laptop machine, make sure you have compatible GPU installed. You can see the compatible GPU list here. In this case you may skip to next session Getting started with MATLAB GPU Prog.
  2. TAMUQ HPC Cluster users can request for access to Graphics queue, which will allow them to access GPU resources. Please email helpdesk if you require access.
  3. You need to submit an interactive job requesting a Graphics node, do the following
  4. qsub -IV -q graphics -l walltime=08:00:00 -l select=1:ncpus=1:mem=4Gb
    
  5. Open MATLAB session interactively
  6. module load matlab/2014a
    matlab &
    

Getting started with MATLAB GPU Programming

Once you have submitted an interactive job and have started the MATLAB session, you can follow section below;

View Technical details of GPU

First lets see the technical details of the GPU device you have in your machine, issue “gpuDevice” command on MATLAB Command line window. It will give you detailed information about GPU device.

>> gpuDevice
ans = 
  CUDADevice with properties:
                      Name: 'Tesla K20c'
                     Index: 1
         ComputeCapability: '3.5'
            SupportsDouble: 1
             DriverVersion: 5.5000
        MaxThreadsPerBlock: 1024
          MaxShmemPerBlock: 49152
        MaxThreadBlockSize: [1024 1024 64]
               MaxGridSize: [2.1475e+09 65535]
                 SIMDWidth: 32
               TotalMemory: 5.0327e+09
                FreeMemory: 4.9179e+09
       MultiprocessorCount: 13
              ClockRateKHz: 705500
               ComputeMode: 'Default'
      GPUOverlapsTransfers: 1
    KernelExecutionTimeout: 0
          CanMapHostMemory: 1
           DeviceSupported: 1
            DeviceSelected: 1