/* MAIN PROGRAM */ int main() { int i, j, k; int tag = 1; /* tape tag */ int taskCount = 0; int pFW, pRV, pTR, degree, keep; /* forward/reverse parameters */ int evalCount; /* # of evaluations */ /****************************************************************************/ /* READ CONTROL PARAMETERS FROM FILE */ int controlParameters[cpCount]; FILE* controlFile; /*------------------------------------------------------------------------*/ /* open file to read */ if ((controlFile = fopen(controlFileName,"r")) == NULL) { fprintf(stdout,"ERROR: Could not open control file %s\n", controlFileName); exit(-1); } /*------------------------------------------------------------------------*/ /* read all values */ for (i=0; i<cpCount; i++) fscanf(controlFile,"%d%*[^\n]",&controlParameters[i]); indepDim = controlParameters[cpDimension]; pFW = controlParameters[cpVecCountFW]; pRV = controlParameters[cpVecCountRV]; pTR = controlParameters[cpVecCountTR]; degree = controlParameters[cpDegree]; evalCount = controlParameters[cpAverageCount]; /*------------------------------------------------------------------------*/ /* close control file */ fclose(controlFile); /****************************************************************************/ /* VARIABLES & INITIALIZATION */ /*------------------------------------------------------------------------*/ /* Initialize all problem parameters (including dimension) */ initProblemParameters(); /*------------------------------------------------------------------------*/ /* Initialize the independent variables */ double* indeps = new double[indepDim]; initIndependents(indeps); /*------------------------------------------------------------------------*/ /* Check main parameters */ if (evalCount <= 0) { fprintf(stdout," # of evaluations to average over = ? "); fscanf(stdin,"%d",&evalCount); fprintf(stdout,"\n"); } if ((degree <= 1) && (controlParameters[cpHosFW] || controlParameters[cpHovFW] || controlParameters[cpHosRV] || controlParameters[cpHovRV] || controlParameters[cpTensor])) { fprintf(stdout," degree = ? "); fscanf(stdin,"%d",°ree); fprintf(stdout,"\n"); } keep = degree + 1; if ((pFW < 1) && (controlParameters[cpFovFW] || controlParameters[cpHovFW])) { fprintf(stdout," # of vectors in vector forward mode = ? "); fscanf(stdin,"%d",&pFW); fprintf(stdout,"\n"); } if ((pRV < 1) && (controlParameters[cpFovRV] || controlParameters[cpHovRV])) { fprintf(stdout," # of vectors in vector reverse mode = ? "); fscanf(stdin,"%d",&pRV); fprintf(stdout,"\n"); } if ((pTR < 1) && (controlParameters[cpTensor])) { fprintf(stdout," # of vectors in tensor mode = ? "); fscanf(stdin,"%d",&pTR); fprintf(stdout,"\n"); } /*------------------------------------------------------------------------*/ /* Necessary variable */ double depOrig=0.0, depTape; /* function value */ double ***XPPP, **XPP; double ***YPPP, **YPP, *YP; double ***ZPPP, **ZPP, *ZP; double *UP, u; double *VP; double *WP; double *JP; short **nzPP; int retVal=0; /* return value */ double t00, t01, t02, t03; /* time values */ double **TPP; double **SPP; double **HPP; int dim; /****************************************************************************/ /* NORMALIZE TIMER */ /****************************************************************************/ /* 0. ORIGINAL FUNCTION EVALUATION */ /* ---> always */ fprintf(stdout,"\nTASK %d: Original function evaluation\n", taskCount++); t00 = myclock(); for (i=0; i<evalCount; i++) depOrig = originalScalarFunction(indeps); t01 = myclock(); double timeUnit; if (t01-t00) { timeUnit = 1.0/(t01-t00); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,1.0, (t01-t00)/evalCount); } else { fprintf(stdout," !!! zero timing !!!\n"); fprintf(stdout," set time unit to 1.0\n"); timeUnit = 1; } /****************************************************************************/ /* 1. TAPING THE FUNCTION */ /* ---> always */ fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: Taping the function\n", taskCount++); t00 = myclock(); /* NOTE: taping will be performed ONCE only */ depTape = tapingScalarFunction(tag,indeps); t01 = myclock(); size_t tape_stats[STAT_SIZE]; tapestats(tag,tape_stats); fprintf(stdout,"\n independents %zu\n",tape_stats[NUM_INDEPENDENTS]); fprintf(stdout," dependents %zu\n",tape_stats[NUM_DEPENDENTS]); fprintf(stdout," operations %zu\n",tape_stats[NUM_OPERATIONS]); fprintf(stdout," operations buffer size %zu\n",tape_stats[OP_BUFFER_SIZE]); fprintf(stdout," locations buffer size %zu\n",tape_stats[LOC_BUFFER_SIZE]); fprintf(stdout," constants buffer size %zu\n",tape_stats[VAL_BUFFER_SIZE]); fprintf(stdout," maxlive %zu\n",tape_stats[NUM_MAX_LIVES]); fprintf(stdout," valstack size %zu\n\n",tape_stats[TAY_STACK_SIZE]); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit*evalCount, (t01-t00)); /****************************************************************************/ /* 2. ZOS_FORWARD */ if (controlParameters[cpZosFW]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: forward(tag, m=1, n=%d, keep, X[n], Y[m])\n", taskCount++,indepDim); fprintf(stdout," ---> zos_forward\n"); /*----------------------------------------------------------------------*/ /* NO KEEP */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = forward(tag,1,indepDim,0,indeps,&depTape); t01 = myclock(); fprintf(stdout," NO KEEP"); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* KEEP */ t02 = myclock(); for (i=0; i<evalCount; i++) retVal = forward(tag,1,indepDim,1,indeps,&depTape); t03 = myclock(); fprintf(stdout," KEEP "); fprintf(stdout,TIMEFORMAT,(t03-t02)*timeUnit, (t03-t02)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpZosFW] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); fprintf(stdout," Should be the same values:\n"); fprintf(stdout," (original) %12.8E =? %12.8E (forward from tape)\n", depOrig,depTape); } } /****************************************************************************/ /* 3. FOS_FORWARD */ if (controlParameters[cpFosFW]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: forward(tag, m=1, n=%d, d=1, keep, X[n][d+1], Y[d+1])\n", taskCount++,indepDim); fprintf(stdout," ---> fos_forward\n"); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ XPP = new double*[indepDim]; for (i=0; i<indepDim; i++) { XPP[i] = new double[2]; XPP[i][0] = indeps[i]; XPP[i][1] = (double)rand(); } YP = new double[2]; /*----------------------------------------------------------------------*/ /* NO KEEP */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = forward(tag,1,indepDim,1,0,XPP,YP); t01 = myclock(); fprintf(stdout," NO KEEP"); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* KEEP */ t02 = myclock(); for (i=0; i<evalCount; i++) retVal = forward(tag,1,indepDim,1,2,XPP,YP); t03 = myclock(); fprintf(stdout," KEEP "); fprintf(stdout,TIMEFORMAT,(t03-t02)*timeUnit, (t03-t02)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpFosFW] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); fprintf(stdout," Should be the same values:\n"); fprintf(stdout," (original) %12.8E =? %12.8E (forward from tape)\n", depOrig,YP[0]); } /*----------------------------------------------------------------------*/ /* Free tensors */ for (i=0; i<indepDim; i++) delete[] XPP[i]; delete[] XPP; delete[] YP; } /****************************************************************************/ /* 4. HOS_FORWARD */ if (controlParameters[cpHosFW]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: forward(tag, m=1, n=%d, d=%d, keep, X[n][d+1], Y[d+1])\n", taskCount++,indepDim,degree); fprintf(stdout," ---> hos_forward\n"); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ XPP = new double*[indepDim]; for (i=0; i<indepDim; i++) { XPP[i] = new double[1+degree]; XPP[i][0] = indeps[i]; for (j=1; j<=degree; j++) XPP[i][j] = (double)rand(); } YP = new double[1+degree]; /*----------------------------------------------------------------------*/ /* NO KEEP */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = forward(tag,1,indepDim,degree,0,XPP,YP); t01 = myclock(); fprintf(stdout," NO KEEP"); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* KEEP */ t02 = myclock(); for (i=0; i<evalCount; i++) retVal = forward(tag,1,indepDim,degree,keep,XPP,YP); t03 = myclock(); fprintf(stdout," KEEP "); fprintf(stdout,TIMEFORMAT,(t03-t02)*timeUnit, (t03-t02)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpHosFW] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); fprintf(stdout," Should be the same values:\n"); fprintf(stdout," (original) %12.8E =? %12.8E (forward from tape)\n", depOrig,YP[0]); } /*----------------------------------------------------------------------*/ /* Free tensors */ for (i=0; i<indepDim; i++) delete[] XPP[i]; delete[] XPP; delete[] YP; } /****************************************************************************/ /* 5. FOV_FORWARD */ if (controlParameters[cpFovFW]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: forward(tag, m=1, n=%d, p=%d, x[n], X[n][p], y[m], Y[m][p])\n", taskCount++,indepDim,pFW); fprintf(stdout," ---> fov_forward\n"); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ XPP = new double*[indepDim]; for (i=0; i<indepDim; i++) { XPP[i] = new double[pFW]; for (j=0; j<pFW; j++) XPP[i][j] = (double)rand(); } YP = new double[1]; YPP = new double*[1]; YPP[0] = new double[pFW]; /*----------------------------------------------------------------------*/ /* always NO KEEP */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = forward(tag,1,indepDim,pFW,indeps,XPP,YP,YPP); t01 = myclock(); fprintf(stdout," (NO KEEP)"); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpFovFW] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ for (i=0; i<indepDim; i++) delete[] XPP[i]; delete[] XPP; delete[] YP; delete[] YPP[0]; delete[] YPP; } /****************************************************************************/ /* 6. HOV_FORWARD */ if (controlParameters[cpHovFW]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: forward(tag, m=1, n=%d, d=%d, p=%d, x[n], X[n][p][d], y[m], Y[m][p][d])\n", taskCount++,indepDim,degree,pFW); fprintf(stdout," ---> hov_forward\n"); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ XPPP = new double**[indepDim]; for (i=0; i<indepDim; i++) { XPPP[i] = new double*[pFW]; for (j=0; j<pFW; j++) { XPPP[i][j] = new double[degree]; for (k=0; k<degree; k++) XPPP[i][j][k] = (double)rand(); } } YP = new double[1]; YPPP = new double**[1]; YPPP[0] = new double*[pFW]; for (j=0; j<pFW; j++) YPPP[0][j] = new double[degree]; /*----------------------------------------------------------------------*/ /* always NO KEEP */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = forward(tag,1,indepDim,degree,pFW,indeps,XPPP,YP,YPPP); t01 = myclock(); fprintf(stdout," (NO KEEP)"); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpHovFW] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ for (i=0; i<indepDim; i++) { for (j=0; j<pFW; j++) delete[] XPPP[i][j]; delete[] XPPP[i]; } delete[] XPPP; delete[] YP; for (j=0; j<pFW; j++) delete[] YPPP[0][j]; delete[] YPPP[0]; delete[] YPPP; } /****************************************************************************/ /* 7. FOS_REVERSE */ if (controlParameters[cpFosRV]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: reverse(tag, m=1, n=%d, d=0, u, Z[n])\n", taskCount++,indepDim); fprintf(stdout," ---> fos_reverse\n"); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ ZP = new double[indepDim]; u = (double)rand(); /*----------------------------------------------------------------------*/ /* Forward with keep*/ forward(tag,1,indepDim,1,indeps,&depTape); /*----------------------------------------------------------------------*/ /* Reverse */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = reverse(tag,1,indepDim,0,u,ZP); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpFosRV] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ delete[] ZP; } /****************************************************************************/ /* 8. HOS_REVERSE */ if (controlParameters[cpHosRV]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: reverse(tag, m=1, n=%d, d=%d, u, Z[n][d+1])\n", taskCount++,indepDim,degree); fprintf(stdout," ---> hos_reverse\n"); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ ZPP = new double*[indepDim]; for (i=0; i<indepDim; i++) ZPP[i] = new double[degree+1]; u = (double)rand(); XPP = new double*[indepDim]; for (i=0; i<indepDim; i++) { XPP[i] = new double[1+degree]; XPP[i][0] = indeps[i]; for (j=1; j<=degree; j++) XPP[i][j] = (double)rand(); } YP = new double[1+degree]; /*----------------------------------------------------------------------*/ /* Forward with keep*/ forward(tag,1,indepDim,degree,keep,XPP,YP); /*----------------------------------------------------------------------*/ /* Reverse */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = reverse(tag,1,indepDim,degree,u,ZPP); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpHosRV] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ for (i=0; i<indepDim; i++) delete[] ZPP[i]; delete[] ZPP; for (i=0; i<indepDim; i++) delete[] XPP[i]; delete[] XPP; delete[] YP; } /****************************************************************************/ /* 9. FOV_REVERSE */ if (controlParameters[cpFovRV]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: reverse(tag, m=1, n=%d, d=0, p=%d, U[p], Z[p][n])\n", taskCount++,indepDim,pRV); fprintf(stdout," ---> fov_reverse\n"); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ ZPP = new double*[pRV]; for (i=0; i<pRV; i++) ZPP[i] = new double[indepDim]; UP = new double[pRV]; for (i=0; i<pRV; i++) UP[i] = (double)rand(); /*----------------------------------------------------------------------*/ /* Forward with keep*/ forward(tag,1,indepDim,1,indeps,&depTape); /*----------------------------------------------------------------------*/ /* Reverse */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = reverse(tag,1,indepDim,0,pRV,UP,ZPP); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpFovRV] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ for (i=0; i<pRV; i++) delete[] ZPP[i]; delete[] ZPP; delete[] UP; } /****************************************************************************/ /* 10. HOV_REVERSE */ if (controlParameters[cpHovRV]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: reverse(tag, m=1, n=%d, d=%d, p=%d, U[p], Z[p][n][d+1], nz[p][n])\n", taskCount++,indepDim,degree,pRV); fprintf(stdout," ---> hov_reverse\n"); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ ZPPP = new double**[pRV]; for (i=0; i<pRV; i++) { ZPPP[i] = new double*[indepDim]; for (j=0; j<indepDim; j++) ZPPP[i][j] = new double[degree+1]; } UP = new double[pRV]; for (i=0; i<pRV; i++) UP[i] = (double)rand(); XPP = new double*[indepDim]; for (i=0; i<indepDim; i++) { XPP[i] = new double[1+degree]; XPP[i][0] = indeps[i]; for (j=1; j<=degree; j++) XPP[i][j] = (double)rand(); } YP = new double[1+degree]; nzPP = new short*[pRV]; for (i=0; i<pRV; i++) nzPP[i] = new short[indepDim]; /*----------------------------------------------------------------------*/ /* Forward with keep*/ forward(tag,1,indepDim,degree,keep,XPP,YP); /*----------------------------------------------------------------------*/ /* Reverse without nonzero pattern*/ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = reverse(tag,1,indepDim,degree,pRV,UP,ZPPP); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Reverse with nonzero pattern*/ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = reverse(tag,1,indepDim,degree,pRV,UP,ZPPP,nzPP); t01 = myclock(); fprintf(stdout," (NZ)"); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpHovRV] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ for (i=0; i<pRV; i++) { for (j=0; j<indepDim; j++) delete[] ZPPP[i][j]; delete[] ZPPP[i]; delete[] nzPP[i]; } delete[] ZPPP; delete[] nzPP; delete[] UP; for (i=0; i<indepDim; i++) delete[] XPP[i]; delete[] XPP; delete[] YP; } /****************************************************************************/ /* 11. FUNCTION */ if (controlParameters[cpFunction]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: function(tag, m=1, n=%d, X[n], Y[m])\n", taskCount++,indepDim); /*----------------------------------------------------------------------*/ /* Function evaluation */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = function(tag,1,indepDim,indeps,&depTape); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpFunction] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); fprintf(stdout," Should be the same values:\n"); fprintf(stdout," (original) %12.8E =? %12.8E (forward from tape)\n", depOrig,depTape); } } /****************************************************************************/ /* 12. JACOBIAN */ if (controlParameters[cpJacobian]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: gradient(tag, n=%d, X[n], G[n])\n", taskCount++,indepDim); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ JP = new double[indepDim]; /*----------------------------------------------------------------------*/ /* Gradient evaluation */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = gradient(tag,indepDim,indeps,JP); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpJacobian] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ delete[] JP; } /****************************************************************************/ /* 13. VECJAC */ if (controlParameters[cpVecJac]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: vec_jac(tag, m=1, n=%d, repeat, X[n], U[m], V[n])\n", taskCount++,indepDim); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ UP = new double[1]; UP[0] = (double)rand(); VP = new double[indepDim]; /*----------------------------------------------------------------------*/ /* Evaluation without repeat */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = vec_jac(tag,1,indepDim,0,indeps,UP,VP); t01 = myclock(); fprintf(stdout,"(no repeat)"); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Evaluation with repeat */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = vec_jac(tag,1,indepDim,1,indeps,UP,VP); t01 = myclock(); fprintf(stdout," (repeat)"); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpVecJac] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ delete[] UP; delete[] VP; } /****************************************************************************/ /* 14. JACVEC */ if (controlParameters[cpJacVec]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: jac_vec(tag, m=1, n=%d, X[n], V[n], U[m])\n", taskCount++,indepDim); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ UP = new double[1]; VP = new double[indepDim]; for (i=0; i<indepDim; i++) VP[i] = (double)rand(); /*----------------------------------------------------------------------*/ /* Evaluation */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = jac_vec(tag,1,indepDim,indeps,VP,UP); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpJacVec] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ delete[] UP; delete[] VP; } /****************************************************************************/ /* 15. HESSIAN */ if (controlParameters[cpHessian]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: hessian(tag, n=%d, X[n], lower triangle of H[n][n])\n", taskCount++,indepDim); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ HPP = new double*[indepDim]; for (i=0; i<indepDim; i++) HPP[i] = new double[indepDim]; /*----------------------------------------------------------------------*/ /* Evaluation */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = hessian(tag,indepDim,indeps,HPP); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpHessian] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ for (i=0; i<indepDim; i++) delete[] HPP[i]; delete[] HPP; } /****************************************************************************/ /* 16. HESSVEC */ if (controlParameters[cpHessVec]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: hess_vec(tag, n=%d, X[n], V[n], W[n])\n", taskCount++,indepDim); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ VP = new double[indepDim]; for (i=0; i<indepDim; i++) VP[i] = (double)rand(); WP = new double[indepDim]; /*----------------------------------------------------------------------*/ /* Evaluation */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = hess_vec(tag,indepDim,indeps,VP,WP); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpHessVec] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ delete[] VP; delete[] WP; } /****************************************************************************/ /* 17. LAGHESSVEC */ if (controlParameters[cpLagHessVec]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: lagra_hess_vec(tag, m=1, n=%d, X[n], U[m], V[n], W[n])\n", taskCount++,indepDim); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ UP = new double[1]; UP[0] = (double)rand(); VP = new double[indepDim]; for (i=0; i<indepDim; i++) VP[i] = (double)rand(); WP = new double[indepDim]; /*----------------------------------------------------------------------*/ /* Evaluation */ t00 = myclock(); for (i=0; i<evalCount; i++) retVal = lagra_hess_vec(tag,1,indepDim,indeps,UP,VP,WP); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpLagHessVec] > 1) { fprintf(stdout,"\n Return value: %d\n",retVal); } /*----------------------------------------------------------------------*/ /* Free tensors */ delete[] VP; delete[] WP; delete[] UP; } /****************************************************************************/ /* 18. TENSOR */ if (controlParameters[cpTensor]) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: tensor_eval(tag, m =1, n=%d, d=%d, p=%d, X[n], tensor[m][dim], S[n][p])\n", taskCount++,indepDim,degree, pTR); fprintf(stdout,"\n dim = ((p+d) over d)\n"); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ dim = binomi(pTR+degree,degree); TPP = new double*[1]; TPP[0] = new double[dim]; SPP = new double*[indepDim]; for (i=0; i<indepDim; i++) { SPP[i] = new double[pTR]; for (j=0; j<pTR; j++) SPP[i][j]=(i==j)?1.0:0.0; } /*----------------------------------------------------------------------*/ /* tensor evaluation */ t00 = myclock(); for (i=0; i<evalCount; i++) tensor_eval(tag,1,indepDim,degree,pTR,indeps,TPP,SPP); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpTensor] > 1) {} /*----------------------------------------------------------------------*/ /* Free tensors */ delete[] TPP[0]; delete[] TPP; for (i=0; i<indepDim; i++) delete[] SPP[i]; delete[] SPP; } /****************************************************************************/ /* 19. INVERSE TENSOR */ if (controlParameters[cpInvTensor] && (1==indepDim)) { fprintf(stdout,"--------------------------------------------------------"); fprintf(stdout,"\nTASK %d: inverse_tensor_eval(tag, m=n=1, d=%d, p=%d, X[n], tensor[m][dim], S[n][p])\n", taskCount++,degree, pTR); fprintf(stdout,"\n dim = ((p+d) over d)\n"); /*----------------------------------------------------------------------*/ /* Allocation & initialisation of tensors */ dim = binomi(pTR+degree,degree); TPP = new double*[1]; TPP[0] = new double[dim]; SPP = new double*[1]; SPP[0] = new double[pTR]; for (j=0; j<pTR; j++) SPP[0][j]=(0==j)?1.0:0.0; /*----------------------------------------------------------------------*/ /* tensor evaluation */ t00 = myclock(); for (i=0; i<evalCount; i++) inverse_tensor_eval(tag,1,degree,pTR,indeps,TPP,SPP); t01 = myclock(); fprintf(stdout," "); fprintf(stdout,TIMEFORMAT,(t01-t00)*timeUnit, (t01-t00)/evalCount); /*----------------------------------------------------------------------*/ /* Debug infos */ if (controlParameters[cpInvTensor] > 1) {} /*----------------------------------------------------------------------*/ /* Free tensors */ delete[] TPP[0]; delete[] TPP; delete[] SPP[0]; delete[] SPP; } return 1; }
double evaluate_derivatives(int n, int m, double* x, int* options) { int order = options[0]; int nnz; double t1 = k_getTime(); if (options[1] == 0) { // Teed = new double*[n]; assert(m == 1); double** seed = new double*[n]; for (int i = 0; i < n; i++) { seed[i] = new double[n]; for (int j = 0; j < n; j++) { seed[i][j] = ((i==j)?1.0:0.0); } } int dim = binomi(n+order, order); double** tensorhelp = myalloc2(1, dim); tensor_eval(TAG, 1, n, order, n, x, tensorhelp, seed); for (int i = 0; i < n; i++) { delete[] seed[i]; } delete[] seed; myfree2(tensorhelp); } else { if (order == 2) { // Hessian assert(m == 1); if (options[1] == 1 || options[1] == 2) { // Direct or Indirect int opt[2] = {0, 0}; // default is indirect; if (options[1] == 1) {opt[0] = 1;} // set direct; unsigned int * rind = NULL; unsigned int * cind = NULL; double * values = NULL; sparse_hess(TAG, n, 0, x, &nnz, &rind, &cind, &values, opt); #ifdef PRINT_RESULT for (int i = 0; i < nnz; i++) { printf("H[%d, %d] = %.6f\n", rind[i], cind[i], values[i]); } #endif free(rind); free(cind); free(values); } else if (options[1] == 3) { // FullHess double** H = new double*[n]; for (int i = 0; i < n; i++) { H[i] = new double[n]; } hessian(TAG, n, x, H); nnz = n*n; #ifdef PRINT_RESULT for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { printf("H[%d, %d] = %.6f\n", i, j, H[i][j]); } } #endif for (int i = 0; i < n; i++) { delete[] H[i]; } delete[] H; } else if (options[1] == 4) { // Single Hv double v[n]; double Hv[n]; for (int i = 0; i < n; i++) { v[i] = 1.0; Hv[i] = 0.0; } hess_vec(TAG, n, x, v, Hv); nnz = n; } else if (options[1] == 5) { // dense second order reverse double** H = new double*[n]; for (int i = 0; i < n; i++) { H[i] = new double[n]; } hessian_dense(TAG, n, x, H); nnz = n*n; #ifdef PRINT_RESULT for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { printf("H[%d, %d] = %.6f\n", i, j, H[i][j]); } } #endif for (int i = 0; i < n; i++) { delete[] H[i]; } delete[] H; } else if (options[1] == 6){ // sparse second order reverse unsigned int * rind = NULL; unsigned int * cind = NULL; double * values = NULL; hessian_sparse(TAG, n, x, &nnz, &rind, &cind, &values); #ifdef PRINT_RESULT for (int i = 0; i < nnz; i++) { printf("H[%d, %d] = %.6f\n", rind[i], cind[i], values[i]); } #endif free(rind); free(cind); free(values); } else if (options[1] == 7) { // Hess-matrix options double** H = myalloc2(n, n); double y; double*** Xppp = myalloc3(n, n, 1); double*** Yppp = myalloc3(1, n, 1); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Xppp[i][j][0] = 0; } Xppp[i][i][0] = 1.0; } double** Upp = myalloc2(1,2); Upp[0][0] = 1; Upp[0][1] = 0; double*** Zppp = myalloc3(n, n, 2); int ret_val = hov_wk_forward(TAG,1,n,1,2,n,x,Xppp,&y,Yppp); ret_val = hos_ov_reverse(TAG,1,n,1,n,Upp,Zppp); for (int i = 0; i < n; ++i) { for (int l = 0; l < n; ++l) { H[l][i] = Zppp[i][l][1]; } } #ifdef PRINT_RESULT for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { printf("H[%d, %d] = %.6f\n", i, j, H[i][j]); } } #endif myfree2(H); myfree3(Xppp); myfree3(Yppp); myfree2(Upp); myfree3(Zppp); } } else if (order == 1) { // Gradient or Jacobian if (m == 1) { // gradient double g[n]; gradient(TAG, n, x, g); #ifdef PRINT_RESULT for (int i = 0; i < n; i++) { printf("g[%d] = %.6f\n", i, g[i]); } #endif } else { // jacobian double** J = new double*[m]; for (int i = 0; i < m; i++) { J[i] = new double[n]; } jacobian(TAG, m, n, x, J); #ifdef PRINT_RESULT for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("J[%d][%d] = %.6f\n", i, j, J[i][j]); } } #endif for (int i = 0; i < m; i++) { delete[] J[i]; } delete[] J; } nnz = n*m; } } double time_elapsed = k_getTime() - t1; size_t size; size_t** tind; double* values; printf("ADOLC nnz[%d] method[%d] order[%d] timing = %.6f\n", nnz, options[1], options[0], time_elapsed); return time_elapsed; }