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

the IF statement in IVF

ocean
Beginner
1,063 Views
I'm migrating several Fortan/C++ projects from CVF to IVF9.0. I'm surprised at Intel's implementation of the IF statement in their Fortran compiler.CVF, when processingIF statementscomposed of several logical expressions joined by the logical AND, short-circuits the statement once the first false expression is encountered. IVF, however, evaluates all the expressions first, and then applies the logical AND.
Depending on how you developed your code in CVF, you could actually get completely different results when yourIF statements (those containing embedded ANDs)are compile with IVF.
We have thousands and thousands of IF statements throughout our projects, and it will be quite a challenge to actually go back and analyze each statement for potential problmes.
Does anyone know if there's a work around to make the code behave like in CVF?
Any feed back on this issue is appreciated.
Thanks,
Manny
0 Kudos
10 Replies
Steven_L_Intel1
Employee
1,063 Views
You are mistaken. CVF does not behave the way you describe. If it happened to in your program, it was by coincidence, not design. I certainly answered enough customer questions about CVF where it didn't "short circuit" the IF and it is ironic that customers who didn't complain before (because they thought they were getting a feature) are now complaining that IVF doesn't do the same.

Neither compiler deliberately short-circuts. You get whatever the optimizer feels like doing at the time.

I am aware that the IVF Building Applications manual section on CVF compatibility says that CVF did short-circuiting - it is wrong. I verified this today with the engineer who worked on this part of the compiler.

You need to fix your code. It's broken. See this article I wrote in 1999 for more details.

Sorry if this sounds blunt, but I don't know of a better way to put it.
0 Kudos
ocean
Beginner
1,063 Views

Steve,

Thanks for your reply. If CVF and IVF don't behave the way I described them, why hasn't Intel fixed the documentation. We just bought compiler licenses, and the documentation clearly indicates that CVF and IVF behave the way I described them. It can be very confusing for someone migrating from one environment to the other.

Manny

0 Kudos
Steven_L_Intel1
Employee
1,063 Views
Because it just came to our attention that the documentation said this. I've already filed a bug report on it. I'm unsure where that text came from.
0 Kudos
Steven_L_Intel1
Employee
1,063 Views
Um, what part of the documentation are you referring to? The part that says that IVF doesn't short circuit? That part is true. The error is saying that CVF did - I've already filed a bug report on that.

If nothing else, this documentation error might alert users to look for this kind of bug in their code.
0 Kudos
ocean
Beginner
1,063 Views

Steve,

The IVF 9.0 documentation that I'm referring to comes from the section describing Compatibility with Compact Visual Fortran, and it reads as follows:

Expressions in an IF statement containing clauses joined by a logical AND are handled differently in Intel Visual Fortran 8.x and later versions than in Compaq Visual Fortran. CVF evaluated the first clause, and if it was false, did not evaluate the remaining clauses. Intel Visual Fortran first evaluates all clauses and then performs the logical AND. Since the Fortran standard neither requires nor forbids short-circuiting of logical expressions, both behaviors are correct.

If there's something wrong with the above Intel interpretation of both CVF and IVF, I really would appreciate if you could clarify it (how shouldthe statement aboveread?).

Thanks for your help,

Manny

0 Kudos
Les_Neilson
Valued Contributor II
1,063 Views

In your first post I believe you say that it might be possible to get different results if the IF conditions are evaluated differently in CVF and IVF. For example:

IF (A .and. B .and. C .and. D) then

ENDIF

I do not see howit ispossible to get different results in the restof the codewhether A,B,C and D are *all* evaluated before the testor if the test fails on A and therefor B,C, and D are not evaluated (short circuit). UNLESS there are "side effects". that is if any of B,C,D are functions say,which change the values of variables in common blocks or modules used elsewhere.

Have I understood correctly?

Les

0 Kudos
Steven_L_Intel1
Employee
1,063 Views
Manny,

The text you cite is the text that is wrong in that it should not say that CVF stops evaluating if the "first clause" is false. This entire paragraph should be deleted and I have asked for it to be removed, as this is not a difference between the compilers.

The usual case where this coding error bites you is if you write something like:

if (j > 0 .and. array(j) > 5)

People who write this, often C programmers where the C language DOES specify left to right and short-circuit evaluation, assume that if J is less than or equal to zero that the array(j) reference won't occur. They are then surprised by array bounds errors or other odd behavior.

The correct way to code this in Fortran is as a series of nested IF-THEN clauses. Yes, it's awkward, but them's the language rules. There have been proposals for AND_THEN and OR_ELSE operators but they never got very far in the standards committee.
0 Kudos
ocean
Beginner
1,063 Views

Thanks for the clarification.

Manny

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,063 Views
Just a side remark Steve: in my experience, CVF did behave that way (short-circuiting) most of the time, even in debug mode (no optimization). I am not saying that it was built-in behavior and that it happened always, just that it happened often. I too had to fix several lines (not mine :-) ) with IF (j>0 .AND. foo(j)>0) after I switched to IVF.
0 Kudos
Steven_L_Intel1
Employee
1,063 Views
It's application dependent. For every app where it seemed to do that, I could show you another where it didn't. I certainly have the pile of support requests to back that up. It's all a matter of what the optimizer felt was best to do first, usually based on "register pressure" (get things that need the most registers out of the way first.)
0 Kudos
Reply