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);
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);
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 
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-settings -q GPUUtilization -q useddedicatedgpumemory

To install gpustat just write on your terminal:

git clone
cd gpustat
sudo python install

Screenshot: gpustat -cp


Popular posts from this blog

from: can't read /var/mail/ in Python scripts

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

Fix "Set scan parameters failed: Input/output error" on Ubuntu 16.04