How do I convert the following matlab code loops to batched form?
Show older comments
I want to multiply many small matrices together on the GPU.
First I initialize some dummy data and send it to the GPU:
clear;clc;
Num=50;%the array is NumxNum blocks
SIZE=5;%each block is SIZExSIZE
%initialize dummy matrices:
for n=1:Num
for m=1:Num
a(:,:,n,m)=rand(SIZE);
b(:,:,n,m)=rand(SIZE);
c(:,:,n,m)=rand(SIZE); %initialize small matrices (Num x Num entries, each entry is a SIZExSIZE matrix).
end
end
%initialize dummy array:
range=4;%
for n=1:Num
Ind(n,:)=randi(range,1,4);
end
%send data to GPU:
a=gpuArray(a);
b=gpuArray(a);
c=gpuArray(a);
Ind=gpuArray(ind);
Then, I perform LOTS (Num^2*range^2) small matrix multiplications:
%perform lots of small matrix multiplications
for n=1:Num
for m=1:Num
for i=Ind(n,:)
for j=Ind(m,:)
d(:,:,n,m)=a(:,:,n,i)*b(:,:,i,j)*c(:,:,j,m);
end
end
end
end
Each of the d(:,:,n,m) above is completely independent from the others so the operation above should be embarrassingly parallelizeable, i.e. each cuda core should be able to handle a different d(:,:,n,m).
The problem is that I don't know how to reformulate the loops above in terms of Matlab's pagefun. How do I replace the above loop with batched operations in matlab?
Accepted Answer
More Answers (0)
Categories
Find more on GPU Computing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!