Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of using Matlab with GPUS and doing all computation on the GPU
%Brock Palen brockp@umich.edu 1/2014
%To compare against a single core run:
% hwloc-bind core:0 matlab -r gen\(1e4\) #first core
% hwloc-bind socket:0 matlab -r gen\(1e4\) #first socket all cores
function gen(dim)
if ischar(dim)
dim=str2num(dim);
end
%preallocate
a = zeros(dim);
%wakeup the gpu takes ~12 seconds
II = gpuArray.eye(10,'int32');
%slow way, ommitted
%disp('Time to use loop for allocation')
%tic;
%for x = 1:dim,
% for y = 1:dim,
% a(x, y) = sin(6 * pi * (x^2 + y^2));
% end
%end
%toc
disp('Vector Form')
tic;
x = 1:dim;
y = 1:dim;
[X, Y] = meshgrid(x,y);
a = sin(6 * pi * (X.^2 + Y.^2));
fftn(a);
toc
disp('GPU Form')
tic;
Gx = gpuArray(1:dim);
Gy = gpuArray(1:dim);
[GX, GY] = meshgrid(Gx, Gy);
Ga = sin(6 *pi * (GX.^2 + GY.^2));
clear GX GY Gx Gy;
fftn(Ga);
toc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment