CONOPT
Loading...
Searching...
No Matches
mp_lincns2.f90
Go to the documentation of this file.
1!> @file mp_lincns2.f90
2!! @ingroup FORTOPENMP_EXAMPLES
3!!
4!!
5!! Large Linear Dense CNS model.
6!! Used to test the inversion routine for a very dense model.
7!! The model is similar to Lincns.f90, but many nonzeros are so small
8!! that they are removed by the factorization routine. The
9!! factorization will therefore have to handle many fairly large
10!! blocks with many pivots and streamlining the updates is a
11!! challenge.
12!!
13!!
14!! For more information about the individual callbacks, please have a look at the source code.
15
16#if defined(_WIN32) && !defined(_WIN64)
17#define dec_directives_win32
18#endif
19
20!> Main program. A simple setup and call of CONOPT
21!!
22Program lincns
23 Use proginfop
25 Use omp_lib
26 IMPLICIT NONE
27!
28! Declare the user callback routines as Integer, External:
29!
30 Integer, External :: lincns_readmatrix ! Mandatory Matrix definition routine defined below
31 Integer, External :: std_status ! Standard callback for displaying solution status
32 Integer, External :: lincns_solution ! Standard callback for displaying solution values
33 Integer, External :: std_message ! Standard callback for managing messages
34 Integer, External :: std_errmsg ! Standard callback for managing error messages
35 Integer, External :: std_triord ! Standard callback for triangular order
36#ifdef dec_directives_win32
37!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lincns_ReadMatrix
38!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
39!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lincns_Solution
40!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
41!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
42!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
43#endif
44!
45! Control vector
46!
47 INTEGER, Dimension(:), Pointer :: cntvect
48 INTEGER :: coi_error
49!
50! Locals
51!
52 Integer :: n
53
54 real*8 time0, time1, time2
55!
56! Create and initialize a Control Vector
57!
58 call startup
59 coi_error = coi_create( cntvect )
60!
61! Define the number of variables and constraints
62!
63 n = 1000
64!
65! Tell CONOPT about the size of the model by populating the Control Vector:
66!
67! Number of variables = N
68!
69 coi_error = max( coi_error, coidef_numvar( cntvect, n ) )
70!
71! Number of equations = M
72!
73 coi_error = max( coi_error, coidef_numcon( cntvect, n ) )
74!
75! Number of nonzeros in the Jacobian: N*N
76!
77 coi_error = max( coi_error, coidef_numnz( cntvect, n*n ) )
78!
79! Number of nonlinear nonzeros. 0 -- the model is linear
80!
81 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) )
82!
83! Square system ( No objective is defined)
84!
85 coi_error = max( coi_error, coidef_square( cntvect, 1 ) )
86!
87! Options file is test.opt
88!
89 coi_error = max( coi_error, coidef_optfile( cntvect, 'mp_lincns2.opt' ) )
90!
91! Tell CONOPT about the callback routines:
92!
93 coi_error = max( coi_error, coidef_readmatrix( cntvect, lincns_readmatrix ) )
94 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
95 coi_error = max( coi_error, coidef_solution( cntvect, lincns_solution ) )
96 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
97 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
98 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
99
100#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
101 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
102#endif
103
104 If ( coi_error .ne. 0 ) THEN
105 write(*,*)
106 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
107 write(*,*)
108 call flog( "Skipping Solve due to setup errors", 1 )
109 ENDIF
110!
111! Start CONOPT with a single thread:
112!
113 time0 = omp_get_wtime()
114 coi_error = coi_solve( cntvect )
115 time1 = omp_get_wtime() - time0
116
117 write(*,*)
118 write(*,*) 'Single Thread: End of LinCns2. Return code=',coi_error
119
120 If ( coi_error /= 0 ) then
121 call flog( "Single Thread: Errors encountered during solution", 1 )
122 elseif ( stacalls == 0 .or. solcalls == 0 ) then
123 call flog( "Single Thread: Status or Solution routine was not called", 1 )
124 elseif ( mstat /= 15 .or. sstat /= 1 ) then ! 15 since the model is linear
125 call flog( "Single Thread: The model or solver status was not (15,1) as expected", 1 )
126 elseif ( miter /= 1 ) then ! we expect one iteration for a linear CNS
127 call flog( "Single Thread: The iteration count was not 1 as expected", 1 )
128 endif
129!
130! Start CONOPT again using multiple threads everywhere:
131!
132 coi_error = max( coi_error, coidef_threads( cntvect, 0 ) ) ! 0 means use as many threads as you can
133 time0 = omp_get_wtime()
134 coi_error = coi_solve( cntvect )
135 time2 = omp_get_wtime() - time0
136
137 write(*,*)
138 write(*,*) 'Multiple Threads: End of LinCns2. Return code=',coi_error
139
140 If ( coi_error /= 0 ) then
141 call flog( "Multiple Threads: Errors encountered during solution", 1 )
142 elseif ( stacalls == 0 .or. solcalls == 0 ) then
143 call flog( "Multiple Threads: Status or Solution routine was not called", 1 )
144 elseif ( mstat /= 15 .or. sstat /= 1 ) then ! 15 since the model is linear
145 call flog( "Multiple Threads: The model or solver status was not (15,1) as expected", 1 )
146 elseif ( miter /= 1 ) then ! we expect one iteration for a linear CNS
147 call flog( "Multiple Threads: The iteration count was not 1 as expected", 1 )
148 endif
149
150 if ( coi_free( cntvect ) /= 0 ) call flog( "Error while freeing control vector", 1 )
151
152 write(*,*)
153 write(*,"('Time for single thread',f10.3)") time1
154 write(*,"('Time for multi thread',f10.3)") time2
155 write(*,"('Speedup ',f10.3)") time1/time2
156 write(*,"('Efficiency ',f10.3)") time1/time2/omp_get_max_threads()
157
158 call flog( "Successful Solve", 0 )
159
160end Program lincns
161!
162! =====================================================================
163! Define information about the model structure
164!
165
166!> Define information about the model
167!!
168!! @include{doc} readMatrix_params.dox
169Integer Function lincns_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
170 colsta, rowno, value, nlflag, n, m, nz, usrmem )
171#ifdef dec_directives_win32
172!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lincns_ReadMatrix
173#endif
174 IMPLICIT NONE
175 integer, intent (in) :: n ! number of variables
176 integer, intent (in) :: m ! number of constraints
177 integer, intent (in) :: nz ! number of nonzeros
178 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
179 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
180 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
181 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
182 ! (not defined here)
183 integer, intent (out), dimension(m) :: type ! vector of equation types
184 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
185 ! (not defined here)
186 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
187 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
188 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
189 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
190 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
191 real*8 usrmem(*) ! optional user memory
192
193 Integer :: i, j, k
194!
195! Define the information for the columns. All are unbounded with initial value I
196!
197 do i = 1, n
198 curr(i) = i
199 enddo
200!
201! All are equalities:
202!
203 do i = 1, m
204 type(i) = 0
205 enddo
206!
207! Right hand sides: Initialize to 0 and revise so the solution becomes
208! x(i) = 1.0
209!
210 do i = 1, m
211 rhs(i) = 0.d0
212 enddo
213!
214! Define the structure and content of the Jacobian:
215! We have a pattern with the largest values on the cross-diagonal from
216! (1,N) to (N,1) with gradually smaller value away from this diagonal.
217!
218 k = 1
219 do i = 1, n
220 colsta(i) = k
221 do j = 1, n
222 rowno(k) = j
223 value(k) = 2.0d0**(-abs(i+j-(n+1))) ! many value are really small
224 rhs(j) = rhs(j) + value(k)
225 k = k + 1
226 enddo
227 enddo
228 colsta(n+1) = k
229
231
232end Function lincns_readmatrix
233
234Integer Function lincns_solution( XVAL, XMAR, XBAS, XSTA, YVAL, YMAR, YBAS, YSTA, N, M, USRMEM )
235#ifdef dec_directives_win32
236!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lincns_Solution
237#endif
238!
239! We write the solution values to the 'Documentation file' on unit 10
240! and test that the solution values are 1.0
241!
242 Use proginfop
243 IMPLICIT NONE
244 INTEGER, Intent(IN) :: n, m
245 INTEGER, Intent(IN), Dimension(N) :: xbas, xsta
246 INTEGER, Intent(IN), Dimension(M) :: ybas, ysta
247 real*8, Intent(IN), Dimension(N) :: xval, xmar
248 real*8, Intent(IN), Dimension(M) :: yval, ymar
249 real*8, Intent(IN OUT) :: usrmem(*)
250
251 INTEGER i
252 CHARACTER*5, Parameter, Dimension(4) :: stat = (/ 'Lower','Upper','Basic','Super' /)
253
254 Logical error
255
256 error = .false.
258 WRITE(10,"(/' Variable Solution value Reduced cost B-stat')")
259 DO i = 1, n
260 WRITE(10,"(1P,I7,E20.6,E16.6,4X,A5 )") i, xval(i), xmar(i), stat(1+xbas(i))
261 If ( abs(xval(i)-1.d0) > 1.d-7 ) then
262 write(10,*) '**** Bad solution value'
263 error = .true.
265 Endif
266 ENDDO
267
268 WRITE(10,"(/' Constrnt Activity level Marginal cost B-stat')")
269 DO i = 1, m
270 WRITE(10,"(1P,I7,E20.6,E16.6,4X,A5 )") i, yval(i), ymar(i), stat(1+ybas(i))
271 ENDDO
272
273 solcalls = solcalls + 1
274
275END Function lincns_solution
276
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:88
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:205
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:289
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer(c_int) function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
Definition conopt.f90:1265
integer(c_int) function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
Definition conopt.f90:1238
integer(c_int) function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
Definition conopt.f90:1212
integer(c_int) function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
Definition conopt.f90:1111
integer(c_int) function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
Definition conopt.f90:1291
integer(c_int) function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
Definition conopt.f90:928
integer(c_int) function coidef_triord(cntvect, coi_triord)
define callback routine for providing the triangular order information.
Definition conopt.f90:1371
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition conopt.f90:293
integer(c_int) function coidef_square(cntvect, square)
square models.
Definition conopt.f90:447
integer(c_int) function coidef_threads(cntvect, threads)
number of threads allowed internally in CONOPT.
Definition conopt.f90:627
integer(c_int) function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition conopt.f90:97
integer(c_int) function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition conopt.f90:121
integer(c_int) function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition conopt.f90:167
integer(c_int) function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition conopt.f90:144
integer(c_int) function coi_create(cntvect)
initializes CONOPT and creates the control vector.
Definition conopt.f90:1726
integer(c_int) function coi_free(cntvect)
frees the control vector.
Definition conopt.f90:1749
integer(c_int) function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition conopt.f90:1625
integer function lincns_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
integer function lincns_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
program lincns
Main program. A simple setup and call of CONOPT.
Definition mp_lincns.f90:19
subroutine flog(msg, code)
Definition comdeclp.f90:48
integer miter
Definition comdeclp.f90:22
subroutine startup
Definition comdeclp.f90:31