double do_time_adolc(double *x, double **seed, int d, int nloop,
                     int tag, const F& f)
{
  Teuchos::Time timer("F", false);
  int n = F::n;
  trace_on(tag);
  adouble *x_ad = new adouble[n];
  for (int i=0; i<n; i++)
    x_ad[i] <<= x[i];
  adouble y_ad = 0.0;
  f(x_ad, y_ad);
  double y;
  y_ad >>= y;
  delete [] x_ad;
  trace_off();

  double **jac = new double*[1];
  jac[0] = new double[d];
  timer.start(true);
  for (int j=0; j<nloop; j++)
    forward(tag, 1, n, d, x, seed, &y, jac);
  timer.stop();

  delete [] jac[0];
  delete [] jac;

  return timer.totalElapsedTime() / nloop;
}
Пример #2
0
/*                                                   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 = activeRosenbrock(indepDim, activeIndeps);
    double res = 0;
    ares >>= res;
    trace_off();
    return res;
}
Пример #3
0
/*                                                             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;
}
Пример #4
0
Файл: ileaf.c Проект: Zkin/tux3
static void ileaf_dump(struct btree *btree, void *vleaf)
{
	if (!tux3_trace)
		return;

	struct ileaf_attr_ops *attr_ops = btree->ops->private_ops;
	struct ileaf *leaf = vleaf;
	inum_t inum = ibase(leaf);
	__be16 *dict = ileaf_dict(btree, leaf);
	unsigned offset = 0;

	trace_on("inode table block 0x%Lx/%i (%x bytes free)",
		 ibase(leaf), icount(leaf), ileaf_free(btree, leaf));

	for (int i = 0; i < icount(leaf); i++, inum++) {
		int limit = __atdict(dict, i + 1), size = limit - offset;
		if (!size)
			continue;
		if (size < 0)
			trace_on("  0x%Lx: <corrupt>\n", inum);
		else if (!size)
			trace_on("  0x%Lx: <empty>\n", inum);
		else if (attr_ops == &iattr_ops) {
			/* FIXME: this doesn't work in kernel */
			struct tux3_inode tuxnode = {};
			struct inode *inode = &tuxnode.vfs_inode;
			void *attrs = leaf->table + offset;

			inode->i_sb = vfs_sb(btree->sb),
			attr_ops->decode(btree, inode, attrs, size);

			free_xcache(inode);
		}
		offset = limit;
	}
}
Пример #5
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++;
    activeEutroph(activeIndEPS_,activeDEPS_);
    aIP = activeDEPS_;
    iP  = dEPS_;
    for (i=0; i<depDim; i++)
        *aIP++ >>= *iP++;
    trace_off();
}
Пример #6
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++;
    activeGearFunction(activeIndeps,activeDeps);
    aIP = activeDeps;
    iP  = deps;
    for (i=0; i<depDim; i++)
        *aIP++ >>= *iP++;
    trace_off();
}
Пример #7
0
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;

}
Пример #8
0
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(..).
    */
}
Пример #9
0
void adolc_init_fill(bool retape,
                     int tag,
                     const ElemData& e,
                     unsigned int neqn,
                     const std::vector<double>& x,
                     const std::vector< std::vector<double> >& w,
                     std::vector<double>& x_local,
                     std::vector<adouble>& x_ad,
                     double** w_local) {
    if (retape)
        trace_on(tag, 1);
    for (unsigned int node=0; node<e.nnode; node++)
        for (unsigned int eqn=0; eqn<neqn; eqn++) {
            x_local[node*neqn+eqn] = x[e.gid[node]*neqn+eqn];
            if (retape)
                x_ad[node*neqn+eqn] <<= x_local[node*neqn+eqn];
        }

    for (unsigned int col=0; col<w.size(); col++)
        for (unsigned int node=0; node<e.nnode; node++)
            for (unsigned int eqn=0; eqn<neqn; eqn++)
                w_local[col][node*neqn+eqn] = w[col][e.gid[node]*neqn+eqn];
}
Пример #10
0
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++;
      }
Пример #11
0
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;
}
Пример #12
0
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
	{
Пример #13
0
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;
}
Пример #14
0
/*                                                             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;
}
Пример #15
0
void trace_pulse(void)
{
	trace_on();
	_delay_us(1);
	trace_off();
}
Пример #16
0
// 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
}
Пример #17
0
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;
}
Пример #18
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;
}
Пример #19
0
void run_client(const char *host, int port, int tun_fd, const char *username, const char *password, int use_ssl) {
    int flags = fcntl(tun_fd, F_GETFL, 0);
    assert(fcntl(tun_fd, F_SETFL, flags | O_NONBLOCK) == 0);

    char buff[BUFF_SZ];
    char url[URL_SZ];
    int read_len;

    CURL *curl;
    CURLcode res;
    double speed_upload, total_time;
    struct curl_httppost *post;
    struct curl_httppost *last_post;

    int backoff = 0;

    if (MAX_HOST_LEN < strlen(host)) {
        log_crit("client", "host-name too long");
        return;
    }
    make_pkt_post_url(host, port, use_ssl, url, URL_SZ);
    curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, url);
    if (trace_on()) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&tun_fd);
    curl_easy_setopt(curl, CURLOPT_USERNAME, username);
    curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
    if (use_ssl) {
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
    }

    assert(curl != NULL);
    post = last_post = NULL;
    while(! do_stop) {
        read_len = read(tun_fd, buff, BUFF_SZ);
        if (read_len == -1 && errno != EAGAIN)
            log_warn("client", "Failed to read from tun");

        if (read_len > 0) {
            if (post != NULL) {
                curl_formfree(post);
                post = last_post = NULL;
            }
            curl_formadd(&post, &last_post,
                         CURLFORM_COPYNAME, "pkt",
                         CURLFORM_BUFFER, "pkt",
                         CURLFORM_BUFFERPTR, buff,
                         CURLFORM_BUFFERLENGTH, read_len,
                         CURLFORM_END);
            curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
            backoff = MIN_BACKOFF_MICRO_SEC;
            log_debug("client", "Sending %d bytes of data in current request", read_len);
        } else {
            increase_backoff(&backoff);
            curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
            log_debug("client", "Sending NO-data in current request");
        }
        res = curl_easy_perform(curl);

        if(res != CURLE_OK) {
            log_warnx("client", "curl_easy_perform() failed: %s", curl_easy_strerror(res));
        } else {
            curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
            curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
            log_debug("client", "Speed: %.3f bytes/sec during %.3f seconds\n", speed_upload, total_time);
        }

        do_backoff(backoff, tun_fd);
    }
    if (post != NULL) curl_formfree(post);
    curl_easy_cleanup(curl);
}
Пример #20
0
/*                                                             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;
}