Skip to content

Commit 45f8bc0

Browse files
committed
250910.022752.CST 1. matlab: call setenv(GFORTRAN_ERROR_BACKTRACE, 1) before calling the fortran solver, so that a backtrace is printed when a serious run-time error occurs (unless MATLAB hijacks stderr); 2. fortran: revise the definition of maxfun, and validate npt > 0 at the beginning of uobyqa
1 parent 08cfc40 commit 45f8bc0

File tree

7 files changed

+14
-6
lines changed

7 files changed

+14
-6
lines changed

fortran/common/preproc.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module preproc_mod
66
!
77
! Started: July 2020
88
!
9-
! Last Modified: Tue 09 Sep 2025 08:30:45 PM CST
9+
! Last Modified: Wed 10 Sep 2025 02:16:58 AM CST
1010
!--------------------------------------------------------------------------------------------------!
1111

1212
! N.B.:
@@ -166,7 +166,7 @@ subroutine preproc(solver, n, iprint, maxfun, maxhist, ftarget, rhobeg, rhoend,
166166
if (maxfun > 0) then
167167
maxfun = int(min_maxfun, kind(maxfun))
168168
else ! We assume that non-positive values of MAXFUN are produced by overflow.
169-
maxfun = max(min_maxfun, 10_IK**min(range(maxfun), 5)) !!MATLAB: maxfun = max(min_maxfun, 10^5);
169+
maxfun = int(max(min_maxfun, 10**min(range(maxfun), 5)), kind(maxfun)) !!MATLAB: maxfun = max(min_maxfun, 10^5);
170170
! N.B.: Do NOT set MAXFUN to HUGE(MAXFUN), as it may cause overflow and infinite cycling
171171
! when used as the upper bound of DO loops. This occurred on 20240225 with gfortran 13. See
172172
! https://fortran-lang.discourse.group/t/loop-variable-reaching-integer-huge-causes-infinite-loop

fortran/uobyqa/uobyqa.f90

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module uobyqa_mod
1919
!
2020
! Started: February 2022
2121
!
22-
! Last Modified: Thursday, February 22, 2024 PM03:29:24
22+
! Last Modified: Wed 10 Sep 2025 02:03:43 AM CST
2323
!--------------------------------------------------------------------------------------------------!
2424

2525
implicit none
@@ -159,7 +159,7 @@ subroutine uobyqa(calfun, x, &
159159
use, non_intrinsic :: consts_mod, only : MAXFUN_DIM_DFT
160160
use, non_intrinsic :: consts_mod, only : RHOBEG_DFT, RHOEND_DFT, FTARGET_DFT, IPRINT_DFT
161161
use, non_intrinsic :: consts_mod, only : RP, IK, TWO, HALF, TEN, TENTH, EPS
162-
use, non_intrinsic :: debug_mod, only : assert, warning
162+
use, non_intrinsic :: debug_mod, only : assert, warning, validate
163163
use, non_intrinsic :: evaluate_mod, only : moderatex
164164
use, non_intrinsic :: history_mod, only : prehist
165165
use, non_intrinsic :: infnan_mod, only : is_nan, is_finite, is_posinf
@@ -207,6 +207,7 @@ subroutine uobyqa(calfun, x, &
207207
integer(IK) :: n
208208
integer(IK) :: nf_loc
209209
integer(IK) :: nhist
210+
integer(IK) :: npt
210211
real(RP) :: eta1_loc
211212
real(RP) :: eta2_loc
212213
real(RP) :: f_loc
@@ -221,6 +222,8 @@ subroutine uobyqa(calfun, x, &
221222

222223
! Sizes
223224
n = int(size(x), kind(n))
225+
npt = (n + 1_IK) * (n + 2_IK) / 2_IK
226+
call validate(npt > 0, 'NPT > 0', srname) ! Validate that NPT does not overflow.
224227

225228
! Replace any NaN in X by ZERO and Inf/-Inf in X by REALMAX/-REALMAX.
226229
x = moderatex(x)
@@ -263,7 +266,7 @@ subroutine uobyqa(calfun, x, &
263266
if (present(maxfun)) then
264267
maxfun_loc = maxfun
265268
else
266-
maxfun_loc = max(MAXFUN_DIM_DFT * n, (n + 1_IK) * (n + 2_IK) / 2_IK + 1_IK)
269+
maxfun_loc = max(MAXFUN_DIM_DFT * n, npt + 1_IK)
267270
end if
268271

269272
if (present(iprint)) then
@@ -305,7 +308,7 @@ subroutine uobyqa(calfun, x, &
305308
if (present(maxhist)) then
306309
maxhist_loc = maxhist
307310
else
308-
maxhist_loc = maxval([maxfun_loc, 1_IK + (n + 1_IK) * (n + 2_IK) / 2_IK, MAXFUN_DIM_DFT * n])
311+
maxhist_loc = maxval([maxfun_loc, npt + 1_IK, MAXFUN_DIM_DFT * n])
309312
end if
310313

311314
! Preprocess the inputs in case some of them are invalid.

matlab/interfaces/bobyqa.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336
mexdir = fullfile(mfiledir, 'private');
337337
fsolver = str2func(get_mexname(solver, precision, debug_flag, variant, mexdir));
338338
try
339+
setenv('GFORTRAN_ERROR_BACKTRACE', '1'); % Enable Fortran backtrace if the compiler is gfortran
339340
[x, fx, exitflag, nf, xhist, fhist] = ...
340341
fsolver(fun, x0, lb, ub, rhobeg, rhoend, eta1, eta2, gamma1, gamma2, ftarget, ...
341342
maxfun, npt, iprint, maxhist, double(output_xhist));

matlab/interfaces/cobyla.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@
440440
% The mexified Fortran Function is a private function generating only private errors;
441441
% however, public errors can occur due to, e.g., evalobj; error handling needed.
442442
try
443+
setenv('GFORTRAN_ERROR_BACKTRACE', '1'); % Enable Fortran backtrace if the compiler is gfortran
443444
[x, fx, constrviolation, nlconstr, exitflag, nf, xhist, fhist, chist, nlchist] = ...
444445
fsolver(funcon, x0, f_x0, nlconstr_x0, Aineq, bineq, Aeq, beq, lb, ub, rhobeg, rhoend, ...
445446
eta1, eta2, gamma1, gamma2, ftarget, ctol, cweight, maxfun, iprint, maxhist, ...

matlab/interfaces/lincoa.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@
367367
% The mexified Fortran function is a private function generating only private errors;
368368
% however, public errors can occur due to, e.g., evalobj; error handling needed
369369
try
370+
setenv('GFORTRAN_ERROR_BACKTRACE', '1'); % Enable Fortran backtrace if the compiler is gfortran
370371
[x, fx, constrviolation, exitflag, nf, xhist, fhist, chist] = ...
371372
fsolver(fun, x0, Aineq, bineq, Aeq, beq, lb, ub, rhobeg, rhoend, eta1, eta2, gamma1, ...
372373
gamma2, ftarget, ctol, cweight, maxfun, npt, iprint, maxhist, double(output_xhist), maxfilt);

matlab/interfaces/newuoa.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@
280280
fsolver = str2func(get_mexname(solver, precision, debug_flag, variant, mexdir));
281281
% The mexified Fortran Function is a private function generating only private errors;
282282
% however, public errors can occur due to, e.g., evalobj; error handling needed.
283+
setenv('GFORTRAN_ERROR_BACKTRACE', '1'); % Enable Fortran backtrace if the compiler is gfortran
283284
[x, fx, exitflag, nf, xhist, fhist] = ...
284285
fsolver(fun, x0, rhobeg, rhoend, eta1, eta2, gamma1, gamma2, ftarget, maxfun, npt, ...
285286
iprint, maxhist, double(output_xhist));

matlab/interfaces/uobyqa.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
% The mexified Fortran Function is a private function generating only private errors;
285285
% however, public errors can occur due to, e.g., evalobj; error handling needed.
286286
try
287+
setenv('GFORTRAN_ERROR_BACKTRACE', '1'); % Enable Fortran backtrace if the compiler is gfortran
287288
[x, fx, exitflag, nf, xhist, fhist] = ...
288289
fsolver(fun, x0, rhobeg, rhoend, eta1, eta2, gamma1, gamma2, ftarget, maxfun, ...
289290
iprint, maxhist, double(output_xhist));

0 commit comments

Comments
 (0)