yizhang-yiz / fazang

Fazang is a Fortran library for reverse-mode automatic differentiation, inspired by Stan/Math library.
BSD 3-Clause "New" or "Revised" License
40 stars 3 forks source link

Optional data argument for `gradient` and `jacobian` functions #9

Open Nicholaswogan opened 2 years ago

Nicholaswogan commented 2 years ago

Would be nice if the gradient and jacobian functions accepted an optional data parameter. The interface would look something like this:

  abstract interface
     function dependent_function (x, dat) result (fx)
       import :: var
       type(var), intent(in) :: x(:)
       class(*), optional, intent(in) :: dat
       type(var) :: fx
     end function dependent_function
  end interface
yizhang-yiz commented 2 years ago

A more flexible way to put the dependent function in a module and use whatever the module provides inside the function. This way doesn't require user to unpack data.

Nicholaswogan commented 2 years ago

But the module approach will make the data global. This makes the code thread-unsafe, if there are work arrays being written to in the data. It also prevents any object/class/type which uses fazang to have multiple different instantiations.

yizhang-yiz commented 2 years ago

I should've pointed out that fazang itself is not thread safe. Currently one cannot safely call gradient in separate threads. It's planned but not there yet.