CppAD: A C++ Algorithmic Differentiation Package 20110419
omp_max_thread.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_OMP_MAX_THREAD_INCLUDED
00003 # define CPPAD_OMP_MAX_THREAD_INCLUDED
00004 
00005 /* --------------------------------------------------------------------------
00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell
00007 
00008 CppAD is distributed under multiple licenses. This distribution is under
00009 the terms of the 
00010                     Common Public License Version 1.0.
00011 
00012 A copy of this license is included in the COPYING file of this distribution.
00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
00014 -------------------------------------------------------------------------- */
00015 /*
00016 $begin omp_max_thread$$
00017 $spell
00018         omp
00019         OpenMp
00020         CppAD
00021 $$
00022 
00023 $section OpenMP Maximum Thread Number$$
00024 
00025 $index omp_max_thread$$
00026 $index thread, multiple$$
00027 $index multiple, thread$$
00028 $index OpenMP, CppAD$$
00029 $index CppAD, OpenMP$$
00030 
00031 $head Syntax$$
00032 $syntax%AD<%Base%>::omp_max_thread(%number%)
00033 %$$
00034 
00035 $head Purpose$$
00036 By default, for each $syntax%AD<%Base%>%$$ class there is only one 
00037 tape that records $cref/AD of Base/glossary/AD of Base/$$ operations.
00038 This tape is a global variable and hence it cannot be used
00039 by multiple OpenMP threads at the same time. 
00040 The $code omp_max_thread$$ function is used to set the 
00041 maximum number of OpenMP threads that can be active.
00042 In this case, there is a different tape corresponding to each 
00043 $syntax%AD<%Base%>%$$ class and thread pair. 
00044 
00045 $head number$$
00046 The argument $italic number$$ has prototype
00047 $syntax%
00048         size_t %number%
00049 %$$
00050 It must be greater than zero and specifies the maximum number of
00051 OpenMp threads that will be active at one time.
00052 
00053 
00054 $head Independent$$
00055 Each call to $cref/Independent(x)/Independent/$$ 
00056 creates a new $cref/active/glossary/Tape/Active/$$ tape.
00057 All of the operations with the corresponding variables 
00058 must be preformed by the same OpenMP thread.
00059 This includes the corresponding call to 
00060 $cref/f.Dependent(x,y)/Dependent/$$ or the 
00061 $cref/ADFun f(x, y)/FunConstruct/Sequence Constructor/$$
00062 during which the tape stops recording and the variables
00063 become parameters.
00064 
00065 $head Restriction$$
00066 No tapes can be 
00067 $cref/active/glossary/Tape/Active/$$ when this function is called.
00068 
00069 $head Example and Tests$$
00070 $children%
00071         openmp/run.sh
00072 %$$
00073 The shell script $cref/openmp_run.sh/$$ can be used to 
00074 compile and run the OpenMP examples and tests.
00075 
00076 $end
00077 -----------------------------------------------------------------------------
00078 */
00079 
00080 // BEGIN CppAD namespace
00081 namespace CppAD {
00082 
00083 template <class Base>
00084 size_t AD<Base>::omp_max_thread(size_t number)
00085 {       static size_t max_thread = 1;
00086 
00087         // number equal zero case is not part of user interface
00088         if( number > 0 )
00089         {
00090 # ifndef NDEBUG
00091                 CPPAD_ASSERT_KNOWN(
00092                         number <= CPPAD_MAX_NUM_THREADS,
00093                         "omp_max_thread argument is too large."
00094                 );
00095 # endif
00096 
00097                 max_thread = number;
00098         }
00099 
00100         // the return value is not part of the user interface
00101         return max_thread;
00102 }
00103 
00104 } // END CppAD namespace
00105 
00106 # endif