<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic In your latest examples, the in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050275#M115014</link>
    <description>&lt;P&gt;In your latest examples, the value of `variable` is never used.&amp;nbsp; If the compiler is clever enough (and often with these sorts of examples ... it is) it will recognise that and then decide that it doesn't actually need to execute the assignment to `variable`.&amp;nbsp; It may then realize that the loops aren't doing anything useful, and with progressive analysis replace them with simple assignments to i, j [and k].&amp;nbsp; It may then recognize that i, j and k aren't being used, and get rid of them too.&amp;nbsp; All up, your code could end up being...&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;call cpu_time(tstart)
call cpu_time(tend)
write (*,*) tend-tstart&lt;/PRE&gt;

&lt;P&gt;which is possibly not all that interesting to test.&lt;/P&gt;

&lt;P&gt;As stated elsewhere - different code will result in different optimization outcomes.&amp;nbsp; From an outsiders point of view it is very difficult to predict what the compilers optimizer will do, beyond simple cases, and what it does may change with version and other compile options.&amp;nbsp; So if a bit of code is that important that you care deeply that it be optimized most effectively, you need to measure that specific code, in a manner that tests that specific code in a realistic fashion.&amp;nbsp; In your opening post you say you've done that measurement... so you know the answer to your question.&lt;/P&gt;

&lt;P&gt;But I would be getting pretty desperate to be embarking on hand optimization like this.&amp;nbsp; As a very good general rule, write the code that is easiest to write/easiest to understand/easiest to maintain, and make optimization a problem for the optimizer.&amp;nbsp; Is your code easier to write/understand/maintain with the intermediate variable?&amp;nbsp; That's your call.&lt;/P&gt;</description>
    <pubDate>Tue, 16 Sep 2014 02:37:00 GMT</pubDate>
    <dc:creator>IanH</dc:creator>
    <dc:date>2014-09-16T02:37:00Z</dc:date>
    <item>
      <title>optimization do loop</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050271#M115010</link>
      <description>&lt;P&gt;I am wondering if in the following DO-loop, the introduction of the new INTEGER variable upper_index in place of the two identical calculations x_index(i,l) + N will have a benefit to the performance?&amp;nbsp; I have already timed this loop and compared it to the original loop, and the runtime ratio indicates ~1.40 performance gain.&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;  DO i = 1,N
    upper_index = x_index(i,l)+N
    coef(i) = 0.5 * vz(x_index(i,l),l)
    g2(i) = 2. * ( a(upper_index,k,l) - a(x_index(i,l),k,l) ) / ( dz(x_index(i,l)) * (1.0 + az1(x_index(i,l)) ) )
    IF (s(x_index(i,l),l) &amp;lt;= p1 .OR. s(upper_index,l) &amp;lt;= p1) g2(i)=0.0
  END DO&lt;/PRE&gt;

&lt;P&gt;This loop is nested in loops over K and L&lt;/P&gt;

&lt;P&gt;However, when I tested the same idea in the following simple loop, I did not see meaningful difference in the speed of the code.&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;do i = 1,nloop1
  do j = 1,nloop2
    k = i+j
    variable(k) = k
  end do
end do&lt;/PRE&gt;

&lt;P&gt;Which one of the above two observations I can possibly trust?&lt;/P&gt;

&lt;P&gt;Also, I noticed that the use of IF-ELSEIF construct is noticeably less efficient than using multiple IF statements in a DO-loop.&amp;nbsp; Is this observation correct? I am using Intel Fortran compiler 2015, with O2 optimization flag.&lt;/P&gt;

&lt;P&gt;Thanks you in advance,&lt;/P&gt;</description>
      <pubDate>Tue, 16 Sep 2014 01:05:36 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050271#M115010</guid>
      <dc:creator>DataScientist</dc:creator>
      <dc:date>2014-09-16T01:05:36Z</dc:date>
    </item>
    <item>
      <title>It's not unusual, in my</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050272#M115011</link>
      <description>&lt;P&gt;It's not unusual, in my experience, that a compiler can benefit from your help in simplifying any but the simplest loops.&lt;/P&gt;

&lt;P&gt;Particularly in cases of auto-vectorization, replacing&lt;/P&gt;

&lt;P&gt;if(condition)then&lt;/P&gt;

&lt;P&gt;...&lt;/P&gt;

&lt;P&gt;else&lt;/P&gt;

&lt;P&gt;...&lt;/P&gt;

&lt;P&gt;by&lt;/P&gt;

&lt;P&gt;if(condition)..&lt;/P&gt;

&lt;P&gt;if(.not. condition)&lt;/P&gt;

&lt;P&gt;.....&lt;/P&gt;

