/* The interface function */ double tapingScalarFunction( int tag, double* indeps ) { int i; trace_on(tag); adouble* activeIndeps = new adouble[indepDim]; adouble* aIP = activeIndeps; double* iP = indeps; for (i=0; i<indepDim; i++) *aIP++ <<= *iP++; adouble ares = activeSpeelpenning(indepDim, activeIndeps); double res = 0; ares >>= res; trace_off(); return res; }
/* MAIN PROGRAM */ int main() { int i, vc; int tag = 1; double z, t, tmin, tmax, tdist, dz; /*--------------------------------------------------------------------------*/ /* Preparation */ fprintf(stdout,"CUBIC LIGHTHOUSE Using CARDAN (ADOL-C Example)\n\n"); tmin = 0.15; tmax = 0.24; fprintf(stdout,"How many values = ? \n"); scanf("%d",&vc); /*--------------------------------------------------------------------------*/ t = 0.1; adouble az,at; trace_on(tag); at <<= t; az = activeCubicLighthouse(at); az >>= z; trace_off(); /*--------------------------------------------------------------------------*/ 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]); /*--------------------------------------------------------------------------*/ tdist = (tmax-tmin)/((double) (vc-1)); t = tmin; for (i=0; i<vc; i++) { function(tag,1,1,&t,&z); gradient(tag,1,&t,&dz); fprintf(stdout,"%e %e %e\n",t,z,dz); t += tdist; } /*--------------------------------------------------------------------------*/ return 1; }
/* The interface function */ void tapingVectorFunction( int tag, double* indeps, double* deps ) { int i; trace_on(tag); adouble* activeIndeps = new adouble[indepDim]; adouble* activeDeps = new adouble[depDim]; adouble* aIP = activeIndeps; double* iP = indeps; for (i=0; i<indepDim; i++) *aIP++ <<= *iP++; activeGearFunction(activeIndeps,activeDeps); aIP = activeDeps; iP = deps; for (i=0; i<depDim; i++) *aIP++ >>= *iP++; trace_off(); }
/* The interface function */ void tapingVectorFunction( int tag, double* indEPS_, double* dEPS_ ) { int i; trace_on(tag); adouble* activeIndEPS_ = new adouble[indepDim]; adouble* activeDEPS_ = new adouble[depDim]; adouble* aIP = activeIndEPS_; double* iP = indEPS_; for (i=0; i<indepDim; i++) *aIP++ <<= *iP++; activeEutroph(activeIndEPS_,activeDEPS_); aIP = activeDEPS_; iP = dEPS_; for (i=0; i<depDim; i++) *aIP++ >>= *iP++; trace_off(); }
int main(int argc, char* argv[]) { vector<double> ind; int n = 0; int m = 0; set_up(argc, argv, ind, n, m); std::cout << "num_dep = " << m << ", num_ind = " << n << std::endl; adouble* xad = new adouble[n]; adouble* yad = new adouble[m]; double* y = new double[m]; double* x = new double[n]; for (int i = 0; i < n; i++) { x[i] = ind[i]; } trace_on(TAG); for (int i = 0; i < n; i++) { xad[i] <<= x[i]; } func_eval<adouble>(n, xad, m, yad); #ifdef DUMMY_SCALAR adouble zad = 0; double z; for (int i = 0; i < m; i++) { zad = zad + yad[i]; } zad >>= z; std::cout << "func_eval = " << z << std::endl; m = 1; #else for (int i = 0; i < m; i++) { yad[i] >>= y[i]; //std::cout << "y["<<i<<"] = " << y[i] << std::endl; } #endif trace_off(); tear_down(); // Evaluate derivatives; int options[2] = {ORDER, METHOD}; double t; std::cout << "m = " << m << " n = " << n << std::endl; t = evaluate_derivatives(n, m, x, options); delete[] xad; delete[] yad; }
/* log a change in tracing */ void tracelevel_msg(const char *pat, int dump) /* -1=no dump, 0=default, 1=force */ { static const char *off_msgs[MAX_TRACELEVEL] = { "Tracing actions stopped", "Tracing packets stopped", "Tracing packet contents stopped", "Tracing kernel changes stopped", "Tracing routing socket messages stopped", }; static const char *on_msgs[MAX_TRACELEVEL] = { "Tracing actions started", "Tracing packets started", "Tracing packet contents started", "Tracing kernel changes started", "Tracing routing socket messages started", }; uint_t old_tracelevel = tracelevel; if (new_tracelevel < 0) new_tracelevel = 0; else if (new_tracelevel > MAX_TRACELEVEL) new_tracelevel = MAX_TRACELEVEL; if (new_tracelevel < tracelevel) { if (new_tracelevel <= 0) { trace_off(pat, off_msgs[0]); } else { do { tmsg(pat, off_msgs[tracelevel]); } while (--tracelevel != new_tracelevel); } } else if (new_tracelevel > tracelevel) { do { tmsg(pat, on_msgs[tracelevel++]); } while (tracelevel != new_tracelevel); } if (dump > 0 || (dump == 0 && old_tracelevel == 0 && tracelevel != 0)) trace_dump(); }
void adolc_process_fill(bool retape, int tag, const ElemData& e, unsigned int neqn, unsigned int ncol, std::vector<double>& x_local, double **w_local, std::vector<adouble>& f_ad, std::vector<double>& f_local, std::vector<double>& f, double **adj_local, std::vector< std::vector<double> >& adj) { if (retape) { for (unsigned int node=0; node<e.nnode; node++) for (unsigned int eqn=0; eqn<neqn; eqn++) f_ad[node*neqn+eqn] >>= f_local[node*neqn+eqn]; trace_off(); } else
int main() { double xyz[3], f; /* variables */ adouble ax, ay, az, af; /* active varaibles */ xyz[0] = 1.2; xyz[1] = 2.6; /* initialize any values */ xyz[2] = 0.03; /* TRACING THE EVALUATION TAPE */ trace_on(1); /* start tracing of an evaluation tape with the identifier 1 */ ax <<= xyz[0]; /* marking independent variables */ ay <<= xyz[1]; az <<= xyz[2]; af = myf(ax,ay,az); /* calling the 'active' version of the function to be differentiated to generate a tape of the evaluation process; NOTE: Instead of calling a C function the whole evaluation code can be placed here (see example file DEX/powerexam.C) */ af >>= f; /* marking the only one dependent variable */ trace_off(1); /* stop tracing */ /* NOTE: trace_off(..) is called with the value 1 (for the optional ! argument). This forces ADOL-C to save the generated tapes ! on harddisc. In particular these are the files ! ! _adol-op_tape.1 (operations = opcodes) ! _adol-in_tape.1 (integers = locations) ! _adol-rl_tape.1 (real values = doubles) ! ! The appendix '1' is determined by the used tape ! identifier, which was passed to trace_on(..). */ }
bool fwd::run(SEXP xTrgt, SEXP xAryTrgt, SEXP xCtrl, SEXP xAryCtrl, SEXP xYrs) { if (!Trgt.Init(xTrgt,xAryTrgt,niters())) return false; if (!Ctrl.Init(xCtrl,xAryTrgt,niters())) return false; MinProjYear = (int)REAL(xYrs)[0]; MaxProjYear = (int)REAL(xYrs)[0]; int i; for (i=1; i<LENGTH(xYrs); i++) { if (MinProjYear>REAL(xYrs)[i]) MinProjYear=(int)REAL(xYrs)[i]; if (MaxProjYear<REAL(xYrs)[i]) MaxProjYear=(int)REAL(xYrs)[i]; } //ADol-C stuff int n=1; double *depen, *indep, *r, **jac; adouble *depen_ad, *indep_ad; int iter = 0; //get N at start of year double x=1.0; for (iter=1; iter<=niters(); iter++) project(&x, MinProjYear-1 ,iter, TRUE, TRUE); int tag = 0; for (int iYr=MinProjYear; iYr<=MaxProjYear; iYr++) { int _tag = 0; //(tag % 6 + 1); n = (int)Trgt.n(iYr); //n of independent variables MUST = n of equations if (Ctrl.n(iYr) == n) { depen = new double[n]; indep = new double[n]; r = new double[n]; jac = new double*[n]; depen_ad = new adouble[n]; indep_ad = new adouble[n]; for (i=0; i<n; i++) jac[i] = new double[n]; for (iter=1; iter<=niters(); iter++) { // set independent variables to estimaate int j=0; for (int iFleet=1; iFleet<=nfleet(); iFleet++) for (int iMetier=1; iMetier<=nmetier(); iMetier++) if (Ctrl.fit(iYr, iFleet, iMetier)) indep[j++] = 1.0; // Taping the computation of the jacobian trace_on(_tag); // marking independent variables for (i=0; i<n; i++) indep_ad[i] <<= indep[i]; project(indep_ad,depen_ad,iYr,iter); // marking dependent variables for (i=0; i<n; i++) depen_ad[i] >>= depen[i]; trace_off(_tag); //jacobian(tag,m,n,indep,jac); r[0]=1.0; function(_tag,n,n,indep,r); int NIters=0; while (norm(r,n) > 1e-10 && NIters++<50) { jac_solv(_tag,n,indep,r,0,2); for (i=0; i<n; i++) indep[i] -= r[i]; function(_tag,n,n,indep,r); } project(indep, iYr, iter); } delete[] depen; delete[] indep; delete[] r; delete[] depen_ad; delete[] indep_ad; for (i=0; i<n; i++) delete[] jac[i]; delete[] jac; } tag++; }
bool link_poly( size_t size , size_t repeat , CppAD::vector<double> &a , // coefficients of polynomial CppAD::vector<double> &z , // polynomial argument value CppAD::vector<double> &ddp ) // second derivative w.r.t z { // ----------------------------------------------------- // setup int tag = 0; // tape identifier int keep = 1; // keep forward mode results in buffer int m = 1; // number of dependent variables int n = 1; // number of independent variables int d = 2; // order of the derivative double f; // function value int i; // temporary index // choose a vector of polynomial coefficients CppAD::uniform_01(size, a); // AD copy of the polynomial coefficients std::vector<adouble> A(size); for(i = 0; i < int(size); i++) A[i] = a[i]; // domain and range space AD values adouble Z, P; // allocate arguments to hos_forward double *x0 = 0; x0 = CPPAD_TRACK_NEW_VEC(n, x0); double *y0 = 0; y0 = CPPAD_TRACK_NEW_VEC(m, y0); double **x = 0; x = CPPAD_TRACK_NEW_VEC(n, x); double **y = 0; y = CPPAD_TRACK_NEW_VEC(m, y); for(i = 0; i < n; i++) { x[i] = 0; x[i] = CPPAD_TRACK_NEW_VEC(d, x[i]); } for(i = 0; i < m; i++) { y[i] = 0; y[i] = CPPAD_TRACK_NEW_VEC(d, y[i]); } // Taylor coefficient for argument x[0][0] = 1.; // first order x[0][1] = 0.; // second order extern bool global_retape; if( global_retape ) while(repeat--) { // choose an argument value CppAD::uniform_01(1, z); // declare independent variables trace_on(tag, keep); Z <<= z[0]; // AD computation of the function value P = CppAD::Poly(0, A, Z); // create function object f : Z -> P P >>= f; trace_off(); // get the next argument value CppAD::uniform_01(1, z); x0[0] = z[0]; // evaluate the polynomial at the new argument value hos_forward(tag, m, n, d, keep, x0, x, y0, y); // second derivative is twice second order Taylor coef ddp[0] = 2. * y[0][1]; } else {
void trace_pulse(void) { trace_on(); _delay_us(1); trace_off(); }
// alternative implementation calling high-level _sys->continuous //-------------------------------------------------------------------------- void Omu_IntODE::syseq_forward(double t, const VECP y, const VECP u, VECP f) { #ifdef OMU_WITH_ADOLC int i, j; Omu_DependentVec &Ft = *_Ft_ptr; // // form a vector of independent variables // for (i = 0; i < _nd; i++) { _x[i] = u[i]; } for (i = 0; i < _n; i++) { _x[_nd + i] = y[i]; _x[_nd + _n + i] = 0.0; // yprime[i] } for (i = 0; i < _nu; i++) { _x[_nd + 2 * _n + i] = u[_nd + i]; } // // evaluate residual // // adoublev ax(_nd + _n); static adoublev ax; ax.alloc(_nd + _n); // adoublev adx(_nd + _n); static adoublev adx; adx.alloc(_nd + _n); // adoublev au(_nu); static adoublev au; au.alloc(_nu); // adoublev aF(_nd + _n); static adoublev aF; aF.alloc(_nd + _n); for (i = 0; i < _nd; i++) adx[i] = 0.0; for (i = _nd; i < _nxt; i++) aF[i] = 0.0; if (_sa) trace_on(3); // tape 3 ax <<= _x->ve; for (i = 0; i < _n; i++) adx[_nd + i] <<= _x->ve[_nd + _n + i]; au <<= _x->ve + _nd + 2 * _n; _sys->continuous(_kk, t, ax, au, adx, aF); for (i = _nd; i < _nxt; i++) { aF[i] >>= f[i - _nd]; f[i - _nd] /= -Ft.Jdx[i][i]; } if (_sa) { trace_off(); int nindep = _nd + 2 * _n + _nu; int npar = _nx + _nu; m_zero(_X2); for (i = 0; i < _nd; i++) { _X2[i][i] = 1.0; } for (i = 0; i < _n; i++) { for (j = 0; j < npar; j++) { _X2[_nd + i][j] = y[(1 + j) * _n + i]; _X2[_nd + _n + i][j] = 0.0; // yprime[(1 + j) * _n + i]; } } for (i = 0; i < _nu; i++) { _X2[_nd + 2 * _n + i][_nd + _n + i] = 1.0; } forward(3, _n, nindep, npar, _x->ve, _X2->me, f->ve, _Y2->me); for (i = _nd; i < _nxt; i++) { f[i - _nd] /= -Ft.Jdx[i][i]; for (j = 0; j < npar; j++) { f[(1 + j) * _n + i - _nd] = _Y2[i - _nd][j] / -Ft.Jdx[i][i]; } } } _res_evals++; if (_sa) _sen_evals++; #else m_error(E_NULL, "Omu_IntODE::syseq_forward: was compiled without ADOL-C"); #endif }
/* MAIN PROGRAM */ int main() { int n,i,it; size_t tape_stats[STAT_SIZE]; /*--------------------------------------------------------------------------*/ /* Input */ fprintf(stdout,"SPEELPENNINGS PRODUCT Type 1 (ADOL-C Example)\n\n"); fprintf(stdout,"number of independent variables = ? \n"); scanf("%d",&n); int itu; fprintf(stdout,"number of evaluations = ? \n"); scanf("%d",&itu); /*--------------------------------------------------------------------------*/ double yp=0.0; /* 0. time (undifferentiated double code) */ double *xp = new double[n]; /* Init */ for (i=0;i<n;i++) xp[i] = (i+1.0)/(2.0+i); double t00 = myclock(1); for (it=0; it<itu; it++) { yp = 1.0; for (i=0; i<n; i++) yp *= xp[i]; } double t01 = myclock(); /*--------------------------------------------------------------------------*/ double yout=0; /* 1. time (tracing ! no keep) */ double t10 = myclock(); trace_on(TAG); adouble* x; x = new adouble[n]; adouble y; y = 1; for (i=0; i<n; i++) { x[i] <<= xp[i]; y *= x[i]; } y >>= yout; delete [] x; trace_off(); double t11 = myclock(); fprintf(stdout,"%E =? %E function values should be the same \n",yout,yp); /*--------------------------------------------------------------------------*/ 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]); /*--------------------------------------------------------------------------*/ double **r = new double*[1]; r[0] = new double[1]; r[0][0] = yp; double err; double *z = new double[n]; double *g = new double[n]; double* h = new double[n]; double *ind = new double[n]; /*--------------------------------------------------------------------------*/ double t60 = myclock(); /* 6. time (forward no keep) */ for (it=0; it<itu; it++) forward(TAG,1,n,0,xp,*r); double t61 = myclock(); /*--------------------------------------------------------------------------*/ double t20 = myclock(); /* 2. time (forward+keep) */ for (it=0; it<itu; it++) forward(TAG,1,n,1,xp,*r); double t21 = myclock(); /*--------------------------------------------------------------------------*/ double t30 = myclock(); /* 3. time (reverse) */ for (it=0; it<itu; it++) reverse(TAG,1,n,0,1.0,g); double t31 = myclock(); err=0; for (i=0; i<n; i++) // Compare with deleted product { err = maxabs(err,xp[i]*g[i]/r[0][0] - 1.0); ind[i] = xp[i]; } fprintf(stdout,"%E = maximum relative errors in gradient (fw+rv)\n",err); /*--------------------------------------------------------------------------*/ double t40 = myclock(); /* 4. time (gradient) */ for (it=0; it<itu; it++) gradient(TAG,n,ind,z); //last argument lagrange is ommitted double t41 = myclock(); err = 0; for (i=0; i<n; i++) // Compare with previous numerical result err = maxabs(err,g[i]/z[i] - 1.0); fprintf(stdout,"%E = gradient error should be exactly zero \n",err); /*--------------------------------------------------------------------------*/ double *tan = new double[n]; /* 5. time (first row of Hessian) */ for (i=1; i<n; i++) tan[i] = 0.0 ; tan[0]=1.0; double t50 = myclock(); for (it=0; it<itu; it++) hess_vec(TAG,n,ind,tan,h); // Computes Hessian times direction tan. double t51 = myclock(); err = abs(h[0]); for (i=1; i<n; i++) //Compare with doubly deleted product err = maxabs(err,xp[0]*h[i]/g[i]-1.0); fprintf(stdout,"%E = maximum relative error in Hessian column \n",err); /*--------------------------------------------------------------------------*/ double h1n = h[n-1]; /* Check for symmetry */ tan[0]=0; tan[n-1]=1; hess_vec(TAG,n,ind,tan,h); // Computes Hessian times direction tan. fprintf(stdout, "%E = %E (1,n) and (n,1) entry should be the same\n",h1n,h[0]); /*--------------------------------------------------------------------------*/ /* output of results */ if (t01-t00) { double rtu = 1.0/(t01-t00); fprintf(stdout,"\n\n times for "); fprintf(stdout,"\n unitime : \t%E seconds",(t01-t00)/itu); fprintf(stdout,"\n tracing : \t%E",(t11-t10)*rtu*itu); fprintf(stdout," units \t%E seconds",(t11-t10)); fprintf(stdout, "\n----------------------------------------------------------"); fprintf(stdout,"\n forward (no keep): \t%E",(t61-t60)*rtu); fprintf(stdout," units \t%E seconds",(t61-t60)/itu); fprintf(stdout,"\n forward + keep : \t%E",(t21-t20)*rtu); fprintf(stdout," units \t%E seconds",(t21-t20)/itu); fprintf(stdout,"\n reverse : \t%E",(t31-t30)*rtu); fprintf(stdout," units \t%E seconds",(t31-t30)/itu); fprintf(stdout, "\n----------------------------------------------------------"); fprintf(stdout,"\n gradient : \t%E",(t41-t40)*rtu); fprintf(stdout," units \t%E seconds",(t41-t40)/itu); fprintf(stdout,"\n hess*vec : \t%E",(t51-t50)*rtu); fprintf(stdout," units \t%E seconds\n",(t51-t50)/itu); } else fprintf(stdout,"\n-> zero timing due to small problem dimension \n"); return 1; }
int main(int argc, char *argv[]) { int n=get_num_ind(); int i,j; struct timeval tv1,tv2; adouble *xad; adouble fad; double f; double *x; x=new double[n]; xad=new adouble[n]; get_initial_value(x); printf("evaluating the function..."); trace_on(tag); for(i=0;i<n;i++) { xad[i] <<= x[i]; } fad=func_eval(xad); fad >>= f; trace_off(); printf("done!\n"); // printf("function value =<%10.20f>\n",f); // function(tag,1,n,x,&f); // printf("adolc func value=<%10.20f>\n",f); //tape_doc(tag,1,n,x,&f); #ifdef _compare_with_full double **H; H = myalloc2(n,n); printf("computing full hessain...."); gettimeofday(&tv1,NULL); hessian(tag,n,x,H); printf("done\n"); gettimeofday(&tv2,NULL); printf("Computing the full hessian cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #ifdef _PRINTOUT for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf("H[%d][%d]=<%10.10f>",i,j,H[i][j]); } printf("\n"); } printf("\n"); #endif #endif #ifdef edge_pushing unsigned int *rind = NULL; unsigned int *cind = NULL; double *values = NULL; int nnz; int options[2]; options[0]=PRE_ACC; options[1]=COMPUT_GRAPH; gettimeofday(&tv1,NULL); // edge_hess(tag, 1, n, x, &nnz, &rind, &cind, &values, options); sparse_hess(tag,n,0,x, &nnz, &rind, &cind, &values, options); gettimeofday(&tv2,NULL); printf("Sparse Hessian: edge pushing cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #ifdef _PRINTOUT for(i=0;i<nnz;i++){ printf("<%d,%d>:<%10.10f>\n",cind[i],rind[i],values[i]); // printf("%d %d \n", rind[i], cind[i]); } #endif #endif #ifdef _compare_with_full #ifdef edge_pushing compare_matrix(n,H,nnz,cind,rind,values); #endif myfree2(H); #endif #ifdef edge_pushing printf("nnz=%d\n", nnz); free(rind); rind=NULL; free(cind); cind=NULL; free(values); values=NULL; #endif delete[] x; delete[] xad; return 0; }
/* The interface function */ void tapingVectorFunction( int tag, double* indeps, double* deps ) { int i; trace_on(tag); adouble* activeIndeps = new adouble[indepDim]; adouble* activeDeps = new adouble[depDim]; adouble* aIP = activeIndeps; double* iP = indeps; for (i=0; i<indepDim; i++) *aIP++ <<= *iP++; i = 3; adouble * activeRadMotCoeff = activeIndeps+i; i += radMotDegree; adouble * activeVerMotCoeff = activeIndeps+i; i += verMotDegree; adouble * activeHorMotCoeff = activeIndeps+i; i += horMotDegree; adouble * activeHelMotCoeff = activeIndeps+i; i += helMotDegree; adouble * activeAngMotCoeff = activeIndeps+i; i += angMotDegree; adouble * activeModRolCoeff = activeIndeps+i; activeGearFunction( activeIndeps, activeDeps, // jetzt kommen die ganzen Parameter xmk, // Messerversatz ymk, // MK-Versatz kopspw, // Kopfspanwinkel flaspw, // Flankenspanwinkel meschw, // Messerschwenkwinkel flkrrd, // Flugkreisradius e, // Exzentrizitaet exzenw, // Exzentrizitaetswinkel thetas, // Messerkopfschwenkung thetan, // Messerkopfneigung xmw, // MK-x ymw, // MK-y zmw, // MK-z thetaw, // Wiegenwinkel m, // Achsversatz zwr, // Verschiebung Werkradachse delta, // Teilkegeloeffnungswinkel omega, // c, r, // Kopfradius rs, // Sphaerikradius ys, // Sphaerik-Mitte-Y zs, // Sphaerik-Mitte-Z // jetzt die Zusatzbewegungen radMotDegree, activeRadMotCoeff, verMotDegree, activeVerMotCoeff, horMotDegree, activeHorMotCoeff, helMotDegree, activeHelMotCoeff, angMotDegree, activeAngMotCoeff, modRolDegree, activeModRolCoeff ); aIP = activeDeps; iP = deps; for (i=0; i<depDim; i++) *aIP++ >>= *iP++; trace_off(); delete [] activeDeps; delete [] activeIndeps; }
/* MAIN PROGRAM */ int main() { /*------------------------------------------------------------------------*/ /* variables */ const int tag = 1; // tape tag const int size = 5; // system size const int indep = size*size+size; // # of indeps const int depen = size; // # of deps double A[size][size], a1[size], a2[size], // passive variables b[size], x[size]; adouble **AA, *AAp, *Abx; // active variables double *args = myalloc1(indep); // arguments double **jac = myalloc2(depen,indep); // the Jacobian double *laghessvec = myalloc1(indep); // Hessian-vector product int i,j; /*------------------------------------------------------------------------*/ /* Info */ fprintf(stdout,"LINEAR SYSTEM SOLVING by " "LU-DECOMPOSITION (ADOL-C Example)\n\n"); /*------------------------------------------------------------------------*/ /* Allocation und initialization of the system matrix */ AA = new adouble*[size]; AAp = new adouble[size*size]; for (i=0; i<size; i++) { AA[i] = AAp; AAp += size; } Abx = new adouble[size]; for(i=0; i<size; i++) { a1[i] = i*0.25; a2[i] = i*0.33; } for(i=0; i<size; i++) { for(j=0; j<size; j++) A[i][j] = a1[i]*a2[j]; A[i][i] += i+1; b[i] = -i-1; } /*------------------------------------------------------------------------*/ /* Taping the computation of the determinant */ trace_on(tag); /* marking indeps */ for(i=0; i<size; i++) for(j=0; j<size; j++) AA[i][j] <<= (args[i*size+j] = A[i][j]); for(i=0; i<size; i++) Abx[i] <<= (args[size*size+i] = b[i]); /* LU-factorization and computation of solution */ LUfact(size,AA); LUsolve(size,AA,Abx); /* marking deps */ for (i=0; i<size; i++) Abx[i] >>= x[i]; trace_off(); fprintf(stdout," x[0] (original): %16.4le\n",x[0]); /*------------------------------------------------------------------------*/ /* Recomputation */ function(tag,depen,indep,args,x); fprintf(stdout," x[0] (from tape): %16.4le\n",x[0]); /*------------------------------------------------------------------------*/ /* Computation of Jacobian */ jacobian(tag,depen,indep,args,jac); fprintf(stdout," Jacobian:\n"); for (i=0; i<depen; i++) { for (j=0; j<indep; j++) fprintf(stdout," %14.6le",jac[i][j]); fprintf(stdout,"\n"); } /*------------------------------------------------------------------------*/ /* Computation of Lagrange-Hessian-vector product */ lagra_hess_vec(tag,depen,indep,args,args,x,laghessvec); fprintf(stdout," Part of Lagrange-Hessian-vector product:\n"); for (i=0; i<size; i++) { for (j=0; j<size; j++) fprintf(stdout," %14.6le",laghessvec[i*size+j]); fprintf(stdout,"\n"); } /*------------------------------------------------------------------------*/ /* Tape-documentation */ tape_doc(tag,depen,indep,args,x); /*------------------------------------------------------------------------*/ /* Tape statistics */ int tape_stats[STAT_SIZE]; tapestats(tag,tape_stats); fprintf(stdout,"\n independents %d\n",tape_stats[NUM_INDEPENDENTS]); fprintf(stdout," dependents %d\n",tape_stats[NUM_DEPENDENTS]); fprintf(stdout," operations %d\n",tape_stats[NUM_OPERATIONS]); fprintf(stdout," operations buffer size %d\n",tape_stats[OP_BUFFER_SIZE]); fprintf(stdout," locations buffer size %d\n",tape_stats[LOC_BUFFER_SIZE]); fprintf(stdout," constants buffer size %d\n",tape_stats[VAL_BUFFER_SIZE]); fprintf(stdout," maxlive %d\n",tape_stats[NUM_MAX_LIVES]); fprintf(stdout," valstack size %d\n\n",tape_stats[TAY_STACK_SIZE]); /*------------------------------------------------------------------------*/ /* That's it */ return 1; }
int tree_chop(struct btree *btree, struct delete_info *info, millisecond_t deadline) { int depth = btree->root.depth, level = depth - 1, suspend = 0; struct cursor *cursor; struct buffer_head *leafbuf, **prev, *leafprev = NULL; struct btree_ops *ops = btree->ops; struct sb *sb = btree->sb; int ret; cursor = alloc_cursor(btree, 0); prev = malloc(sizeof(*prev) * depth); memset(prev, 0, sizeof(*prev) * depth); down_write(&btree->lock); probe(btree, info->resume, cursor); leafbuf = level_pop(cursor); /* leaf walk */ while (1) { ret = (ops->leaf_chop)(btree, info->key, bufdata(leafbuf)); if (ret) { mark_buffer_dirty(leafbuf); if (ret < 0) goto error_leaf_chop; } /* try to merge this leaf with prev */ if (leafprev) { struct vleaf *this = bufdata(leafbuf); struct vleaf *that = bufdata(leafprev); /* try to merge leaf with prev */ if ((ops->leaf_need)(btree, this) <= (ops->leaf_free)(btree, that)) { trace(">>> can merge leaf %p into leaf %p", leafbuf, leafprev); (ops->leaf_merge)(btree, that, this); remove_index(cursor, level); mark_buffer_dirty(leafprev); brelse_free(btree, leafbuf); //dirty_buffer_count_check(sb); goto keep_prev_leaf; } brelse(leafprev); } leafprev = leafbuf; keep_prev_leaf: //nanosleep(&(struct timespec){ 0, 50 * 1000000 }, NULL); //printf("time remaining: %Lx\n", deadline - gettime()); // if (deadline && gettime() > deadline) // suspend = -1; if (info->blocks && info->freed >= info->blocks) suspend = -1; /* pop and try to merge finished nodes */ while (suspend || level_finished(cursor, level)) { /* try to merge node with prev */ if (prev[level]) { assert(level); /* node has no prev */ struct bnode *this = cursor_node(cursor, level); struct bnode *that = bufdata(prev[level]); trace_off("check node %p against %p", this, that); trace_off("this count = %i prev count = %i", bcount(this), bcount(that)); /* try to merge with node to left */ if (bcount(this) <= sb->entries_per_node - bcount(that)) { trace(">>> can merge node %p into node %p", this, that); merge_nodes(that, this); remove_index(cursor, level - 1); mark_buffer_dirty(prev[level]); brelse_free(btree, level_pop(cursor)); //dirty_buffer_count_check(sb); goto keep_prev_node; } brelse(prev[level]); } prev[level] = level_pop(cursor); keep_prev_node: /* deepest key in the cursor is the resume address */ if (suspend == -1 && !level_finished(cursor, level)) { suspend = 1; /* only set resume once */ info->resume = from_be_u64((cursor->path[level].next)->key); } if (!level) { /* remove depth if possible */ while (depth > 1 && bcount(bufdata(prev[0])) == 1) { trace("drop btree level"); btree->root.block = bufindex(prev[1]); mark_btree_dirty(btree); brelse_free(btree, prev[0]); //dirty_buffer_count_check(sb); depth = --btree->root.depth; vecmove(prev, prev + 1, depth); //set_sb_dirty(sb); } //sb->snapmask &= ~snapmask; delete_snapshot_from_disk(); //set_sb_dirty(sb); //save_sb(sb); ret = suspend; goto out; } level--; trace_off(printf("pop to level %i, block %Lx, %i of %i nodes\n", level, bufindex(cursor->path[level].buffer), cursor->path[level].next - cursor_node(cursor, level)->entries, bcount(cursor_node(cursor, level)));); } /* push back down to leaf level */ while (level < depth - 1) { struct buffer_head *buffer = sb_bread(vfs_sb(sb), from_be_u64(cursor->path[level++].next++->block)); if (!buffer) { ret = -EIO; goto out; } level_push(cursor, buffer, ((struct bnode *)bufdata(buffer))->entries); trace_off(printf("push to level %i, block %Lx, %i nodes\n", level, bufindex(buffer), bcount(cursor_node(cursor, level)));); }
bool link_sparse_hessian( size_t repeat , CppAD::vector<double> &x_arg , CppAD::vector<size_t> &i , CppAD::vector<size_t> &j , CppAD::vector<double> &h ) { // ----------------------------------------------------- // setup size_t k, m; size_t order = 0; // derivative order corresponding to function size_t tag = 0; // tape identifier size_t keep = 1; // keep forward mode results in buffer size_t n = x_arg.size(); // number of independent variables size_t ell = i.size(); // number of indices in i and j double f; // function value typedef CppAD::vector<double> DblVector; typedef CppAD::vector<adouble> ADVector; typedef CppAD::vector<size_t> SizeVector; ADVector X(n); // AD domain space vector double *x; // double domain space vector double **H; // Hessian ADVector Y(1); // AD range space value DblVector tmp(2 * ell); // double temporary vector x = 0; x = CPPAD_TRACK_NEW_VEC(n, x); H = 0; H = CPPAD_TRACK_NEW_VEC(n, H); for(k = 0; k < n; k++) { H[k] = 0; H[k] = CPPAD_TRACK_NEW_VEC(n, H[k]); } // choose a value for x CppAD::uniform_01(n, x); for(k = 0; k < n; k++) x_arg[k] = x[k]; // ------------------------------------------------------ while(repeat--) { // get the next set of indices CppAD::uniform_01(2 * ell, tmp); for(k = 0; k < ell; k++) { i[k] = size_t( n * tmp[k] ); i[k] = std::min(n-1, i[k]); // j[k] = size_t( n * tmp[k + ell] ); j[k] = std::min(n-1, j[k]); } // declare independent variables trace_on(tag, keep); for(k = 0; k < n; k++) X[k] <<= x[k]; // AD computation of f(x) CppAD::sparse_evaluate(X, i, j, order, Y); // create function object f : X -> Y Y[0] >>= f; trace_off(); // evaluate and return the hessian of f hessian(int(tag), int(n), x, H); } for(k = 0; k < n; k++) { for(m = 0; m <= k; m++) { h[ k * n + m] = H[k][m]; h[ m * n + k] = H[k][m]; } CPPAD_TRACK_DEL_VEC(H[k]); } CPPAD_TRACK_DEL_VEC(H); CPPAD_TRACK_DEL_VEC(x); return true; }
int main(int argc, char *argv[]) { int n=NUM_IND; int i,j; struct timeval tv1,tv2; adouble *xad; adouble fad; double f; double *x; x=new double[n]; xad=new adouble[n]; get_initials(x, n); // printf("evaluating the function..."); trace_on(tag); for(i=0;i<n;i++) { xad[i] <<= x[i]; } fad=eval_func<adouble>(xad, n); fad >>= f; trace_off(); // printf("done!\n"); std::cout << "y = " << f << std::endl; #ifdef COMPARE_WITH_FULL_HESS double **H; H = myalloc2(n,n); printf("computing full hessain...."); gettimeofday(&tv1,NULL); hessian(tag,n,x,H); printf("done\n"); gettimeofday(&tv2,NULL); printf("Computing the full hessian cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #ifdef PRINT_RESULTS for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf("H[%d][%d]=<%10.10f>",i,j,H[i][j]); } printf("\n"); } printf("\n"); #endif #endif unsigned int *rind = NULL; unsigned int *cind = NULL; double *values = NULL; int nnz; int options[2]; #ifdef LIVARH options[0]=0; options[1]=1; gettimeofday(&tv1,NULL); edge_hess(tag, 1, n, x, &nnz, &rind, &cind, &values, options); gettimeofday(&tv2,NULL); printf("Sparse Hessian: LivarH cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #endif #ifdef LIVARHACC options[0]=1; options[1]=1; gettimeofday(&tv1,NULL); edge_hess(tag, 1, n, x, &nnz, &rind, &cind, &values, options); gettimeofday(&tv2,NULL); printf("Sparse Hessian: LivarHACC cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #endif // Sparse ADOL-C drivers report the upper matrix #ifdef DIRECT options[0]=0; options[1]=1; gettimeofday(&tv1,NULL); sparse_hess(tag, n, 0, x, &nnz, &cind, &rind, &values, options); gettimeofday(&tv2,NULL); printf("Sparse Hessian: direct recovery cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #endif #ifdef INDIRECT options[0]=0; options[1]=0; gettimeofday(&tv1,NULL); sparse_hess(tag, n, 0, x, &nnz, &cind, &rind, &values, options); gettimeofday(&tv2,NULL); printf("Sparse Hessian: indirect recovery cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #endif #ifdef PRINT_RESULTS for(i=0;i<nnz;i++){ printf("<%d,%d>:<%10.10f>\n",rind[i],cind[i],values[i]); } #endif #ifdef COMPARE_WITH_FULL_HESS compare_matrix(n,H,nnz,rind,cind,values); myfree2(H); #endif free(rind); rind=NULL; free(cind); cind=NULL; free(values); values=NULL; delete[] x; delete[] xad; return 0; }