Skip to content

Error when differentiating Explicit template class  #1510

@a17brah

Description

@a17brah

I noticed a weird behaviour when used EnzymeAD with an explicit template class. If the method bump is defined in the cpp file, I get this error "declare dso_local noundef double @_ZNK8RiskViewIdE4bumpEdd(ptr noundef nonnull align 8 dereferenceable(304), double noundef, double noundef) local_unnamed_addr #7
Assertion failed: 0 && "attempting to differentiate function without definition" "

template<typename T>
class RiskView {
public:
    RiskView();

    RiskView(Array<T> &times, Array<T> &strikes, Array<T> &forwards, Matrix<T> &spreads);

    T bump(T x, T t) const;;

    void out();

private:
    bool isDefault_;
    Array<T> times_;
    Array<T> strikes_;
    Array<T> forwards_;
    Matrix<T> spreads_;
    std::vector<Array<T>> logMoneyness_;
    std::vector<Array<T>> spreadsVec_;
    std::vector<Interpolator<T, SmoothStepInterpolation, FlatExtrapolation>> interpolators_;
};

//EnzymeAD works when this is defined in the header
template<typename T>
T RiskView<T>::bump(T x, T t) const { //error if defined in cpp
    if (!isDefault_) {
        //find index in t then interpolate index-1, index=> give linear interpolation of that.
        int index = search_sorted<T>(t, times_.data(), times_.size(), true);
        //case index = 0 => t< times[0]
        if (index == 0) {
            T t_right = times_[0];
            T y_right = interpolators_[0].interpolate(x);
            return t * y_right / t_right;
        } else {
            T t_left = times_[index - 1];
            T t_right = times_[index];
            T y_left = interpolators_[index - 1].interpolate(x);;
            T y_right = interpolators_[index].interpolate(x);
            return y_left + (t - t_left) * (y_right - y_left) / (t_right - t_left);
        }
    }
    return 0.0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions