When I run the command: compute(1,1100000)I get back the message:
>> Elapsed time is 12.940753 seconds.
Now let's try running the function in parallel. First we must open a Matlab pool with the workers we want. To do that simply write:
matlabpool open i % Opens a matlab pool with i workers.After that we just need to substitute the for in line 11 by a parfor. Running the command compute(1,1100000)again I get the message:
>> Elapsed time is 3.713284 seconds.
That's a major improvement! Our parallel code is around 3.5 times faster than his serial equivalent! Let's plot the time difference and ratio of our function for different values of in2 and with in1 fixed ( in1= 1 ).
As we can see the ratio converges to a value around 3.5/3.6, which is pretty good. As we increase the number of simulations the difference between the duration of the simulations also increase, which makes this function a good subject of parallelization.
Obviously the time it takes for the function to run depends not only in our code but also in the processor we're using. Unless you have an identical processor you will achieve different time differences. However the ratio between a serial and parallel execution will always be the same!
I wrote the ratio as the time it takes for the parallel code to run divided by the time it takes for the serial code to run. However it is more common to do the inverse. We define the ratio between the execution time of the serial code (Ts) divided by the execution time of the parallel code with n workers (Tn) as the speedup of the parallel algorithm.
speedup = Ts / TnThis is an important property of the code as it doesn't depend on the type of processors. as long as the number of processors is the same. Try to run the code above with 4 workers and I'm sure you will achieve a similar speedup.
If you feel like trying to implement the parfor loop in some code I suggest you try to write a code, both in series and in parallel, that tests Girko's circular law. Girko's circular law states the following:
You don't need to understand the math behind it. All you need to test is the value of
y = max(svd(randn(N)));for a big set of integers N. Try N between 1 and 800. The code is pretty simple. All you need is a loop and a vector y to store the values calculated in the loop. Write it in parallel and in series and find out the difference between of time between both executions and, more importantly, its speedup.

No comments:
Post a Comment