Changeset 1614


Ignore:
Timestamp:
Dec 23, 2009 11:37:00 AM (11 years ago)
Author:
bradbell
Message:

trunk: Copy Taylor coefs and sparsity information in ADFun assingment.

fun_assing.cpp: demonstate and test use of newly copied values.
for_sparse_jac.cpp: demonstrate and test f.size_forward_bool.
whats_new_09.omh: changes from the users point of view.
ad_fun.hpp: alphabetize size_ member functions, add ones for sparsity info.
sparse_set.hpp: change memory() to number_elements() (what it knows).
for_sparse_jacobian.hpp: document size_forward_bool, size_forward_set.
sparse_pack.hpp: fix doxygen command.
cap_taylor.hpp: change copy order so closer to sequentail in memory (faster).
fun_construct.hpp: implement new version of ADFun assingment here.

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/cppad/local/ad_fun.hpp

    r1598 r1614  
    294294        );
    295295
     296        /// amount of memeory used for Jacobain sparsity pattern
     297        size_t size_forward_bool(void) const
     298        {       return for_jac_sparse_pack_.memory(); }
     299
     300        /// free memeory used for Jacobain sparsity pattern
     301        void size_forward_bool(size_t zero)
     302        {       CPPAD_ASSERT_KNOWN(
     303                        zero == 0,
     304                        "size_forward_bool: argument not equal to zero"
     305                );
     306                for_jac_sparse_pack_.resize(0, 0);
     307        }
     308
     309        /// total number of elements used for Jacobian sparsity pattern
     310        size_t size_forward_set(void) const
     311        {       return for_jac_sparse_set_.number_elements(); }
     312
     313        /// free memeory used for Jacobain sparsity pattern
     314        void size_forward_set(size_t zero)
     315        {       CPPAD_ASSERT_KNOWN(
     316                        zero == 0,
     317                        "size_forward_bool: argument not equal to zero"
     318                );
     319                for_jac_sparse_set_.resize(0, 0);
     320        }
     321
     322        /// number of operators in the operation sequence
     323        size_t size_op(void) const
     324        {       return play_.num_rec_op(); }
     325
     326        /// number of operator arguments in the operation sequence
     327        size_t size_op_arg(void) const
     328        {       return play_.num_rec_op_arg(); }
     329
     330        /// amount of memory required for the operation sequence
     331        size_t size_op_seq(void) const
     332        {       return play_.Memory(); }
     333
     334        /// number of parameters in the operation sequence
     335        size_t size_par(void) const
     336        {       return play_.num_rec_par(); }
     337
     338        /// number of taylor_ coefficients currently calculated (per variable)
     339        size_t size_taylor(void) const
     340        {       return taylor_per_var_; }
     341
     342        /// number of characters in the operation sequence
     343        size_t size_text(void) const
     344        {       return play_.num_rec_text(); }
     345
    296346        /// number of variables in opertion sequence
    297347        size_t size_var(void) const
    298348        {       return total_num_var_; }
    299349
    300         /// number of parameters in the operation sequence
    301         size_t size_par(void) const
    302         {       return play_.num_rec_par(); }
    303 
    304         /// number of operators in the operation sequence
    305         size_t size_op(void) const
    306         {       return play_.num_rec_op(); }
    307 
    308         /// number of operator arguments in the operation sequence
    309         size_t size_op_arg(void) const
    310         {       return play_.num_rec_op_arg(); }
    311 
    312         /// number of characters in the operation sequence
    313         size_t size_text(void) const
    314         {       return play_.num_rec_text(); }
    315 
    316350        /// number of VecAD indices in the operation sequence
    317351        size_t size_VecAD(void) const
    318352        {       return play_.num_rec_vecad_ind(); }
    319 
    320         /// amount of memory required for the operation sequence
    321         size_t size_op_seq(void) const
    322         {       return play_.Memory(); }
    323 
    324         /// number of taylor_ coefficients currently calculated (per variable)
    325         size_t size_taylor(void) const
    326         {       return taylor_per_var_; }
    327353
    328354        /// set number of coefficients currently allocated (per variable)
     
    419445        {       return taylor_per_var_ - 1; }
    420446
    421         /// deprecated: amount of memory for this object
     447        /// Deprecated: amount of memory for this object
     448        /// Note that an approximation is used for the std::set<size_t> memory
    422449        size_t Memory(void) const
    423450        {       size_t pervar  = taylor_col_dim_ * sizeof(Base)
    424451                + for_jac_sparse_pack_.memory()
    425                 + for_jac_sparse_set_.memory();
     452                + 3 * sizeof(size_t) * for_jac_sparse_set_.number_elements();
    426453                size_t total   = total_num_var_ * pervar + play_.Memory();
    427454                return total;
  • trunk/cppad/local/cap_taylor.hpp

    r1598 r1614  
    146146        // copy the old data into the new matrix
    147147        CPPAD_ASSERT_UNKNOWN( (taylor_per_var_ == 0) | (taylor_ != CPPAD_NULL) );
    148         for(j = 0; j < p; j++)
    149         {       for(i = 0; i < total_num_var_; i++)
     148        for(i = 0; i < total_num_var_; i++)
     149        {       for(j = 0; j < p; j++)
    150150                {       newptr[i * c + j]  = taylor_[i * taylor_col_dim_ + j];
    151151                }
  • trunk/cppad/local/for_sparse_jac.hpp

    r1556 r1614  
    6060for each of the variables in the operation sequence
    6161is stored in the object $icode f$$.
    62 
     62These sparsity patterns are stored with elements of type $code bool$$
     63or elements of type $code std::set<size_t>$$
     64(see $xref/ForSparseJac/VectorSet/VectorSet/$$ below).
     65 
     66$subhead size_forward_bool$$
     67After this operation, if $icode s$$ has is a $code size_t$$ object,
     68$codei%
     69        %s% = %f%.size_forward_bool()
     70%$$
     71will set $icode s$$ to the amount of memory (in unsigned character units)
     72used to store the sparsity pattern with elements of type $code bool$$.
     73If the sparsity patterns for this operation use elements of type $code bool$$,
     74the return value for $code size_forward_bool$$ will be non-zero.
     75Otherwise, its return value will be zero.
     76This sparsity pattern is stored for use by $cref/RevSparseHes/$$.
     77When it is not longer needed, it can be deleted (and the corresponding memory
     78freed) by the function call
     79$code%
     80        %f%size_forward_bool(0)
     81%$$
     82After this call, $icode%f%.size_forward_bool()%$$ will return zero.
     83 
     84$subhead size_forward_set$$
     85After this operation, if $icode s$$ has is a $code size_t$$ object,
     86$codei%
     87        %s% = %f%.size_forward_set()
     88%$$
     89will set $icode s$$ to the
     90total number of elements in all the sets corresponding
     91to the sparsity pattern stored in $icode f$$.
     92If the sparsity patterns for this operation use elements of type $code bool$$,
     93the return value for $code size_forward_set$$ will be zero.
     94Otherwise, its return value will be non-zero
     95(unless the entire sparsity pattern is false).
     96This sparsity pattern is stored for use by $cref/RevSparseHes/$$.
     97When it is not longer needed, it can be deleted (and the corresponding memory
     98freed) by the function call
     99$code%
     100        %f%size_forward_set(0)
     101%$$
     102After this call, $icode%f%.size_forward_set()%$$ will return zero.
    63103
    64104$head x$$
  • trunk/cppad/local/fun_construct.hpp

    r1598 r1614  
    1616$begin FunConstruct$$
    1717$spell
     18        Jac
     19        bool
    1820        taylor_
    1921        var
    2022        ADvector
    2123        const
     24        Jacobian
    2225$$
    2326
     
    167170can be $code const$$.
    168171Any operation sequence or other information in $icode g$$ is lost.
    169 Directly after this operation, no Taylor coefficients are
    170 stored in $icode g$$; i.e.,
    171 $codei%
    172         %g%.size_taylor()
    173 %$$
    174 is zero (see $cref/size_taylor/$$).
    175 
     172
     173$subhead Taylor Coefficients$$
     174The Taylor coefficient information currently stored in $icode f$$
     175(computed by $cref/f.Forward/Forward/$$) is
     176copied to $icode g$$.
     177Hence, directly after this operation
     178$codei%
     179        %g%.size_taylor() == %f%.size_taylor()
     180%$$
     181
     182$subhead Sparsity Patterns$$
     183The forward Jacobian sparsity pattern currently stored in $icode f$$
     184(computed by $cref/f.ForSparseJac/ForSparseJac/$$) is
     185copied to $icode g$$.
     186Hence, directly after this operation
     187$codei%
     188        %g%.size_forward_bool() == %f%.size_forward_bool()
     189        %g%.size_forward_set()  == %f%.size_forward_set()
     190%$$
    176191
    177192$head OpenMP$$
     
    292307        for_jac_sparse_pack_.resize(0, 0);
    293308        for_jac_sparse_set_.resize(0, 0);
     309
     310        // allocate and copy the Taylor coefficients
     311        taylor_per_var_     = f.taylor_per_var_;
     312        taylor_col_dim_     = f.taylor_col_dim_;
     313        size_t length       = total_num_var_ * taylor_col_dim_;
     314        if( length > 0 ) taylor_   = CPPAD_TRACK_NEW_VEC(length, taylor_);
     315        size_t i, j;
     316        for(i = 0; i < total_num_var_; i++)
     317        {       for(j = 0; j < taylor_per_var_; j++)
     318                {       taylor_[ i * taylor_col_dim_ + j ] =
     319                                f.taylor_[ i * taylor_col_dim_ + j ];
     320                }
     321        }
     322
     323        // allocate and copy the forward sparsity information
     324        size_t n_set = f.for_jac_sparse_pack_.n_set();
     325        size_t end   = f.for_jac_sparse_pack_.end();
     326        if( n_set > 0 )
     327        {       CPPAD_ASSERT_UNKNOWN( n_set == total_num_var_ );
     328                CPPAD_ASSERT_UNKNOWN( f.for_jac_sparse_set_.n_set() == 0 );
     329                for_jac_sparse_pack_.resize(n_set, end);
     330                for(i = 0; i < total_num_var_ ; i++)
     331                {       for_jac_sparse_pack_.assignment(
     332                                i                       ,
     333                                i                       ,
     334                                f.for_jac_sparse_pack_
     335                        );
     336                }
     337        }
     338        n_set = f.for_jac_sparse_set_.n_set();
     339        end   = f.for_jac_sparse_set_.end();
     340        if( n_set > 0 )
     341        {       CPPAD_ASSERT_UNKNOWN( n_set == total_num_var_ );
     342                CPPAD_ASSERT_UNKNOWN( f.for_jac_sparse_pack_.n_set() == 0 );
     343                for_jac_sparse_set_.resize(n_set, end);
     344                for(i = 0; i < total_num_var_; i++)
     345                {       for_jac_sparse_set_.assignment(
     346                                i                       ,
     347                                i                       ,
     348                                f.for_jac_sparse_set_
     349                        );
     350                }
     351        }
     352
    294353}
    295354
  • trunk/cppad/local/sparse_pack.hpp

    r1588 r1614  
    298298        /*! Amount of memory used by this vector of sets
    299299 
    300         /return
     300        \return
    301301        The amount of memory in units of type unsigned char memory.
    302302        */
  • trunk/cppad/local/sparse_set.hpp

    r1588 r1614  
    188188
    189189        // -----------------------------------------------------------------
    190         /*! Assing a set equal to the union of two other sets.
     190        /*! Assign a set equal to the union of two other sets.
    191191
    192192        \param this_target
     
    237237        }
    238238        // -----------------------------------------------------------------
    239         /*! Amount of memory used by this vector of sets
     239        /*! Sum over all sets of the number of elements
    240240 
    241241        /return
    242         The amount of memory in units of type unsigned char memory.
    243 
    244         This is just counts the memory for the set elements, not for any
    245         of the supporting information.
     242        The the total number of elements
    246243        */
    247         size_t memory(void) const
     244        size_t number_elements(void) const
    248245        {       size_t i, count;
     246                count = 0;
    249247                for(i = 0; i < n_set_; i++)
    250                         count += sizeof(size_t) * data_[i].size();
     248                        count += data_[i].size();
    251249                return count;
    252250        }
  • trunk/example/for_sparse_jac.cpp

    r1594 r1614  
    8383        ok &= (s[ 2 * n + 0 ] == false); // Y[2] does not depend on X[0]
    8484        ok &= (s[ 2 * n + 1 ] == true);  // Y[2] does     depend on X[1]
     85
     86        // check that values are stored
     87        ok &= (f.size_forward_bool() > 0);
     88
     89        // free values from forward calculation
     90        f.size_forward_bool(0);
     91        ok &= (f.size_forward_bool() == 0);
    8592
    8693        return ok;
  • trunk/example/fun_assign.cpp

    r1561 r1614  
    3434        using CppAD::AD;
    3535        using CppAD::NearEqual;
     36        size_t i, j;
    3637
    3738        // ten times machine percision
     
    3940
    4041        // two ADFun<double> objects
    41         CppAD::ADFun<double> f, g;
     42        CppAD::ADFun<double> g;
    4243
    4344        // domain space vector
    44         size_t n  = 1;
     45        size_t n  = 3;
    4546        CPPAD_TEST_VECTOR< AD<double> > x(n);
    46         x[0]      = .5;
     47        for(j = 0; j < n; j++)
     48                x[j] = AD<double>(j + 2);
    4749
    4850        // declare independent variables and start tape recording
     
    5052
    5153        // range space vector
    52         size_t m = 1;
     54        size_t m = 2;
    5355        CPPAD_TEST_VECTOR< AD<double> > y(m);
    54         y[0] = 1. + 2. * x[0] + 3. * x[0] * x[0];
     56        y[0] = x[0] + x[0] * x[1];
     57        y[1] = x[1] * x[2] + x[2];
    5558
    56         // store operation sequence in f
    57         f.Dependent(x, y);
     59        // Store operation sequence, and order zero forward results, in f.
     60        CppAD::ADFun<double> f(x, y);
     61
     62        // sparsity pattern for the identity matrix
     63        CPPAD_TEST_VECTOR< std::set<size_t> > r(n);
     64        for(j = 0; j < n; j++)
     65                r[j].insert(j);
     66
     67        // Store forward mode sparsity pattern in f
     68        f.ForSparseJac(n, r);
    5869
    5970        // make a copy in g
    6071        g = f;
    6172
    62         // zero order forward mode at two different points
    63         // (this could be useful if f and g were executed in parallel)
    64         CPPAD_TEST_VECTOR<double> x_f(n), x_g(n), y_f(m), y_g(m);
    65         x_f[0] = 1.;
    66         x_g[0] = 2.;
    67         y_f    = f.Forward(0, x_f);
    68         y_g    = g.Forward(0, x_g);
    69         ok   &= NearEqual(y_f[0], 1.+2.*x_f[0]+3.*x_f[0]*x_f[0], eps, eps);
    70         ok   &= NearEqual(y_g[0], 1.+2.*x_g[0]+3.*x_g[0]*x_g[0], eps, eps);
     73        // check values that should be equal
     74        ok &= ( g.size_taylor()       == f.size_taylor() );
     75        ok &= ( g.size_forward_bool() == f.size_forward_bool() );
     76        ok &= ( g.size_forward_set()  == f.size_forward_set() );
    7177
    72         // first order reverse mode
    73         // (this could be useful if f and g were executed in parallel)
    74         CPPAD_TEST_VECTOR<double> wf(m), wg(m), dy_f(n), dy_g(n);
    75         wf[0] = 1.;
    76         wg[0] = 1.;
    77         dy_f  = f.Reverse(1, wf);
    78         dy_g  = g.Reverse(1, wg);
    79         ok   &= NearEqual(dy_f[0], 2.+6.*x_f[0], eps, eps);
    80         ok   &= NearEqual(dy_g[0], 2.+6.*x_g[0], eps, eps);
     78        // Use zero order Taylor coefficient from f for first order
     79        // calculation using g.
     80        CPPAD_TEST_VECTOR<double> dx(n), dy(m);
     81        for(i = 0; i < n; i++)
     82                dx[i] = 0.;
     83        dx[1] = 1;
     84        dy    = g.Forward(1, dx);
     85        ok &= NearEqual(dy[0], x[0], eps, eps); // partial y[0] w.r.t x[1]
     86        ok &= NearEqual(dy[1], x[2], eps, eps); // partial y[1] w.r.t x[1]
     87
     88        // Use forward Jacobian sparsity pattern from f to calculate
     89        // Hessian sparsity pattern using g.
     90        CPPAD_TEST_VECTOR< std::set<size_t> > s(1), h(n);
     91        s[0].insert(0); // Compute sparsity pattern for Hessian of y[0]
     92        h =  f.RevSparseHes(n, s);
     93
     94        // check sparsity pattern for Hessian of y[0] = x[0] + x[0] * x[1]
     95        ok  &= ( h[0].find(0) == h[0].end() ); // zero     w.r.t x[0], x[0]
     96        ok  &= ( h[0].find(1) != h[0].end() ); // non-zero w.r.t x[0], x[1]
     97        ok  &= ( h[0].find(2) == h[0].end() ); // zero     w.r.t x[0], x[2]
     98
     99        ok  &= ( h[1].find(0) != h[1].end() ); // non-zero w.r.t x[1], x[0]
     100        ok  &= ( h[1].find(1) == h[1].end() ); // zero     w.r.t x[1], x[1]
     101        ok  &= ( h[1].find(2) == h[1].end() ); // zero     w.r.t x[1], x[2]
     102
     103        ok  &= ( h[2].find(0) == h[2].end() ); // zero     w.r.t x[2], x[0]
     104        ok  &= ( h[2].find(1) == h[2].end() ); // zero     w.r.t x[2], x[1]
     105        ok  &= ( h[2].find(2) == h[2].end() ); // zero     w.r.t x[2], x[2]
    81106
    82107        return ok;
  • trunk/omh/whats_new_09.omh

    r1612 r1614  
    4747        Jacobians
    4848        Jac
     49        Taylor
    4950$$
    5051
     
    5960(Comments about developer documentation are only important if you are
    6061trying to read and understand the CppAD source code.)
     62
     63$head 12-23$$
     64Change the
     65$code ADFun$$ $cref/assignment operator/FunConstruct/Assignment Operator/$$
     66was changed so that it now copies forward mode
     67Taylor coefficients and sparsity pattern information.
     68(This assignment operator was added on $cref/10-24/whats_new_09/10-24/$$.)
     69You can use $cref/capacity_taylor/$$ to delete the Taylor coefficients
     70before copying them.
     71Two new functions were added so that you can query and delete the
     72forward mode sparsity information;
     73see
     74$cref/size_forward_bool/ForSparseJac/f/size_forward_bool/$$ and
     75$cref/size_forward_set/ForSparseJac/f/size_forward_set/$$.
    6176
    6277$head 12-22$$
Note: See TracChangeset for help on using the changeset viewer.