21#if defined(_WIN32) && !defined(_WIN64)
22#define dec_directives_win32
26 Integer,
parameter :: Nobs = 700
27 Integer,
parameter :: DimX = 500
28 real*8,
dimension(Nobs,DimX) ::
a,
b
29 real*8,
dimension(Nobs) ::
obs
40 Integer,
save :: seed = 12359
42 seed = mod(seed*1027+25,1048576)
43 rndx = float(seed)/float(1048576)
55 real*8,
Parameter :: xtarg = -1.0
56 real*8,
Parameter :: noise = 1.0
57 Real,
External :: Rndx
65 o = o +
a(i,j) * xtarg +
b(i,j) * xtarg**2
67 obs(i) = o + noise * rndx()
93#ifdef dec_directives_win32
104 INTEGER,
Dimension(:),
Pointer :: cntvect
107 real*8 time0, time1, time2, time4
108 Integer :: i, maxthread
109 Logical :: same12, same14, same24
129 coi_error = max( coi_error,
coidef_optfile( cntvect,
'mp_leastsq14.opt' ) )
141#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
142 coi_error = max( coi_error,
coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
145 If ( coi_error .ne. 0 )
THEN
147 write(*,*)
'**** Fatal Error while loading CONOPT Callback routines.'
149 call flog(
"Skipping Solve due to setup errors", 1 )
151 maxthread = omp_get_max_threads()
152 coi_error = max( coi_error,
coidef_threadc( cntvect, maxthread ) )
157 time0 = omp_get_wtime()
159 time1 = omp_get_wtime() - time0
162 write(*,*)
'End of Least Square example 1 with 1 thread. Return code=',coi_error
164 If ( coi_error /= 0 )
then
165 call flog(
"One thread: Errors encountered during solution", 1 )
166 elseif ( stacalls == 0 .or. solcalls == 0 )
then
167 call flog(
"One thread: Status or Solution routine was not called", 1 )
168 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) )
then
169 call flog(
"One thread: Solver or Model status was not as expected (1,2)", 1 )
170 elseif ( abs(
obj - 19.44434311d0 ) > 1.d-7 )
then
171 call flog(
"One thread: Incorrect objective returned", 1 )
178 time0 = omp_get_wtime()
180 time2 = omp_get_wtime() - time0
183 write(*,*)
'Two threads: End of Least Square example 1. Return code=',coi_error
185 If ( coi_error /= 0 )
then
186 call flog(
"Two threads: Errors encountered during solution", 1 )
187 elseif ( stacalls == 0 .or. solcalls == 0 )
then
188 call flog(
"Two threads: Status or Solution routine was not called", 1 )
189 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) )
then
190 call flog(
"Two threads: Solver or Model status was not as expected (1,2)", 1 )
191 elseif ( abs(
obj - 19.44434311d0 ) > 1.d-7 )
then
192 call flog(
"Two threads: Incorrect objective returned", 1 )
195 if ( maxthread >= 4 )
then
200 time0 = omp_get_wtime()
202 time4 = omp_get_wtime() - time0
205 write(*,*)
'Four threads: End of Least Square example 1. Return code=',coi_error
207 If ( coi_error /= 0 )
then
208 call flog(
"Four threads: Errors encountered during solution", 1 )
209 elseif ( stacalls == 0 .or. solcalls == 0 )
then
210 call flog(
"Four threads: Status or Solution routine was not called", 1 )
211 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) )
then
212 call flog(
"Four threads: Solver or Model status was not as expected (1,2)", 1 )
213 elseif ( abs(
obj - 19.44434311d0 ) > 1.d-7 )
then
214 call flog(
"Four threads: Incorrect objective returned", 1 )
225 write(*,*)
'Solution 2 is different from Solution 1. First diff for index',i
229 if ( maxthread >= 4 )
then
232 write(*,*)
'Solution 4 is different from Solution 1. First diff for index',i
238 write(*,*)
'Solution 4 is different from Solution 2. First diff for index',i
245 write(*,
"('Time for single thread ',f10.3)") time1
246 write(*,
"('Time for two threads',f10.3)") time2
247 if ( maxthread >= 4 ) &
248 write(*,
"('Time for four threads',f10.3)") time4
249 write(*,
"('Speedup two threads',f10.3)") time1/time2
250 write(*,
"('Efficiency two threads',f10.3)") time1/time2/2
251 if ( maxthread >= 4 ) &
252 write(*,
"('Speedup four threads',f10.3)") time1/time4
253 if ( maxthread >= 4 ) &
254 write(*,
"('Efficiency four threads',f10.3)") time1/time4/4
258 if ( same12 .and. same14 .and. same24 )
then
259 write(*,*)
'All solutions are the same'
261 write(*,*)
'The solutions are NOT the same'
262 call flog(
"Solutions are not the same", 1 )
266 if (
coi_free( cntvect ) /= 0 )
call flog(
"Error while freeing control vector", 1 )
268 call flog(
"Successful Solve", 0 )
279Integer Function lsq_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
280 colsta, rowno, value, nlflag, n, m, nz, &
282#ifdef dec_directives_win32
287 integer,
intent (in) :: n
288 integer,
intent (in) :: m
289 integer,
intent (in) :: nz
290 real*8,
intent (in out),
dimension(n) :: lower
291 real*8,
intent (in out),
dimension(n) :: curr
292 real*8,
intent (in out),
dimension(n) :: upper
293 integer,
intent (in out),
dimension(n) :: vsta
295 integer,
intent (out),
dimension(m) ::
type
296 integer,
intent (in out),
dimension(m) :: esta
298 real*8,
intent (in out),
dimension(m) :: rhs
299 integer,
intent (in out),
dimension(n+1) :: colsta
300 integer,
intent (out),
dimension(nz) :: rowno
301 integer,
intent (in out),
dimension(nz) :: nlflag
302 real*8,
intent (in out),
dimension(nz) ::
value
386Integer Function lsq_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
387 n, nz, thread, usrmem )
388#ifdef dec_directives_win32
393 integer,
intent (in) :: n
394 integer,
intent (in) :: rowno
395 integer,
intent (in) :: nz
396 real*8,
intent (in),
dimension(n) :: x
397 real*8,
intent (in out) :: g
398 real*8,
intent (in out),
dimension(n) :: jac
399 integer,
intent (in),
dimension(nz) :: jcnm
401 integer,
intent (in) :: mode
403 integer,
intent (in) :: ignerr
405 integer,
intent (in out) :: errcnt
407 integer,
intent (in) :: thread
415 if ( rowno .eq.
nobs+1 )
then
419 if ( mode .eq. 1 .or. mode .eq. 3 )
then
429 if ( mode .eq. 2 .or. mode .eq. 3 )
then
441 if ( mode .eq. 1 .or. mode .eq. 3 )
then
444 s = s +
a(rowno,j)*x(j) +
b(rowno,j)*x(j)**2
451 if ( mode .eq. 2 .or. mode .eq. 3 )
then
453 jac(j) =
a(rowno,j) + 2.d0*
b(rowno,j)*x(j)
461Integer Function lsq_solution( XVAL, XMAR, XBAS, XSTA, YVAL, YMAR, YBAS, YSTA, N, M, USRMEM )
462#ifdef dec_directives_win32
472 INTEGER,
Intent(IN) :: n, m
473 INTEGER,
Intent(IN),
Dimension(N) :: xbas, xsta
474 INTEGER,
Intent(IN),
Dimension(M) :: ybas, ysta
475 real*8,
Intent(IN),
Dimension(N) :: xval, xmar
476 real*8,
Intent(IN),
Dimension(M) :: yval, ymar
477 real*8,
Intent(IN OUT) :: usrmem(*)
480 CHARACTER*5,
Parameter,
Dimension(4) :: stat = (/
'Lower',
'Upper',
'Basic',
'Super' /)
482 WRITE(10,
"(/' Variable Solution value Reduced cost B-stat')")
484 WRITE(10,
"(1P,I7,E20.6,E16.6,4X,A5 )") i, xval(i), xmar(i), stat(1+xbas(i))
487 WRITE(10,
"(/' Constrnt Activity level Marginal cost B-stat')")
489 WRITE(10,
"(1P,I7,E20.6,E16.6,4X,A5 )") i, yval(i), ymar(i), stat(1+ybas(i))
491 xprim(1:n) = xval(1:n)
494 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(c_int) function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer(c_int) function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer(c_int) function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer(c_int) function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer(c_int) function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer(c_int) function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
integer(c_int) function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
integer(c_int) function coidef_threadc(cntvect, threadc)
check for thread compatibility.
integer(c_int) function coidef_debugfv(cntvect, debugfv)
turn Debugging of FDEval on and off.
integer(c_int) function coidef_threads(cntvect, threads)
number of threads allowed internally in CONOPT.
integer(c_int) function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
integer(c_int) function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
integer(c_int) function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
integer(c_int) function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
integer(c_int) function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
integer(c_int) function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
integer(c_int) function coi_create(cntvect)
initializes CONOPT and creates the control vector.
integer(c_int) function coi_free(cntvect)
frees the control vector.
integer(c_int) 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)