[ADOL-C] "if" brach dependent on adoubles

Kshitij Kulshreshtha kshitij at math.upb.de
Wed Oct 2 07:55:20 EDT 2013


There are two solutions to your problem.

1. Retaping or creating branched tapes.
For retaping you can have a function

void create_tape(double *point_of_evaluation) {
  int tag=1, keep=0;

  adouble eg, a, b, c;
  // assign values to a, b, c, from point_of_evalutation

  eg = fun(a,b,c);
  if (eg < 0 )
    // Some stuff
    // Some other stuff

Then check the return value from eg. forward/reverse/gradient/jacobian
call. If the return value is negative then branch switch is detected
(and warning will be printed) and you should call create_tape(x) with
current x, followed by another forward/reverse/gradient/jacobian call.

For creating branched tapes, you can take a set of points where you know
that they lie on different branches and call create_tape with each of
them, while changing the tag all for each of them. Then you can call
forward/reverse/gradient/jacobian etc using the correct tag (or use the
return value from these functions to know which one is not correct).

2. The other solution is to use advector. In this case you will need to
put each branch code in a separate function and create a tape by calling
all of them one after the other and storing the values returned or
computed in these branches in an advector and then choosing the relevant
one using the deciding variable. This is basically a generalisation of
condassign, which chooses only between two different values.

I hope this helps.


As on 2013-09-27 15:37h, RICARDO PUENTE RICO did write:
> Hello all,
> I´m working with a code that uses some conditional branches where the
> condition is specified on the value of an adouble. For example
> int tag=0,keep=0;
> trace_on(tag,keep);
> adouble eg,a,b,c;
> eg = fun(a,b,c);
> if(eg < 0)
>   Some stuff..
> else
>   Other stuff...
> endif
> trace_off();
> This compiles but complains at runtime with the following message:
> ADOL-C Warning: Branch switch detected in comparison (operator neq_zero).
> Forward sweep aborted! Retaping recommended!
> I understand the problem has to do with the handling of the continuity
> issues generated by the code branching, but I haven´t been able to solve
> the problem.
> Things that come to mind are:
>   - In my case, no derivative value depends on eg. But as its own value
> depends on adoubles, it needs to be an adouble too.
>     Is there a conversion method from adoubles to passive types that I´m
> not aware of?
>   - Retaping. This is what the error suggests, but I really don´t know
> how to implement it. I´ve tried using different values for "tag",
>     "keep" and "repeat" to no avail.
>   - Using the condassign() method. I haven´t found yet a way to make it
> work in my case, where the branches comprise a lot of code, not
>     merely assignments, but I´m still thinking on it.
> Regards,

Dr. Kshitij Kulshreshtha

Institut für Mathematik,
Universität Paderborn,
Warburger Straße 100,
33098 Paderborn.

Büro: A3.235

Arnikaweg 62
33100 Paderborn.

More information about the ADOL-C mailing list