Changeset 2281


Ignore:
Timestamp:
Feb 9, 2012 9:26:59 AM (8 years ago)
Author:
bradbell
Message:

Add base_alloc a test case where base type allocates memory for each element.

dependent.hpp: add doxygen documentation.
memory_leak.hpp: extend so works when there is static allocated memory.
nan.hpp: add discussion of static memory allocation.
thread_alloc.hpp: fix some zero values that are actually null pointers.

Location:
trunk
Files:
1 added
9 edited

Legend:

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

    r2085 r2281  
    44
    55/* --------------------------------------------------------------------------
    6 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-11 Bradley M. Bell
     6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell
    77
    88CppAD is distributed under multiple licenses. This distribution is under
     
    143143namespace CppAD {
    144144
     145/*!
     146Determine the \c tape corresponding to this exeuction thread and then use
     147<code>Dependent(tape, y)</code> to store this tapes recording in a function.
     148
     149\param y [in]
     150The dependent variable vector for the corresponding function.
     151*/
    145152template <typename Base>
    146153template <typename ADvector>
     
    157164}
    158165
     166
     167/*!
     168Determine the \c tape corresponding to this exeuction thread and then use
     169<code>Dependent(tape, y)</code> to store this tapes recording in a function.
     170
     171\param x [in]
     172The independent variable vector for this tape. This informaiton is
     173also stored in the tape so a check is done to make sure it is correct
     174(if NDEBUG is not defined).
     175
     176\param y [in]
     177The dependent variable vector for the corresponding function.
     178*/
    159179template <typename Base>
    160180template <typename ADvector>
     
    198218        Dependent(tape, y);
    199219}
    200                
     220
     221/*!
     222Replace the floationg point operations sequence for this function object.
     223
     224\param tape
     225is a tape that contains the new floating point operation sequence
     226for this function.
     227After this operation, all memory allocated for this tape is deleted.
     228
     229\param y
     230The dependent variable vector for the function being stored in this object.
     231*/
    201232
    202233template <typename Base>
  • trunk/cppad/memory_leak.hpp

    r2233 r2281  
    44
    55/* --------------------------------------------------------------------------
    6 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-11 Bradley M. Bell
     6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell
    77
    88CppAD is distributed under multiple licenses. This distribution is under
     
    1616$begin memory_leak$$
    1717$spell
     18        num
    1819        alloc
    1920        hpp
     
    2627$index leak, memory$$
    2728$index check, memory leak$$
     29$index static, memory leak check$$
    2830
    2931$head Syntax$$
    30 $icode%flag% = %memory_leak()%$$
     32$icode%flag% = %memory_leak()
     33%$$
     34$icode%flag% = %memory_leak(%set_static%)%$$
     35
     36$head Purpose$$
     37This routine checks that the are no memory leaks
     38caused by improper use of $cref thread_alloc$$ memory allocator.
     39The deprecated memory allocator $cref/TrackNewDel/$$ is also checked.
     40Memory errors in the deprecated $cref omp_alloc$$ allocator are
     41reported as being in $code thread_alloc$$.
     42
     43$head set_static$$
     44Static variables hold onto memory forever.
     45If the argument $icode set_static$$ is present (and true),
     46$code memory_leak$$ sets the amount of memory that
     47is $cref/inuse/ta_inuse/$$ and corresponds to static variables in the program.
     48It therefore necessary to call to all the routines that
     49have static variables and that allocate memory using
     50$cref/get_memory/ta_get_memory/$$ before making a call
     51with $icode set_static$$ true.
     52It is also necessary to have no other $code inuse$$ memory when
     53$icode set_static$$ is called.
     54Since multiple statics may be allocated in different places in the program,
     55it is OK to use this option more that once.
    3156
    3257$head flag$$
     
    3560        bool %flag%
    3661%$$
    37 
    38 $head Purpose$$
    39 This routine checks that the are no memory leaks
    40 caused by improper use of $cref thread_alloc$$ memory allocator.
    41 The deprecated memory allocator $cref/TrackNewDel/$$ is also checked.
    42 Memory errors in the deprecated allocator $cref omp_alloc$$ are
    43 reported as being in $code thread_alloc$$.
     62If $icode set_static$$ is true,
     63the return value for $code memory_leak$$ is false.
     64Otherwise, the return value for $code memory_leak$$ should be false
     65(indicating that the only allocated memory corresponds to static variables).
    4466
    4567$head inuse$$
    4668It is assumed that, when $code memory_leak$$ is called,
    4769there should not be any memory
    48 $cref/inuse/ta_inuse/$$ or $cref omp_inuse$$ for any thread.
     70$cref/inuse/ta_inuse/$$ or $cref omp_inuse$$ for any thread
     71(except for inuse memory corresponding to static variables).
    4972If there is, a message is printed and $code memory_leak$$ returns false.
    5073
     
    6487
    6588$head Error Message$$
    66 If this routine returns false, it prints a message
    67 to standard output describing the condition before returning false.
     89If this is the first call to $code memory_leak$$, no message is printed.
     90Otherwise, if it returns true, an error message is printed
     91to standard output describing the memory leak that was detected.
    6892
    6993$head Multi-Threading$$
    70 This routine cannot be used in $cref/in_parallel/ta_in_parallel/$$
    71 execution mode.
     94This routine can only be called when the number of threads
     95$cref/num_threads/ta_num_threads/$$ is one.
    7296
    7397$end
     
    88112Function that checks
    89113allocator \c thread_alloc for misuse that results in memory leaks.
    90 The deprecated routines
    91 in track_new_del.hpp and omp_alloc.hpp are also checked.
     114Deprecated routines in track_new_del.hpp and omp_alloc.hpp are also checked.
    92115
    93116\return
     117If this is the first call to \c memory_leak, the return value is \c true
     118and the amount of <code>thread_alloc::inuse()</code> memory corresponding
     119to thread zero is set.
     120
     121If this not the first call, if the return value is \c true,
     122if thread zero does not have the expected amount of inuse memory,
     123if one of the other threads has any inuse memory,
     124or any thread has available memory.
     125
    94126returns \c true, if no error is detected and \c false otherwise.
    95127
     
    98130output.
    99131*/
    100 inline bool memory_leak(void)
    101 {
    102         bool leak = false;
    103         size_t thread;
     132inline bool memory_leak(bool set_static = false)
     133{       // CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL not necessary given asserts below
     134        static size_t thread_zero_static_inuse     = 0;
    104135        using std::cout;
    105136        using std::endl;
     
    120151                "attempt to use thread_leak while num_threads > 1."
    121152        );
    122         for(thread = 0; thread < CPPAD_MAX_NUM_THREADS; thread++)
     153        if( set_static )
     154        {       thread_zero_static_inuse  = thread_alloc::inuse(0);
     155                return true;
     156        }
     157        bool leak     = false;
     158        size_t thread = 0;
     159
     160        // check that memory in use for thread zero corresponds to statics
     161        size_t num_bytes = thread_alloc::inuse(thread);
     162        if( num_bytes != thread_zero_static_inuse )
     163        {       leak = true;
     164                cout << "thread zero: static inuse = " << thread_zero_static_inuse;
     165                cout << "current inuse(thread)     = " << num_bytes << endl;
     166        }
     167        // check that no memory is currently available for this thread
     168        num_bytes = thread_alloc::available(thread);
     169        if( num_bytes != 0 )
     170        {       leak = true;
     171                cout << "thread zero: available    = ";
     172                cout << num_bytes << endl;
     173        }
     174        for(thread = 1; thread < CPPAD_MAX_NUM_THREADS; thread++)
    123175        {
    124176                // check that no memory is currently in use for this thread
    125                 size_t num_bytes = thread_alloc::inuse(thread);
     177                num_bytes = thread_alloc::inuse(thread);
    126178                if( num_bytes != 0 )
    127179                {       leak = true;
    128                         cout << "thread_alloc::inuse(thread) = ";
     180                        cout << "thread " << thread << ": inuse(thread) = ";
    129181                        cout << num_bytes << endl;
    130182                }
     
    133185                if( num_bytes != 0 )
    134186                {       leak = true;
    135                         cout << "thread_alloc::available(thread) = ";
     187                        cout << "thread " << thread << ": available(thread) = ";
    136188                        cout << num_bytes << endl;
    137189                }
  • trunk/cppad/nan.hpp

    r2085 r2281  
    44
    55/* --------------------------------------------------------------------------
    6 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-11 Bradley M. Bell
     6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell
    77
    88CppAD is distributed under multiple licenses. This distribution is under
     
    143143$index parallel, user_atomic$$
    144144$index user_atomic, parallel$$
    145 For each type $icode Scalar$$,
     145The function $code isnan$$ uses static variables.
     146Hence for each type $icode Scalar$$,
    146147the first call to
    147148$codei%
     
    150151must not be $cref/parallel/ta_in_parallel/$$ execution mode;
    151152see $code isnan$$ in $cref/parallel_ad/parallel_ad/isnan/$$.
     153
     154$head Memory Allocation$$
     155This function is called by CppAD even if it is not
     156explicitly referenced by the users code.
     157Hence any corresponding memory allocation for a static value
     158of type $icode Scalar$$ my be preformed.
    152159
    153160$children%
  • trunk/cppad/thread_alloc.hpp

    r2273 r2281  
    571571                // free memory allocated by call to get_memory above
    572572                return_memory(v_ptr);
    573                 free_available( set_get_thread_num(0) );
     573                free_available( set_get_thread_num(CPPAD_NULL) );
    574574
    575575                // delay this so thread_num() call above is in previous mode
     
    701701        /// Get current thread number
    702702        static size_t thread_num(void)
    703         {       return set_get_thread_num(0); }
     703        {       return set_get_thread_num(CPPAD_NULL); }
    704704/* -----------------------------------------------------------------------
    705705$begin ta_get_memory$$
  • trunk/omh/whats_new_12.omh

    r2279 r2281  
    3434The purpose of this section is to
    3535assist you in learning about changes between various versions of CppAD.
     36
     37$head 02-09$$
     38Add the $icode set_static$$ to $cref memory_leak$$.
     39This is a precursor to testing base types that allocate memory
     40for each element.
    3641
    3742$head 01-30$$
  • trunk/test_more/makefile.am

    r2268 r2281  
    6363        atan.cpp \
    6464        atan_2.cpp \
     65        base_alloc.cpp \
    6566        check_simple_vector.cpp \
    6667        compare.cpp \
  • trunk/test_more/makefile.in

    r2268 r2281  
    4545am__test_more_SOURCES_DIST = base_adolc.cpp test_more.cpp abs.cpp \
    4646        acos.cpp asin.cpp add.cpp add_eq.cpp add_zero.cpp atan.cpp \
    47         atan_2.cpp check_simple_vector.cpp compare.cpp \
     47        atan_2.cpp base_alloc.cpp check_simple_vector.cpp compare.cpp \
    4848        compare_change.cpp cond_exp.cpp cond_exp_ad.cpp copy.cpp \
    4949        cos.cpp cosh.cpp dbl_epsilon.cpp div.cpp div_eq.cpp \
     
    6464        abs.$(OBJEXT) acos.$(OBJEXT) asin.$(OBJEXT) add.$(OBJEXT) \
    6565        add_eq.$(OBJEXT) add_zero.$(OBJEXT) atan.$(OBJEXT) \
    66         atan_2.$(OBJEXT) check_simple_vector.$(OBJEXT) \
    67         compare.$(OBJEXT) compare_change.$(OBJEXT) cond_exp.$(OBJEXT) \
     66        atan_2.$(OBJEXT) base_alloc.$(OBJEXT) \
     67        check_simple_vector.$(OBJEXT) compare.$(OBJEXT) \
     68        compare_change.$(OBJEXT) cond_exp.$(OBJEXT) \
    6869        cond_exp_ad.$(OBJEXT) copy.$(OBJEXT) cos.$(OBJEXT) \
    6970        cosh.$(OBJEXT) dbl_epsilon.$(OBJEXT) div.$(OBJEXT) \
     
    285286        atan.cpp \
    286287        atan_2.cpp \
     288        base_alloc.cpp \
    287289        check_simple_vector.cpp \
    288290        compare.cpp \
     
    405407@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/atan_2.Po@am__quote@
    406408@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/base_adolc.Po@am__quote@
     409@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/base_alloc.Po@am__quote@
    407410@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check_simple_vector.Po@am__quote@
    408411@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/compare.Po@am__quote@
  • trunk/test_more/test_more.cpp

    r2240 r2281  
    11/* $Id$ */
    22/* --------------------------------------------------------------------------
    3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-11 Bradley M. Bell
     3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell
    44
    55CppAD is distributed under multiple licenses. This distribution is under
     
    2727extern bool Atan2(void);
    2828extern bool base_adolc(void);
     29extern bool base_alloc(void);
    2930extern bool check_simple_vector(void);
    3031extern bool Compare(void);
     
    129130        ok &= Run( Atan,            "Atan"           );
    130131        ok &= Run( Atan2,           "Atan2"          );
     132        ok &= Run( base_alloc,      "base_alloc"     );
    131133        ok &= Run( check_simple_vector, "check_simple_vector" );
    132134        ok &= Run( Compare,         "Compare"        );
  • trunk/test_more/test_more.vcproj

    r2091 r2281  
    211211                        </File>
    212212                        <File
     213                                RelativePath=".\base_alloc.cpp"
     214                                >
     215                        </File>
     216                        </File>
     217                        <File
    213218                                RelativePath=".\check_simple_vector.cpp"
    214219                                >
Note: See TracChangeset for help on using the changeset viewer.