Skip to content
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

H5Easy read into existing object #695

Open
jjcasmar opened this issue Feb 28, 2023 · 6 comments
Open

H5Easy read into existing object #695

jjcasmar opened this issue Feb 28, 2023 · 6 comments

Comments

@jjcasmar
Copy link

jjcasmar commented Feb 28, 2023

I have an std::vector<Eigen::Vector3f> already allocated and I want to populate the values of that vector with data coming from a dataset. Right now, to populate, I am doing something like this

std::vector<Eigen::Vector3f> v;
v.resize(100);
Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, 3, Eigen::RowMajor>>(v.data(), v.size(), 3) = H5Easy::load<Eigen::Matrix<float, Eigen::Dynamic, 3, Eigen::RowMajor>(file, path);

This however has a caveat. HighFive is allocating space for the Eigen::Matrix where it has to load the data, and the a copy is made from the Eigen::Matrix to the Eigen::Map.

Would it be possible to add API to H5Easy to be able to pass an existing object where we want the data to be loaded?

std::vector<Eigen::Vector3f> v;
Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, 3, Eigen::RowMajor>> iface;
H5Easy::load(file, path, iface);
@tdegeus
Copy link
Collaborator

tdegeus commented Mar 2, 2023

You might want to consider HighFive's 'normal' API on this:

file.getDataSet(path).read(iface.data());

If we would want to consider amending H5Easy functionality I do think a different name should be used. Otherwise overloading becomes a mess, and you'd need H5Easy::load<>(...). However, since you can do a one-liner I would say that we should debate if we would like to have another function.

@jjcasmar
Copy link
Author

jjcasmar commented Mar 2, 2023

The "normal" API wont work in this case, since it doesn't implement loading something as an Eigen::Map or Eigen::RowMajor, afaik, as discussed in #694

@tdegeus
Copy link
Collaborator

tdegeus commented Mar 2, 2023

I may have looked too quickly ;). Can you get an iterator from Eigen::Map that iterates over the underlying memory as if it were contiguous?

@jjcasmar
Copy link
Author

jjcasmar commented Mar 2, 2023

Not sure if that is possible, maybe yes. However, that implies kind of redoing all the logic HighFive already does when loading a dataset into an Eigen::Matrix using H5Easy::load<Eigen::Matrix<...>>, like taking care of Eigen::RowMajor/ColMajor, rows/cols...

@jjcasmar
Copy link
Author

jjcasmar commented Mar 2, 2023

Regarding giving the function a different name, I totally agree, maybe load_to(o). nlohmann::json for example uses get and get_to

@tdegeus
Copy link
Collaborator

tdegeus commented Mar 3, 2023

load_to is a good idea. Happy to review a PR!

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

No branches or pull requests

3 participants