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

eigen: vectors written as rank-2 arrays #66

Closed
eudoxos opened this issue May 15, 2020 · 3 comments
Closed

eigen: vectors written as rank-2 arrays #66

eudoxos opened this issue May 15, 2020 · 3 comments

Comments

@eudoxos
Copy link

eudoxos commented May 15, 2020

Rank functions are always returning a 2-tuple for Eigen, so even 1d structures are written as rank-2 arrays where one dimension is 1:

#include<Eigen/Core>
#include<h5cpp/all>

int main(void){
	auto fd=h5::create("eig.h5",H5F_ACC_TRUNC);
	h5::write(fd,"1d-array",Eigen::ArrayXf::Random(10).eval());
	h5::write(fd,"1d-vector",Eigen::VectorXf::Random(10).eval());
};
$ h5c++ -std=c++17 -I/usr/include/eigen3 eig.cc -o eig && rm -rf eig.h5 && ./eig && h5dump eig.h5
HDF5 "eig.h5" {
GROUP "/" {
   DATASET "1d-array" {
      DATATYPE  H5T_IEEE_F32LE
      DATASPACE  SIMPLE { ( 1, 10 ) / ( 1, 10 ) }
      DATA {
      (0,0): 0.680375, -0.211234, 0.566198, 0.59688, 0.823295, -0.604897,
      (0,6): -0.329554, 0.536459, -0.444451, 0.10794
      }
   }
   DATASET "1d-vector" {
      DATATYPE  H5T_IEEE_F32LE
      DATASPACE  SIMPLE { ( 1, 10 ) / ( 1, 10 ) }
      DATA {
      (0,0): -0.0452059, 0.257742, -0.270431, 0.0268018, 0.904459, 0.83239,
      (0,6): 0.271423, 0.434594, -0.716795, 0.213938
      }
   }
}

Just for the record. After the new type-deduction system is out, I can have a look to see how to fix it. I could fix it for the current one but tell me whether I am not wasting time.

@steven-varga
Copy link
Owner

Thank you for reporting this. Before you fix something, did you try:

Eigen::VectorXf v(5);
for(int i=0; i<5; i++) v[i] = i;
h5::write(fd,"1d-vector",v, h5::current_dims{10}, h5::offset{3}, h5::count{5});

H5CPP already provides mechanism to save data into arbitrary shape.
Grepping the Eigen3 source tree:

* \li \c VectorXf is a dynamic-size vector of floats (\c Matrix<float, Dynamic, 1>

indicates that Eigen3 follows suit with the majority of linalg systems, providing mechanism to differentiate rovectors and colvectors. Am I wrong?
hdf5 arrays per se are not supported in the current master branch, will be in the new one.

best:steve

@eudoxos
Copy link
Author

eudoxos commented May 15, 2020

Yes, Eigen supports both colum-vectors and row-vectors in which case one of the compile-time (template) dimensions is 1 (RowsAtCompileTime or ColsAtCompileTIme).

With this code (the second line):

	h5::write(fd,"vec-default",Eigen::VectorXf::LinSpaced(10,0,9).eval());
	h5::write(fd,"vec-explicit",Eigen::VectorXf::LinSpaced(10,0,9).eval(),h5::current_dims{10},h5::count{10});

I was able to save as 1D array :)

GROUP "/" {
   DATASET "vec-default" {
      DATATYPE  H5T_IEEE_F32LE
      DATASPACE  SIMPLE { ( 1, 10 ) / ( 1, 10 ) }
      DATA {
      (0,0): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
      }
   }
   DATASET "vec-explicit" {
      DATATYPE  H5T_IEEE_F32LE
      DATASPACE  SIMPLE { ( 10 ) / ( 10 ) }
      DATA {
      (0): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
      }
   }
}
}

@steven-varga
Copy link
Owner

Great! This also get's simpler in the upcoming version... (erroneously the h5::count is not computed automatically from the object, the h5::current_dimension is OK )

The first dataset is a rowvector, shape encoded; the second one is a rank 1 object -- no difference in performance; no 'standards' to adhere to :(

As for the arrays, vectors and ...; I am working on providing a flexible mechanism to facilitate inter library format. then some other time get the crowd involved and see how to save data with good characteristics such that reading it back from other systems such as Julia, Python, R ... major C++ linalg, ... we get similar result. (just think about row major column major differences and their efficient mapping, or sparse matrix representations... )

To coordinate such thing and make it happen is a serious undertaking ... In any event, am having a C++ presentation for the Toronto user group -- most likely online -- : if you have interest in attending will give you a shout, and you are free to ask away.

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