patr-schm / TinyAD

Automatic Differentiation in Geometry Processing Made Simple
MIT License
371 stars 17 forks source link

Changing the linear solver? #9

Closed the13fools closed 8 months ago

the13fools commented 9 months ago

Hello there,

I've been using this library to create a reference implementation of a project I've been working on for a long time.

I'm generally pretty satisfied, thank you for creating this! I've been waiting for quite some time for someone to implement a convenient autodiff framework that supports sparse hessians, and after years of implementing these by hand during my phd, having auto diff generate hessians for me has been a welcome relief that has helped accelerate my research progress.

However, I've got one thing that's been bothering me, and wanted to ask about it, as perhaps I'm simply not seeing the answer to immediately, but otherwise would like to request that you patch the library so that I don't need to create a fork to support my reference implementation.

In particular, I see that TinyAD seems to have built in support for changing out the linear solver that is used in the convenience methods like the line search, but I can't seem to figure out how to use this feature effectively.

I created a tinyad wrapper that I've been using in my project here: https://github.com/the13fools/gpgpt/blob/main/src/ADWrapper/ADFuncRunner.h#L82.

Ideally, I'd like to initialize a solver like so in this code: TinyAD::LinearSolver<double, Eigen::CholmodSupernodalLLT< Eigen::SparseMatrix> > > solver;

But when I do something like this the tinyad code gives me some templating issues. I think this should be a pretty straightforward thing to patch in the library code?

Thanks for creating this and for the support!

Best,

Josh

patr-schm commented 9 months ago

Hi Josh,

thanks for the kind words! I fixed the interface of functions like newton_direction(), etc. It should now be possible to pass different solvers as you described:

TinyAD::LinearSolver<double, Eigen::CholmodSupernodalLLT<Eigen::SparseMatrix<PassiveT>>> solver;
Eigen::VectorXd d = newton_direction(g, H, solver);

I've tested this with a few of the default Eigen solvers, but don't have a Cholmod installation currently. Please let me know if this works for you now.

Best Patrick