Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to provide custom pushforwards for functions that use functors #1023

Open
gojakuch opened this issue Aug 2, 2024 · 0 comments · May be fixed by #1038
Open

Impossible to provide custom pushforwards for functions that use functors #1023

gojakuch opened this issue Aug 2, 2024 · 0 comments · May be fixed by #1038

Comments

@gojakuch
Copy link
Collaborator

gojakuch commented Aug 2, 2024

In this case, the compilation fails:

#include "clad/Differentiator/Differentiator.h"
#include <iostream>

namespace clad::custom_derivatives {
template <typename F>
void use_functor_pushforward(double x, F& f, double d_x, F& d_f) {
    f.operator_call_pushforward(x, &d_f, d_x);
}
}

template <typename F>
void use_functor(double x, F& f) {
    f(x);
}

struct Foo {
    double &y;
    double operator()(double x) {
        y = 2*x;

        return x;
    }
};

double fn(double x) {
    Foo func {x};

    use_functor(x, func);

    return x;
}

int main() {
  auto df = clad::differentiate(fn, "x");
  std::cout << "f(3)=" << fn(3.0);
  std::cout << "; df(3)=" << df.execute(3.0) << '\n';
  return 0;
}

because at the time of parsing the custom derivative, the compiler doesn't know we're going to insert the operator_call_pushforward. we need to somehow bypass this in a user-friendly way, since this is a common pattern in modern frameworks like Kokkos.

gojakuch added a commit to gojakuch/clad that referenced this issue Aug 13, 2024
Previously, if a user wanted to provide a custom pushforward for a
function that uses functors in it, it was impossible to use a generated
pushforwards for that functors' call operators. This commit aims to fix
for basic functors that don't have multiple call operator overloads.

Fixes: vgvassilev#1023
gojakuch added a commit to gojakuch/clad that referenced this issue Aug 13, 2024
Previously, if a user wanted to provide a custom pushforward for a
function that uses functors in it, it was impossible to use a generated
pushforwards for that functors' call operators. This commit aims to fix
for basic functors that don't have multiple call operator overloads.

Fixes: vgvassilev#1023
gojakuch added a commit to gojakuch/clad that referenced this issue Aug 13, 2024
Previously, if a user wanted to provide a custom pushforward for a
function that uses functors in it, it was impossible to use generated
pushforwards for that functors' call operators. This commit aims to fix
this for basic functors that don't have multiple call operator overloads.

Fixes: vgvassilev#1023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant