Replies: 1 comment
-
That type of discussion should be under a pull request or a bug report as it is very specific for a particular issue. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In this discussion I would like to propose a solution for computing same derived function multiple times and would like to discuss improvements to this solution and if this solution is a good fit for clad or not.
Along with solving computing same derived function multiple times, this proposal will also solve:
fdump-derived-fn
fdump-derived-fn-ast
fgenerate-source-file
I would also like to modify behaviour of
fdump-source-fn
andfdump-source-fn-ast
:fdump-source-fn
andfdump-source-fn-ast
when used with higher order derivatives (clad::differentiate<N>(...)
) also dumps derived functions and derived functions AST respectively.This feature seems helpful for developers of clad, but for end users this can be surprising.
The proposed solution
Call to compute derivative of a function is made in
CladPlugin::ProcessDiffRequest
function, here we can check if the requested derivative of a function have already been computed, if yes, then we can reuse the already computed derivative instead of callingm_DerivativeBuilder->Derive(...)
We can check if derived function already exists using derived function name and
DeclContext
of function to be derived.For this to work 2 things will always be needed to be true:
Both of these conditions are true as of now in clad :)
I have assumed, the pair
{functionName, functionDeclContext}
will be unique for functions without any overloads (Please correct me if I am wrong).Thus we can create a map,
std::map<std::pair(const IdentifierInfo*, const DeclContext*), FunctionDecl*>
to store derived functions. We will also need a function which returns name of the derived function using theDiffRequest
object.Is
DerivativeBuilder
a good place forDerivedFns
(map object to store derived functions) andgetDerivedFnName
function ?If yes, then we will be able to access these in
CladPlugin::ProcessDiffRequest
usingm_DerivativeBuilder
member ofCladPlugin
Now, few problems with this approach
ForwardModeVisitor::Derive
andReverseModeVisitor::Derive
respectively, computing it 2 times (one inCladPlugin::ProcessDiffRequest
) is waste of resources.For this,
We can just compute derived function name in
CladPlugin::ProcessDiffRequest
and add an attributederivedFnName
inDiffRequest
, so we will not need to compute it again inForwardModeVisitor::Derive
andReverseModeVisitor::Derive
.ForwardModeVisitor
andReverseModeVisitor
class are assigned to correct value while computing derived function name, if we shift computing derived function name toCladPlugin::ProcessDiffRequest
then these variables will not be set to correct value.For this too, we can add
DiffRequest
attributes to store these information.Please suggest improvements/other ideas for this problem and solution.
Beta Was this translation helpful? Give feedback.
All reactions