Changeset 1594


Ignore:
Timestamp:
Nov 28, 2009 9:47:54 AM (11 years ago)
Author:
bradbell
Message:

trunk: Improve hash coding of operators.

for_sparse_jac.cpp: simplier construction of identity sparsity pattern.
rev_sparse_jac.cpp: simplier construction of identity sparsity pattern.
rev_sparse_hes.cpp: simplier construction of identity sparsity pattern.
glossary.omh: fix error in description of sparsity patterns.
hash_code.hpp: quicker and more separation of operators.

Location:
trunk
Files:
5 edited

Legend:

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

    r1590 r1594  
    8787\par Checked Assertions
    8888\li \c std::numeric_limits<unsigned short>::max() == CPPAD_HASH_TABLE_SIZE - 1
    89 \li \c size_t(op) < CPPAD_HASH_TABLE_SIZE
     89\li \c size_t(op) <= size_t(SubvvOp) < CPPAD_HASH_TABLE_SIZE
    9090\li \c sizeof(size_t) is even
    9191\li \c sizeof(unsigned short)  == 2
     
    9393
    9494inline unsigned short hash_code(OpCode op, const size_t* arg)
    95 {       static size_t half_table_size = CPPAD_HASH_TABLE_SIZE / 2;
    96 
    97         CPPAD_ASSERT_UNKNOWN(
     95{       CPPAD_ASSERT_UNKNOWN(
    9896                std::numeric_limits<unsigned short>::max()
    9997                ==
    10098                (CPPAD_HASH_TABLE_SIZE-1)
    10199        );
    102         CPPAD_ASSERT_UNKNOWN( size_t (op) < CPPAD_HASH_TABLE_SIZE );
     100        CPPAD_ASSERT_UNKNOWN( size_t (op) <= size_t(SubvvOp) );
    103101        CPPAD_ASSERT_UNKNOWN( sizeof(unsigned short) == 2 );
    104102        CPPAD_ASSERT_UNKNOWN( sizeof(size_t) % 2  == 0 );
     103        static unsigned short op_fac = static_cast<unsigned short> (
     104        CPPAD_HASH_TABLE_SIZE / ( 1 + static_cast<unsigned short>(SubvvOp) )
     105        );
     106        CPPAD_ASSERT_UNKNOWN( op_fac > 0 );
    105107
    106108        // number of shorts per size_t value
     
    110112        n = n * NumArg(op);
    111113
    112         unsigned short code = static_cast<unsigned short>(op);
    113         if( code > 0 ) while( code < half_table_size )
    114                 code *= 2;
     114        // initialize with value that separates operators as much as possible
     115        unsigned short code = static_cast<unsigned short>(
     116                static_cast<unsigned short>(op) * op_fac
     117        );
    115118
     119        // now code in the operands
    116120        const unsigned short* v
    117121                = reinterpret_cast<const unsigned short*>(arg);
  • trunk/example/for_sparse_jac.cpp

    r1563 r1594  
    6969        for(i = 0; i < n; i++)
    7070        {       for(j = 0; j < n; j++)
    71                         r[ i * n + j ] = false;
    72                 r[ i * n + i ] = true;
     71                        r[ i * n + j ] = (i == j);
    7372        }
    7473
  • trunk/example/rev_sparse_hes.cpp

    r1563 r1594  
    8181        for(i = 0; i < n; i++)
    8282        {       for(j = 0; j < n; j++)
    83                         r[ i * n + j ] = false;
    84                 r[ i * n + i ] = true;
     83                        r[ i * n + j ] = (i == j);
    8584        }
    8685
  • trunk/example/rev_sparse_jac.cpp

    r1563 r1594  
    6767        for(i = 0; i < m; i++)
    6868        {       for(j = 0; j < m; j++)
    69                         s[ i * m + j ] = false;
    70                 s[ i * m + i ] = true;
     69                        s[ i * m + j ] = (i == j);
    7170        }
    7271
  • trunk/omh/glossary.omh

    r1550 r1594  
    202202For example, if $latex A$$ is the identity matrix,
    203203$latex \[
    204         S_i =  \{ j \}
     204        S_i =  \{ i \}
    205205\] $$
    206206defines an efficient sparsity pattern for $latex A$$.
Note: See TracChangeset for help on using the changeset viewer.