Thread Subject: Vecorizing for speed(?)

Subject: Vecorizing for speed(?)

From: Simon Preston

Date: 4 Jan, 2008 22:06:45

Message: 1 of 10

Hi all,

I have a function in which I calculate a tensor using nested
loops. I'm trying to vectorize some of the computations for
speed, but I'm finding some odd behaviour which I hope
somebody can explain.

Inside 4 'for' loops cycling through i,j,alpha,beta, when I
replace:

tempsum=0;
for l=1:n
   tempsum = tempsum + ...
      summation_kernel(i,l,alpha)*...
      summation_kernel(j,l,beta);
end output_c(i,alpha,j,beta) = tempsum/n;

with:

output_c(i,alpha,j,beta) = ...
   summation_kernel(i,:,alpha)*...
   summation_kernel(j,:,beta)'/n;

and check using tic toc around these expressions then the
vectorized version is much faster, as expected. But the
strange thing is that the function as a whole runs SLOWER
with this change. Anybody got any ideas why this should happen?

Many thanks, S

Subject: Vecorizing for speed(?)

From: Simon Preston

Date: 14 Jan, 2008 10:21:01

Message: 2 of 10

"Simon Preston" <preston.simon+mathsworks@gmail.com> wrote
in message <flmall$suc$1@fred.mathworks.com>...
> Hi all,
>
> I have a function in which I calculate a tensor using nested
> loops. I'm trying to vectorize some of the computations for
> speed, but I'm finding some odd behaviour which I hope
> somebody can explain.
>
> Inside 4 'for' loops cycling through i,j,alpha,beta, when I
> replace:
>
> tempsum=0;
> for l=1:n
> tempsum = tempsum + ...
> summation_kernel(i,l,alpha)*...
> summation_kernel(j,l,beta);
> end output_c(i,alpha,j,beta) = tempsum/n;
>
> with:
>
> output_c(i,alpha,j,beta) = ...
> summation_kernel(i,:,alpha)*...
> summation_kernel(j,:,beta)'/n;
>
> and check using tic toc around these expressions then the
> vectorized version is much faster, as expected. But the
> strange thing is that the function as a whole runs SLOWER
> with this change. Anybody got any ideas why this should
happen?
>
> Many thanks, S
>

Does anybody have any thoughts on this?

Subject: Vecorizing for speed(?)

From: Yumnam Kirani Singh

Date: 14 Jan, 2008 12:30:18

Message: 3 of 10

Your explaination is not clear to understand your problem. Before giving any explaination to your problem, I think it is necessary to understand your problem. You please explain your problem in a little elaborate way with full codes.

Subject: Vecorizing for speed(?)

From: Anh Huy Phan

Date: 14 Jan, 2008 13:06:01

Message: 4 of 10

Simon Preston:

How many elements do you have in the second dimension?
Try with n greater than 10 elements.
You can feel the difference.

Anh Huy Phan
RIKEN - BSI

Subject: Vecorizing for speed(?)

From: Simon Preston

Date: 14 Jan, 2008 13:17:02

Message: 5 of 10

Yumnam Kirani Singh <kirani.singh@gmail.com> wrote in
message
<7625549.1200313848871.JavaMail.jakarta@nitrogen.mathforum.org>...
> Your explaination is not clear to understand your problem.
Before giving any explaination to your problem, I think it
is necessary to understand your problem. You please explain
your problem in a little elaborate way with full codes.

Maybe including the code in the original post was what made
it unclear - I'll try again.

Suppose OLD CODE and NEW CODE calculate the same quantity
but in a different way. If I do:

function
...
tic;
OLD CODE;
toc;
...
end

function
...
tic;
NEW CODE;
toc;
...
end

then NEW CODE is better. But if I do:

tic;
function
...
OLD CODE;
...
end
toc;

tic;
function
...
NEW CODE;
...
end
toc;

then OLD CODE is better. Why is this?

NEW CODE is a vectorized version of OLD CODE, using vector
products in place of loops.

Thanks, S



Subject: Vecorizing for speed(?)

From: Jos

Date: 14 Jan, 2008 14:21:01

Message: 6 of 10

