How can I do faster a loop using the GPU? (GPU: RTX 3070)

6 views (last 30 days)
n=700;
A=rand(n,n);
%%
Ms=gpuArray(M);
As=gpuArray(A);
% First questions: this loop takes to much time. How can I do this loop using the cores available in the GPU
tic
for i=1:100;
AA(:,:,i)=As; %this is just an example, in practice, As is different for each i of the loop
MM(:,:,i)=Ms; %Ms is the same for all the indexes i.
end
toc
%I m using this array to make 2 multiplications and a pointwise produt
% This are the multiplications
AGpage=gpuArray(AA);
Fs=gpuArray(fftshift(dftmtx(m)));%
Gs=gpuArray(fftshift(dftmtx(n)));
CG=pagefun(@mtimes,AGpage,Fs);
Hs=pagefun(@mtimes,Gs,CG);
% what I need next is this result: sum(Ms.*Hs,[2 1]);
%But, I use a reshape to use all the cores in the pointwise multiplication
Ms2=gpuArray(MM);
MMT=reshape(Ms2,l,l,49*100);
ST=reshape(Hs,l,l,49*100);
for iter=1:30
aG2=sum(MMT.*ST,[2 1]);
%%
% I need to go back to recover the sum of the pointwise multiplication sum(Ms.*Hs,[2 1]);
%First questions: But now, again this loop is too slow
for index=1:100
tramo=aG2((index-1)*49+1:49*index);
aG3(index)=sum(tramo);
end
%how can I do faster a loop using the GPU?

Answers (1)

Ishu
Ishu on 5 Apr 2024 at 8:52
Hi Sandra,
I understand that you want to make the comutation for "for" loop faster. To make the computation faster you can use Parallel computation in MATLAB. For this you have to replace "for" loop with "parfor", this will execute for-loop iterations in parallel on workers and making your computations faster.
In addition to this, you can improve code execution time by preallocating the maximum amount of space required for the arrays.
%Example
x = 0;
for k = 2:10000
x(k) = x(k-1) + 5;
end
% preallocating x for faster computations
x = zeros(1,10000);
for k = 2:10000
x(k) = x(k-1) + 5;
end
For more information you can refer below documentation:
Hope it helps!

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!