How to use GPU in Matlab scripts

If you have an Nvidia Graphic Card, you must install nvidia drivers to get the best performance.
To save an array to your GPU memory, just save it with gpuArray and to return it to your Ram use gather.

A simple CPU example is the following:

function [time]= cpu(n)
A = rand(n,n);
tic;
B = fft(A);
time = toc;

If we want use the GPU, just add the bold lines:

function [time]= gpu(n)
A = rand(n,n);
% send A to the GPU
A = gpuArray(A);
tic;
B = fft(A);
time = toc
% return  B to the CPU
B = gather(B)

To know how many bytes will need your CPU to storage a gpuArray, before gather it, you can use this equation:

numel(B) * 8 for double-precision arrays 
or
numel(B) * 16 if the array is complex

To checkout the variable size, use the command: whos

If you choise a large value for n, probably you will see the following message:

Out of memory on device. To view more detail about available memory on the GPU, use 'gpuDevice()'. If the problem persists,
reset the GPU by calling 'gpuDevice(1)'.

This means that you don't have sufficient ram on your GPU. To get more ram, an option is run it without GUI. Close your desktop manager and run matlab on terminal.

To show how many Ram are you using in your GPU, you can use:

nvidia-msi
or
nvidia-settings -q GPUUtilization -q useddedicatedgpumemory
or
gpustat

To install gpustat just write on your terminal:

git clone https://github.com/wookayin/gpustat
cd gpustat
sudo python setup.py install

Screenshot: gpustat -cp



Comments

Popular posts from this blog

How to fix Android when developer options are not available for this user

Exception: Could not find a default OpenFlow controller in Mininet

v4l2: open /dev/video0: Permission denied