Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29393 Discussions

Line 7: astr(I)=cstr executed twice when inside an IF

cean
New Contributor II
714 Views

t.gifHi,

I use F11 to  debug this code. When Line 7 executed, it  backs up to the If and then executes again, then down to the next line to Print. 

Why it not moves down at the first time? In my real code, it gives me wrong result. If remove the IF, it only executes once which is right.

Thanks for help.

Cheers,

Cean 

 

    integer i
    integer :: isL=1
    character(80) :: astr(20),cstr
    cstr='tetxt'
    do i=1,3
        if (isL==1) then
            astr(I)=cstr
            print *, '>0'
        endif    
    end do
    end

 

0 Kudos
3 Replies
andrew_4619
Honored Contributor III
613 Views

I am not sure you can read too much into the slightly erratic movements of the cursor in debug. I have noted with IFX that the correspondence of the cursor is not so prescice as ifort was and does things like this on branches on quite a few occasions. Not sure this is linked to your other problems. I bet the var only changes on the second touch in the watch window.

0 Kudos
Ron_Green
Moderator
582 Views

and you built with Debug configuration without any non-default options?  O0?

 

At optimization I can see where there could be a lot of code changes that essentially replace this whole loop with 2 versions of a fully unrolled version.  if (isl=1) is loop invariant.  this IF could be fully removed by creating 2 versions of the loop, one when isl=1 and one when it is not.  At runtime a branch could be made to one version or the other based on the value of isl at runtime - and NO the compiler does not know that you initalized it to 1 in the declaration.  That eliminates the IF test.  Next, the loop 1 to 3 could be fully unrolled to completely eliminate the loops inside these 2 versions.  I haven't looked at the Qopt-report for this example at default optimization  but I suspect it could do a lot of transforms on this simple example.

 

What exactly do you mean "gives me the wrong results"?  what compiler and version, what compiler options, and what is a "wrong" result? 

0 Kudos
cean
New Contributor II
547 Views

I have just created an empty project and wrote this simple code. Yes debug. No other change.

 
Now, I have figured out my problem and the result is right. 
0 Kudos
Reply