libprima / prima

PRIMA is a package for solving general nonlinear optimization problems without using derivatives. It provides the reference implementation for Powell's derivative-free optimization methods, i.e., COBYLA, UOBYQA, NEWUOA, BOBYQA, and LINCOA. PRIMA means Reference Implementation for Powell's methods with Modernization and Amelioration, P for Powell.
http://libprima.net
BSD 3-Clause "New" or "Revised" License
296 stars 38 forks source link

gcc -Wmaybe-uninitialized warnings #30

Closed jschueller closed 1 year ago

jschueller commented 1 year ago

I enabled -Wall with gfortran 11.3 and saw some -Wmaybe-uninitialized warnings:

/home/schueller/projects/prima/fortran/bobyqa/trustregion.f90:540:111:

  540 |         xbdi(iact) = nint(sign(ONE, d(iact) - diact), kind(xbdi))  !!MATLAB: xbdi(iact) = sign(d(iact) - diact)
      |                                                                                                               ^
Warning: ‘diact’ may be used uninitialized in this function [-Wmaybe-uninitialized]
[ 63%] Building Fortran object fortran/CMakeFiles/primaf.dir/lincoa/trustregion.f90.o
/home/schueller/projects/prima/fortran/bobyqa/rescue.f90:504:52:

  504 |             xxpt = xp * xpt(ip, :) + xq * xpt(iq, :)
      |                                                    ^
Warning: ‘xq’ may be used uninitialized in this function [-Wmaybe-uninitialized]
/home/schueller/projects/prima/fortran/bobyqa/rescue.f90:504:52: Warning: ‘xp’ may be used uninitialized in this function [-Wmaybe-uninitialized]

it goes away if I set these to 0 after declaration

zaikunzhang commented 1 year ago

Hi @jschueller , thank you for the feedback. This is very interesting.

What is the exact command of the compilation? Below is what I tried on my computer. Note that my gfortran-11 is also gfortran 11.3.

cd fortran
gfortran-11 -c -g -Wall -Wextra  ./common/consts.F90 ./common/infos.f90 ./common/debug.F90 ./common/inf.F90 ./common/infnan.F90 ./common/memory.F90 ./common/string.f90 ./common/linalg.f90 ./common/univar.f90 ./common/rand.f90 ./common/ratio.f90 ./common/redrho.f90 ./common/xinbd.f90 ./common/history.f90 ./common/selectx.f90 ./common/checkexit.f90 ./common/fprint.f90 ./common/message.f90 ./common/preproc.f90 ./common/pintrf.f90 ./common/evaluate.f90 ./common/powalg.f90 ./common/shiftbase.f90 ./bobyqa/geometry.f90 ./bobyqa/initialize.f90 ./bobyqa/update.f90 ./bobyqa/rescue.f90 ./bobyqa/trustregion.f90 ./bobyqa/bobyqb.f90 ./bobyqa/bobyqa.f90 

This does raise the following warning about uninitialized variables, but diact and xp are not among them.

bobyqa.f90 
./common/string.f90:164:86:

  164 |     sformat = '(1PE'//int2str(wx)//'.'//int2str(ndgt_loc)//'E'//int2str(nexp_loc)//')'
      |                                                                                      ^
Warning: ‘.sformat’ may be used uninitialized [-Wmaybe-uninitialized]
./common/linalg.f90:2121:24:

 2121 | direction_loc = 'ascend'
      |                        ^
Warning: ‘.direction_loc’ may be used uninitialized [-Wmaybe-uninitialized]
./common/fprint.f90:83:21:

   83 |     fname_loc = fname
      |                     ^
Warning: ‘.fname_loc’ may be used uninitialized [-Wmaybe-uninitialized]
./common/fprint.f90:99:23:

   99 |     position = 'append'
      |                       ^
Warning: ‘.position’ may be used uninitialized [-Wmaybe-uninitialized]
./common/fprint.f90:112:60:

  112 |     fstat = merge(tsource='old', fsource='new', mask=fexist)
      |                                                            ^
Warning: ‘.fstat’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:382:14:

  382 |     fname = ''
      |              ^
Warning: ‘.fname’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:403:70:

  403 | delta_message = newline//state//' step with radius = '//num2str(delta)
      |                                                                      ^
Warning: ‘.delta_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:407:63:

  407 |         & spaces//'Constraint violation = '//num2str(cstrv_loc)
      |                                                               ^
Warning: ‘.nf_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:413:86:

  413 |     x_message = newline//'The corresponding X is: '//num2str(x)  ! Printed in one line
      |                                                                                      ^
Warning: ‘.x_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:420:101:

  420 |         constr_message = newline//'The constraint value is: '//num2str(constr)  ! Printed in one line
      |                                                                                                     ^
Warning: ‘.constr_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:429:62:

  429 | message = delta_message//nf_message//x_message//constr_message
      |                                                              ^
