0

I have a script that I'm running, and at one point I have a loop over n objects, where I want n to be fairly large.

I have access to a server, so I put in a parfor loop. However, this is incredibly slow compared with a standard for loops.

For example, running a certain configuration ( the one below ) with the parfor loop on 35 workers took 68 seconds, whereas the for loop took 2.3 seconds.

I know there's stuff to do with array-broadcasting that can cause issues, but I don't know a lot about this.


n = 20;
r = 1/30;

tic

X = rand([2,n-1]);
X = [X,[0.5;0.5]];
D = sq_distance(X,X);
A = sparse((D < r) - eye(n));

% Infected set I
I = n;
[S,C] = graphconncomp(A);
compnum = C(I);
I_new = find(C == compnum);
I = I_new;

figure%('visible','off')
gplot(A,X')
hold on
plot(X(1,I),X(2,I),'r.')
hold off
title('time = 0')
axis([0,1,0,1])

time = 0;
t_max = 10; t_int = 1/100;
TIME = 1; T_plot = t_int^(-1) /100;

loops = t_max / T_plot;
F(loops) = struct('cdata',[],'colormap',[]);
F(1) = getframe;

% Probability of healing in interval of length t_int
heal_rate = 1/3; % (higher number is faster heal)
p_heal = t_int * heal_rate;
numhealed = 0;

while time < t_max
    time = time+t_int;
    steps = poissrnd(t_int,[n,1]);
    parfor k = 1:n
        for s = 1:steps(k)
            unit_vec = unif_unitvector;
            X_new = X(:,k) + unit_vec*t_int;
            if (   X_new <  1 == ones(2,1) ) ...
               & ( X_new >  0 == ones(2,1) )
                   X(:,k) = X_new;
            end
        end
    end
    D = sq_distance(X,X);
    A = sparse((D < r) - eye(n));
    [S,C] = graphconncomp(A);

    particles_healed = binornd(ones(length(I),1),p_heal);
    still_infected = find(particles_healed == 0);
    I = I(still_infected);
    numhealed = numhealed + sum(particles_healed);

    I_new = I;
%     compnum = zeros(length(I),1);
    for i = 1:length(I)
       compnum = C(I(i));
       I_new = union(I_new,find(C == compnum));
    end
    I = I_new;

    if time >= T_plot*TIME        
        gplot(A,X')
        hold on
        plot(X(1,I),X(2,I),'r.')
        hold off
        title(sprintf('time = %1g',time))
        axis([0,1,0,1])

%         fprintf('number healed = %1g\n',numhealed)
        numhealed = 0;

        F(TIME) = getframe;
        TIME = TIME + 1;
    end
end

toc
Sam OT
  • 420
  • 1
  • 4
  • 19
  • Parallel computing is faster than serial computing *only sometimes*, and it depends highly in the task itself. If parallel computing woudl always be faster, we would just GPUs, and would have no CPU in our PCs – Ander Biguri Oct 13 '17 at 12:42
  • Are you sure this isn't just the overhead of setting up the parallel pool? What happens if you use fewer workers? Are you using `parpool` to start up the pool before running your code, or just letting MATLAB start it automatically? – nekomatic Oct 13 '17 at 13:19
  • @nekomatic I already have the parallel pool running before I run the script... :/ -- in particular, if I try to run two consecutively, then they're both slow – Sam OT Oct 13 '17 at 14:45
  • @AnderBiguri Indeed, as hinted at by my comment on array broadcasting. However here it seems strange that it should be the case. It's only very basic calculations, and the for loop is big. As a test, change `n=20` to `n=500`. Runs ok, but a bit slow, in serial -- *very* slow in parallel. – Sam OT Oct 13 '17 at 14:47
  • Array broadcasting refers to the case where each worker needs the entirety of the array to do the maths. Memory transfer is *very slow* in parallel computing, thus if that is the case, the total computation time gets shadowed by the amount of time that the data transfer needs. A good parallel algorithm will make sure that each worker only uses a small portion of the total data. – Ander Biguri Oct 13 '17 at 14:53
  • Yeah, I understand that (and that's what I was asking about). Is the issue likely to be that I'm broadcasting `X` which is of size `2n`? (Still, when `n=20` surely this isn't too bad?) I only use the `k`-th pair of `X` for the `k`-th worker; can you see a way that I can stop the whole lot broadcasting? – Sam OT Oct 13 '17 at 17:01

0 Answers0