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

If stmt of break cond is not added before compound for-loop inc diff when using TBR analysis in reverse mode #1123

Open
kchristin22 opened this issue Oct 23, 2024 · 0 comments
Assignees

Comments

@kchristin22
Copy link
Collaborator

kchristin22 commented Oct 23, 2024

This issue is opened in advance of PR #833 getting merged so not to forget this afterwards

When we have a break stmt in a for loop, we shouldn't perform the for loop increment differentiation in the first iteration in the reverse pass (as the original stmt is also not performed in the forward pass after the break stmt). This is why we add an if stmt to check whether this is the path that the break stmt corresponds to in the reverse pass, to not perform the inc diff stmt. Examples and code found in PR #833.
When the inc diff is a compound stmt it seems that this if-check is not added when using TBR analysis, even though it is added without it:

double fn26(double i, double j) {
  double res = 0;
  for (int c = 0; (res += i * j); ++c, res=7*i*j) {
        if(c == 0)
          break;
    }
  return res;
}

Produced code in PR833:

void fn26_grad(double i, double j, double *_d_i, double *_d_j) {
    int _d_c = 0;
    int c = 0;
    clad::tape<double> _t1 = {};
    clad::tape<double> _t2 = {};
    clad::tape<bool> _cond0 = {};
    clad::tape<unsigned long> _t3 = {};
    double _d_res = 0.;
    double res = 0;
    unsigned long _t0 = 0UL;
    for (c = 0; ; clad::push(_t2, res) , (++c , res = 7 * i * j)) {
        {
            {
                clad::push(_t1, res);
            }
            if (!(res += i * j))
                break;
        }
        _t0++;
        {
            clad::push(_cond0, c == 0);
            if (clad::back(_cond0)) {
                clad::push(_t3, 1UL);
                break;
            }
        }
        clad::push(_t3, 2UL);
    }
    _d_res += 1;
    for (;; _t0--) {
        {
            if (!_t0 || (clad::size(_t3) != 0 && clad::back(_t3) != 1)) {
                res = clad::pop(_t1);
                double _r_d0 = _d_res;
                *_d_i += _r_d0 * j;
                *_d_j += i * _r_d0;
            }
            if (!_t0)
                break;
        }
       {
            res = clad::pop(_t2);
            double _r_d1 = _d_res;
            _d_res = 0.;
            *_d_i += 7 * _r_d1 * j;
            *_d_j += 7 * i * _r_d1;
            _d_c += 0;
            --c;
        }
        switch (clad::pop(_t3)) {
          case 2UL:
            ;
            {
                if (clad::back(_cond0))
                  case 1UL:
                    ;
                clad::pop(_cond0);
            }
        }
    }
}
  • Without TBR analysis:
if (clad::size(_t3) != 0 && clad::back(_t3) != 1) {
            res = clad::pop(_t2);
            double _r_d1 = _d_res;
            _d_res = 0.;
            *_d_i += 7 * _r_d1 * j;
            *_d_j += 7 * i * _r_d1;
            _d_c += 0;
            --c;
}
  • With TBR analysis:
{
           res = clad::pop(_t2);
           double _r_d1 = _d_res;
           _d_res = 0.;
           *_d_i += 7 * _r_d1 * j;
           *_d_j += 7 * i * _r_d1;
           _d_c += 0;
           --c;
}
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

No branches or pull requests

2 participants