17#if defined(_WIN32) && !defined(_WIN64)
18#define dec_directives_win32
23 integer,
Parameter :: Tmin = 21,
tmax = 60
24 integer,
Parameter :: Vpp = 7
25 integer,
Parameter ::
epp = 6
26 real*8,
Dimension(Tmax, Tmax*Vpp) :: xkeep
27 real*8,
Dimension(Tmax, Tmax*Vpp) :: xkeep1
28 real*8,
Dimension(Tmax, Tmax*Vpp) :: xkeep2
34 Integer,
Dimension(:),
Pointer :: cntvect
61#ifdef dec_directives_win32
73 Integer,
Dimension(:),
pointer :: cntvect
74 real*8,
Dimension(:),
Allocatable :: usrmem
80 Integer :: maxthreadomp, maxthreadcon, maxthread, thread
81 real*8 time0, time1, time2, time3, time4
82 Logical dif12, dif13, dif23
83 Integer :: ndif12, ndif13, ndif23
92 open(12,file=
'Thread1.txt',status=
'Unknown');
93 open(13,file=
'Thread2.txt',status=
'Unknown');
94 open(14,file=
'Thread3.txt',status=
'Unknown');
95 open(15,file=
'Thread4.txt',status=
'Unknown');
102 maxthreadomp = omp_get_max_threads()
103 write(*,*)
'Initial MaxThread=',maxthreadomp,
' from OpenMP'
105 write(*,*)
'Initial MaxThread=',maxthreadcon,
' from COIGET_MaxThreads'
106 If ( maxthreadcon /= maxthreadomp )
Then
107 call flog(
"Error in COIGET_MaxThread functions", 1 )
109 If ( maxthreadomp < 4 )
then
111 call omp_set_num_threads(maxthread)
112 write(*,*)
'Revised MaxThread=',maxthread
114 maxthread = maxthreadomp
117 write(*,*)
'Revised MaxThread=',maxthreadcon,
' from COIGET_MaxThreads'
127 Allocate( usrmem(2*maxthread) )
128 DO thread = 1, maxthread
133 coi_error = max( coi_error,
coi_create( cntvect ) )
157 coi_error = max( coi_error,
coidef_usrmem( cntvect, usrmem(2*thread-1) ) )
159#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
160 coi_error = max( coi_error,
coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
164 If ( coi_error .ne. 0 )
THEN
166 write(*,*)
'**** Fatal Error while loading CONOPT Callback routines.'
168 call flog(
"Skipping First Solve due to setup errors", 1 )
181 time0 = omp_get_wtime()
184 write(*,*)
'Sequential: Solving period',t,
' using thread',thread
185 write(11,*)
'Sequential: Starting period',t,
' using thread',thread
187 write(10,*)
'Sequential: Starting period',t,
' using thread',thread
188 usrmem(2*thread-1) = dfloat(t)
189 usrmem(2*thread ) = dfloat(thread)
203 coi_error = max( coi_error,
coidef_numnz( cntvect, 17 * t + 4 * (t-1) ) )
210 If ( coi_error .ne. 0 )
THEN
212 write(*,*)
'**** Fatal Error while Solving for T=',t,
' COI_Error=',coi_error
214 call flog(
"Errors encountered during Solve.", 1 )
217 time1 = omp_get_wtime() - time0
219 time0 = omp_get_wtime()
220 write(*,*)
'Finished single-thread loop. Going to start static multi-threading loop'
221 write(11,*)
'Finished single-thread loop. Going to start static multi-threading loop'
223 write(10,*)
'Finished single-thread loop. Going to start static multi-threading loop'
233 thread = 1+omp_get_thread_num()
236 write(*,*)
'Static: Solving period',t,
' using thread',thread
237 write(11,*)
'Static: Starting period',t,
' using thread',thread
239 write(10,*)
'Static: Starting period',t,
' using thread',thread
240 usrmem(2*thread-1) = dfloat(t)
241 usrmem(2*thread ) = dfloat(thread)
255 coi_error = max( coi_error,
coidef_numnz( cntvect, 17 * t + 4 * (t-1) ) )
261 If ( maxthreadcon /= 1 )
Then
264 write(*,*)
'**** Fatal Error in MaxThread while Solving Parallel for T=',t
268 coi_error = abs(coi_error)
272 If ( coi_error .ne. 0 )
THEN
274 write(*,*)
'**** Fatal Error while Solving Static OMP loop. COI_Error=',coi_error
276 call flog(
"Errors encountered during Solvein Static OMP loop.", 1 )
278 time2 = omp_get_wtime() - time0
280 time0 = omp_get_wtime()
281 write(*,*)
'Finished static multi-thread loop. Going to start dynamic multi-threading loop'
282 write(11,*)
'Finished static multi-thread loop. Going to start dynamic multi-threading loop'
284 write(10,*)
'Finished static multi-thread loop. Going to start dynamic multi-threading loop'
291 thread = 1+omp_get_thread_num()
294 write(*,*)
'Dynamic: Solving period',t,
' using thread',thread
295 write(11,*)
'Dynamic: Starting period',t,
' using thread',thread
297 write(10,*)
'Dynamic: Starting period',t,
' using thread',thread
298 usrmem(2*thread-1) = dfloat(t)
299 usrmem(2*thread ) = dfloat(thread)
313 coi_error = max( coi_error,
coidef_numnz( cntvect, 17 * t + 4 * (t-1) ) )
320 coi_error = abs(coi_error)
323 If ( coi_error .ne. 0 )
THEN
325 write(*,*)
'**** Fatal Error while Solving Dynamic OMP loop. COI_Error=',coi_error
327 call flog(
"Errors encountered during Solvein Dynamic OMP loop.", 1 )
329 time3 = omp_get_wtime() - time0
330 time0 = omp_get_wtime()
331 write(*,*)
'Finished dynamic multi-thread loop. Going to start guided multi-threading loop'
332 write(11,*)
'Finished dynamic multi-thread loop. Going to start guided multi-threading loop'
334 write(10,*)
'Finished dynamic multi-thread loop. Going to start guided multi-threading loop'
341 thread = 1+omp_get_thread_num()
344 write(*,*)
'Guided: Solving period',t,
' using thread',thread
345 write(11,*)
'Guided: Starting period',t,
' using thread',thread
347 write(10,*)
'Guided: Starting period',t,
' using thread',thread
348 usrmem(2*thread-1) = dfloat(t)
349 usrmem(2*thread ) = dfloat(thread)
363 coi_error = max( coi_error,
coidef_numnz( cntvect, 17 * t + 4 * (t-1) ) )
370 coi_error = abs(coi_error)
373 If ( coi_error .ne. 0 )
THEN
375 write(*,*)
'**** Fatal Error while Solving Guided OMP loop. COI_Error=',coi_error
377 call flog(
"Errors encountered during Solvein Guided OMP loop.", 1 )
379 write(*,*)
'Finished guided multi-thread loop.'
380 write(11,*)
'Finished guided multi-thread loop.'
382 write(10,*)
'Finished guided multi-thread loop.'
383 time4 = omp_get_wtime() - time0
387 DO thread = 1, maxthread
393 write(*,*)
'End of PinThread Model.'
396 dif12 = .false.; ndif12 = 0
399 if ( xkeep1(t,i) .ne. xkeep2(t,i) )
then
402 if ( ndif12 <= 10 ) &
403 write(*,1000) t, i,
thbase(t),
' XBase =',xkeep1(t,i),
thstatic(t),
' XStat =',xkeep2(t,i), (xkeep1(t,i)-xkeep2(t,i))
408 write(*,*)
'Difference between base and static solutions =',dif12
410 dif13 = .false.; ndif13 = 0
413 if ( xkeep(t,i) .ne. xkeep1(t,i) )
then
416 if ( ndif13 <= 10 ) &
417 write(*,1000) t, i,
thbase(t),
' XBase =',xkeep1(t,i),
thdynamic(t),
' XDynm =',xkeep(t,i), (xkeep1(t,i)-xkeep(t,i))
422 write(*,*)
'Difference between base and dynamic solutions =',dif13
424 dif23 = .false.; ndif23 = 0
427 if ( xkeep(t,i) .ne. xkeep2(t,i) )
then
430 if ( ndif23 <= 10 ) &
431 write(*,1000) t, i,
thstatic(t),
' XStat =',xkeep2(t,i),
thdynamic(t),
' XDynm =',xkeep(t,i), (xkeep2(t,i)-xkeep(t,i))
436 write(*,*)
'Difference between static and dynamic solutions=',dif23
4381000
Format(
'T=',i2,
' I=',i3,
' Thread=',i2,a9,1p,e22.14/ &
439 10x,
' Thread=',i2,a9,1p,e22.14,
' Diff=',1p,e22.14)
440 write(*,*)
'MaxThread =',maxthread
441 write(*,*)
'Time Single thread =',time1
442 write(*,*)
'Time Static Multi thread =',time2
443 write(*,*)
'Time Dynamic Multi thread =',time3
444 write(*,*)
'Time Guided Multi thread =',time4
445 write(*,*)
'Speedup, static =',time1/time2
446 write(*,*)
'Speedup, dynamic =',time1/time3
447 write(*,*)
'Efficiency, static =',time1/time2/maxthread
448 write(*,*)
'Efficiency, dynamic =',time1/time3/maxthread
449 write(*,*)
'Efficiency, guided =',time1/time4/maxthread
456 call flog(
"Successful Solve", 0 )
459 call flog(
"Base and Static solutions are not identical",1)
460 else if ( dif13 )
then
461 call flog(
"Base and Dynamic solutions are not identical",1)
462 else if ( dif23 )
then
463 call flog(
"Static and Dynamic solutions are not identical",1)
465 call flog(
"Successful Solve", 0 )
475Integer Function pin_option( ncall, rval, ival, lval, usrmem, name )
476#ifdef dec_directives_win32
479 integer ncall, ival, lval
480 character(Len=*) :: name
505Integer Function pin_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
506 colsta, rowno, value, nlflag, n, m, nz, usrmem )
507#ifdef dec_directives_win32
512 integer,
intent (in) :: n
513 integer,
intent (in) :: m
514 integer,
intent (in) :: nz
515 real*8,
intent (in out),
dimension(n) :: lower
516 real*8,
intent (in out),
dimension(n) :: curr
517 real*8,
intent (in out),
dimension(n) :: upper
518 integer,
intent (in out),
dimension(n) :: vsta
520 integer,
intent (out),
dimension(m) ::
type
521 integer,
intent (in out),
dimension(m) :: esta
523 real*8,
intent (in out),
dimension(m) :: rhs
524 integer,
intent (in out),
dimension(n+1) :: colsta
525 integer,
intent (out),
dimension(nz) :: rowno
526 integer,
intent (in out),
dimension(nz) :: nlflag
527 real*8,
intent (in out),
dimension(nz) ::
value
530 Integer :: it, is, i, icol, iz
559 curr(is+4) = curr(is+1) - curr(is+3)
560 if ( it .gt. 1 )
then
561 curr(is+5) = curr(is+5-
vpp) - curr(is+4)
563 curr(is+5) = 500.d0 - curr(is+4)
591 rhs(is+1) = 1.d0+2.3d0*1.015d0**(it-1)
596 rhs(2) = rhs(2) + 0.87d0*18.d0
600 rhs(3) = 0.75d0*6.5d0
655 if ( it .lt. t )
then
673 if ( it .lt. t )
then
696 if ( it .lt. t )
then
730 if ( it .lt. t )
then
757 value(iz) = 1.05d0**(1-it)
778Integer Function pin_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
779 n, nz, thread, usrmem )
780#ifdef dec_directives_win32
787 integer,
intent (in) :: n
788 integer,
intent (in) :: rowno
789 integer,
intent (in) :: nz
790 real*8,
intent (in),
dimension(n) :: x
791 real*8,
intent (in out) :: g
792 real*8,
intent (in out),
dimension(n) :: jac
793 integer,
intent (in),
dimension(nz) :: jcnm
795 integer,
intent (in) :: mode
797 integer,
intent (in) :: ignerr
799 integer,
intent (in out) :: errcnt
801 integer,
intent (in) :: thread
806 Integer :: maxthreads
816 If ( maxthreads /= 1 )
Then
818 write(*,*)
'In parallel FDEval. COIGET_MaxThreads=',maxthreads,
' and expected 1.'
820 call flog(
"Error in COIGET_MaxThreads in Parallel FDEval", 1 )
825 write(*,*)
'In parallel FDEval. COIGET_MaxThreads=',maxthreads,
' and expected',
maxthreadini
827 call flog(
"Error in COIGET_MaxThreads in Sequential FDEval", 1 )
837 if ( rowno == (it-1)*
epp+3 )
then
841 h1 = (1.1d0+0.1d0*x(is+6))
842 h2 = 1.02d0**(-x(is+2)/7.d0)
843 if ( mode == 1 .or. mode == 3 )
then
846 if ( mode == 2 .or. mode == 3 )
then
847 jac(is+2) = h1*h2*log(1.02d0)/7.d0
848 jac(is+6) = -h2*0.1d0
850 elseif ( rowno == (it-1)*
epp+7 )
then
854 if ( mode == 1 .or. mode == 3 )
then
855 g = -x(is+4)*(x(is+6)-250.d0/x(is+5))
857 if ( mode == 2 .or. mode == 3 )
then
858 jac(is+4) = -(x(is+6)-250.d0/x(is+5))
859 jac(is+5) = -x(is+4)*250d0/x(is+5)**2
867 write(*,*)
'Error. FDEval called with rowno=',rowno
876INTEGER FUNCTION pin_solution( XVAL, XMAR, XBAS, XSTA, YVAL, YMAR, YBAS, YSTA, N, M, USRMEM )
877#ifdef dec_directives_win32
883 INTEGER,
Intent(IN) :: n, m
884 INTEGER,
Intent(IN),
Dimension(N) :: xbas, xsta
885 INTEGER,
Intent(IN),
Dimension(M) :: ybas, ysta
886 real*8,
Intent(IN),
Dimension(N) :: xval, xmar
887 real*8,
Intent(IN),
Dimension(M) :: yval, ymar
888 real*8,
Intent(IN OUT) :: usrmem(*)
893 xkeep(t, 1:n) = xval(1:n)
901Integer Function pin_status( MODSTA, SOLSTA, ITER, OBJVAL, USRMEM )
902#ifdef dec_directives_win32
908 INTEGER,
Intent(IN) :: modsta, solsta, iter
909 real*8,
Intent(IN) :: objval
910 real*8,
Intent(IN OUT) :: usrmem(*)
912 stacalls = stacalls + 1
921Integer Function pin_message( SMSG, DMSG, NMSG, LLEN, USRMEM, MSGV )
922#ifdef dec_directives_win32
929 INTEGER,
Intent(IN) :: smsg, dmsg, nmsg
930 INTEGER,
Intent(IN) ,
Dimension(*) :: llen
931 CHARACTER(Len=133),
Intent(IN),
Dimension(*) :: msgv
932 real*8,
Intent(IN OUT) :: usrmem(*)
934 integer :: t, thread, i
940 write(11,
"(i2,':',A)") t, msgv(i)(1:llen(i))
945 write(11+thread,
"(i2,':',A)") t, msgv(i)(1:llen(i))
953Integer Function pin_errmsg( ROWNO, COLNO, POSNO, MSGLEN, USRMEM, MSG )
954#ifdef dec_directives_win32
961 INTEGER,
Intent(IN) :: rowno, colno, posno, msglen
962 CHARACTER(Len=*),
Intent(IN) :: msg
963 real*8,
Intent(IN OUT) :: usrmem(*)
974 IF ( rowno .EQ. 0 )
THEN
975 WRITE(11,1001) t, colno, msg(1:msglen)
976 ELSEIF ( colno .EQ. 0 )
THEN
977 WRITE(11,1002) t, rowno, msg(1:msglen)
979 WRITE(11,1000) t, colno, rowno, msg(1:msglen)
983 IF ( rowno .EQ. 0 )
THEN
984 WRITE(11+thread,1001) t, colno, msg(1:msglen)
985 ELSEIF ( colno .EQ. 0 )
THEN
986 WRITE(11+thread,1002) t, rowno, msg(1:msglen)
988 WRITE(11+thread,1000) t, colno, rowno, msg(1:msglen)
9921001
FORMAT(i2,
':Variable',i8,
' : ',a)
9931002
FORMAT(i2,
':Equation',i8,
' : ',a)
9941000
FORMAT(i2,
':Variable',i8,
' appearing in Equation',i8,
' : ',a)
integer function pin_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
integer function pin_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
integer function pin_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
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_option(cntvect, coi_option)
define callback routine for defining runtime options.
integer(c_int) function coidef_usrmem(cntvect, usrmem)
provides a pointer to user memory that is available in all callback functions. NOTE: this is not a ca...
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
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(c_int) function coiget_maxthreads(cntvect)
returns the maximum number of threads that can be used by CONOPT.
program pinthread
Main program. A simple setup and call of CONOPT.
integer function pin_errmsg(rowno, colno, posno, msglen, usrmem, msg)
integer function pin_status(modsta, solsta, iter, objval, usrmem)
integer function pin_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
integer, dimension(tmax) thguided
integer, dimension(tmax) thstatic
type(controlv), dimension(:), pointer cntvect1
integer, dimension(tmax) thbase
integer, dimension(tmax) thdynamic
subroutine flog(msg, code)
integer function pin_option(ncall, rval, ival, lval, usrmem, name)
Sets runtime options.