Skip to content

Add variadic version of scalar_seq_view #2951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
andrjohns opened this issue Sep 28, 2023 · 3 comments
Open

Add variadic version of scalar_seq_view #2951

andrjohns opened this issue Sep 28, 2023 · 3 comments

Comments

@andrjohns
Copy link
Collaborator

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

@SteveBronder
Copy link
Collaborator

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
Copy link
Collaborator

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

@andrjohns
Copy link
Collaborator Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants