Changeset 2273


Ignore:
Timestamp:
Jan 25, 2012 2:34:02 AM (8 years ago)
Author:
bradbell
Message:

Enable user to control on when thread_alloc holds onto memory
(this is a change to the API, but only when parallel_setup has been called);
see whats_new_12.

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/cppad/omp_alloc.hpp

    r2085 r2273  
    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
     
    8181                        number, omp_alloc::in_parallel, omp_alloc::get_thread_num
    8282                );
     83                thread_alloc::hold_memory(number > 1);
    8384        }
    8485        /*!
  • trunk/cppad/thread_alloc.hpp

    r2259 r2273  
    139139                thread_alloc* root_inuse_;
    140140        };
     141        // ---------------------------------------------------------------------
     142        /*!
     143        Set and Get hold available memory flag.
     144
     145        \param set [in]
     146        if true, the value returned by this return is changed.
     147
     148        \param new_value [in]
     149        if \a set is true, this is the new value returned by this routine.
     150        Otherwise, \c new_value is ignored.
     151
     152        \return
     153        the current setting for this routine (which is initially false).
     154        */
     155        static bool set_get_hold_memory(bool set, bool new_value = false)
     156        {       static bool value = false;
     157                if( set )
     158                        value = new_value;
     159                return value;
     160        }
     161        // ---------------------------------------------------------------------
    141162        /*!
    142163        Get pointer to the information for this thread.
     
    427448not in parallel mode.
    428449
     450$head Speed$$
     451It should be faster, even when $icode num_thread$$ is equal to one,
     452for $code thread_alloc$$ to hold onto memory.
     453This can be accomplished using the function call
     454$codei%
     455        thread_alloc::hold_memory(true)
     456%$$
     457see $cref/hold_memory/ta_hold_memory/$$.
     458
    429459$head num_threads$$
    430460This argument has prototype
     
    964994
    965995                // check for case where we just return the memory to the system
    966                 if( num_threads() == 1 )
     996                if( ! set_get_hold_memory(false) )
    967997                {       ::operator delete( reinterpret_cast<void*>(node) );
    968998                        return;
     
    10591089        }
    10601090/* -----------------------------------------------------------------------
     1091$begin ta_hold_memory$$
     1092$spell
     1093        alloc
     1094        num
     1095$$
     1096
     1097$section Control When Thread Alloc Retains Memory For Future Use$$
     1098$index thread_alloc, hold memory$$
     1099$index hold, thread_alloc memory$$
     1100$index memory, thread_alloc hold$$
     1101
     1102$head Syntax$$
     1103$codei%thread_alloc::hold_memory(%value%)%$$
     1104
     1105$head Purpose$$
     1106It should be faster, even when $icode num_thread$$ is equal to one,
     1107for $code thread_alloc$$ to hold onto memory.
     1108Calling $icode hold_memory$$ with $icode value$$ equal to true,
     1109instructs $code thread_alloc$$ to hold onto memory,
     1110and put it in the $cref/available/ta_available/$$ pool,
     1111after each call to $cref/return_memory/ta_return_memory/$$.
     1112
     1113$head value$$
     1114If $icode value$$ is true,
     1115$code thread_alloc$$ with hold onto memory for future quick use.
     1116If it is false, future calls to $cref/return_memory/ta_return_memory/$$
     1117will return the corresponding memory to the system.
     1118
     1119$head free_available$$
     1120Memory that is held by $code thread_alloc$$ can be returned
     1121to the system using $cref/free_available/ta_free_available/$$.
     1122
     1123$end
     1124*/
     1125        /*!
     1126        Change the thread_alloc hold memory setting.
     1127
     1128        \param value [in]
     1129        New value for the thread_alloc hold memory setting.
     1130        */
     1131        static void hold_memory(bool value)
     1132        {       bool set = true;
     1133                set_get_hold_memory(set, value);
     1134        }       
     1135       
     1136/* -----------------------------------------------------------------------
    10611137$begin ta_inuse$$
    10621138$spell
  • trunk/example/thread_alloc.cpp

    r2111 r2273  
    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
     
    185185        // Tell thread_alloc that there are two threads so it starts holding
    186186        // onto memory (but actuall the there is only on thread with id zero).
    187         thread_alloc::parallel_setup(2, in_parallel_false, thread_num_zero);
     187        thread_alloc::parallel_setup( NUMBER_THREADS,
     188                in_parallel_false, thread_num_zero
     189        );
     190        thread_alloc::hold_memory(true);
    188191        ok  &= thread_alloc::num_threads() == 2;
    189192        ok  &= thread_alloc::thread_num() == 0;
     
    198201        // return allocator to single thread mode
    199202        thread_alloc::parallel_setup(1, in_parallel_false, thread_num_zero);
     203        thread_alloc::hold_memory(false);
    200204        return ok;
    201205}
  • trunk/multi_thread/bthread/team_bthread.cpp

    r2233 r2273  
    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
     
    153153        // call setup for using CppAD::AD<double> in parallel mode.
    154154        thread_alloc::parallel_setup(num_threads, in_parallel, thread_number);
     155        thread_alloc::hold_memory(true);
    155156        CppAD::parallel_ad<double>();
    156157
     
    259260        using CppAD::thread_alloc;
    260261        thread_alloc::parallel_setup(num_threads_, in_parallel, thread_number);
     262        thread_alloc::hold_memory(false);
    261263
    262264        return ok;
  • trunk/multi_thread/openmp/team_openmp.cpp

    r2233 r2273  
    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
     
    6161        // setup for using CppAD::AD<double> in parallel
    6262        CppAD::thread_alloc::parallel_setup(num_threads, in_parallel, thread_num);
     63        CppAD::thread_alloc::hold_memory(true);
    6364        CppAD::parallel_ad<double>();
    6465
     
    9899        using CppAD::thread_alloc;
    99100        thread_alloc::parallel_setup(num_threads_, in_parallel, thread_num);
     101        thread_alloc::hold_memory(false);
    100102
    101103        return ok;
  • trunk/multi_thread/pthread/team_pthread.cpp

    r2233 r2273  
    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
     
    197197        // call setup for using CppAD::AD<double> in parallel mode.
    198198        thread_alloc::parallel_setup(num_threads, in_parallel, thread_number);
     199        thread_alloc::hold_memory(true);
    199200        CppAD::parallel_ad<double>();
    200201
     
    336337        using CppAD::thread_alloc;
    337338        thread_alloc::parallel_setup(num_threads_, in_parallel, thread_number);
     339        thread_alloc::hold_memory(false);
    338340
    339341        return ok;
  • trunk/omh/faq.omh

    r2210 r2273  
    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
     
    263263more than the time required for the optimization.
    264264
     265$subhead Memory Allocation$$
     266You may also increase execution speed
     267by calling $code hold_memory$$ with
     268$cref/value/ta_hold_memory/value/$$ equal to true.
     269
    265270$head Tape Storage: Disk or Memory$$
    266271$index storage, tape$$
  • trunk/omh/multi_thread.omh

    r2214 r2273  
    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
     
    5353$cref/parallel_ad/$$.
    5454
     55$head hold_memory$$
     56Memory allocation should be much faster after calling
     57$code hold_memory$$ with
     58$cref/value/ta_hold_memory/value/$$ equal to true.
     59This should be true even if there is only one thread.
     60
    5561$head Parallel AD$$
    5662One must first call
  • trunk/omh/whats_new_12.omh

    r2272 r2273  
    1313$begin whats_new_12$$
    1414$spell
     15        num
     16        alloc
    1517        makefile
    1618        hpp
     
    3234The purpose of this section is to
    3335assist you in learning about changes between various versions of CppAD.
     36
     37$head 01-24$$
     38It used to be that $cref thread_alloc$$ did not hold onto memory
     39when $icode num_threads$$ was one in the previous call to
     40$cref/parallel_setup/ta_parallel_setup/$$.
     41Holding onto memory is now controlled by the separate routine
     42$cref/hold_memory/ta_hold_memory/$$.
     43This give the user more control over the memory allocator
     44and the ability to obtain a speed up even
     45when there is only one thread.
     46To convert old code to the new interface, after each call to
     47$codei%
     48        thread_alloc::parallel_setup(%num_threads%, %in_parallel%, %thread_num%);
     49%$$
     50put the following call
     51$codei%
     52        thread_alloc::hold_memory(%num_threads% > 1);
     53%$$
    3454
    3555$head 01-23$$
Note: See TracChangeset for help on using the changeset viewer.