You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a suggested way to have ancillary (not central to obtaining derivatives) access to values during tape execution?
Say I have a function like
std::vector foo(std::vector<double> x) {
// some math creates y from x
do_something_else_without_math(y); // ancillary, like maybe generating output or storing in some other object
// more math creates result
return result;
}
I want to CppAD-ize the function, and it would be ideal if I can still do the ancillary step and have it invoked during tape sweeps, at least forward0.
A workaround I've used is to embed the ancillary step in a derived atomic class with a dummy output. Then I invoke the atomic so it's in the tape. The dummy output is a CppAD::AD<double> that I add to result. The value of the dummy output is always 0, and by adding that to the result I think I am tricking the tape into not optimizing away the use of the atomic. (If the dummy output of the atomic was not added to the result, I think it would look like a dangling node in the computation graph recorded on the tape and could in theory be optimized away, although I don't know exactly what CppAD's optimize does.)
This workaround seems to work well, but the whole situation I'm doing this in is highly configurable, and I'm not sure I can always easily protect against having the atomic optimized out of the tape or if that is really something to worry about.
So I'm wondering if there is already a solution for this, or if the need makes sense and would be easy to support, or if there is a tweak on the solution with atomics that you would suggest. Thanks.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi @bradbell
Is there a suggested way to have ancillary (not central to obtaining derivatives) access to values during tape execution?
Say I have a function like
I want to CppAD-ize the function, and it would be ideal if I can still do the ancillary step and have it invoked during tape sweeps, at least forward0.
A workaround I've used is to embed the ancillary step in a derived atomic class with a dummy output. Then I invoke the atomic so it's in the tape. The dummy output is a
CppAD::AD<double>
that I add to result. The value of the dummy output is always 0, and by adding that to the result I think I am tricking the tape into not optimizing away the use of the atomic. (If the dummy output of the atomic was not added to the result, I think it would look like a dangling node in the computation graph recorded on the tape and could in theory be optimized away, although I don't know exactly what CppAD'soptimize
does.)This workaround seems to work well, but the whole situation I'm doing this in is highly configurable, and I'm not sure I can always easily protect against having the atomic optimized out of the tape or if that is really something to worry about.
So I'm wondering if there is already a solution for this, or if the need makes sense and would be easy to support, or if there is a tweak on the solution with atomics that you would suggest. Thanks.
Beta Was this translation helpful? Give feedback.
All reactions