For indexing in gpuArray, how can I improve my code?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
zihao chang
am 16 Mär. 2021
Bearbeitet: Joss Knight
am 18 Mär. 2021
function dists = compute_distances(obj,X)%X is a matrix with 10000 X 1024
num_test=size(X, 1);
num_train=size(obj.x_train,1);
temp_dists=double(zeros(num_test,num_train));
gpu_X=gpuArray(X);
gpu_xtrain=gpuArray(obj.x_train);%obj.x_train is a matrix with 1000 X 1024
for i = 1:num_test
for j =1:num_train
tmp=X(i,:)-obj.x_train(j,:);
temp_dists(i,j)=sqrt(sum(tmp.*tmp));
end
end
dists=temp_dists;
end
My CPU code executed in 5 mins.
The GPU code executed in 25mins.
I am trying to calculate the distance, but I noticed that I can use gpuArray to improve the speed.
I have no idea why it doesn't work.
0 Kommentare
Akzeptierte Antwort
Joss Knight
am 18 Mär. 2021
Bearbeitet: Joss Knight
am 18 Mär. 2021
Secondly, say no to loops:
Y = reshape(obj.x_train',1,[],num_train);
delta = X-Y;
dists = squeeze(sqrt(sum(delta.*delta,2)));
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu GPU Computing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!