Changeset 1658


Ignore:
Timestamp:
Mar 3, 2010 11:42:54 AM (10 years ago)
Author:
bradbell
Message:

/home/bradbell/cppad/trunk: Fix bool packed bug in case where set size is a multiple of n_bit.

spars_jacobian.cpp: add test for case where set size is a multiple of n_bit.
*/makefile.in: update makefile.am version corresponding to this file.
whats_new_10.omh: user's view of the changes.
sparse_pack.hpp: avoid accessing of end of data_ vector.

Location:
trunk
Files:
11 edited

Legend:

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

    r1641 r1658  
    2727class sparse_pack {
    2828private:
    29         /// type used to pack elements
     29        /// Type used to pack elements (should be the same as corresponding
     30        /// typedef in multiple_n_bit() in test_more/sparse_hacobian.cpp)
    3031        typedef size_t Pack;
    3132        /// Number of bits per Pack value
     
    168169        {       static Pack one(1);
    169170                CPPAD_ASSERT_UNKNOWN( next_index_ < n_set_ );
     171                CPPAD_ASSERT_UNKNOWN( next_element_ <= end_ );
    170172
    171173                if( next_element_ == end_ )
     
    178180                size_t k  = next_element_ - j * n_bit_;
    179181
    180                 // search for next element of the set
     182                // start search at this packed value
    181183                Pack check = data_[ next_index_ * n_pack_ + j ];
    182                 while( next_element_ < end_ )
    183                 {       if( check & (one << k) )
    184                         {       next_element_++;
     184                while( true )
     185                {       // increment next element before checking this one
     186                        next_element_++;
     187                        // check if this element is in the set
     188                        if( check & (one << k) )
    185189                                return next_element_ - 1;
    186                         }
    187                         next_element_++;
     190                        // check if no more elements in the set
     191                        if( next_element_ == end_ )
     192                                return end_;
     193                        // increment bit index in Pack value so corresponds to
     194                        // next element
    188195                        k++;
    189196                        CPPAD_ASSERT_UNKNOWN( k <= n_bit_ );
    190197                        if( k == n_bit_ )
    191                         {       k     = 0;
     198                        {       // get next packed value
     199                                k     = 0;
    192200                                j++;
    193201                                CPPAD_ASSERT_UNKNOWN( j < n_pack_ );
     
    195203                        }
    196204                }
    197                 next_element_ = end_;
     205                // should never get here
     206                CPPAD_ASSERT_UNKNOWN(false);
    198207                return end_;
    199208        }
  • trunk/makefile.in

    r1657 r1658  
    233233@CppAD_POSTFIX_FALSE@postfix_dir = .
    234234
    235 # $Id: makefile.am 1654 2010-02-10 04:27:46Z bradbell $
     235# $Id: makefile.am 1657 2010-02-19 13:51:17Z bradbell $
    236236# -----------------------------------------------------------------------------
    237237# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
  • trunk/omh/whats_new_10.omh

    r1655 r1658  
    1313$begin whats_new_10$$
    1414$spell
     15        sizeof
    1516        libcppad_ipopt.a
    1617        Microsoft
     
    3637The purpose of this section is to
    3738assist you in learning about changes between various versions of CppAD.
     39
     40$head 03-03$$
     41Fixed a bug in handling
     42$cref/vector of boolean/glossary/Sparsity Pattern/Vector of Boolean/$$
     43sparsity patterns.
     44(when the number of elements per set was a multiple of
     45$code sizeof(size_t))$$.
    3846
    3947$head 02-11$$
  • trunk/speed/adolc/makefile.in

    r1655 r1658  
    8282CYGPATH_W = @CYGPATH_W@
    8383
    84 # $Id: makefile.am 1654 2010-02-10 04:27:46Z bradbell $
     84# $Id: makefile.am 1655 2010-02-11 19:59:43Z bradbell $
    8585# -----------------------------------------------------------------------------
    8686# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
  • trunk/speed/cppad/makefile.in

    r1655 r1658  
    8282CYGPATH_W = @CYGPATH_W@
    8383
    84 # $Id: makefile.am 1654 2010-02-10 04:27:46Z bradbell $
     84# $Id: makefile.am 1655 2010-02-11 19:59:43Z bradbell $
    8585# -----------------------------------------------------------------------------
    8686# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
  • trunk/speed/double/makefile.in

    r1655 r1658  
    8282CYGPATH_W = @CYGPATH_W@
    8383
    84 # $Id: makefile.am 1654 2010-02-10 04:27:46Z bradbell $
     84# $Id: makefile.am 1655 2010-02-11 19:59:43Z bradbell $
    8585# -----------------------------------------------------------------------------
    8686# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
  • trunk/speed/example/makefile.in

    r1655 r1658  
    8383CYGPATH_W = @CYGPATH_W@
    8484
    85 # $Id: makefile.am 1654 2010-02-10 04:27:46Z bradbell $
     85# $Id: makefile.am 1655 2010-02-11 19:59:43Z bradbell $
    8686# -----------------------------------------------------------------------------
    8787# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
  • trunk/speed/fadbad/makefile.in

    r1655 r1658  
    8282CYGPATH_W = @CYGPATH_W@
    8383
    84 # $Id: makefile.am 1654 2010-02-10 04:27:46Z bradbell $
     84# $Id: makefile.am 1655 2010-02-11 19:59:43Z bradbell $
    8585# -----------------------------------------------------------------------------
    8686# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
  • trunk/speed/profile/makefile.in

    r1655 r1658  
    8282CYGPATH_W = @CYGPATH_W@
    8383
    84 # $Id: makefile.am 1654 2010-02-10 04:27:46Z bradbell $
     84# $Id: makefile.am 1655 2010-02-11 19:59:43Z bradbell $
    8585# -----------------------------------------------------------------------------
    8686# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
  • trunk/speed/sacado/makefile.in

    r1655 r1658  
    8282CYGPATH_W = @CYGPATH_W@
    8383
    84 # $Id: makefile.am 1654 2010-02-10 04:27:46Z bradbell $
     84# $Id: makefile.am 1655 2010-02-11 19:59:43Z bradbell $
    8585# -----------------------------------------------------------------------------
    8686# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
  • trunk/test_more/sparse_jacobian.cpp

    r1558 r1658  
    11/* $Id$ */
    22/* --------------------------------------------------------------------------
    3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-09 Bradley M. Bell
     3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
    44
    55CppAD is distributed under multiple licenses. This distribution is under
     
    259259        return ok;
    260260}
     261
     262bool multiple_of_n_bit()
     263{       bool ok = true;
     264        using CppAD::AD;
     265        using CppAD::vector;
     266        size_t i, j;
     267
     268        // should be the same as the corresponding typedef in
     269        // cppad/local/sparse_pack.hpp
     270        typedef size_t Pack;
     271
     272        // number of bits per packed value
     273        size_t n_bit = std::numeric_limits<Pack>::digits;
     274
     275        // check case where number of variables is equal to n_bit
     276        vector< AD<double> > x(n_bit);
     277        vector< AD<double> > y(n_bit);
     278
     279        // create an AD function with domain and range dimension equal to n_bit
     280        CppAD::Independent(x);
     281        for(i = 0; i < n_bit; i++)
     282                y[i] = x[n_bit - i - 1];
     283        CppAD::ADFun<double> f(x, y);
     284
     285        // Jacobian sparsity patterns
     286        vector<bool> r(n_bit * n_bit);
     287        vector<bool> s(n_bit * n_bit);
     288        for(i = 0; i < n_bit; i++)
     289        {       for(j = 0; j < n_bit; j++)
     290                        r[ i * n_bit + j ] = (i == j);
     291        }
     292        s = f.ForSparseJac(n_bit, r);
     293
     294        // check the result
     295        for(i = 0; i < n_bit; i++)
     296        {       for(j = 0; j < n_bit; j++)
     297                {       if( i == n_bit - j - 1 )
     298                                ok = ok & s[ i * n_bit + j ];
     299                        else    ok = ok & (! s[i * n_bit + j] );
     300                }
     301        }
     302
     303        return ok;
     304}
    261305} // End empty namespace
    262306# include <vector>
     
    291335        // ok &= reverse_set< std::valarray<double>, std_valarray_set >();
    292336        // ---------------------------------------------------------------
    293         return ok;
    294 }
     337        //
     338        ok &= multiple_of_n_bit();
     339        return ok;
     340}
Note: See TracChangeset for help on using the changeset viewer.