The Parfor-Loop:

The runtime of a for-loop can sometimes take too long to be practical.

This is mainly due to the sequential mechanism used by for-loops.

To counter this, MATLAB provides a parallel processing option that can be

applied to for-loops: Parfor-loops.

A "parfor-loop" is a parallel for-loop, which means that each iteration of the loop

can be completed independently of the other iterations. This means that recursive

algorithms aren't well-suited for parallelization.

___________________________________________________________________________________

e.g.

Variable 'k' is evaluated recursively; its position in the loop's queue must be known.

for i=1:n

k=k+i;

end

____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____

Variable 'k' is evaluated independently of its position in the loop's queue.

parfor i=1:n

k=i;

end

___________________________________________________________________________________

Utilizing a parallel loop can be a great speed advantage. To demonstrate this, we will time

computationally intensive tasks using a for-loop and a parfor loop.

First, begin by saving this image to your current MATLAB directory:

A=imread('fludrop.jpeg');

After this, take a look (just to see what you're working with). This is a good idea to do after every transformation:

imshow(A)

Now we're going
to convert the image to grayscale (remember that RGB images
are

3-dimensional, and we would like to simplifiy the process by transforming it into 2 dimensions):

3-dimensional, and we would like to simplifiy the process by transforming it into 2 dimensions):

B=rgb2gray(A);

imshow(B)

imshow(B)

Now, we will
create a binary image by using a logical operator to impose a global
threshold:

C=B>T;

imshow(C)

(where ' T ' is a scalar threshold value)

imshow(C)

(where ' T ' is a scalar threshold value)

This should
return a black and white version of the original. Finally, our
compuationally-intensive task awaits.

We will design a for-loop to count the total amount of 1's in the binary image 'C'.

We will design a for-loop to count the total amount of 1's in the binary image 'C'.

k=0;

for i=1:length(C(:,1))

for j=1:length(C(1,:))

(fill in the blank)

end

end

This summation can be completed relatively fast once, but what if we needed to complete this loop...

...>1000 times?

(let n>1000)

for i=1:n

k=0;

for i=1:length(C(:,1))

for j=1:length(C(1,:))

(fill in the blank)

end

end

end

^^^ Using tic & toc, we can find this to be complete in a matter of seconds

See below for timing code:

tic

for i=1:n

k=0;

for i=1:length(C(:,1))

for j=1:length(C(1,:))

(fill in the blank)

end

end

end

toc

___________________________________________________________________________________

Let's compare this to the time required to

complete >1000 runs in-parallel

To begin a parallel for-loop, you must start a parallel pool. This can be done with the button

in the bottom-left corner of the MATLAB window:

^^^^Click here

Select "Start parallel pool"

Next, select the only other option "Parallel Preferences"

Make sure that 4 workers are specified !!!

______________________________________________________________________________________________________

Now let's time the same loop, but in parallel:

(let n>1000)

%% (PREVIOUS FOR-LOOP)

tic

for i=1:n

k=0;

for i=1:length(C(:,1))

for j=1:length(C(1,:))

(fill in the blank)

end

end

end

toc

%% (PARFOR-LOOP)

tic

parfor i=1:n

k=0;

for i=1:length(C(:,1))

for j=1:length(C(1,:))

(fill in the blank)

end

end

end

toc

**note the difference in runtime!!**

DON'T FORGET ABOUT THE EXTRA CREDIT OPPORTUNITY!!!!!

Can you build a code that can reliably count the circles in today's example image?

Here is an example of code that works (this is the same code shown in today's slides):

diam=2;

T=45;

vert_size=43;

horiz_size=22;

n=2;

A=imresize(rgb2gray(imread('fludrop.jpeg')),[vert_size,horiz_size])>T;

w=size(A,2);

d=0;

dd=0;

for x = 2:w

for i = diam+1:vert_size

if A(i -diam:i, x - 1)==1

A(i -diam:i, x )=0;

end

end

for i = 2:vert_size

if A(i , x )~=0

if A(i -1, x )==A(i , x )

d=d+1;

if d==diam

d=0;

dd=dd+1;

end

else, d=0;

end

end

end

end

dd