1 #ifndef HAVE_CHECKPOINT_HPP 2 #define HAVE_CHECKPOINT_HPP 11 template <
class ADFun,
bool packed_ = false>
13 static const bool packed = packed_;
16 while ((*this).size() <= n) {
17 (*this).push_back((*this).back().WgtJacFun());
32 static const bool packed =
false;
33 std::vector<Scalar> x_prev;
34 bool operator()(
const std::vector<Scalar> &x);
46 size_t n = (*this)[0].Domain();
47 std::vector<Scalar> x = args.
x_segment(0, n);
48 bool change = test(x);
51 (*this)[0] = ADFun(F, x);
96 typedef std::shared_ptr<T> Base;
98 std::shared_ptr<std::vector<std::weak_ptr<T> > > weak_refs;
101 : sp(x), weak_refs(std::make_shared<std::vector<std::weak_ptr<T> > >()) {
102 (*weak_refs).resize(TMBAD_MAX_NUM_THREADS);
103 (*weak_refs)[TMBAD_THREAD_NUM] = x;
106 if ((*weak_refs)[TMBAD_THREAD_NUM].expired()) {
107 sp = std::make_shared<T>(*other);
109 (*weak_refs)[TMBAD_THREAD_NUM] = sp;
111 sp = (*weak_refs)[TMBAD_THREAD_NUM].lock();
116 T *operator->()
const {
return sp.get(); }
117 explicit operator bool()
const {
return (
bool)sp; }
166 template <
class DerivativeTable>
168 static const bool have_input_size_output_size =
true;
169 static const bool add_forward_replay_copy =
true;
171 TMBAD_SHARED_PTR<DerivativeTable> dtab;
176 AtomOp(
const T1 &F) : dtab(std::make_shared<DerivativeTable>(F)),
order(0) {}
177 template <
class T1,
class T2>
178 AtomOp(
const T1 &F,
const T2 &x)
179 : dtab(std::make_shared<DerivativeTable>(F, x)),
order(0) {}
180 template <
class T1,
class T2,
class T3>
181 AtomOp(
const T1 &F,
const T2 &x,
const T3 &t)
182 : dtab(std::make_shared<DerivativeTable>(F, x, t)),
order(0) {}
184 Index input_size()
const {
return (*dtab)[
order].Domain(); }
185 Index output_size()
const {
return (*dtab)[
order].Range(); }
188 (*dtab).retape(args);
190 (*dtab).requireOrder(order);
192 size_t n = input_size();
193 size_t m = output_size();
201 size_t n = input_size();
202 size_t m = output_size();
211 size_t n = input_size();
212 size_t m = output_size();
214 std::vector<global::Replay> x = args.
x_segment(0, n);
215 if (DerivativeTable::packed) x = repack(x);
216 std::vector<global::Replay> w = args.
dy_segment(0, m);
217 std::vector<global::Replay> xw;
218 xw.insert(xw.end(), x.begin(), x.end());
219 xw.insert(xw.end(), w.begin(), w.end());
221 (*dtab).requireOrder(order + 1);
233 const char *op_name() {
return "AtomOp"; }
237 Rcout <<
"order=" << order <<
" ";
238 Rcout <<
"(*dtab).size()=" << (*dtab).size() <<
" ";
239 Rcout <<
"dtab=" << &(*dtab) <<
"\n";
240 (*dtab)[
order].print(cfg);
252 template <
class Functor>
255 PackWrap(
const Functor &F) : F(F) {}
260 Index K = ScalarPack<SegmentRef>::size;
261 size_t n = xp.size() / K;
262 TMBAD_ASSERT2(n * K == xp.size(),
"Invalid packed arguments");
263 std::vector<ad_segment> x(n);
264 for (
size_t i = 0; i < n; i++) x[i] =
unpack(xp, i);
267 std::vector<T> ans = concat(std::vector<ad_segment>(1, yp));
273 Index K = ScalarPack<SegmentRef>::size;
274 size_t n = xp.size() / K;
275 TMBAD_ASSERT2(n * K == xp.size(),
"Invalid packed arguments");
276 std::vector<Scalar *> x(n);
277 for (
size_t i = 0; i < n; i++) x[i] =
unpack(xp, i);
283 #endif // HAVE_CHECKPOINT_HPP Automatic differentiation library designed for TMB.
segment_ref< ReverseArgs, dx_write > dx_segment(Index from, Index size)
segment version
Vector class used by TMB.
segment_ref< ReverseArgs, x_read > x_segment(Index from, Index size)
segment version
segment_ref< ForwardArgs, x_read > x_segment(Index from, Index size)
segment version
bool operator()(const std::vector< Scalar > &xp)
Transformed 'tester' assuming original maps std::vector<Scalar*> to bool
void retape(ForwardArgs< Scalar > &args)
Retape the zero derivative and remove all higher orders from the table.
segment_ref< ForwardArgs, y_write > y_segment(Index from, Index size)
segment version
Access input/output values and derivatives during a reverse pass. Write access granted for the input ...
Contiguous set of variables on the current tape.
Transform a functor to have packed input/output.
Automatic differentiation function object.
vector< Type > operator*(matrix< Type > A, vector< Type > x)
Default tester for retaping_derivative_table.
Configuration of print method.
segment_ref< ReverseArgs, dy_read > dy_segment(Index from, Index size)
segment version
void requireOrder(size_t n)
Add derivatives up to this order.
retaping_derivative_table(const Functor &F, const V &x, Test test=Test())
Set zero order functor used to retape this derivative table.
Manage shared operator data across multiple threads.
ad_segment unpack(const ad_segment &x)
Unpack consecutive values on the tape.
ad_segment pack(const ad_segment &x)
Pack consecutive values on the tape.
std::vector< size_t > order(std::vector< T > x)
Get permutation that sorts a vector.
Operator auto-completion.
Operator that requires dynamic allocation. Compile time known input/output size.
Fixed derivative table used by AtomOp
std::vector< T > operator()(const std::vector< T > &xp)
Transformed functor assuming original maps std::vector<ad_segment> to ad_segment
void retape(ForwardArgs< Scalar > &args)
Retaping this derivative table has no effect.
standard_derivative_table(const ADFun &F)
Set zero order function of this derivative table.
Generic checkpoint operator.
Adaptive derivative table used by AtomOp