1 | /* $Id: recorder.hpp 3607 2015-01-20 16:20:41Z bradbell $ */ |
---|
2 | # ifndef CPPAD_RECORDER_INCLUDED |
---|
3 | # define CPPAD_RECORDER_INCLUDED |
---|
4 | /* -------------------------------------------------------------------------- |
---|
5 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 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 | # include <cppad/local/hash_code.hpp> |
---|
15 | # include <cppad/local/pod_vector.hpp> |
---|
16 | |
---|
17 | namespace CppAD { // BEGIN_CPPAD_NAMESPACE |
---|
18 | /*! |
---|
19 | \file recorder.hpp |
---|
20 | File used to define the recorder class. |
---|
21 | */ |
---|
22 | |
---|
23 | /*! |
---|
24 | Class used to store an operation sequence while it is being recorded |
---|
25 | (the operation sequence is copied to the player class for playback). |
---|
26 | |
---|
27 | \tparam Base |
---|
28 | This is an AD< \a Base > operation sequence recording; i.e., |
---|
29 | it records operations of type AD< \a Base >. |
---|
30 | */ |
---|
31 | template <class Base> |
---|
32 | class recorder { |
---|
33 | friend class player<Base>; |
---|
34 | |
---|
35 | private: |
---|
36 | /// operator index at which to abort recording with an error |
---|
37 | /// (do not abort when zero) |
---|
38 | size_t abort_op_index_; |
---|
39 | |
---|
40 | /// offset for this thread in the static hash table |
---|
41 | const size_t thread_offset_; |
---|
42 | |
---|
43 | /// Number of variables in the recording. |
---|
44 | size_t num_var_rec_; |
---|
45 | |
---|
46 | /// Number vecad load operations (LdpOp or LdvOp) currently in recording. |
---|
47 | size_t num_load_op_rec_; |
---|
48 | |
---|
49 | /// The operators in the recording. |
---|
50 | pod_vector<CPPAD_OP_CODE_TYPE> op_rec_; |
---|
51 | |
---|
52 | /// The VecAD indices in the recording. |
---|
53 | pod_vector<addr_t> vecad_ind_rec_; |
---|
54 | |
---|
55 | /// The argument indices in the recording |
---|
56 | pod_vector<addr_t> op_arg_rec_; |
---|
57 | |
---|
58 | /// The parameters in the recording. |
---|
59 | /// Note that Base may not be plain old data, so use false in consructor. |
---|
60 | pod_vector<Base> par_rec_; |
---|
61 | |
---|
62 | /// Character strings ('\\0' terminated) in the recording. |
---|
63 | pod_vector<char> text_rec_; |
---|
64 | // ---------------------- Public Functions ----------------------------------- |
---|
65 | public: |
---|
66 | /// Default constructor |
---|
67 | recorder(void) : |
---|
68 | thread_offset_( thread_alloc::thread_num() * CPPAD_HASH_TABLE_SIZE ) , |
---|
69 | num_var_rec_(0) , |
---|
70 | num_load_op_rec_(0) , |
---|
71 | op_rec_( std::numeric_limits<addr_t>::max() ) , |
---|
72 | vecad_ind_rec_( std::numeric_limits<addr_t>::max() ) , |
---|
73 | op_arg_rec_( std::numeric_limits<addr_t>::max() ) , |
---|
74 | par_rec_( std::numeric_limits<addr_t>::max() ) , |
---|
75 | text_rec_( std::numeric_limits<addr_t>::max() ) |
---|
76 | { |
---|
77 | abort_op_index_ = 0; |
---|
78 | } |
---|
79 | |
---|
80 | /// Set the abort index |
---|
81 | void set_abort_op_index(size_t abort_op_index) |
---|
82 | { abort_op_index_ = abort_op_index; } |
---|
83 | |
---|
84 | /// Get the abort index |
---|
85 | size_t get_abort_op_index(void) |
---|
86 | { return abort_op_index_; } |
---|
87 | |
---|
88 | /// Destructor |
---|
89 | ~recorder(void) |
---|
90 | { } |
---|
91 | |
---|
92 | /*! |
---|
93 | Frees all information in recording. |
---|
94 | |
---|
95 | Frees the operation sequence store in this recording |
---|
96 | (the operation sequence is empty after this operation). |
---|
97 | The buffers used to store the current recording are returned |
---|
98 | to the system (so as to conserve on memory). |
---|
99 | */ |
---|
100 | void free(void) |
---|
101 | { num_var_rec_ = 0; |
---|
102 | num_load_op_rec_ = 0; |
---|
103 | op_rec_.free(); |
---|
104 | vecad_ind_rec_.free(); |
---|
105 | op_arg_rec_.free(); |
---|
106 | par_rec_.free(); |
---|
107 | text_rec_.free(); |
---|
108 | } |
---|
109 | /// Put next operator in the operation sequence. |
---|
110 | inline size_t PutOp(OpCode op); |
---|
111 | /// Put a vecad load operator in the operation sequence (special case) |
---|
112 | inline size_t PutLoadOp(OpCode op); |
---|
113 | /// Add a value to the end of the current vector of VecAD indices. |
---|
114 | inline size_t PutVecInd(size_t vec_ind); |
---|
115 | /// Find or add a parameter to the current vector of parameters. |
---|
116 | inline size_t PutPar(const Base &par); |
---|
117 | /// Put one operation argument index in the recording |
---|
118 | inline void PutArg(addr_t arg0); |
---|
119 | /// Put two operation argument index in the recording |
---|
120 | inline void PutArg(addr_t arg0, addr_t arg1); |
---|
121 | /// Put three operation argument index in the recording |
---|
122 | inline void PutArg(addr_t arg0, addr_t arg1, addr_t arg2); |
---|
123 | /// Put four operation argument index in the recording |
---|
124 | inline void PutArg(addr_t arg0, addr_t arg1, addr_t arg2, addr_t arg3); |
---|
125 | /// Put five operation argument index in the recording |
---|
126 | inline void PutArg(addr_t arg0, addr_t arg1, addr_t arg2, addr_t arg3, |
---|
127 | addr_t arg4); |
---|
128 | /// Put six operation argument index in the recording |
---|
129 | inline void PutArg(addr_t arg0, addr_t arg1, addr_t arg2, addr_t arg3, |
---|
130 | addr_t arg4, addr_t arg5); |
---|
131 | |
---|
132 | // Reserve space for a specified number of arguments |
---|
133 | inline size_t ReserveArg(size_t n_arg); |
---|
134 | |
---|
135 | // Replace an argument value |
---|
136 | void ReplaceArg(size_t i_arg, size_t value); |
---|
137 | |
---|
138 | /// Put a character string in the text for this recording. |
---|
139 | inline size_t PutTxt(const char *text); |
---|
140 | |
---|
141 | /// Number of variables currently stored in the recording. |
---|
142 | size_t num_var_rec(void) const |
---|
143 | { return num_var_rec_; } |
---|
144 | |
---|
145 | /// Number of LdpOp and LdvOp operations currently in the recording. |
---|
146 | size_t num_load_op_rec(void) const |
---|
147 | { return num_load_op_rec_; } |
---|
148 | |
---|
149 | /// Number of operators currently stored in the recording. |
---|
150 | size_t num_op_rec(void) const |
---|
151 | { return op_rec_.size(); } |
---|
152 | |
---|
153 | /// Approximate amount of memory used by the recording |
---|
154 | size_t Memory(void) const |
---|
155 | { return op_rec_.capacity() * sizeof(CPPAD_OP_CODE_TYPE) |
---|
156 | + vecad_ind_rec_.capacity() * sizeof(size_t) |
---|
157 | + op_arg_rec_.capacity() * sizeof(addr_t) |
---|
158 | + par_rec_.capacity() * sizeof(Base) |
---|
159 | + text_rec_.capacity() * sizeof(char); |
---|
160 | } |
---|
161 | }; |
---|
162 | |
---|
163 | /*! |
---|
164 | Put next operator in the operation sequence. |
---|
165 | |
---|
166 | This sets the op code for the next operation in this recording. |
---|
167 | This call must be followed by putting the corresponding |
---|
168 | \verbatim |
---|
169 | NumArg(op) |
---|
170 | \endverbatim |
---|
171 | argument indices in the recording. |
---|
172 | |
---|
173 | \param op |
---|
174 | Is the op code corresponding to the the operation that is being |
---|
175 | recorded (which must not be LdpOp or LdvOp). |
---|
176 | |
---|
177 | \return |
---|
178 | The return value is the index of the primary (last) variable |
---|
179 | corresponding to the result of this operation. |
---|
180 | The number of variables corresponding to the operation is given by |
---|
181 | \verbatim |
---|
182 | NumRes(op) |
---|
183 | \endverbatim |
---|
184 | With each call to PutOp or PutLoadOp, |
---|
185 | the return index increases by the number of variables corresponding |
---|
186 | to the call. |
---|
187 | This index starts at zero after the default constructor |
---|
188 | and after each call to Erase. |
---|
189 | */ |
---|
190 | template <class Base> |
---|
191 | inline size_t recorder<Base>::PutOp(OpCode op) |
---|
192 | { size_t i = op_rec_.extend(1); |
---|
193 | CPPAD_ASSERT_KNOWN( |
---|
194 | (abort_op_index_ == 0) || (abort_op_index_ != i), |
---|
195 | "Operator index equals abort_op_index in Independent" |
---|
196 | ); |
---|
197 | op_rec_[i] = static_cast<CPPAD_OP_CODE_TYPE>(op); |
---|
198 | CPPAD_ASSERT_UNKNOWN( op_rec_.size() == i + 1 ); |
---|
199 | CPPAD_ASSERT_UNKNOWN( (op != LdpOp) & (op != LdvOp) ); |
---|
200 | |
---|
201 | // first operator should be a BeginOp and NumRes( BeginOp ) > 0 |
---|
202 | num_var_rec_ += NumRes(op); |
---|
203 | CPPAD_ASSERT_UNKNOWN( num_var_rec_ > 0 ); |
---|
204 | |
---|
205 | return num_var_rec_ - 1; |
---|
206 | } |
---|
207 | |
---|
208 | /*! |
---|
209 | Put next LdpOp or LdvOp operator in operation sequence (special cases). |
---|
210 | |
---|
211 | This sets the op code for the next operation in this recording. |
---|
212 | This call must be followed by putting the corresponding |
---|
213 | \verbatim |
---|
214 | NumArg(op) |
---|
215 | \endverbatim |
---|
216 | argument indices in the recording. |
---|
217 | |
---|
218 | \param op |
---|
219 | Is the op code corresponding to the the operation that is being |
---|
220 | recorded (which must be LdpOp or LdvOp). |
---|
221 | |
---|
222 | \return |
---|
223 | The return value is the index of the primary (last) variable |
---|
224 | corresponding to the result of this operation. |
---|
225 | The number of variables corresponding to the operation is given by |
---|
226 | \verbatim |
---|
227 | NumRes(op) |
---|
228 | \endverbatim |
---|
229 | which must be one for this operation. |
---|
230 | With each call to PutLoadOp or PutOp, |
---|
231 | the return index increases by the number of variables corresponding |
---|
232 | to this call to the call. |
---|
233 | This index starts at zero after the default constructor |
---|
234 | and after each call to Erase. |
---|
235 | |
---|
236 | \par num_load_op_rec() |
---|
237 | The return value for <code>num_load_op_rec()</code> |
---|
238 | increases by one after each call to this function |
---|
239 | (and starts at zero after the default constructor or Erase). |
---|
240 | */ |
---|
241 | template <class Base> |
---|
242 | inline size_t recorder<Base>::PutLoadOp(OpCode op) |
---|
243 | { size_t i = op_rec_.extend(1); |
---|
244 | CPPAD_ASSERT_KNOWN( |
---|
245 | (abort_op_index_ == 0) || (abort_op_index_ != i), |
---|
246 | "This is the abort operator index specified by " |
---|
247 | "Independent(x, abort_op_index)." |
---|
248 | ); |
---|
249 | op_rec_[i] = static_cast<CPPAD_OP_CODE_TYPE>(op); |
---|
250 | CPPAD_ASSERT_UNKNOWN( op_rec_.size() == i + 1 ); |
---|
251 | CPPAD_ASSERT_UNKNOWN( (op == LdpOp) | (op == LdvOp) ); |
---|
252 | |
---|
253 | // first operator should be a BeginOp and NumRes( BeginOp ) > 0 |
---|
254 | num_var_rec_ += NumRes(op); |
---|
255 | CPPAD_ASSERT_UNKNOWN( num_var_rec_ > 0 ); |
---|
256 | |
---|
257 | // count this vecad load operation |
---|
258 | num_load_op_rec_++; |
---|
259 | |
---|
260 | return num_var_rec_ - 1; |
---|
261 | } |
---|
262 | |
---|
263 | /*! |
---|
264 | Add a value to the end of the current vector of VecAD indices. |
---|
265 | |
---|
266 | For each VecAD vector, this routine is used to store the length |
---|
267 | of the vector followed by the parameter index corresponding to each |
---|
268 | value in the vector. |
---|
269 | This value for the elements of the VecAD vector corresponds to the |
---|
270 | beginning of the operation sequence. |
---|
271 | |
---|
272 | \param vec_ind |
---|
273 | is the index to be palced at the end of the vector of VecAD indices. |
---|
274 | |
---|
275 | \return |
---|
276 | is the index in the vector of VecAD indices corresponding to this value. |
---|
277 | This index starts at zero after the recorder default constructor |
---|
278 | and after each call to Erase. |
---|
279 | It increments by one for each call to PutVecInd.. |
---|
280 | */ |
---|
281 | template <class Base> |
---|
282 | inline size_t recorder<Base>::PutVecInd(size_t vec_ind) |
---|
283 | { size_t i = vecad_ind_rec_.extend(1); |
---|
284 | vecad_ind_rec_[i] = vec_ind; |
---|
285 | CPPAD_ASSERT_UNKNOWN( vecad_ind_rec_.size() == i + 1 ); |
---|
286 | |
---|
287 | return i; |
---|
288 | } |
---|
289 | |
---|
290 | /*! |
---|
291 | Find or add a parameter to the current vector of parameters. |
---|
292 | |
---|
293 | \param par |
---|
294 | is the parameter to be found or placed in the vector of parameters. |
---|
295 | |
---|
296 | \return |
---|
297 | is the index in the parameter vector corresponding to this parameter value. |
---|
298 | This value is not necessarily placed at the end of the vector |
---|
299 | (because values that are identically equal may be reused). |
---|
300 | */ |
---|
301 | template <class Base> |
---|
302 | size_t recorder<Base>::PutPar(const Base &par) |
---|
303 | { static size_t hash_table[CPPAD_HASH_TABLE_SIZE * CPPAD_MAX_NUM_THREADS]; |
---|
304 | size_t i; |
---|
305 | size_t code; |
---|
306 | |
---|
307 | CPPAD_ASSERT_UNKNOWN( |
---|
308 | thread_offset_ / CPPAD_HASH_TABLE_SIZE |
---|
309 | == |
---|
310 | thread_alloc::thread_num() |
---|
311 | ); |
---|
312 | |
---|
313 | // get hash code for this value |
---|
314 | code = static_cast<size_t>( hash_code(par) ); |
---|
315 | CPPAD_ASSERT_UNKNOWN( code < CPPAD_HASH_TABLE_SIZE ); |
---|
316 | |
---|
317 | // If we have a match, return the parameter index |
---|
318 | i = hash_table[code + thread_offset_]; |
---|
319 | if( i < par_rec_.size() && IdenticalEqualPar(par_rec_[i], par) ) |
---|
320 | return i; |
---|
321 | |
---|
322 | // place a new value in the table |
---|
323 | i = par_rec_.extend(1); |
---|
324 | par_rec_[i] = par; |
---|
325 | CPPAD_ASSERT_UNKNOWN( par_rec_.size() == i + 1 ); |
---|
326 | |
---|
327 | // make the hash code point to this new value |
---|
328 | hash_table[code + thread_offset_] = i; |
---|
329 | |
---|
330 | // return the parameter index |
---|
331 | return i; |
---|
332 | } |
---|
333 | // -------------------------- PutArg -------------------------------------- |
---|
334 | /*! |
---|
335 | Prototype for putting operation argument indices in the recording. |
---|
336 | |
---|
337 | The following syntax |
---|
338 | \verbatim |
---|
339 | rec.PutArg(arg0) |
---|
340 | rec.PutArg(arg0, arg1) |
---|
341 | . |
---|
342 | . |
---|
343 | . |
---|
344 | rec.PutArg(arg0, arg1, ..., arg5) |
---|
345 | \endverbatim |
---|
346 | places the values passed to PutArg at the current end of the |
---|
347 | operation argument indices for the recording. |
---|
348 | \a arg0 comes before \a arg1, etc. |
---|
349 | The proper number of operation argument indices |
---|
350 | corresponding to the operation code op is given by |
---|
351 | \verbatim |
---|
352 | NumArg(op) |
---|
353 | \endverbatim |
---|
354 | The number of the operation argument indices starts at zero |
---|
355 | after the default constructor and each call to Erase. |
---|
356 | It increases by the number of indices placed by each call to PutArg. |
---|
357 | */ |
---|
358 | inline void prototype_put_arg(void) |
---|
359 | { // This routine should not be called |
---|
360 | CPPAD_ASSERT_UNKNOWN(false); |
---|
361 | } |
---|
362 | /*! |
---|
363 | Put one operation argument index in the recording |
---|
364 | |
---|
365 | \param arg0 |
---|
366 | The operation argument index |
---|
367 | |
---|
368 | \copydetails prototype_put_arg |
---|
369 | */ |
---|
370 | template <class Base> |
---|
371 | inline void recorder<Base>::PutArg(addr_t arg0) |
---|
372 | { |
---|
373 | size_t i = op_arg_rec_.extend(1); |
---|
374 | op_arg_rec_[i] = static_cast<addr_t>( arg0 ); |
---|
375 | CPPAD_ASSERT_UNKNOWN( op_arg_rec_.size() == i + 1 ); |
---|
376 | } |
---|
377 | /*! |
---|
378 | Put two operation argument index in the recording |
---|
379 | |
---|
380 | \param arg0 |
---|
381 | First operation argument index. |
---|
382 | |
---|
383 | \param arg1 |
---|
384 | Second operation argument index. |
---|
385 | |
---|
386 | \copydetails prototype_put_arg |
---|
387 | */ |
---|
388 | template <class Base> |
---|
389 | inline void recorder<Base>::PutArg(addr_t arg0, addr_t arg1) |
---|
390 | { |
---|
391 | size_t i = op_arg_rec_.extend(2); |
---|
392 | op_arg_rec_[i++] = static_cast<addr_t>( arg0 ); |
---|
393 | op_arg_rec_[i] = static_cast<addr_t>( arg1 ); |
---|
394 | CPPAD_ASSERT_UNKNOWN( op_arg_rec_.size() == i + 1 ); |
---|
395 | } |
---|
396 | /*! |
---|
397 | Put three operation argument index in the recording |
---|
398 | |
---|
399 | \param arg0 |
---|
400 | First operation argument index. |
---|
401 | |
---|
402 | \param arg1 |
---|
403 | Second operation argument index. |
---|
404 | |
---|
405 | \param arg2 |
---|
406 | Third operation argument index. |
---|
407 | |
---|
408 | \copydetails prototype_put_arg |
---|
409 | */ |
---|
410 | template <class Base> |
---|
411 | inline void recorder<Base>::PutArg(addr_t arg0, addr_t arg1, addr_t arg2) |
---|
412 | { |
---|
413 | size_t i = op_arg_rec_.extend(3); |
---|
414 | op_arg_rec_[i++] = static_cast<addr_t>( arg0 ); |
---|
415 | op_arg_rec_[i++] = static_cast<addr_t>( arg1 ); |
---|
416 | op_arg_rec_[i] = static_cast<addr_t>( arg2 ); |
---|
417 | CPPAD_ASSERT_UNKNOWN( op_arg_rec_.size() == i + 1 ); |
---|
418 | } |
---|
419 | /*! |
---|
420 | Put four operation argument index in the recording |
---|
421 | |
---|
422 | \param arg0 |
---|
423 | First operation argument index. |
---|
424 | |
---|
425 | \param arg1 |
---|
426 | Second operation argument index. |
---|
427 | |
---|
428 | \param arg2 |
---|
429 | Third operation argument index. |
---|
430 | |
---|
431 | \param arg3 |
---|
432 | Fourth operation argument index. |
---|
433 | |
---|
434 | \copydetails prototype_put_arg |
---|
435 | */ |
---|
436 | template <class Base> |
---|
437 | inline void recorder<Base>::PutArg(addr_t arg0, addr_t arg1, addr_t arg2, |
---|
438 | addr_t arg3) |
---|
439 | { |
---|
440 | size_t i = op_arg_rec_.extend(4); |
---|
441 | op_arg_rec_[i++] = static_cast<addr_t>( arg0 ); |
---|
442 | op_arg_rec_[i++] = static_cast<addr_t>( arg1 ); |
---|
443 | op_arg_rec_[i++] = static_cast<addr_t>( arg2 ); |
---|
444 | op_arg_rec_[i] = static_cast<addr_t>( arg3 ); |
---|
445 | CPPAD_ASSERT_UNKNOWN( op_arg_rec_.size() == i + 1 ); |
---|
446 | |
---|
447 | } |
---|
448 | /*! |
---|
449 | Put five operation argument index in the recording |
---|
450 | |
---|
451 | \param arg0 |
---|
452 | First operation argument index. |
---|
453 | |
---|
454 | \param arg1 |
---|
455 | Second operation argument index. |
---|
456 | |
---|
457 | \param arg2 |
---|
458 | Third operation argument index. |
---|
459 | |
---|
460 | \param arg3 |
---|
461 | Fourth operation argument index. |
---|
462 | |
---|
463 | \param arg4 |
---|
464 | Fifth operation argument index. |
---|
465 | |
---|
466 | \copydetails prototype_put_arg |
---|
467 | */ |
---|
468 | template <class Base> |
---|
469 | inline void recorder<Base>::PutArg(addr_t arg0, addr_t arg1, addr_t arg2, |
---|
470 | addr_t arg3, addr_t arg4) |
---|
471 | { |
---|
472 | size_t i = op_arg_rec_.extend(5); |
---|
473 | op_arg_rec_[i++] = static_cast<addr_t>( arg0 ); |
---|
474 | op_arg_rec_[i++] = static_cast<addr_t>( arg1 ); |
---|
475 | op_arg_rec_[i++] = static_cast<addr_t>( arg2 ); |
---|
476 | op_arg_rec_[i++] = static_cast<addr_t>( arg3 ); |
---|
477 | op_arg_rec_[i] = static_cast<addr_t>( arg4 ); |
---|
478 | CPPAD_ASSERT_UNKNOWN( op_arg_rec_.size() == i + 1 ); |
---|
479 | |
---|
480 | } |
---|
481 | /*! |
---|
482 | Put six operation argument index in the recording |
---|
483 | |
---|
484 | \param arg0 |
---|
485 | First operation argument index. |
---|
486 | |
---|
487 | \param arg1 |
---|
488 | Second operation argument index. |
---|
489 | |
---|
490 | \param arg2 |
---|
491 | Third operation argument index. |
---|
492 | |
---|
493 | \param arg3 |
---|
494 | Fourth operation argument index. |
---|
495 | |
---|
496 | \param arg4 |
---|
497 | Fifth operation argument index. |
---|
498 | |
---|
499 | \param arg5 |
---|
500 | Sixth operation argument index. |
---|
501 | |
---|
502 | \copydetails prototype_put_arg |
---|
503 | */ |
---|
504 | template <class Base> |
---|
505 | inline void recorder<Base>::PutArg(addr_t arg0, addr_t arg1, addr_t arg2, |
---|
506 | addr_t arg3, addr_t arg4, addr_t arg5) |
---|
507 | { |
---|
508 | size_t i = op_arg_rec_.extend(6); |
---|
509 | op_arg_rec_[i++] = static_cast<addr_t>( arg0 ); |
---|
510 | op_arg_rec_[i++] = static_cast<addr_t>( arg1 ); |
---|
511 | op_arg_rec_[i++] = static_cast<addr_t>( arg2 ); |
---|
512 | op_arg_rec_[i++] = static_cast<addr_t>( arg3 ); |
---|
513 | op_arg_rec_[i++] = static_cast<addr_t>( arg4 ); |
---|
514 | op_arg_rec_[i] = static_cast<addr_t>( arg5 ); |
---|
515 | CPPAD_ASSERT_UNKNOWN( op_arg_rec_.size() == i + 1 ); |
---|
516 | } |
---|
517 | // -------------------------------------------------------------------------- |
---|
518 | /*! |
---|
519 | Reserve space for arguments, but delay placing values there. |
---|
520 | |
---|
521 | \param n_arg |
---|
522 | number of arguements to reserve space for |
---|
523 | |
---|
524 | \return |
---|
525 | is the index in the argument vector corresponding to the |
---|
526 | first of the arguments being reserved. |
---|
527 | */ |
---|
528 | template <class Base> |
---|
529 | inline size_t recorder<Base>::ReserveArg(size_t n_arg) |
---|
530 | { |
---|
531 | size_t i = op_arg_rec_.extend(n_arg); |
---|
532 | CPPAD_ASSERT_UNKNOWN( op_arg_rec_.size() == i + n_arg ); |
---|
533 | return i; |
---|
534 | } |
---|
535 | |
---|
536 | /*! |
---|
537 | \brief |
---|
538 | Replace an argument value in the recording |
---|
539 | (intended to fill in reserved values). |
---|
540 | |
---|
541 | \param i_arg |
---|
542 | is the index, in argument vector, for the value that is replaced. |
---|
543 | |
---|
544 | \param value |
---|
545 | is the new value for the argument with the specified index. |
---|
546 | */ |
---|
547 | template <class Base> |
---|
548 | inline void recorder<Base>::ReplaceArg(size_t i_arg, size_t value) |
---|
549 | { op_arg_rec_[i_arg] = static_cast<addr_t>( value ); } |
---|
550 | // -------------------------------------------------------------------------- |
---|
551 | /*! |
---|
552 | Put a character string in the text for this recording. |
---|
553 | |
---|
554 | \param text |
---|
555 | is a '\\0' terminated character string that is to be put in the |
---|
556 | vector of characters corresponding to this recording. |
---|
557 | The terminator '\\0' will be included. |
---|
558 | |
---|
559 | \return |
---|
560 | is the offset with in the text vector for this recording at which |
---|
561 | the character string starts. |
---|
562 | */ |
---|
563 | template <class Base> |
---|
564 | inline size_t recorder<Base>::PutTxt(const char *text) |
---|
565 | { |
---|
566 | // determine length of the text including terminating '\0' |
---|
567 | size_t n = 0; |
---|
568 | while( text[n] != '\0' ) |
---|
569 | n++; |
---|
570 | CPPAD_ASSERT_UNKNOWN( n <= 1000 ); |
---|
571 | n++; |
---|
572 | CPPAD_ASSERT_UNKNOWN( text[n-1] == '\0' ); |
---|
573 | |
---|
574 | // copy text including terminating '\0' |
---|
575 | size_t i = text_rec_.extend(n); |
---|
576 | size_t j; |
---|
577 | for(j = 0; j < n; j++) |
---|
578 | text_rec_[i + j] = text[j]; |
---|
579 | CPPAD_ASSERT_UNKNOWN( text_rec_.size() == i + n ); |
---|
580 | |
---|
581 | return i; |
---|
582 | } |
---|
583 | // ------------------------------------------------------------------------- |
---|
584 | |
---|
585 | |
---|
586 | } // END_CPPAD_NAMESPACE |
---|
587 | # endif |
---|