"Simon Preston" <preston.simon+mathsworks@gmail.com> wrote
in message <fmfnce$760$1@fred.mathworks.com>...
> Yumnam Kirani Singh <kirani.singh@gmail.com> wrote in
> message
>
<7625549.1200313848871.JavaMail.jakarta@nitrogen.mathforum.org>...
> > Your explaination is not clear to understand your problem.
> Before giving any explaination to your problem, I think it
> is necessary to understand your problem. You please explain
> your problem in a little elaborate way with full codes.
>
> Maybe including the code in the original post was what made
> it unclear - I'll try again.
>
> Suppose OLD CODE and NEW CODE calculate the same quantity
> but in a different way. If I do:
>
> function
> ...
> tic;
> OLD CODE;
> toc;
> ...
> end
>
> function
> ...
> tic;
> NEW CODE;
> toc;
> ...
> end
>
> then NEW CODE is better. But if I do:
>
> tic;
> function
> ...
> OLD CODE;
> ...
> end
> toc;
>
> tic;
> function
> ...
> NEW CODE;
> ...
> end
> toc;
>
> then OLD CODE is better. Why is this?
>
> NEW CODE is a vectorized version of OLD CODE, using vector
> products in place of loops.
>
> Thanks, S
>
>
>

and everything represented by the ellipses (...) is exactly
the same between the old and new code?

Jos

Subject: Vecorizing for speed(?)

From: Simon Preston

Date: 14 Jan, 2008 15:04:02

Message: 7 of 10

>
> and everything represented by the ellipses (...) is exactly
> the same between the old and new code?
>
> Jos
>

Yep, that's right.

Subject: Vecorizing for speed(?)

From: Volkan

Date: 14 Jan, 2008 17:24:02

Message: 8 of 10

Hi Simon,

Are you sure you are getting the exact same results from
both your code.

try this:

a = rand(1,200);
a*a' == sum(a.*a)

Usually they are not equal at the least significant bit. If
the rest of your code has precision sensitive decision
nodes, it may be executing in a whole different path with
your vectorized code.

Volkan

Subject: Vecorizing for speed(?)

From: Simon Preston

Date: 14 Jan, 2008 18:28:03

Message: 9 of 10

"Volkan " <volkan@buyukgungor.gmail.com> wrote in message
<fmg5ri$4vd$1@fred.mathworks.com>...
> Hi Simon,
>
> Are you sure you are getting the exact same results from
> both your code.
>
> try this:
>
> a = rand(1,200);
> a*a' == sum(a.*a)
>
> Usually they are not equal at the least significant bit. If
> the rest of your code has precision sensitive decision
> nodes, it may be executing in a whole different path with
> your vectorized code.
>
> Volkan

Hi Volkan,

Good thinking, but no, none of the code in the ellipsis runs
conditionally on the value calculated in OLD CODE/NEW CODE

Thanks for the suggestion. But it's still a mystery

Subject: Vecorizing for speed(?)

From: Loren Shure

Date: 15 Jan, 2008 14:16:30

Message: 10 of 10

In article <fmg9ji$ck8$1@fred.mathworks.com>,
preston.simon+mathsworks@gmail.com says...
> "Volkan " <volkan@buyukgungor.gmail.com> wrote in message
> <fmg5ri$4vd$1@fred.mathworks.com>...
> > Hi Simon,
> >
> > Are you sure you are getting the exact same results from
> > both your code.
> >
> > try this:
> >
> > a = rand(1,200);
> > a*a' == sum(a.*a)
> >
> > Usually they are not equal at the least significant bit. If
> > the rest of your code has precision sensitive decision
> > nodes, it may be executing in a whole different path with
> > your vectorized code.
> >
> > Volkan
>
> Hi Volkan,
>
> Good thinking, but no, none of the code in the ellipsis runs
> conditionally on the value calculated in OLD CODE/NEW CODE
>
> Thanks for the suggestion. But it's still a mystery
>
>

are you reusing variables in the second code so that the memory
allocation step is avoided. assign to new variables each time for a
real comparison perhaps. or preallocate for both before the timing.

--
Loren
http://blogs.mathworks.com/loren/

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
vectorize Ned Gulley 21 Jan, 2008 00:00:14
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com