28#pragma warning (disable : 3280)
50int COI_CALLCONV
Pin_ReadMatrix(
double LOWER[],
double CURR[],
double UPPER[],
int VSTA[],
int TYPE[],
51 double RHS[],
int ESTA[],
int COLSTA[],
int ROWNO[],
double VALUE[],
52 int NLFLAG[],
int NUMVAR,
int NUMCON,
int NUMNZ,
void* USRMEM )
54 int it, is, i, icol, iz;
73 for ( it=1; it<=
T; it++ ) {
84 CURR[is+3] = CURR[is] - CURR[is+2];
86 CURR[is+4] = CURR[is+4-
Vpp] - CURR[is+3];
88 CURR[is+4] = 500 - CURR[is+3];
104 for ( i = 1; i < NUMCON; i++ )
112 for ( it = 1; it<=
T; it++ ) {
114 RHS[is] = 1.0+2.3*pow(1.015,(it-1.));
119 RHS[1] = RHS[1]+ 0.87*18.;
162 for ( it = 1; it <=
T; it++ )
198 ROWNO[iz] = is+2+
Epp;
220 ROWNO[iz] = is+1+
Epp;
252 ROWNO[iz] = is+4+
Epp;
276 VALUE[iz] = pow(1.05,(1.0-it));
297int COI_CALLCONV
Pin_FDEval(
const double X[],
double* G,
double JAC[],
int ROWNO,
const int JACNUM[],
int MODE,
298 int IGNERR,
int* ERRCNT,
int NUMVAR,
int NUMJAC,
int THREAD,
void* USRMEM )
306 it = ( ROWNO+
Epp-1) /
Epp;
308 if ( 1+(it-1)*
Epp+1 == ROWNO ) {
312 h1 = (1.1+0.1*X[is+5]);
313 h2 = pow(1.02,-X[is+1]/7.0);
314 if ( 1 == MODE || 3 == MODE )
316 if ( 2 == MODE || 3 == MODE ) {
317 JAC[is+1] = h1*h2*log(1.02)/7.0;
321 else if ( 1+(it-1)*
Epp+5 == ROWNO ) {
325 if ( 1 == MODE || 3 == MODE )
326 *G = -X[is+3]*(X[is+5]-250./X[is+4]);
327 if ( 2 == MODE || 3 == MODE ) {
328 JAC[is+3] = -(X[is+5]-250./X[is+4]);
329 JAC[is+4] = -X[is+3]*250./pow(X[is+4],2);
330 JAC[is+5] = -X[is+3];
337 printf(
"\nError. Pin_FDEval called with ROWNO = %d. MODE = %d\n\n", ROWNO, MODE);
349int COI_CALLCONV
Pin_Message(
int SMSG,
int DMSG,
int NMSG,
char* MSGV[],
void* USRMEM )
361 for( i=0; i<SMSG;i++ ) printf(
"%d: %s\n",
T, MSGV[i]);
362 for( i=0; i<DMSG;i++ ) fprintf(
fd,
"%d: %s\n",
T, MSGV[i]);
363 for( i=0; i<SMSG;i++ ) fprintf(
fs,
"%d: %s\n",
T, MSGV[i]);
368int COI_CALLCONV
Pin_ErrMsg(
int ROWNO,
int COLNO,
int POSNO,
const char* MSG,
void* USRMEM )
374 if ( ROWNO == -1 ) fprintf(
fd,
"%d: Variable %d : ",
T, COLNO);
375 else if ( COLNO == -1 ) fprintf(
fd,
"%d: Equation %d : ",
T, ROWNO);
376 else fprintf(
fd,
"%d: Variable %d appearing in Equation %d : ",
T, COLNO, ROWNO);
377 fprintf(
fd,
"%s\n", MSG);
379 if ( ROWNO == -1 ) fprintf(
fs,
"%d: Variable %d : ",
T, COLNO);
380 else if ( COLNO == -1 ) fprintf(
fs,
"%d: Equation %d : ",
T, ROWNO);
381 else fprintf(
fs,
"%d: Variable %d appearing in Equation %d : ",
T, COLNO, ROWNO);
382 fprintf(
fs,
"%s\n", MSG);
385 fflush(
fd); fflush(
fs);
390int COI_CALLCONV
Pin_Status(
int MODSTA,
int SOLSTA,
int ITER,
double OBJVAL,
void* USRMEM )
402 fprintf(
fd,
"%d: \n",
T );
403 fprintf(
fd,
"%d: CONOPT has finished Optimizing\n",
T );
404 fprintf(
fd,
"%d: Model status = %8d\n",
T , MODSTA);
405 fprintf(
fd,
"%d: Solver status = %8d\n",
T , SOLSTA);
406 fprintf(
fd,
"%d: Iteration count = %8d\n",
T , ITER);
407 fprintf(
fd,
"%d: Objective value = %10f\n",
T , OBJVAL);
409 fprintf(
fs,
"%d: \n",
T);
410 fprintf(
fs,
"%d: Model status = %8d\n",
T , MODSTA);
411 fprintf(
fs,
"%d: Solver status = %8d\n",
T , SOLSTA);
412 fprintf(
fs,
"%d: Objective value = %10f\n",
T , OBJVAL);
419 fflush(
fd); fflush(
fs);
424int COI_CALLCONV
Pin_Solution(
const double XVAL[],
const double XMAR[],
const int XBAS[],
const int XSTA[],
425 const double YVAL[],
const double YMAR[],
const int YBAS[],
const int YSTA[],
426 int NUMVAR,
int NUMCON,
void* USRMEM )
434 for ( i=0; i<NUMCON; i++ )
459 while ( (c = getchar()) != EOF && i <
MAXLINE )
pname[i++] = (char)c;
467 fc = fopen(
fname,
"w");
468 fprintf(fc,
"%s: %s.\n",
pname, msgt );
494 double time0, time1, time2, time3;
499 maxthread = omp_get_max_threads();
500 printf(
"Initial maxthread= %d\n",maxthread);
501 if ( maxthread < 4 ) {
503 omp_set_num_threads(maxthread);
505 printf(
"Revised maxthread= %d\n",maxthread);
510 printf(
"\nSolving Pindyck Model using CONOPT:\n" );
512 CntVect1 = (coiHandle_t *) malloc( maxthread*
sizeof(coiHandle_t) );
515 Usrmem = (
struct Periods **)malloc( maxthread*
sizeof(
struct Periods *) );
516 for ( thread=0; thread<maxthread; thread++ )
518 Usrmem[thread] = (
struct Periods *)malloc(
sizeof(
struct Periods));
525 for ( thread=0; thread<maxthread; thread++ )
530 printf(
"Could not create Conopt object: %s\n", msg);
548#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
554 printf(
"Setup errors during loading of callback routines. COI_Error = %d\n",
COI_Error);
555 c_log(
"Skipping Solve due to errors loading callback routines",
COI_Error);
560 time0 = omp_get_wtime();
566 Usrmem[thread]->
NT =
T;
584 printf(
"Skipping COI_Solve due to setup errors. T = %d. COI_Error = %d\n",
T,
COI_Error);
587 printf(
"Starting to solve for T=%d using thread %d\n",
T, thread );
589 printf(
"After solving for T = %d. COI_Error =%d\n",
T,
COI_Error);
592 printf(
"\n** Finished solving sequential loop.\n\n"); fflush(NULL);
593 time1 = omp_get_wtime() - time0;
595 time0 = omp_get_wtime();
596#pragma omp parallel for default(shared) private(COI_Error,thread,CntVect) schedule(static)
602 thread = omp_get_thread_num();
603 printf(
"Solving period %d with thread %d.\n",
T, thread); fflush(NULL);
604 Usrmem[thread]->
NT =
T;
621 printf(
"After solving for T = %d. COI_Error =%d\n",
T,
COI_Error); fflush(NULL);
623 time2 = omp_get_wtime() - time0;
624 printf(
"\n** Finished solving parallel loop with static schedule.\n\n"); fflush(NULL);
626 time0 = omp_get_wtime();
628#pragma omp parallel for default(shared) private(COI_Error,thread,CntVect) schedule(dynamic)
634 thread = omp_get_thread_num();
635 printf(
"Solving period %d with thread %d.\n",
T, thread); fflush(NULL);
636 Usrmem[thread]->
NT =
T;
653 printf(
"After solving for T = %d. COI_Error =%d\n",
T,
COI_Error); fflush(NULL);
655 time3 = omp_get_wtime() - time0;
656 printf(
"\n** Finished solving parallel loop with dynamic schedule.\n\n"); fflush(NULL);
664 printf(
"\nEnd of PinThread Model.\n");
665 printf(
"Difference between base and static solutions = %d\n",dif1);
666 printf(
"Difference between base and dynamic solutions= %d\n",dif2);
667 printf(
"MaxThread = %d\n",maxthread);
668 printf(
"Time Single thread = %10.3f\n",time1);
669 printf(
"Time Static Multi thread = %10.3f\n",time2);
670 printf(
"Time Dynamic Multi thread = %10.3f\n",time3);
671 printf(
"Speedup, static = %10.3f\n",time1/time2);
672 printf(
"Speedup, dynamic = %10.3f\n",time1/time3);
673 printf(
"Efficiency, static = %10.3f\n",time1/time2/maxthread);
674 printf(
"Efficiency, dynamic = %10.3f\n",time1/time3/maxthread);
679 c_log(
"Status or Solution routine was not called", -1); }
681 c_log(
"Base and static solutions are different", -1); }
683 c_log(
"Base and dynamic solutions are different", -1); }
685 for ( thread=0; thread<maxthread; thread++ )
689 free(Usrmem[thread]);
694 c_log(
"Successful Solve",
OK );
int COI_CALLCONV COIDEF_ReadMatrix(coiHandle_t cntvect, COI_READMATRIX_t coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
int COI_CALLCONV COIDEF_Message(coiHandle_t cntvect, COI_MESSAGE_t coi_message)
define callback routine for handling messages returned during the solution process.
int COI_CALLCONV COIDEF_Solution(coiHandle_t cntvect, COI_SOLUTION_t coi_solution)
define callback routine for returning the final solution values.
int COI_CALLCONV COIDEF_Status(coiHandle_t cntvect, COI_STATUS_t coi_status)
define callback routine for returning the completion status.
int COI_CALLCONV COIDEF_ErrMsg(coiHandle_t cntvect, COI_ERRMSG_t coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
int COI_CALLCONV COIDEF_FDEval(coiHandle_t cntvect, COI_FDEVAL_t coi_fdeval)
define callback routine for performing function and derivative evaluations.
int COI_CALLCONV COIDEF_UsrMem(coiHandle_t cntvect, void *usrmem)
provides a pointer to user memory that is available in all callback functions. NOTE: this is not a ca...
int COI_CALLCONV COIDEF_DebugFV(coiHandle_t cntvect, int debugfv)
turn Debugging of FDEval on and off.
int COI_CALLCONV COIDEF_License(coiHandle_t cntvect, int licint1, int licint2, int licint3, const char *licstring)
define the License Information.
int COI_CALLCONV COIDEF_ObjCon(coiHandle_t cntvect, int objcon)
defines the Objective Constraint.
int COI_CALLCONV COIDEF_NumVar(coiHandle_t cntvect, int numvar)
defines the number of variables in the model.
int COI_CALLCONV COIDEF_NumNz(coiHandle_t cntvect, int numnz)
defines the number of nonzero elements in the Jacobian.
int COI_CALLCONV COIDEF_NumCon(coiHandle_t cntvect, int numcon)
defines the number of constraints in the model.
int COI_CALLCONV COIDEF_OptDir(coiHandle_t cntvect, int optdir)
defines the Optimization Direction.
int COI_CALLCONV COIDEF_NumNlNz(coiHandle_t cntvect, int numnlnz)
defines the Number of Nonlinear Nonzeros.
void COI_CALLCONV coiFinalize(void)
finializes the solving process for CONOPT. This must be called when using OpenMP. It will terminate t...
int COI_CALLCONV coiCreate(coiHandle_t *cntvect)
initialises and create the control vector.
int COI_CALLCONV coiFree(coiHandle_t *cntvect)
frees the control vector.
int COI_CALLCONV COI_Solve(coiHandle_t cntvect)
method for starting the solving process of CONOPT.
int COI_CALLCONV Pin_ErrMsg(int ROWNO, int COLNO, int POSNO, const char *MSG, void *USRMEM)
int COI_CALLCONV Pin_ReadMatrix(double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[], double RHS[], int ESTA[], int COLSTA[], int ROWNO[], double VALUE[], int NLFLAG[], int NUMVAR, int NUMCON, int NUMNZ, void *USRMEM)
Define information about the model.
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
int COI_CALLCONV Pin_Solution(const double XVAL[], const double XMAR[], const int XBAS[], const int XSTA[], const double YVAL[], const double YMAR[], const int YBAS[], const int YSTA[], int NUMVAR, int NUMCON, void *USRMEM)
int COI_CALLCONV Pin_FDEval(const double X[], double *G, double JAC[], int ROWNO, const int JACNUM[], int MODE, int IGNERR, int *ERRCNT, int NUMVAR, int NUMJAC, int THREAD, void *USRMEM)
Compute nonlinear terms and non-constant Jacobian elements.
double xkeep2[Tmax *Tmax *Vpp]
int COI_CALLCONV Pin_Status(int MODSTA, int SOLSTA, int ITER, double OBJVAL, void *USRMEM)
void c_log(char *msgt, int code)
double xkeep[Tmax *Tmax *Vpp]
double xkeep1[Tmax *Tmax *Vpp]
int COI_CALLCONV Pin_Message(int SMSG, int DMSG, int NMSG, char *MSGV[], void *USRMEM)
int COI_CALLCONV Pin_ReadMatrix(double LOWER[], double CURR[], double UPPER[], int VSTA[], int TYPE[], double RHS[], int ESTA[], int COLSTA[], int ROWNO[], double VALUE[], int NLFLAG[], int NUMVAR, int NUMCON, int NUMNZ, void *USRMEM)
Define information about the model.
int COI_CALLCONV Pin_Solution(const double XVAL[], const double XMAR[], const int XBAS[], const int XSTA[], const double YVAL[], const double YMAR[], const int YBAS[], const int YSTA[], int NUMVAR, int NUMCON, void *USRMEM)
int COI_CALLCONV Pin_FDEval(const double X[], double *G, double JAC[], int ROWNO, const int JACNUM[], int MODE, int IGNERR, int *ERRCNT, int NUMVAR, int NUMJAC, int THREAD, void *USRMEM)
Compute nonlinear terms and non-constant Jacobian elements.