2
votes

i'm working on lattice boltzmann method and i've written a matlab code. I would like to parallelize some parts of the code but i'm new to this so i'd appreciate your help. I'd like to know if it's possible to use the parfor for this part(collision operator):

for i=1:lx
    for j=1:ly
        fork=1:9
           f(k,i,j)=f(k,i,j) .* (1 - omega) + omega .* feq(k,i,j);

         end
    end
end      

I've tried to replace the outermost for loop with a parfor but the code seems to be slower.

any suggestions?

thanks in advance

1
What is the value of lx and ly? I could imagine that parfor will actually be slower due to overhead when the for loop is not that bigMichiel
lx and ly in the code are set to 400. I've tried to use higher values but the iteration times grows more and more.andylbm
I have just tested it for 1000x1000 and the parfor is still slower but relatively it gets closer to for. What I noticed though is that parfor completely floods my memory with this loop, probably because it is passing big matrices around. Maybe that is the issue?!Michiel
it's strange beacause replacing the outermost for loop and running the algorithm with lx=ly=1000 it takes 33 seconds instead of 0.4 seconds with the for loop!! Moreover you are right about the memory usage.andylbm
Just a quick check: you do start a matlabpool with multiple processors available, right? Otherwise using parfor won't do anything useful.nhowe

1 Answers

2
votes

You should be able to do this whole operation with a single line of code without the loops:

f = f.*(1 - omega) + omega .* feq;

On my computer with 2 cores and starting with:

f = rand(9,400,400);
feq = rand(9,400,400);

[lx,ly,lz] = size(f);

omega = rand(1);

your loop takes 0.087933 seconds, the parfor loop takes 1.166662 seconds, and this method takes 0.009388 seconds. If you can, always vectorize your code.