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

A = 0d0 does not properly initialize 3x3 matrix

ITUB
Beginner
256 Views

I've found this baffling behavior with the ifx version 2024.0.2 when compiling with -O2 or higher: 

! With `m = 0d0`, the matrix does not actually get zeroed when 
! compiling with -O2! With `m(:,:) = 0d0`, it does.
m = 0.0D0

do i = 1, npnt
m(1, 1) = m(1, 1) + xyz1(1, i) * xyz2(1, i)
m(2, 1) = m(2, 1) + xyz1(2, i) * xyz2(1, i)
m(3, 1) = m(3, 1) + xyz1(3, i) * xyz2(1, i)
m(1, 2) = m(1, 2) + xyz1(1, i) * xyz2(2, i)
m(2, 2) = m(2, 2) + xyz1(2, i) * xyz2(2, i)
m(3, 2) = m(3, 2) + xyz1(3, i) * xyz2(2, i)
m(1, 3) = m(1, 3) + xyz1(1, i) * xyz2(3, i)
m(2, 3) = m(2, 3) + xyz1(2, i) * xyz2(3, i)
m(3, 3) = m(3, 3) + xyz1(3, i) * xyz2(3, i)
enddo

print *, m(2, 2)  

I'm zeroing the matrix before adding to it, yet the output of the print at the bottom suggests that it wasn't actually initialized. The problem goes away if I say m(:,:) = 0.0d0, if I reduce optimizations, or if I use ifort instead of ifx.

The code above is just a snippet, and it takes some additional code both before and after the snippet to make the issue visible. I simplified my function as much as I could (about 50 lines), which you can find in the attachment. This is the output I get, which suggests that m is not getting zeroed, but instead is accumulating over repeated calls to the function:

0.500000000000000 
1.00000000000000
1.50000000000000

Using m(:,:) = 0.0d0, or reducing the optimization level, I get 0.5 printed every time, which is what I would expect.

Either I'm going crazy or this is a bug in the compiler; please let me know.

0 Kudos
0 Replies
Reply