Warning: ‘.message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:315:14:

  315 |     fname = ''
      |              ^
Warning: ‘.fname’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:322:52:

  322 |     message = newline//'Set CPEN to '//num2str(cpen)
      |                                                    ^
Warning: ‘.message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:216:14:

  216 |     fname = ''
      |              ^
Warning: ‘.fname’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:239:34:

  239 |         & 'CPEN = '//num2str(cpen)
      |                                  ^
Warning: ‘.rho_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:245:86:

  245 |     x_message = newline//'The corresponding X is: '//num2str(x)  ! Printed in one line
      |                                                                                      ^
Warning: ‘.x_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:252:98:

  252 |         & 'Least value of F = '//num2str(f)//spaces//'Constraint violation = '//num2str(cstrv_loc)
      |                                                                                                  ^
Warning: ‘.nf_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:259:101:

  259 |         constr_message = newline//'The constraint value is: '//num2str(constr)  ! Printed in one line
      |                                                                                                     ^
Warning: ‘.constr_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:269:73:

  269 |     message = newline//rho_message//nf_message//x_message//constr_message
      |                                                                         ^
Warning: ‘.message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:82:14:

   82 |     fname = ''
      |              ^
Warning: ‘.fname’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:106:53:

  106 |     reason = 'the target function value is achieved.'
      |                                                     ^
Warning: ‘.reason’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:130:73:

  130 | ret_message = newline//'Return from '//solver//' because '//strip(reason)
      |                                                                         ^
Warning: ‘.ret_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:133:86:

  133 |     x_message = newline//'The corresponding X is: '//num2str(x)  ! Printed in one line
      |                                                                                      ^
Warning: ‘.x_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:140:98:

  140 |         & 'Least value of F = '//num2str(f)//spaces//'Constraint violation = '//num2str(cstrv_loc)
      |                                                                                                  ^
Warning: ‘.nf_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:147:101:

  147 |         constr_message = newline//'The constraint value is: '//num2str(constr)  ! Printed in one line
      |                                                                                                     ^
Warning: ‘.constr_message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/message.f90:157:82:

  157 |     message = newline//ret_message//nf_message//x_message//constr_message//newline
      |                                                                                  ^
Warning: ‘.message’ may be used uninitialized [-Wmaybe-uninitialized]
./common/preproc.f90:134:39:

  134 |     min_maxfun_str = '(N+1)(N+2)/2 + 1'
      |                                       ^
Warning: ‘.min_maxfun_str’ may be used uninitialized [-Wmaybe-uninitialized]

The warnings are due to a known bug of gfortran: https://fortran-lang.discourse.group/t/warning-str-may-be-used-uninitialized . It has been fixed in gfortran-12. So no warning is emitted if I use gfortran-12 instead of gfortran-11.

Note that the warnings depend on the optimization level of the compilation. Indeed, if I replace gfortran-11 with gfortran-12 -Og, then the following warning about diact will be raised:

./bobyqa/trustregion.f90:540:111:

  540 |         xbdi(iact) = nint(sign(ONE, d(iact) - diact), kind(xbdi))  !!MATLAB: xbdi(iact) = sign(d(iact) - diact)
      |                                                                                                               ^
Warning: ‘diact’ may be used uninitialized [-Wmaybe-uninitialized]
./bobyqa/trustregion.f90:100:17:

  100 | real(RP) :: diact
      |                 ^
note: ‘diact’ was declared here

But there is still no warning about xp.

What do you think?

Thanks.

Zaikun

jschueller commented 1 year ago

oh, I also had "-fcheck=all", here is the full command:

[ 28%] Building Fortran object fortran/CMakeFiles/primaf.dir/bobyqa/rescue.f90.o
cd /home/schueller/projects/prima/build/fortran && /usr/bin/gfortran -Dprimaf_EXPORTS  -Wall -fcheck=all -O2 -g -DNDEBUG -fPIC -c /home/schueller/projects/prima/fortran/bobyqa/rescue.f90 -o CMakeFiles/primaf.dir/bobyqa/rescue.f90.o
/home/schueller/projects/prima/fortran/bobyqa/rescue.f90:504:52:

  504 |             xxpt = xp * xpt(ip, :) + xq * xpt(iq, :)
      |                                                    ^
Warning: ‘xq’ may be used uninitialized in this function [-Wmaybe-uninitialized]
/home/schueller/projects/prima/fortran/bobyqa/rescue.f90:504:52: Warning: ‘xp’ may be used uninitialized in this function [-Wmaybe-uninitialized]
/usr/bin/cmake -E cmake_copy_f90_mod fortran/rescue_mod.mod fortran/CMakeFiles/primaf.dir/rescue_mod.mod.stamp GNU
zaikunzhang commented 1 year ago

Hello @jschueller , I think this has been fixed by https://github.com/libprima/prima/commit/dd2821f0229660be71fb707b077646b9953090ab . You may verify it. Thanks.