CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
parallel_ad.hpp
Go to the documentation of this file.
1 // $Id$
2 # ifndef CPPAD_CORE_PARALLEL_AD_HPP
3 # define CPPAD_CORE_PARALLEL_AD_HPP
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-16 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 /*
15 $begin parallel_ad$$
16 $spell
17  CppAD
18  num
19  std
20 $$
21 
22 $section Enable AD Calculations During Parallel Mode$$
23 
24 $head Syntax$$
25 $codei%parallel_ad<%Base%>()%$$
26 
27 $head Purpose$$
28 The function
29 $codei%parallel_ad<%Base%>()%$$
30 must be called before any $codei%AD<%Base>%$$ objects are used
31 in $cref/parallel/ta_in_parallel/$$ mode.
32 In addition, if this routine is called after one is done using
33 parallel mode, it will free extra memory used to keep track of
34 the multiple $codei%AD<%Base%>%$$ tapes required for parallel execution.
35 
36 $head Discussion$$
37 By default, for each $codei%AD<%Base%>%$$ class there is only one
38 tape that records $cref/AD of Base/glossary/AD of Base/$$ operations.
39 This tape is a global variable and hence it cannot be used
40 by multiple threads at the same time.
41 The $cref/parallel_setup/ta_parallel_setup/$$ function informs CppAD of the
42 maximum number of threads that can be active in parallel mode.
43 This routine does extra setup
44 (and teardown) for the particular $icode Base$$ type.
45 
46 $head CheckSimpleVector$$
47 This routine has the side effect of calling the routines
48 $codei%
49  CheckSimpleVector< %Type%, CppAD::vector<%Type%> >()
50 %$$
51 where $icode Type$$ is $icode Base$$ and $codei%AD<%Base%>%$$.
52 
53 $head Example$$
54 The files
55 $cref team_openmp.cpp$$,
56 $cref team_bthread.cpp$$, and
57 $cref team_pthread.cpp$$,
58 contain examples and tests that implement this function.
59 
60 $head Restriction$$
61 This routine cannot be called in parallel mode or while
62 there is a tape recording $codei%AD<%Base%>%$$ operations.
63 
64 $end
65 -----------------------------------------------------------------------------
66 */
67 
68 # include <cppad/local/std_set.hpp>
69 
70 // BEGIN CppAD namespace
71 namespace CppAD {
72 
73 /*!
74 Enable parallel execution mode with <code>AD<Base></code> by initializing
75 static variables that my be used.
76 */
77 
78 template <class Base>
79 void parallel_ad(void)
82  "parallel_ad must be called before entering parallel execution mode."
83  );
85  AD<Base>::tape_ptr() == CPPAD_NULL ,
86  "parallel_ad cannot be called while a tape recording is in progress"
87  );
88 
89  // ensure statics in following functions are initialized
94  local::one_element_std_set<size_t>();
95  local::two_element_std_set<size_t>();
96 
97  // the sparse_pack class has member functions with static data
99  sp.resize(1, 1); // so can call add_element
100  sp.add_element(0, 0); // has static data
101  sp.clear(0); // has static data
102  sp.is_element(0, 0); // has static data
103  local::sparse_pack::const_iterator itr(sp, 0); // has static data
104  ++itr; // has static data
105 
106  // statics that depend on the value of Base
111  CheckSimpleVector< Base, CppAD::vector<Base> >();
112  CheckSimpleVector< AD<Base>, CppAD::vector< AD<Base> > >();
113 
114 }
115 
116 } // END CppAD namespace
117 
118 # endif
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
void parallel_ad(void)
Enable parallel execution mode with AD&lt;Base&gt; by initializing static variables that my be used...
Definition: parallel_ad.hpp:79
The CppAD Simple Vector template class.
Definition: vector.hpp:338
Definition: ad.hpp:34
static bool in_parallel(void)
Are we in a parallel execution state; i.e., is it possible that other threads are currently executing...
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
static local::ADTape< Base > * tape_manage(tape_manage_job job)
Create and delete tapes that record AD&lt;Base&gt; operations for current thread.
Definition: tape_link.hpp:210
static local::ADTape< Base > ** tape_handle(size_t thread)
Handle for the tape for this AD&lt;Base&gt; class and the specific thread.
Definition: tape_link.hpp:105
Vector of sets of postivie integers, each set stored as a packed boolean array.
Definition: sparse_pack.hpp:33
Two constant standard sets (currently used for concept checking).
static std::vector< discrete * > & List(void)
List of all objects in this class.
Definition: discrete.hpp:216
cons_iterator for one set of positive integers in a sparse_pack object.
static tape_id_t ** tape_id_handle(size_t thread)
Handle to the tape identifier for this AD&lt;Base&gt; class and the specific thread.
Definition: tape_link.hpp:48
double elapsed_seconds(void)
Returns the elapsed number since the first call to this function.
static Handler & Current(void)
void resize(size_t n_set, size_t end)
Change number of sets, set end, and initialize all sets as empty.