Speed up gain by GPU parallel processing
Afficher commentaires plus anciens
Hi,
I tried to run the following code using both GPU and CPU parallel processing. The amount of speed up I achieved after running this code using GPU parallel processing was significantly lower than what I could have using CPU parallel processing. Does any one know how MATLAB's builtin functions such as "find" can gain an amazing speed up by running on GPUs, but I can't get a comparable speed when I run the code below on GPU?
I appreciate any help you can provide in this direction.
N = 5000000;
Data = rand(N,3);
% V and F are the vertices and faces on a point cloud geomtery,
% respectively. They are connected to each other. We cannot recreate them
% using arbitrary random numbers.
%V = an array of the size (4000000,3);
%F = an array of the size (7000000,3);
SearchWindowSize = 0.02;
Data = gpuArray(Data);
V = gpuArray(V);
F = gpuArray(F);
for i=1:N
C = Data(i,:);
IDsInWindow = find((abs(V(:,2)-C(2))<SearchWindowSize)&(abs(V(:,3)-C(3))<SearchWindowSize)&(V(:,1)>=C(1)));
[a1,b1]=ismember(F(:, 1),IDsInWindow);
[a2,b2]=ismember(F(:, 2),IDsInWindow);
[a3,b3]=ismember(F(:, 3),IDsInWindow);
aT=a1+a2+a3;
f1 = find(aT>0);
F_in = F(f1,:);
if(isempty(F_in)==0)
inter_mat = [];
for j = 1:size(F_in, 1)
F_V = V(F_in(j, :), :);
if((C(2)<=max(F_V(:,2)))&(C(2)>=min(F_V(:,2)))&(C(3)<=max(F_V(:,3)))&(C(3)>=min(F_V(:,3))))
u = ((F_V(2,3) - F_V(3,3))*(C(2) - F_V(3,2)) + (F_V(3,2) - F_V(2,2))*(C(3) - F_V(3,3))) / ((F_V(2,3) - F_V(3,3))*(F_V(1,2) - F_V(3,2)) + (F_V(3,2) - F_V(2,2))*(F_V(1,3) - F_V(3,3)));
v = ((F_V(3,3) - F_V(1,3))*(C(2) - F_V(3,2)) + (F_V(1,2) - F_V(3,2))*(C(3) - F_V(3,3))) / ((F_V(2,3) - F_V(3,3))*(F_V(1,2) - F_V(3,2)) + (F_V(3,2) - F_V(2,2))*(F_V(1,3) - F_V(3,3)));
w = 1 - u - v;
in = u >= 0 && v >= 0 && w >= 0 && u <= 1 && v <= 1 && w <= 1;
if in
inter_mat(j) = 1;
end
end
end
inter_Count = sum(inter_mat);
if mod(inter_Count, 2) == 1
IDs_All(i,1) = 1;
else
IDs_All(i,1) = 0;
end
end
end
2 commentaires
Walter Roberson
le 21 Août 2023
Réponse acceptée
Plus de réponses (1)
The operations that benefit from GPU acceleration are vectorized matrix operations and commands, e.g.,
There appears to be little if any vectorization in your current code.
2 commentaires
Vectorize.
Unfortunately this Answers facility does not have access to a GPU, and unfortunately I would have to boot one of my systems into an old operating system and old MATLAB version to get GPU access.
GPU is not always faster: transfering data back and forth with the CPU slows it down a lot.
AA= rand(1000000,1);
tic
Flag = AA > 0.1;
toc
clear Flag
tic
for i=1:size(AA,1)
if(AA(i,:)>0.1)
Flag(i) = true;
else
Flag(i) = false;
end
end
toc
gpu = gpuDevice();
tic
AA_G = gpuArray(AA);
FlagG = AA_G > 0.1;
Flag = gather(FlagG);
toc
clear Flag
tic
for i = 1 : size(AA_G,1);
if(AA_G(i,:)>0.1)
Flag(i) = true;
else
Flag(i) = false;
end
end
wait(gpu);
toc
Catégories
En savoir plus sur GPU Computing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!