Re: Code optimization [message #75112] |
Thu, 17 February 2011 11:52  |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
On Thursday, February 17, 2011 2:42:47 PM UTC-5, kisCA wrote:
> Jeremy, I am amazed how you manage so quickly to find a way to
> optimize the code. Do you have some tips to achieve it?
It obviously depends on the detailed situation, but the main thing is to understand what you're trying to do at a reasonably high level. So looking at the last statement in the outer loop and saying "that's a multiplication of two vectors", or looking at the inner 2 loops and saying "that's 2 matrix multiplications". Then it's often easy to see how that maps onto built-in IDL operators and functions.
-Jeremy.
|
|
|
|
|
Re: Code optimization [message #75139 is a reply to message #75134] |
Wed, 16 February 2011 13:48   |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
On Wednesday, February 16, 2011 4:25:39 PM UTC-5, kisCA wrote:
> Jeremy, I understand your disappointment about the subscript in the
> loops, but it's a code I get from a collegue who I suppose wrote it in
> Fortran and then just translate it in IDL.
Yeah, it definitely has that look to it. ;-)
> Jeremy, also, could you explain me a bit more the difference between
> [] and ().
> cov, cs, var_factor are variables.
> I am sorry I just moved to idl 3 month ago from matlab and I really
> have difficulty with "the subscript game" going on here...
> Thanks
No problem! A Long Time Ago, IDL used () for both function calls and array subscripts. But that's not a good idea, since it leads to ambiguity not just in reading code but also in executing it (what happens if you have both a variable and function with the same name?). So [] was introduced for array subscripts. Now you can use either by default, but [] is much preferred.
If you use compile_opt idl2, then you can *only* use [] (as well as a few other useful changes).
-Jeremy.
|
|
|
|
|
|
Re: Code optimization [message #75143 is a reply to message #75142] |
Wed, 16 February 2011 12:51   |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
On Feb 16, 3:38 pm, kisCA <ki...@hotmail.com> wrote:
> Hi there,
>
> I am trying, if it's possible to write thes lines in matrix formalism
> but it seems a bit tricky for me.
>
> FOR I=1,NUM DO BEGIN
> SUMI=0.
> FOR K=1,M1 DO BEGIN
> FOR J=1,M1 DO BEGIN
> SUMI=SUMI+COV(J-1,K-1)*CS(J-1,I-1)*CS(K-1,I-1)
> ENDFOR
> ENDFOR
> VAR_ALPHA(I-1)=SUMI*VAR_FACTOR(I-1)
> ENDFOR
>
> Is anyone got an idea it will be welcome
>
> Cheers
Yeah, it looks like that doesn't really need loops...
First you want to replace the inner loop with a matrix
multiplication of COV and CS - call the result R (make
sure you get the row and columns in the proper order).
Then you want to replace the K loop with total(R*CS,1).
Then you want to replace the I loop with a multiplication
of the last result by var_factor.
Something along those lines should work...
Ciao,
Paolo
Ciao,
Paolo
|
|
|
Re: Code optimization [message #75200 is a reply to message #75116] |
Thu, 17 February 2011 12:52  |
Kenneth P. Bowman
Messages: 585 Registered: May 2000
|
Senior Member |
|
|
In article
<ff8c9322-c3c0-4ed0-b9c2-73ae36c71a16@g10g2000vbv.googlegroups.com>,
kisCA <kis93@hotmail.com> wrote:
> Jeremy, I am amazed how you manage so quickly to find a way to
> optimize the code. Do you have some tips to achieve it?
Here is my short list of priorities for code optimization in IDL:
Optimize where it matters - Don't worry about optimizing code
that does not affect the overall cpu time.
Do binary I/O - NetCDF, HDF, or binary, not ASCII.
Access memory in order - Think about the order in which you are
likely to access your data before you define arrays. This will
make better use of caches in modern cpus.
Use IDL built-in functions and operators - There are many
tricks for using REBIN, REFORM, and HISTOGRAM, but be aware
that they are often memory intensive.
Optimize your innermost loop - Loops are not necessarily bad
in IDL if the calculations within a loop are well optimized.
Don't do unnecessary calculations (e.g., move constants out of loops)
Buy the IMSL library if you need those functions - Your time is
probably much more expensive than an IMSL license.
If you keep those things in mind, you will rarely have to resort
to the code profiler or external modules.
This seems sure to start a discussion. ;-)
Ken Bowman
|
|
|
Re: Code optimization [message #75208 is a reply to message #75116] |
Thu, 17 February 2011 12:09  |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
On Feb 17, 2:42 pm, kisCA <ki...@hotmail.com> wrote:
> Jeremy and Fabzou, thanks for these details I think I got it now, I
> will read about compile_opt IDL2...
>
> Jeremy, I am amazed how you manage so quickly to find a way to
> optimize the code. Do you have some tips to achieve it?
Well technically Jeremy's solution is slightly suboptimal in that
it computes a matrix multiplication for 2 arrays, but only uses the
diagonal elements of the results.
If performance matters to you you may want to avoid doing that
2nd matrix multiplication and just compute the result directly
as I suggested.
Ciao,
Paolo
>
> Cheers
|
|
|