CppAD: A C++ Algorithmic Differentiation Package  20130102
parallel_ad.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_PARALLEL_AD_INCLUDED
00003 # define CPPAD_PARALLEL_AD_INCLUDED
00004 /* --------------------------------------------------------------------------
00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 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      isnan
00020      std
00021 $$
00022 
00023 $section Enable AD Calculations During Parallel Mode$$
00024 
00025 $head Syntax$$
00026 $codei%parallel_ad<%Base%>()%$$
00027 
00028 $head Purpose$$
00029 The function
00030 $codei%parallel_ad<%Base%>()%$$
00031 must be called before any $codei%AD<%Base>%$$ objects are used
00032 in $cref/parallel/ta_in_parallel/$$ mode.
00033 In addition, if this routine is called after one is done using 
00034 parallel mode, it will free extra memory used to keep track of 
00035 the multiple $codei%AD<%Base%>%$$ tapes required for parallel execution.
00036 
00037 $head Discussion$$
00038 By default, for each $codei%AD<%Base%>%$$ class there is only one 
00039 tape that records $cref/AD of Base/glossary/AD of Base/$$ operations.
00040 This tape is a global variable and hence it cannot be used
00041 by multiple threads at the same time. 
00042 The $cref/parallel_setup/ta_parallel_setup/$$ function informs CppAD of the
00043 maximum number of threads that can be active in parallel mode.
00044 This routine does extra setup 
00045 (and teardown) for the particular $icode Base$$ type.
00046 
00047 $head isnan$$
00048 This routine has the side effect of calling
00049 $codei%
00050      %b% = isnan(%s%)
00051 %$$
00052 where $icode s$$ has type $icode%Base%$$, $codei%AD<%Base%>%$$, and
00053 $codei%std::complex<double>%$$.
00054 
00055 $head CheckSimpleVector$$
00056 This routine has the side effect of calling the routines
00057 $codei%
00058      CheckSimpleVector< %Type%, CppAD::vector<%Type%> >()
00059 %$$
00060 where $icode Type$$ is $icode Base$$ and $codei%AD<%Base%>%$$.
00061 
00062 $head Example$$
00063 The files 
00064 $cref team_openmp.cpp$$, 
00065 $cref team_bthread.cpp$$, and
00066 $cref team_pthread.cpp$$, 
00067 contain examples and tests that implement this function.   
00068 
00069 $head Restriction$$
00070 This routine cannot be called in parallel mode or while
00071 there is a tape recording $codei%AD<%Base%>%$$ operations.
00072 
00073 $end
00074 -----------------------------------------------------------------------------
00075 */
00076 
00077 # include <cppad/local/std_set.hpp>
00078 
00079 // BEGIN CppAD namespace
00080 namespace CppAD {
00081 
00082 /*!
00083 Enable parallel execution mode with <code>AD<Base></code> by initializing 
00084 static variables that my be used.
00085 */
00086 
00087 template <class Base>
00088 void parallel_ad(void)
00089 {    CPPAD_ASSERT_KNOWN( 
00090           ! thread_alloc::in_parallel() ,
00091           "parallel_ad must be called before entering parallel execution mode."
00092      );
00093      CPPAD_ASSERT_KNOWN(
00094           AD<Base>::tape_ptr() == CPPAD_NULL ,
00095           "parallel_ad cannot be called while a tape recording is in progress"
00096      );
00097 
00098      // ensure statics in following functions are initialized
00099      elapsed_seconds();
00100      ErrorHandler::Current();
00101      isnan( std::complex<double>(0.) );
00102      NumArg(BeginOp);
00103      one_element_std_set<size_t>();
00104      two_element_std_set<size_t>();
00105 
00106      // the sparse_pack class has member functions with static data
00107      sparse_pack sp;
00108      sp.resize(1, 1);       // so can call add_element
00109      sp.add_element(0, 0);  // has static data
00110      sp.begin(0);           // so can call next_element
00111      sp.next_element();     // has static data
00112      sp.clear(0);           // has static data
00113      sp.is_element(0, 0);   // has static data
00114 
00115      // statics that depend on the value of Base
00116      AD<Base>::tape_id_handle(0);
00117      AD<Base>::tape_handle(0);     
00118      AD<Base>::tape_manage(tape_manage_clear);
00119      discrete<Base>::List();
00120      isnan( Base(0.) );
00121      isnan( AD<Base>(0.) );
00122      CheckSimpleVector< Base, CppAD::vector<Base> >();
00123      CheckSimpleVector< AD<Base>, CppAD::vector< AD<Base> > >();
00124 
00125 }
00126 
00127 } // END CppAD namespace
00128 
00129 # endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines