CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_PARALLEL_AD_INCLUDED 00003 # define CPPAD_PARALLEL_AD_INCLUDED 00004 /* -------------------------------------------------------------------------- 00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-13 Bradley M. Bell 00006 00007 CppAD is distributed under multiple licenses. This distribution is under 00008 the terms of the 00009 Eclipse Public License Version 1.0. 00010 00011 A copy of this license is included in the COPYING file of this distribution. 00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00013 -------------------------------------------------------------------------- */ 00014 /* 00015 $begin parallel_ad$$ 00016 $spell 00017 CppAD 00018 num 00019 std 00020 $$ 00021 00022 $section Enable AD Calculations During Parallel Mode$$ 00023 00024 $head Syntax$$ 00025 $codei%parallel_ad<%Base%>()%$$ 00026 00027 $head Purpose$$ 00028 The function 00029 $codei%parallel_ad<%Base%>()%$$ 00030 must be called before any $codei%AD<%Base>%$$ objects are used 00031 in $cref/parallel/ta_in_parallel/$$ mode. 00032 In addition, if this routine is called after one is done using 00033 parallel mode, it will free extra memory used to keep track of 00034 the multiple $codei%AD<%Base%>%$$ tapes required for parallel execution. 00035 00036 $head Discussion$$ 00037 By default, for each $codei%AD<%Base%>%$$ class there is only one 00038 tape that records $cref/AD of Base/glossary/AD of Base/$$ operations. 00039 This tape is a global variable and hence it cannot be used 00040 by multiple threads at the same time. 00041 The $cref/parallel_setup/ta_parallel_setup/$$ function informs CppAD of the 00042 maximum number of threads that can be active in parallel mode. 00043 This routine does extra setup 00044 (and teardown) for the particular $icode Base$$ type. 00045 00046 $head CheckSimpleVector$$ 00047 This routine has the side effect of calling the routines 00048 $codei% 00049 CheckSimpleVector< %Type%, CppAD::vector<%Type%> >() 00050 %$$ 00051 where $icode Type$$ is $icode Base$$ and $codei%AD<%Base%>%$$. 00052 00053 $head Example$$ 00054 The files 00055 $cref team_openmp.cpp$$, 00056 $cref team_bthread.cpp$$, and 00057 $cref team_pthread.cpp$$, 00058 contain examples and tests that implement this function. 00059 00060 $head Restriction$$ 00061 This routine cannot be called in parallel mode or while 00062 there is a tape recording $codei%AD<%Base%>%$$ operations. 00063 00064 $end 00065 ----------------------------------------------------------------------------- 00066 */ 00067 00068 # include <cppad/local/std_set.hpp> 00069 00070 // BEGIN CppAD namespace 00071 namespace CppAD { 00072 00073 /*! 00074 Enable parallel execution mode with <code>AD<Base></code> by initializing 00075 static variables that my be used. 00076 */ 00077 00078 template <class Base> 00079 void parallel_ad(void) 00080 { CPPAD_ASSERT_KNOWN( 00081 ! thread_alloc::in_parallel() , 00082 "parallel_ad must be called before entering parallel execution mode." 00083 ); 00084 CPPAD_ASSERT_KNOWN( 00085 AD<Base>::tape_ptr() == CPPAD_NULL , 00086 "parallel_ad cannot be called while a tape recording is in progress" 00087 ); 00088 00089 // ensure statics in following functions are initialized 00090 elapsed_seconds(); 00091 ErrorHandler::Current(); 00092 NumArg(BeginOp); 00093 one_element_std_set<size_t>(); 00094 two_element_std_set<size_t>(); 00095 00096 // the sparse_pack class has member functions with static data 00097 sparse_pack sp; 00098 sp.resize(1, 1); // so can call add_element 00099 sp.add_element(0, 0); // has static data 00100 sp.begin(0); // so can call next_element 00101 sp.next_element(); // has static data 00102 sp.clear(0); // has static data 00103 sp.is_element(0, 0); // has static data 00104 00105 // statics that depend on the value of Base 00106 AD<Base>::tape_id_handle(0); 00107 AD<Base>::tape_handle(0); 00108 AD<Base>::tape_manage(tape_manage_clear); 00109 discrete<Base>::List(); 00110 CheckSimpleVector< Base, CppAD::vector<Base> >(); 00111 CheckSimpleVector< AD<Base>, CppAD::vector< AD<Base> > >(); 00112 00113 } 00114 00115 } // END CppAD namespace 00116 00117 # endif