How do I convert the following matlab code loops to batched form?

1 view (last 30 days)
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

Edric Ellis
Edric Ellis on 25 Jun 2018
pagefun expects to operate over "pages" of your multi-dimensional array. These pages are slices taking in only the first two dimensions of the array. In your case, you've made the slices of interest be the final two dimensions, so you need to reverse that. In other words, you need to create:
a = rand(SIZE, SIZE, Num, Num, 'gpuArray');
b = rand(SIZE, SIZE, Num, Num, 'gpuArray');
c = rand(SIZE, SIZE, Num, Num, 'gpuArray');
(Note - I've used the multi-dimensional version of rand, and also requested that the arrays are built directly on the GPU)
Then, you can say simply:
d = pagefun(@mtimes, pagefun(@mtimes, a, b), c);

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!