xslittlegrass
xslittlegrass

Reputation: 4976

Why xlf and ifort take differently on Array Bounds Read?

I'm trying to port a code from ifort compiler to ibm xlf compiler. It works well under ifort on redhat but give results contain "NaNQ" under xlf on AIX system. It turns out that there is a array bounds reading in the code cost this problem, here is a simplified example:

program main                                                                                                                                                    
implicit none                                                                                                                                                   
real(8)::a(1,0:10)=0.D0                                                                                                                                         

print *, a(1,-1)                                                                                                                                                
end program main 

Using both compiler I can successfully compile it, without any mistake or warning. On ifort I get result:

0.000000000000000E+000

But on xlf, I get:

0.247032822920623272E-322

However, if I read more beyond the boundary, the xlf won't compile but ifort compile successfully.

program main                                                                                                                                                    
implicit none                                                                                                                                                   
real(8)::a(1,0:10)=0.D0                                                                                                                                         

print *, a(1,-3:-1)                                                                                                                                                
end program main

On ifort I get:

0.000000000000000E+000  0.000000000000000E+000  0.000000000000000E+000

On xlf it won't compile:

"1.f90", line 5.9: 1516-023 (S) Subscript is out of bounds.
** main   === End of Compilation 1 ===
1501-511  Compilation failed for file 1.f90.

Why the ifort and xlf take differently on this cross boundary read? Is there any way to make the compiler to check it strictly and prevent cross boundary read to happen? After all, it took me a long time to catch this bug in our code, since our group have been using this code for more than 15 years without any problems on ifort. Thanks.

Upvotes: 2

Views: 678

Answers (1)

M. S. B.
M. S. B.

Reputation: 29401

Most Fortran compilers have options to check for array bounds errors at runtime. In these examples with constant indices the error can be found at compile time, which some compilers but not others will do without using non-default options. With ifort use -check bounds to request array bounds checking. You can get additional checking with -check all. These options are generally not the default because there is a runtime cost. But the cost of getting a wrong answer can be much higher!! I have found the runtime cost to frequently be surprisingly low and recommend using runtime checks during code development and even in production if the runtime cost is acceptable.

Upvotes: 4

Related Questions