&lt;P&gt;may be helpful (assuming that you don't insist on short-cut evaluation but are willing to have multiple cases evaluated speculatively).&amp;nbsp; I wouldn't&amp;nbsp; normally go so far as to replace an if block by multiple ifs.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Sep 2014 01:19:31 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050272#M115011</guid>
      <dc:creator>TimP</dc:creator>
      <dc:date>2014-09-16T01:19:31Z</dc:date>
    </item>
    <item>
      <title>The second loop does a lot of</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050273#M115012</link>
      <description>&lt;P&gt;The second loop does a lot of repeated assignments to the same locations. It could be replaced by&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;do i=2,nloop1+nloop2
   variable(i)=i
end do&lt;/PRE&gt;

&lt;P&gt;For nloop1 = nloop2 = 100000, the first version of the loop took 4.7 s of CPU time with an i7-2720. The second version took less time than the resolution of the cpu_time() function (the compiler could have even done away with the loop -- I did not check).&lt;/P&gt;

&lt;P&gt;Perhaps you could come up with more realistic sections of an algorithm that are more worthy of optimization. Secondly, compilers are so adept at optimizing these days that one should think twice before trying to help them with loop invariants by doing manual code rewrites.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Sep 2014 01:55:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050273#M115012</guid>
      <dc:creator>mecej4</dc:creator>
      <dc:date>2014-09-16T01:55:00Z</dc:date>
    </item>
    <item>
      <title>Thanks Tim &amp; mecej4.   I</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050274#M115013</link>
      <description>&lt;P&gt;Thanks Tim &amp;amp; mecej4.&amp;nbsp;&amp;nbsp; I think I was not clear enough in my question, and so it created some confusion.&amp;nbsp; The question was to compare the performance of this code:&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;call cpu_time(tstart)
do i = 1,nloop1
  do j = 1,nloop2
    variable(i+j) = i+j
  end do
end do
call cpu_time(tend)
write(*,*) tend-tstart
&lt;/PRE&gt;

&lt;P&gt;with the following,&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;call cpu_time(tstart)
do i = 1,nloop1
  do j = 1,nloop2
    k = i+j
    variable(k) = k
  end do
end do
call cpu_time(tend)
write(*,*) tend-tstart&lt;/PRE&gt;

&lt;P&gt;The only difference between the two is the assignment k = i + j in place of i + j wherever it appears in the loop. So, even though both loops do repeated assignments to the same locations, they should only differ in performance where K is used in place of I+J.&amp;nbsp; I did this simple test to see if this change can have any benefits to performance, in more complicated codes like the very first code snippet that I posted above.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Sep 2014 02:07:38 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050274#M115013</guid>
      <dc:creator>DataScientist</dc:creator>
      <dc:date>2014-09-16T02:07:38Z</dc:date>
    </item>
    <item>
      <title>In your latest examples, the</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050275#M115014</link>
      <description>&lt;P&gt;In your latest examples, the value of `variable` is never used.&amp;nbsp; If the compiler is clever enough (and often with these sorts of examples ... it is) it will recognise that and then decide that it doesn't actually need to execute the assignment to `variable`.&amp;nbsp; It may then realize that the loops aren't doing anything useful, and with progressive analysis replace them with simple assignments to i, j [and k].&amp;nbsp; It may then recognize that i, j and k aren't being used, and get rid of them too.&amp;nbsp; All up, your code could end up being...&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;call cpu_time(tstart)
call cpu_time(tend)
write (*,*) tend-tstart&lt;/PRE&gt;

&lt;P&gt;which is possibly not all that interesting to test.&lt;/P&gt;

&lt;P&gt;As stated elsewhere - different code will result in different optimization outcomes.&amp;nbsp; From an outsiders point of view it is very difficult to predict what the compilers optimizer will do, beyond simple cases, and what it does may change with version and other compile options.&amp;nbsp; So if a bit of code is that important that you care deeply that it be optimized most effectively, you need to measure that specific code, in a manner that tests that specific code in a realistic fashion.&amp;nbsp; In your opening post you say you've done that measurement... so you know the answer to your question.&lt;/P&gt;

&lt;P&gt;But I would be getting pretty desperate to be embarking on hand optimization like this.&amp;nbsp; As a very good general rule, write the code that is easiest to write/easiest to understand/easiest to maintain, and make optimization a problem for the optimizer.&amp;nbsp; Is your code easier to write/understand/maintain with the intermediate variable?&amp;nbsp; That's your call.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Sep 2014 02:37:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050275#M115014</guid>
      <dc:creator>IanH</dc:creator>
      <dc:date>2014-09-16T02:37:00Z</dc:date>
    </item>
    <item>
      <title>You should look again at</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050276#M115015</link>
      <description>&lt;P&gt;You should look again at mecej4's comment as his approach does reduce the number of loop trips in your second loop by an order of N.&lt;/P&gt;

&lt;P&gt;I am suggesting&amp;nbsp;a change for&amp;nbsp;your first loop, only for the reason to make it a bit clearer to read. (most/all optimising compilers would do this anyway.) &amp;nbsp;I also changed the IF test so that g2 is only&amp;nbsp;calculated when the test is false. Without knowing the probability of the IF test being true, I don't know if this is a significant improvement or may hinder auto-vectorisation.&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;&amp;nbsp; DO i = 1,N
&amp;nbsp;&amp;nbsp;&amp;nbsp; ix&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = x_index(i,l)
&amp;nbsp;&amp;nbsp;&amp;nbsp; coef(i) = 0.5 * vz(ix,l)
&amp;nbsp;&amp;nbsp;&amp;nbsp; IF (s(ix,&amp;nbsp; l) &amp;lt;= p1 .OR.&amp;nbsp;&amp;nbsp; &amp;amp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s(ix+N,l) &amp;lt;= p1) then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g2(i) = 0.0
&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g2(i) = 2.0 * ( a(ix+n,k,l) - a(ix,k,l) )&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; / ( dz(ix) * (1.0 + az1(ix) ) )
&amp;nbsp;&amp;nbsp;&amp;nbsp; end if
&amp;nbsp; END DO&lt;/PRE&gt;

&lt;P&gt;I am not sure if Tim's comment on auto-vectorisation applies, as I am not sure that multiple calculations of g2 could be vectorised, given that ix can vary for each cycle of DO I. If the g2 calculation can be vectorised and there is a low probability that the&amp;nbsp;IF test is true, then any coding that assists the vectorisation would help.&lt;/P&gt;

&lt;P&gt;John&lt;/P&gt;</description>
      <pubDate>Tue, 16 Sep 2014 02:40:25 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/optimization-do-loop/m-p/1050276#M115015</guid>
      <dc:creator>John_Campbell</dc:creator>
      <dc:date>2014-09-16T02:40:25Z</dc:date>
    </item>
  </channel>
</rss>

