Skip to content

Commit 5e5e871

Browse files
committed
250812.173457.CST remove the internal subroutine calcfc_internal from cobylb.f90; as of 20250812, internal subroutines passed as actual arguments implies executable stacks for current compilers; executable stacks are not allowed by glibc 2.4+; see https://fortran-lang.discourse.group/t/implementation-of-a-parametrized-objective-function-without-using-module-variables-or-internal-subroutines, JuliaLang/julia#57250, https://forums.gentoo.org/viewtopic-p-8866168.html
1 parent a68377d commit 5e5e871

File tree

6 files changed

+25
-1013
lines changed

6 files changed

+25
-1013
lines changed

.development

fortran/cobyla/cobylb.f90

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module cobylb_mod
1717
!
1818
! Started: July 2021
1919
!
20-
! Last Modified: Thursday, April 04, 2024 AM11:23:03
20+
! Last Modified: Tue 12 Aug 2025 04:58:06 PM CST
2121
!--------------------------------------------------------------------------------------------------!
2222

2323
implicit none
@@ -202,8 +202,8 @@ subroutine cobylb(calcfc, iprint, maxfilt, maxfun, amat, bvec, ctol, cweight, et
202202
! function value (regardless of the constraint violation), and SIM(:, 1:N) holds the displacements
203203
! from the other vertices to SIM(:, N+1). FVAL, CONMAT, and CVAL hold the function values,
204204
! constraint values, and constraint violations on the vertices in the order corresponding to SIM.
205-
call initxfc(calcfc_internal, iprint, maxfun, constr, ctol, f, ftarget, rhobeg, x, nf, chist, conhist, &
206-
& conmat, cval, fhist, fval, sim, simi, xhist, evaluated, subinfo)
205+
call initxfc(calcfc, iprint, maxfun, amat, bvec, constr, ctol, f, ftarget, rhobeg, x, nf, chist, &
206+
& conhist, conmat, cval, fhist, fval, sim, simi, xhist, evaluated, subinfo)
207207

208208
! Report the current best value, and check if user asks for early termination.
209209
terminate = .false.
@@ -393,7 +393,8 @@ subroutine cobylb(calcfc, iprint, maxfilt, maxfun, amat, bvec, ctol, cweight, et
393393
cstrv = cval(j)
394394
else
395395
! Evaluate the objective and constraints at X, taking care of possible Inf/NaN values.
396-
call evaluate(calcfc_internal, x, f, constr)
396+
constr(1:m_lcon) = matprod(x, amat) - bvec
397+
call evaluate(calcfc, x, f, constr(m_lcon + 1:m))
397398
cstrv = maximum([ZERO, constr])
398399
nf = nf + 1_IK
399400
! Save X, F, CONSTR, CSTRV into the history.
@@ -585,7 +586,8 @@ subroutine cobylb(calcfc, iprint, maxfilt, maxfun, amat, bvec, ctol, cweight, et
585586
cstrv = cval(j)
586587
else
587588
! Evaluate the objective and constraints at X, taking care of possible Inf/NaN values.
588-
call evaluate(calcfc_internal, x, f, constr)
589+
constr(1:m_lcon) = matprod(x, amat) - bvec
590+
call evaluate(calcfc, x, f, constr(m_lcon + 1:m))
589591
cstrv = maximum([ZERO, constr])
590592
nf = nf + 1_IK
591593
! Save X, F, CONSTR, CSTRV into the history.
@@ -650,7 +652,8 @@ subroutine cobylb(calcfc, iprint, maxfilt, maxfun, amat, bvec, ctol, cweight, et
650652
! Ensure that D has not been updated after SHORTD == TRUE occurred, or the code below is incorrect.
651653
x = sim(:, n + 1) + d
652654
if (info == SMALL_TR_RADIUS .and. shortd .and. norm(x - sim(:, n + 1)) > 1.0E-3_RP * rhoend .and. nf < maxfun) then
653-
call evaluate(calcfc_internal, x, f, constr)
655+
constr(1:m_lcon) = matprod(x, amat) - bvec
656+
call evaluate(calcfc, x, f, constr(m_lcon + 1:m))
654657
cstrv = maximum([ZERO, constr])
655658
nf = nf + 1_IK
656659
! Save X, F, CONSTR, CSTRV into the history.
@@ -702,25 +705,6 @@ subroutine cobylb(calcfc, iprint, maxfilt, maxfun, amat, bvec, ctol, cweight, et
702705
& 'No point in the history is better than X', srname)
703706
end if
704707

705-
706-
contains
707-
708-
709-
subroutine calcfc_internal(x_internal, f_internal, constr_internal)
710-
!--------------------------------------------------------------------------------------------------!
711-
! This internal subroutine evaluates the objective function and ALL the constraints.
712-
! In MATLAB/Python/R/Julia, this can be implemented as a lambda function / anonymous function.
713-
!--------------------------------------------------------------------------------------------------!b
714-
implicit none
715-
! Inputs
716-
real(RP), intent(in) :: x_internal(:)
717-
! Outputs
718-
real(RP), intent(out) :: f_internal
719-
real(RP), intent(out) :: constr_internal(:)
720-
constr_internal(1:m_lcon) = matprod(x_internal, amat) - bvec
721-
call calcfc(x_internal, f_internal, constr_internal(m_lcon + 1:m))
722-
end subroutine calcfc_internal
723-
724708
end subroutine cobylb
725709

726710

fortran/cobyla/initialize.f90

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module initialize_cobyla_mod
88
!
99
! Started: July 2021
1010
!
11-
! Last Modified: Saturday, March 16, 2024 PM04:48:11
11+
! Last Modified: Tue 12 Aug 2025 04:57:48 PM CST
1212
!--------------------------------------------------------------------------------------------------!
1313

1414
implicit none
@@ -19,8 +19,8 @@ module initialize_cobyla_mod
1919
contains
2020

2121

22-
subroutine initxfc(calcfc, iprint, maxfun, constr0, ctol, f0, ftarget, rhobeg, x0, nf, chist, &
23-
& conhist, conmat, cval, fhist, fval, sim, simi, xhist, evaluated, info)
22+
subroutine initxfc(calcfc, iprint, maxfun, amat, bvec, constr0, ctol, f0, ftarget, rhobeg, x0, nf, &
23+
& chist, conhist, conmat, cval, fhist, fval, sim, simi, xhist, evaluated, info)
2424
!--------------------------------------------------------------------------------------------------!
2525
! This subroutine does the initialization concerning X, function values, and constraints.
2626
!--------------------------------------------------------------------------------------------------!
@@ -33,7 +33,7 @@ subroutine initxfc(calcfc, iprint, maxfun, constr0, ctol, f0, ftarget, rhobeg, x
3333
use, non_intrinsic :: history_mod, only : savehist
3434
use, non_intrinsic :: infnan_mod, only : is_nan, is_posinf, is_finite
3535
use, non_intrinsic :: infos_mod, only : INFO_DFT
36-
use, non_intrinsic :: linalg_mod, only : eye, inv, isinv, maximum
36+
use, non_intrinsic :: linalg_mod, only : eye, inv, isinv, maximum, matprod
3737
use, non_intrinsic :: message_mod, only : fmsg
3838
use, non_intrinsic :: pintrf_mod, only : OBJCON
3939

@@ -43,6 +43,8 @@ subroutine initxfc(calcfc, iprint, maxfun, constr0, ctol, f0, ftarget, rhobeg, x
4343
procedure(OBJCON) :: calcfc ! N.B.: INTENT cannot be specified if a dummy procedure is not a POINTER
4444
integer(IK), intent(in) :: iprint
4545
integer(IK), intent(in) :: maxfun
46+
real(RP), intent(in) :: amat(:, :) ! AMAT(N, M_LCON)
47+
real(RP), intent(in) :: bvec(:) ! BVEC(M_LCON)
4648
real(RP), intent(in) :: constr0(:) ! CONSTR0(M)
4749
real(RP), intent(in) :: ctol
4850
real(RP), intent(in) :: f0
@@ -70,6 +72,7 @@ subroutine initxfc(calcfc, iprint, maxfun, constr0, ctol, f0, ftarget, rhobeg, x
7072
integer(IK) :: j
7173
integer(IK) :: k
7274
integer(IK) :: m
75+
integer(IK) :: m_lcon
7376
integer(IK) :: maxchist
7477
integer(IK) :: maxconhist
7578
integer(IK) :: maxfhist
@@ -84,6 +87,7 @@ subroutine initxfc(calcfc, iprint, maxfun, constr0, ctol, f0, ftarget, rhobeg, x
8487
real(RP), parameter :: itol = TENTH
8588

8689
! Sizes
90+
m_lcon = int(size(bvec), kind(m_lcon))
8791
m = int(size(conmat, 1), kind(m))
8892
n = int(size(sim, 1), kind(n))
8993
maxchist = int(size(chist), kind(maxchist))
@@ -97,6 +101,7 @@ subroutine initxfc(calcfc, iprint, maxfun, constr0, ctol, f0, ftarget, rhobeg, x
97101
call assert(m >= 0, 'M >= 0', srname)
98102
call assert(n >= 1, 'N >= 1', srname)
99103
call assert(abs(iprint) <= 3, 'IPRINT is 0, 1, -1, 2, -2, 3, or -3', srname)
104+
call assert(size(amat, 1) == n .and. size(amat, 2) == size(bvec), 'SIZE(AMAT) == [N, SIZE(BVEC)]', srname)
100105
call assert(size(conmat, 1) == m .and. size(conmat, 2) == n + 1, 'SIZE(CONMAT) = [M, N+1]', srname)
101106
call assert(size(cval) == n + 1, 'SIZE(CVAL) == N+1', srname)
102107
call assert(size(fval) == n + 1, 'SIZE(FVAL) == N+1', srname)
@@ -156,7 +161,8 @@ subroutine initxfc(calcfc, iprint, maxfun, constr0, ctol, f0, ftarget, rhobeg, x
156161
else
157162
j = k - 1_IK
158163
x(j) = x(j) + rhobeg
159-
call evaluate(calcfc, x, f, constr)
164+
constr(1:m_lcon) = matprod(x, amat) - bvec
165+
call evaluate(calcfc, x, f, constr(m_lcon + 1:m))
160166
end if
161167
cstrv = maximum([ZERO, constr])
162168

fortran/newuoa/initialize.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module initialize_newuoa_mod
88
!
99
! Dedicated to the late Professor M. J. D. Powell FRS (1936--2015).
1010
!
11-
! Last Modified: Saturday, March 16, 2024 PM02:13:43
11+
! Last Modified: Tue 12 Aug 2025 04:48:20 PM CST
1212
!--------------------------------------------------------------------------------------------------!
1313

1414
implicit none
@@ -134,7 +134,7 @@ subroutine initxf(calfun, iprint, maxfun, ftarget, rhobeg, x0, ij, kopt, nf, fhi
134134
! Initialize XHIST, FHIST, and FVAL. Otherwise, compilers may complain that they are not
135135
! (completely) initialized if the initialization aborts due to abnormality (see CHECKEXIT).
136136
! N.B.: 1. Initializing them to NaN would be more reasonable (NaN is not available in Fortran).
137-
! 2. Do not initialize the models if the current initialization aborts due to abnormality. Otherwise,
137+
! 2. Do not initialize the models if the current initialization aborts due to abnormality. Otherwise,
138138
! errors or exceptions may occur, as FVAL and XPT etc are uninitialized.
139139
xhist = -REALMAX
140140
fhist = REALMAX

0 commit comments

Comments
 (0)