-
Notifications
You must be signed in to change notification settings - Fork 7
/
main_gpu.m
54 lines (48 loc) · 1.32 KB
/
main_gpu.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
%----------------------------------------------------------------
% File: main_gpu.m
%----------------------------------------------------------------
%
% Author: Marek Rychlik ([email protected])
% Date: Mon Dec 9 07:43:04 2024
% Copying: (C) Marek Rychlik, 2020. All rights reserved.
%
%----------------------------------------------------------------
% Illustrates the use of GPU to find SVD.
targetSize=[128,128];
location = fullfile('lfw');
svd_cache = fullfile('cache','svd.mat');
disp('Creating image datastore...');
imds = imageDatastore(location,'IncludeSubfolders',true,'LabelSource','foldernames',...
'ReadFcn', @(filename)imresize(im2gray(imread(filename)),targetSize));
montage(preview(imds));
disp('Reading all images');
A = readall(imds);
B = cat(3,A{:});
imshow(B(:,:,1))
D = prod(targetSize);
B = reshape(B,D,[]);
disp('Normalizing data...');
B = single(B)./256;
%[N,C,SD] = normalize(B);
N=gpuArray(B);
if existsOnGPU(N)
disp('Successfully moved image data array to GPU')
end
disp('Finding SVD...');
tic;
[Ugpu,Sgpu,Vgpu] = svd(N);
toc;
disp('Status of arrays:')
if existsOnGPU(Ugpu)
disp('U is on GPU.')
end
if existsOnGPU(Vgpu)
disp('V is on GPU.')
end
if existsOnGPU(Sgpu)
disp('S is on GPU.')
end
U = gather(Ugpu);
V = gather(Vgpu);
S = gather(Sgpu);
plot(log(diag(S)),'.');