22 Integer,
parameter ::
nobs = 700
23 Integer,
parameter ::
dimx = 500
24 real*8,
dimension(Nobs,DimX) ::
a,
b
25 real*8,
dimension(Nobs) ::
obs
36 Integer,
save :: seed = 12359
38 seed = mod(seed*1027+25,1048576)
39 rndx = float(seed)/float(1048576)
51 real*8,
Parameter :: xtarg = -1.0
52 real*8,
Parameter :: noise = 1.0
53 Real,
External :: Rndx
61 o = o +
a(i,j) * xtarg +
b(i,j) * xtarg**2
63 obs(i) = o + noise * rndx()
100 INTEGER,
Dimension(:),
Pointer :: cntvect
103 real*8 time0, time1, time2, time4
104 Integer :: i, maxthread
105 Logical :: same12, same14, same24
115 coi_error = coi_createfort( cntvect )
125 coi_error = max( coi_error,
coidef_optfile( cntvect,
'mp_leastsq14.opt' ) )
137#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
138 coi_error = max( coi_error,
coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
141 If ( coi_error .ne. 0 )
THEN
143 write(*,*)
'**** Fatal Error while loading CONOPT Callback routines.'
145 call flog(
"Skipping Solve due to setup errors", 1 )
147 maxthread = omp_get_max_threads()
148 coi_error = max( coi_error,
coidef_threadc( cntvect, maxthread ) )
153 time0 = omp_get_wtime()
155 time1 = omp_get_wtime() - time0
158 write(*,*)
'End of Least Square example 1 with 1 thread. Return code=',coi_error
160 If ( coi_error /= 0 )
then
161 call flog(
"One thread: Errors encountered during solution", 1 )
162 elseif ( stacalls == 0 .or. solcalls == 0 )
then
163 call flog(
"One thread: Status or Solution routine was not called", 1 )
164 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) )
then
165 call flog(
"One thread: Solver or Model status was not as expected (1,2)", 1 )
166 elseif ( abs(
obj - 19.44434311d0 ) > 1.d-7 )
then
167 call flog(
"One thread: Incorrect objective returned", 1 )
174 time0 = omp_get_wtime()
176 time2 = omp_get_wtime() - time0
179 write(*,*)
'Two threads: End of Least Square example 1. Return code=',coi_error
181 If ( coi_error /= 0 )
then
182 call flog(
"Two threads: Errors encountered during solution", 1 )
183 elseif ( stacalls == 0 .or. solcalls == 0 )
then
184 call flog(
"Two threads: Status or Solution routine was not called", 1 )
185 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) )
then
186 call flog(
"Two threads: Solver or Model status was not as expected (1,2)", 1 )
187 elseif ( abs(
obj - 19.44434311d0 ) > 1.d-7 )
then
188 call flog(
"Two threads: Incorrect objective returned", 1 )
191 if ( maxthread >= 4 )
then
196 time0 = omp_get_wtime()
198 time4 = omp_get_wtime() - time0
201 write(*,*)
'Four threads: End of Least Square example 1. Return code=',coi_error
203 If ( coi_error /= 0 )
then
204 call flog(
"Four threads: Errors encountered during solution", 1 )
205 elseif ( stacalls == 0 .or. solcalls == 0 )
then
206 call flog(
"Four threads: Status or Solution routine was not called", 1 )
207 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) )
then
208 call flog(
"Four threads: Solver or Model status was not as expected (1,2)", 1 )
209 elseif ( abs(
obj - 19.44434311d0 ) > 1.d-7 )
then
210 call flog(
"Four threads: Incorrect objective returned", 1 )
221 write(*,*)
'Solution 2 is different from Solution 1. First diff for index',i
225 if ( maxthread >= 4 )
then
228 write(*,*)
'Solution 4 is different from Solution 1. First diff for index',i
234 write(*,*)
'Solution 4 is different from Solution 2. First diff for index',i
241 write(*,
"('Time for single thread ',f10.3)") time1
242 write(*,
"('Time for two threads',f10.3)") time2
243 if ( maxthread >= 4 ) &
244 write(*,
"('Time for four threads',f10.3)") time4
245 write(*,
"('Speedup two threads',f10.3)") time1/time2
246 write(*,
"('Efficiency two threads',f10.3)") time1/time2/2
247 if ( maxthread >= 4 ) &
248 write(*,
"('Speedup four threads',f10.3)") time1/time4
249 if ( maxthread >= 4 ) &
250 write(*,
"('Efficiency four threads',f10.3)") time1/time4/4
254 if ( same12 .and. same14 .and. same24 )
then
255 write(*,*)
'All solutions are the same'
257 write(*,*)
'The solutions are NOT the same'
258 call flog(
"Solutions are not the same", 1 )
262 if ( coi_free( cntvect ) /= 0 )
call flog(
"Error while freeing control vector", 1 )
264 call flog(
"Successful Solve", 0 )
276 colsta, rowno, value, nlflag, n, m, nz, &
283 integer,
intent (in) :: n
284 integer,
intent (in) :: m
285 integer,
intent (in) :: nz
286 real*8,
intent (in out),
dimension(n) :: lower
287 real*8,
intent (in out),
dimension(n) :: curr
288 real*8,
intent (in out),
dimension(n) :: upper
289 integer,
intent (in out),
dimension(n) :: vsta
291 integer,
intent (out),
dimension(m) ::
type
292 integer,
intent (in out),
dimension(m) :: esta
294 real*8,
intent (in out),
dimension(m) :: rhs
295 integer,
intent (in out),
dimension(n+1) :: colsta
296 integer,
intent (out),
dimension(nz) :: rowno
297 integer,
intent (in out),
dimension(nz) :: nlflag
298 real*8,
intent (in out),
dimension(nz) ::
value
382Integer Function lsq_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
383 n, nz, thread, usrmem )
389 integer,
intent (in) :: n
390 integer,
intent (in) :: rowno
391 integer,
intent (in) :: nz
392 real*8,
intent (in),
dimension(n) :: x
393 real*8,
intent (in out) :: g
394 real*8,
intent (in out),
dimension(n) :: jac
395 integer,
intent (in),
dimension(nz) :: jcnm
397 integer,
intent (in) :: mode
399 integer,
intent (in) :: ignerr
401 integer,
intent (in out) :: errcnt
403 integer,
intent (in) :: thread
411 if ( rowno .eq.
nobs+1 )
then
415 if ( mode .eq. 1 .or. mode .eq. 3 )
then
425 if ( mode .eq. 2 .or. mode .eq. 3 )
then
437 if ( mode .eq. 1 .or. mode .eq. 3 )
then
440 s = s +
a(rowno,j)*x(j) +
b(rowno,j)*x(j)**2
447 if ( mode .eq. 2 .or. mode .eq. 3 )
then
449 jac(j) =
a(rowno,j) + 2.d0*
b(rowno,j)*x(j)
457Integer Function lsq_solution( XVAL, XMAR, XBAS, XSTA, YVAL, YMAR, YBAS, YSTA, N, M, USRMEM )
468 INTEGER,
Intent(IN) :: n, m
469 INTEGER,
Intent(IN),
Dimension(N) :: xbas, xsta
470 INTEGER,
Intent(IN),
Dimension(M) :: ybas, ysta
471 real*8,
Intent(IN),
Dimension(N) :: xval, xmar
472 real*8,
Intent(IN),
Dimension(M) :: yval, ymar
473 real*8,
Intent(IN OUT) :: usrmem(*)
476 CHARACTER*5,
Parameter,
Dimension(4) :: stat = (/
'Lower',
'Upper',
'Basic',
'Super' /)
478 WRITE(10,
"(/' Variable Solution value Reduced cost B-stat')")
480 WRITE(10,
"(1P,I7,E20.6,E16.6,4X,A5 )") i, xval(i), xmar(i), stat(1+xbas(i))
483 WRITE(10,
"(/' Constrnt Activity level Marginal cost B-stat')")
485 WRITE(10,
"(1P,I7,E20.6,E16.6,4X,A5 )") i, yval(i), ymar(i), stat(1+ybas(i))
487 xprim(1:n) = xval(1:n)
492 solcalls = solcalls + 1
integer function std_status(modsta, solsta, iter, objval, usrmem)
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
integer function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
integer function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_threads(cntvect, threads)
number of threads allowed internally in CONOPT.
integer function coidef_debugfv(cntvect, debugfv)
turn Debugging of FDEval on and off.
integer function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
integer function coidef_threadc(cntvect, threadc)
check for thread compatibility.
integer function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
integer function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
integer function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
integer function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
integer function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
integer function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
integer function lsq_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
integer function lsq_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
void defdata()
Defines the data for the problem.
float rndx()
Defines a pseudo random number between 0 and 1.
program leastsquare
Main program. A simple setup and call of CONOPT.
integer function lsq_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
real *8, dimension(nobs+dimx) xprim1
real *8, dimension(nobs+dimx) xprim4
real *8, dimension(nobs, dimx) b
real *8, dimension(nobs+dimx) xprim
real *8, dimension(nobs+dimx) xprim2
real *8, dimension(nobs, dimx) a
real *8, dimension(nobs) obs
subroutine flog(msg, code)