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

2 views (last 30 days)
Sandra Martinez on 16 May 2022
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?

### Categories

Find more on GPU Computing in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!