math icon indicating copy to clipboard operation
math copied to clipboard

Add variadic version of `scalar_seq_view`

Open andrjohns opened this issue 2 years ago • 3 comments

Description

In the finite-diff framework PR (and some other functions that I'm planning), we need to be able to sequentially iterate through each element of all arguments passed to a function.

The current approach is to use the serializer/deserializer framework which copies all values to a single Eigen::VectorXd and then re-constructs the arguments to the appropriate shape/size by passing in the original arguments. This obviously introduces pretty significant overhead in both copy and memory costs.

A better alternative would be to extend scalar_seq_view to take a variadic number of arguments, and then in the []/() index operator use a hash table to map the input index to the appropriate value in the appropriate argument

Example

In Math pseudo-code, something like:

Eigen::VectorXd a_vec(2);
Eigen::MatrixXd b_mat(2,2);

scalar_seq_view<Eigen::VectorXd, Eigen::MatrixXd> args_view(a_vec, b_mat);

args_view[0]; // Returns a_vec[0]
args_view[1]; // Returns a_vec[1]
args_view[2]; // Returns b_mat(0, 0)
args_view[3]; // Returns b_mat(1, 0)
args_view[4]; // Returns b_mat(0, 1)
args_view[5]; // Returns b_mat(1, 1)

Current Version:

v4.7.0

andrjohns avatar Sep 28 '23 07:09 andrjohns

Are you only reading through it once in sequential order? For sequential reads we could just do something where we store a tuple of references to the arguments constructor and the sizes and iterate with a next() member function. Toy example below where i didn't figure out the inner array indexing bits but I think is possible.

https://godbolt.org/z/cfbEzTY44

Do you need it to have a operator[]? We could just mock that inside of the class and actually when operator[] is called it always just returns the next scalar. Though that's kind of gross

SteveBronder avatar Sep 28 '23 14:09 SteveBronder

Actually can you show me where you need to do that? Idt I like what I wrote there

SteveBronder avatar Sep 28 '23 14:09 SteveBronder

Oh interesting, thanks for prototyping!

The best example for the use-case is anything that needs to use the gradient or hessian functors. These involve looping through each parameter value and either peturbing it (for finite-diff) or constructing a var/fvar for autodiff.

Are you only reading through it once in sequential order?

It would likely be iterated through multiple times, and I don't know if it could always be guaranteed to be sequential, so that's why I thought an index-lookup approach would be best

andrjohns avatar Sep 29 '23 08:09 andrjohns