This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Working hstack and vstack for C++1y

Tags: None
(comma "," separated)
eacousineau
Registered Member
Posts
15
Karma
0
OS

Working hstack and vstack for C++1y

Mon Apr 24, 2017 6:36 am
I have been tinkering with initializer lists to try and see if there can be slightly more expressive initializations (see https://forum.kde.org/viewtopic.php?f=74&t=123881&p=375217#p375217).

I ran up against a wall that initiailizer_lists must be fully homogeneous, which sucks when you want to try and leverage expression templates.
I was curious to see if tuples could be used, and it appears they can. I've implemented a prototype here:

https://github.com/EricCousineau-TRI/repro/blob/f19ce9f90d57b05c0b003c161dcf8b6232c5118d/eigen_scratch/matrix_hstack_vstack_xpr_tpl.cc
https://github.com/EricCousineau-TRI/repro/blob/f19ce9f90d57b05c0b003c161dcf8b6232c5118d/eigen_scratch/matrix_hstack_vstack_xpr_tpl.output.txt

The functionality I was driving for was ragged sub-expression/sub-block concatenation, which may not work for the existing initializer setup except without temporaries, all while maintaining the elegance of EIgen's ability to work with different matrix types.

Example from the code (from prior post):
Code: Select all
MatrixXs X;
X << vstack(
    hstack( vstack(A, B), C, vstack(D, E) ),
    hstack( F, vstack(hstack(s1, s2), hstack(s3, s4)) )
);


Output:
Code: Select all
X:
A[0] A[1] C[0] D[0] D[1] D[2]
B[0] B[1] C[1] E[0] E[1] E[2]
F[0] F[2] F[4] F[6]   s1   s2
F[1] F[3] F[5] F[7]   s3   s4


Note that this does not require the size to be known beforehand. (It makes things slower, but may provide more convenience.)

I haven't really checked the performance hit in compilation / evaluation time, but my fingers are crossed that they haven't prevented expression templates from delaying their evaluation until when needed. Additionally, the implementation uses some pretty hefty hacks, such as using the mechanics of std::apply borrowed from C++17.

I'll try and post this to the mailing list when I have a chance, if anything to see if (a) it is actually useful, and (b) if it could be incorporated as an experimental / unsupported C++1y feature.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Very nice work, this is very interesting and could help dealing with http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1420.

Does it require a very recent compiler with some c++17 features? It would be nice if c++11 would be enough.
eacousineau
Registered Member
Posts
15
Karma
0
OS
I had developed it under clang-3.9 -std=c++1y, and it does break under -std=c++11. However, I think it's possible to refactor if functors are permitted to have template parameters (which I believe should be permissible). I'll try it out in the next week or so and let you know.
Thanks!
eacousineau
Registered Member
Posts
15
Karma
0
OS
It does look to be possible in C++11:

https://gist.github.com/EricCousineau-T ... 72649d33a1 - can be applied on the original file, minus the name. Dirty dirty coding, but works as proof-of-concept.
(will push the actual commits soon, presently in a spot that I cannot push to GitHub)

I tested this on `clang-3.9 --std=c++11`, and `gcc-4.9 --std=c++11`, and they both compiled and ran identical to the C++1y implementation.

The prior C++1y implementation does not work in C++11, hence the changes :(
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
For the record, I've added a bug entry: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1433


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]