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

Use VisitorBase::getZeroInit() or ConstantFolder::synthesizeLiteral() for assignments and initializations of variables #1074

Open
kchristin22 opened this issue Sep 3, 2024 · 0 comments

Comments

@kchristin22
Copy link
Collaborator

Currently not all variables initialized or assigned to a value use VisitorBase::getZeroInit() or ConstantFolder::synthesizeLiteral() accordingly. Hence, the code is mixed or these functions are not used at all, as the Forward Mode tests indicate.

Example:

#include "clad/Differentiator/Differentiator.h"

double fn(const double* arr, size_t n) {
  double sum = 0;
  for (size_t i=0; i < n; ++i) {
    sum += arr[i];
  }
  return sum;
}


int main() {
  auto grad = clad::gradient(fn);
  return 0;
}

The code produced is:

void fn_grad(const double *arr, size_t n, double *_d_arr, size_t *_d_n) {
    size_t _d_i = 0UL;
    size_t i = 0UL;
    clad::tape<double> _t1 = {};
    double _d_sum = 0.;
    double sum = 0; // should be 0.
    unsigned long _t0 = 0UL;
    for (i = 0; ; ++i) {
        {
            if (!(i < n))
                break;
        }
        _t0++;
        clad::push(_t1, sum);
        sum += arr[i];
    }
    _d_sum += 1;
    for (;; _t0--) {
        {
            if (!_t0)
                break;
        }
        --i;
        {
            sum = clad::pop(_t1);
            double _r_d0 = _d_sum;
            _d_arr[i] += _r_d0;
        }
    }
}

As the comment indicates, the var sum should be initialized as so:

    double sum = 0.; 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant