rjhogan / Adept-2

Combined array and automatic differentiation library in C++
http://www.met.reading.ac.uk/clouds/adept/
Apache License 2.0
163 stars 29 forks source link

Uneffective statements break gradients #11

Closed gray-heron closed 5 years ago

gray-heron commented 5 years ago

Hello, I have another problem. The following program prints {{0, 0}}, which is not correct: https://gist.github.com/acriaer/e1fc3a637148a47a49ecefda9c5310a2

Please take a look at division by 1.0 at line 15 and call to Function at line 17. Both of those should not affect the gradient. However, if I remove either one of them (or both), the program prints correct {{0.707107, 0.707107}}.

This is the furthest I could simplify the case from my application. If I make it simpler, the problem disappears.

Happens every time on GCC 5.4.0, GCC 7.3.0 and Clang 6.0. Adept is compiled with -O3.

rjhogan commented 5 years ago

The problem is that you use "auto", but the return type of the division operation is not adept::aReal, but some weird internal division object whose contents are not guaranteed to be preserved when other things happen in the program. If you replace auto with adept::aReal it will work. Best to avoid "auto" for the return type of an active statement.

gray-heron commented 5 years ago

Got it, it works now, thank you.

However, using auto is not uncommon practice, so it would be nice if Adept C++ Software Library: User Guide warned about it (especially since adept uses C++11 features).

rjhogan commented 5 years ago

You're right, I'll add something to the documentation. This is a problem with many expression template libraries, e.g.: https://eigen.tuxfamily.org/dox/TopicPitfalls.html

Thanks, Robin.