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

MERGE evaulating TSOURCE before MASK is checked

Julian_H_
Beginner
459 Views

Hello,

I am using following line of code which always fails, because the compiler is trying to execute TSOURCE although MASK==.FALSE.. This behaviours only occurse when the TSOURCE has some kind of modification. In this case it is SIZE. So he is trying to get the SIZE of LC and then he is checking MASK if TSOURCE or FSOURCE should be used. Is this behaviour intended? Reminds me of  the non short circuting behaviour in IF statements.

 

CLASS(myClass), INTENT(IN) :: this
INTEGER, INTENT(IN), OPTIONAL :: LC(:)
INTEGER :: nLC

nLC = MERGE(SIZE(LC), this%nLoadcases, PRESENT(LC))

 

0 Kudos
6 Replies
TimP
Honored Contributor III
459 Views

This speculation is typical of compiler optimization levels which permit vectorization of conditional operations.  If you don't wish this behavior, you can try compile options such as /fp:strict (look up implied levels of speculation).  You may have the option of a novector directive, or placing an explicit IF block.  You might think that the equivalent of IF block could actually be an optimization when the mask is invariant, so you could try submitting a request to online service center.

0 Kudos
IanH
Honored Contributor II
459 Views

The code is non-conforming if LC is ever not present.  MERGE is a function - when a function is invoked, all actual argument expressions are evaluated, then the function is executed.  If LC is not present, then SIZE(LC) cannot be evaluated.

To control the execution order, use an if statement.

IF (PRESENT(LC)) THEN
  nLC = SIZE(LC)
ELSE
  nLC = this%nLoadCases
END IF

 

0 Kudos
Julian_H_
Beginner
459 Views

Thanks for the clarification.

ianh wrote:

... when a function is invoked, all actual argument expressions are evaluated, then the function is executed. 

Thats what I thought. Still I like to use MERGE in similar situations because of simplicity of a one line statement.

 

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
459 Views

nLC = SIZE(LC); IF (PRESENT(LC)) nLC = SIZE(LC)

Jim Dempsey

0 Kudos
Julian_H_
Beginner
459 Views

jimdempseyatthecove wrote:

nLC = SIZE(LC); IF (PRESENT(LC)) nLC = SIZE(LC)

Jim Dempsey

Ok right. Like this its possible :)

0 Kudos
jimdempseyatthecove
Honored Contributor III
459 Views

"The programmer doth protest too much, methinks"
Hamlet, by William Shakespeare

IOW you wish to attain a preferable form over function by violating one of the language rules.

"I did it my way"
My Way by Frank Sinatra

Jim Dempsey :)

0 Kudos
Reply