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

FORALL vs. nested DO construct / Compare - Contrast

NotThatItMatters
Beginner
1,468 Views

I have code which has the following block:

FORALL(I = 1:II, J = 1:JJ, K = 1:KK, NULL(I, J, K) == 1)
    V(I, J, K) = FRAC(I, J, K) * DX(I, J, K) * DY(I, J, K) * DZ(I, J, K)
    VN(I, J, K) = DX(I, J, K) * DY(I, J, K) * DZ(I, J, K)
END FORALL

In compiling / running the code after turning off my StackReserveSize number, I was noting this code was eating up stack.  So, I replaced it with

DO K = 1, KK
    DO J = 1, JJ
        DO I = 1, II
            IF (NULL(I, J, K) == 1) THEN
                V(I, J, K) = FRAC(I, J, K) * DX(I, J, K) * DY(I, J, K) * DZ(I, J, K)
                VN(I, J, K) = DX(I, J, K) * DY(I, J, K) * DZ(I, J, K)
            END IF
        END DO
    END DO
END DO

The code no longer gave me stack overflow errors, but it ran differently.  How can that be?

Perplexed

0 Kudos
3 Replies
Steven_L_Intel1
Employee
1,468 Views

FORALL is not a loop construct, to begin with. Your two code excerpts are not equivalent. FORALL is a series of masked array assignments. FORALL has been deprecated in the standard, replaced with DO CONCURRENT which has semantics more likely to be understood by the programmer (and also easier for the compiler to optimize.)

That said, at a quick glance I would expect the end result to be the same. I am always suspicious of code fragments which, in my experience, often don't reflect the actual code. So, can you come up with a buildable and runnable test case that demonstrates the difference? Or at least explain what you mean by "ran differently"?

0 Kudos
NotThatItMatters
Beginner
1,468 Views

Perhaps it is the non-optimization which is causing the differences.  The code snippets provided were the sum total difference between two executables of 20000 lines or more which "ran differently" as in one (the FORALL one) gave errors of 0.02% whereas the other (the DO one) gave errors of 1.76%.  The code snippet is executed once during array initialization.  I know this is not offering you much, but what it is providing me with is an opportunity to note LSB differences in code which, if not properly handled, can cause this sort of thing to be exacerbated.

Are there any good references or rules of thumb on coding which might help in trying to eliminate the susceptibility of my code to this behavior?

Perplexed

0 Kudos
Steven_L_Intel1
Employee
1,468 Views

See the attached presentation.443546

0 Kudos
Reply