comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » vectorising versus loops
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
vectorising versus loops [message #38146] Sun, 22 February 2004 10:16 Go to next message
nasalmon is currently offline  nasalmon
Messages: 33
Registered: August 2003
Member
Does anyone know what the speed increase factor is in IDL programmes
when going from "do loops" to full vectorisation of arrays? I know all
programmes are different and not every process lends itself to
vectorisation. However, there must be some rule of thumb, ie speed
going as a linear function of the number of array elements times some
factor.

Also, are there any tricks to play if you want to vectorise loops that
have IF statement decision in them, or any general rules for neat
vectorisation of looped programmes?

Many thanks,
Neil
Re: vectorising versus loops [message #38207 is a reply to message #38146] Tue, 24 February 2004 02:13 Go to previous messageGo to next message
Norbert Hahn is currently offline  Norbert Hahn
Messages: 46
Registered: May 2003
Member
Craig Markwardt <craigmnet@REMOVEcow.physics.wisc.edu> wrote:

> ;; Make a list of indices
> ii = lindgen(n_elements(x))
>
> ;; Select on X and the index variable
> wh = where(x GE 0 AND ii LT 200)

While this is an example of using the index variable,
this statement by itself can be shortened to

wh = where(x[0:199] ge 0)

Norbert
Re: vectorising versus loops [message #38217 is a reply to message #38146] Mon, 23 February 2004 16:04 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
nasalmon@onetel.net.uk (Neil) writes:
> Craig,
>
> yes many thanks for this valuable information. One of the problems i
> have is that the condition in the WHERE statement has to contain the
> counter in the "do loop", that is basically why i put it in the "do
> loop" to start with. Is there any way i can make the condition depend
> on the counter, ie the vector index.

Yes, you can make a vector of index values with LINDGEN,

;; Make a list of indices
ii = lindgen(n_elements(x))

;; Select on X and the index variable
wh = where(x GE 0 AND ii LT 200)

Good luck,
Craig
Re: vectorising versus loops [message #38441 is a reply to message #38146] Sun, 07 March 2004 09:24 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
nasalmon@onetel.net.uk (Neil) writes:
...
>
> However, there is one small outstanding problem in the vectorisation,
> and this involves vectorisation of a routine that uses the cross or
> vector product, the IDL routine being CROSSP, generating a vector from
> the cross product of two vectors.
>
> Currently i am forced to use this in a loop, as my vectorising
> attempts (see below) have failed. The loop statement below gives the
> correct result (i being the counter of one of the outer loops):
...

Neil, how about using CROSSPN, a vectorized cross product function for
just such purposes?

Craig

P.S. http://cow.physics.wisc.edu/~craigm/idl/idl.html (under Math)

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
Re: vectorising versus loops [message #38723 is a reply to message #38146] Fri, 26 March 2004 11:47 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Neil writes:

> So i was wondering whether there were any other tricks to
> vectorisation that i had missed, that i could use to improve speed?

I presume you have read the "Embodiment of Pure Evil" article:

http://www.dfanning.com/tips/forloops.html

And the follow-up secular advice:

http://www.dfanning.com/tips/forloops2.html

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: vectorising versus loops [message #38725 is a reply to message #38441] Fri, 26 March 2004 11:34 Go to previous messageGo to next message
nasalmon is currently offline  nasalmon
Messages: 33
Registered: August 2003
Member
Craig Markwardt <craigmnet@REMOVEcow.physics.wisc.edu> wrote in message news:<ond67ozi1w.fsf@cow.physics.wisc.edu>...
> nasalmon@onetel.net.uk (Neil) writes:
> ...
>>
>> However, there is one small outstanding problem in the vectorisation,
>> and this involves vectorisation of a routine that uses the cross or
>> vector product, the IDL routine being CROSSP, generating a vector from
>> the cross product of two vectors.
>>
>> Currently i am forced to use this in a loop, as my vectorising
>> attempts (see below) have failed. The loop statement below gives the
>> correct result (i being the counter of one of the outer loops):
> ...
>
> Neil, how about using CROSSPN, a vectorized cross product function for
> just such purposes?
>
> Craig
>
> P.S. http://cow.physics.wisc.edu/~craigm/idl/idl.html (under Math)

Well thanks very much for CROSSSP, it worked like a dream, fitted in
and sailed through perfectly.

In the main programme structure there were loops nested 4 deep, so
vectorising the inner most loop, now leaves only 3 nested loops.
Vectorisation used lots of WHERE statements. As the inner loop dealt
with vectors, inner products of these could be conveniently replaced
with matrix multiplications and transpositions.

However, the improvement factor in speed is only between of one and
five, which is small, but certainly worth having. One of the reasons
why it is this low is because there is lots of stuff in the other
loops and sometimes the inner most loop is very short, may be only 3
elements, while some of the outer loops need to munch thousands of
times.

So i was wondering whether there were any other tricks to
vectorisation that i had missed, that i could use to improve speed? I
can imagine that trying to vectorise the second to inner most loop may
be possible. Are there any strategies or special routines that could
be used in the vectorisation of loops higher in a hierarchy of loops?

many thanks,
Neil
Re: vectorising versus loops [message #39036 is a reply to message #38725] Mon, 12 April 2004 13:14 Go to previous message
stimmins is currently offline  stimmins
Messages: 2
Registered: April 2004
Junior Member
Neil:
You have a common problem with your "busy" inner or outer
loops. To save more time you need to move subsets like x = y(0:1000)
and IF statements out of the inner loops because these can really
dominate the time. If you send me your code (and an idea of how to
construct some dummy data) I will make it run faster!

Sidey Timmins (my first name rhymes with Heidi!)
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: resizing image
Next Topic: IDL jobs anyone?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 04:43:24 PDT 2025

Total time taken to generate the page: 0.88041 seconds