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

IFX: AND operator evaluates 2nd condition even if first one is FALSE

NasarAli
Neuer Beitragender I
3.366Aufrufe

IFORT does not have this issue.

 

In following statement dynamicarr is allocated when noption=1. This statement executed fine in IFORT but IFX gives access violation exception.

noption = 0
if (noption.eq.1 .and. dynamicarr(1).eq.0) then

 

Same exception occurs on optional parameter check. If optional parameter is not passed to a routine then following statement gives access violation exception.

if (PRESENT(ioptional) .AND. ioptional.gt.0) then

 

 

NasarAli_0-1714716707675.png

 

0 Kudos
1 Lösung
Arjen_Markus
Geehrter Beitragender II
3.357Aufrufe

Fortran as per the standard does not enforce short-circuiting such composite logical expressions. That it seems to have worked in IFORT is a mere coincidence. You should simply not rely on it, as it is quite possibly depending on the compiler options you use besides the complexity of the expression and many other things. Other languages do enforce this (C for instance) or have special operators (Ada, IIRC). There have been proposals for Fortran - operators like .andthen. and .orelse. But they are not part of any standard at this moment.

Lösung in ursprünglichem Beitrag anzeigen

13 Antworten
Arjen_Markus
Geehrter Beitragender II
3.358Aufrufe

Fortran as per the standard does not enforce short-circuiting such composite logical expressions. That it seems to have worked in IFORT is a mere coincidence. You should simply not rely on it, as it is quite possibly depending on the compiler options you use besides the complexity of the expression and many other things. Other languages do enforce this (C for instance) or have special operators (Ada, IIRC). There have been proposals for Fortran - operators like .andthen. and .orelse. But they are not part of any standard at this moment.

mecej4
Geehrter Beitragender III
3.328Aufrufe

Replace

if (noption.eq.1 .and. dynamicarr(1).eq.0) then

by

if (noption.eq.1) then
   if( dynamicarr(1).eq.0) then
   ...
end if

The compiler is allowed to evaluate expressions in any order, so sub-expressions should be possible to evaluate in any order.

FortranFan
Geehrter Beitragender III
3.294Aufrufe

Workarounds based on Fortran 2018 and earlier standards are shown upthread.

Starting Fortran 2023, the following will be an option given the conditional nature of the desired instructions:

if ( noption == 1 ? ( dynamicarr(1) == 0 ? .true : .false. ) : .false. ) then
   ..
if ( present(ioptional) ? ( ioptional > 0 ? .true. : .false. ) : .false. ) then
   ..

 

 

Steve_Lionel
Geehrter Beitragender III
3.291Aufrufe

Fortran 2023 adds conditional expressions that will help you here, once supported. For example,

if (noption .eq. 1?dynamicarr(1) .eq. 0) ...

 

FortranFan
Geehrter Beitragender III
3.264Aufrufe

@Steve_Lionel wrote:

Fortran 2023 adds conditional expressions that will help you here, once supported. For example:

 

if (noption .eq. 1?dynamicarr(1) .eq. 0) ...

 

However, the standard requires a clause for when `scalar-logical-expr' (e.g., the expression "noption .eq. 1") evaluates to `false`:

ce.PNG

Thus a colon and a following expr is required, say the following lines, making the statement a bit more verbose than a Fortranner might be prone to presume:

 

if ( noption == 1 ? ( dynamicarr(1) == 0 ? .true : .false. ) : .false. ) then
   ..

 

Steve_Lionel
Geehrter Beitragender III
3.215Aufrufe

Right - I missed that. Thanks.

JohnNichols
Geschätzter Beitragender III
3.198Aufrufe
if ( noption == 1 ? ( dynamicarr(1) == 0 ? .true : .false. ) : .false. ) then

This will be a beast to debug, @mecej4 is the best solution for maintainability and not obscureness. 

It is like LISP without the elegance. 

Steve_Lionel
Geehrter Beitragender III
3.165Aufrufe

I expect most uses will be as expressions in assignments rather than if-then.  Conditional arguments are a related feature that have some interesting use cases.

NasarAli
Neuer Beitragender I
3.088Aufrufe

Thank you to all for providing the alternatives.

The issue is that, it was working in IFORT but failed in IFX.

I am working on large code base and the alternatives will be my last option.

mecej4
Geehrter Beitragender III
3.060Aufrufe

@NasarAliwrote:

Thank you to all for providing the alternatives.

The issue is that, it was working in IFORT but failed in IFX.

I am working on large code base and the alternatives will be my last option.


I think that you are making a grave mistake here. If the code base that you are working on may be used to generate results that will be utilized to manufacture a product, deliver a service or to support decisions that may affect someone's life or a company's viability, and something unfortunate happens as a result of not fixing this bug, you are at risk of being blamed. The risk is higher because you have been informed of the risk and choose to ignore the risk.

You are wrong in concluding that "it was working in IFORT but failed in IFX". A program that does not produce error messages is not necessarily a correct program.

Here is a short program to drive home my points. Run it four times (i) ifort /Od, (ii) ifort /O2, (iii) ifx /Od and (iv) ifx /O2. Compare the results. My challenge to you: Which of those four runs is "correct" and what is your justification for your selection?

 

! demonstrate risk of assuming short circuit evaluation
program shCircBool
   implicit none
   integer :: ij(1) = [1]
   integer k
   call sub(ij,1,k)
   print *,'2 ',k
end program

subroutine sub(ij,n,k)
   implicit none
   integer n,ij(n),k
   k = -1
   if(n.gt.1.or.ij(n+1).gt.0)k=1
   return
end subroutine

 

 

NasarAli
Neuer Beitragender I
3.054Aufrufe
Steve_Lionel
Geehrter Beitragender III
3.037Aufrufe
Arjen_Markus
Geehrter Beitragender II
3.011Aufrufe

Ah, that is the one! I could not find it before, when I posted my reply :).

Antworten