Once again I won't lose time explaining the code. It's very simple and I'm sure no one will have doubts understanding it. Here's the original code:
hit = 0;If I run this command the script will take around 15/16 seconds to run. Let's try to beat that.
tic
for n=1:N
point = rand(1,2);
if norm(point)<=1
hit = hit+1;
end
end
toc
Rewriting the function will require more work than with parfor. With parfor we simply had to replace a word and the code ran smoothly. With spmd we have to explicitly (and manyally) tell what iterations are assigned to each lab. Well, let's get started, shall we?
To start let's open our Matlab pool, in my case with 4 workers, otherwise our work will be fruitless.
matlabpool open 4Now we need to tell each available lab their task, which in this case will be the iterations they'll be responsible for. To do that we can use conditions as switch/case or if/elseif to tell each worker to take care of one quarter of the total iterations, N. It is the easiest way to go and the code is easier to interpret. Since it is so simple I'll leave it to you to write it. I, on the other hand, will write the same code but using only one for loop inside the spmd block, as follows:
N = 4000000;Since each lab has a different labindex and the step is constant the labs will take care of different quarters of the iterations. If you got confused by this I suggest you read this post.
tic
spmd
for n=labindex:numlabs:N
point = rand(1,2);
if norm(point)<=1
hit = hit + 1;
end
end
end
toc
As we run the script we see that it takes around 7 seconds, while the serial version takes around 15/16 seconds to run. This is a major improvement. It is about 2.2 times faster. It is also around the same time we got with parfor. As in our previous examples this ratio is constant, meaning that as we increase the dimension of the simulation the difference between the times of execution will also increase.
I won't lose much more time with this, as this code is not very challenging. In this problem there's really no need to use spmd. A parfor loop achieves identical results and is much simpler to implement.
As a general rule whenever you can use parfor you can also use spmd, it just isn't worth it most times.
Cheers!
Hi Daniel, thanks for this example. Can you comment on why the speedup is 2.2x rather than 4x? I understand there's some overhead associated with setting up spmd, but it sounds like the overhead scales with the size of the problem, rather than being a fixed constant?
ReplyDelete-Adam
Nevermind, read your parfor page, very clear explanation. Excellent work!
ReplyDelete