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

Do LOOP fails when NOWHERE NEAR LIMITS

WSinc
New Contributor I
1,850 Views

This do loop example has NO passes thru it.

   dk4=100000000

    khi4=huge(k4)/2 ! nowhere near upper limit
    klo4=-khi4  !  nowhere near lower limit
   kount=(khi4-klo4)/dk4+1
    print *,"klo,hi=",klo4,khi4," kount 4=",kount
    do k4=klo4,khi4,dk4
        print *,"k4=",k4
    enddo

It gives the correct result (22) for KOUNT, but the compiler apparently gives ZERO for the same thing.

All variables are integer(4)

0 Kudos
24 Replies
WSinc
New Contributor I
381 Views

I agree that having a non-integer for the LOOP index variable is pretty awkward.

My understanding of a "scalar" is a non-array, i.e. you cannot have DO i(3) = i1,i2,10

It would be pretty easy for the compiler to just skip over the DO LOOP if STOP < START, regardless of what expressions

were used.

However, for the START, STOP, and STEP values, you can still have integer expressions.

And those parameters can be in arrays, as well.

 

We still have a problem when STOP - START > Huge() though, unless one uses a higher # of bytes

for the calculation. For 1 and 2 byte LOOP values, INTEL apparently uses 4 byte arithmetic.

I have seen code examples where you can vary the parameters inside the DO LOOP, and the compiler does NOT

complain. Wouldn't that give unpredictable results?

0 Kudos
Steven_L_Intel1
Employee
381 Views

The compiler does not complain? Got an example? 

C:\Projects>type t.f90
do i=1,10
i = i + 1
print *, i
end do
end

C:\Projects>ifort t.f90
Intel(R) Visual Fortran Compiler XE for applications running on IA-32, Version 15.0.2.179 Build 20150121
Copyright (C) 1985-2015 Intel Corporation.  All rights reserved.

t.f90(2): error #8093: A do-variable within a DO body shall not appear in a variable definition context.   
i = i + 1
^
compilation aborted for t.f90 (code 1)

In the case where the compiler can't be sure if you modified the variable, it will generate alternate code that tests the variable rather than a count. Since it can't see if you did the modification, it doesn't warn.

If you mean changing the start or end values after the loop starts, that has no effect.

0 Kudos
WSinc
New Contributor I
381 Views

I was referring to the LOOP PARAMETERS, not the index variable.

Meddling with those inside the loop doesn't make much sense - -

Are you saying there is a way to TEST the INDEX on each step?

(other than coding it myself, that is)

0 Kudos
Steven_L_Intel1
Employee
381 Views

Meddling with the loop parameters inside the loop does nothing useful, as they are not looked at again.

What I meant was that the compiler might choose to do a test rather than use a count, if it feels it is appropriate/optimal. You don't have control over this. Perhaps you want DO WHILE.

0 Kudos
Reply