![]() Registered Member ![]()
|
Dear Eigen2 folks,
Firstly, let me say that Eigen2 fills a tremendous niche and the developer team deserves our thanks. In fact, I am ready to jump into C++ and template from a stictly C world. Which leads me to my question. I would like to quickly get started using Eigen within C, as I begin to learn about C++/templates/expression templates etc. I just ordered a bunch of books and have started reading all I can find. I have read through all the Eigen documentation as well. It would really help me if somebody has a concrete example of wrapping a function written in C++ using Eigen in C and calling it from C. I have found answers to all the little questions in various FAQs, but seeing a complete concrete example still helps. I just need a limited subset of BLAS/LAPACK like functions for dynamic double matrices/vectors. (a) Create an Eigen dynamic matrix/vector MatrixXd/VectorXd object and Map my own double data array created in C. (b) A simple matrix expression wrapped as a function (DGEMM): C <- a*op(A)*op(B) + b*C. I am not sure if to make it efficient when called from C I need multiple specialized functions for each possible invocation: C = a*A*B + b*C; C = a*A.Transpose()*B + b*C; C = a*A*B.Transpose() + b*C; etc... (c) I am not sure if I complie C++/Eigen and C code simultaneously that my C library/application will get the full benefit of complie time optimizations that Eigen2 uses. My eventual goal is to develop everything using Eigen, but I have to start somewhere. Seeing a concrete demonstration will get me started. Thanking ~Russ |
![]() Registered Member ![]()
|
Hi,
Thanks for the kind words. I was curious to see how that could work anyway, so I gave it a try. Find attached to this post an archive, mix_eigen_with_c.tar.gz. To try it out, with GCC:
As you can see, it still requires linking the C program against libstdc++, that's definitely ugly, but fixable, it's just that I'm a noob with these things. Of course it's the binary_library that should be linked against libstdc++. I guess the first step is to make it an actual library instead of just an object file.
Yep, find this in the code.
I didn't add a exactly dgemm but I did matrix multiply and a matrix add functions. I guess you'll be able to generalize that.
Inside of each of the functions of the binary library, Eigen can do all its compile time optimizations. So your best interest is to implement large enough functions in this library, instead of implementing just elementary operations. But actually this is standard practice and the DGEMM that you mention does just that: instead of being just a matrix product, it does a little more to offer more optimization opportunities. FYI, we have the project of implementing a BLAS on top of Eigen, this is a start...
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
![]() Registered Member ![]()
|
Ah, now that I think about it, there is one more limitation to the performance of such a C binding library: all objects are created on the heap. This is negligible for a large enough matrix, but not for a lightweight object such as Map_MatrixXd which is just a light wrapper around an existing array. This also wouldn't be good if you wanted tiny matrices.
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
![]() Registered Member ![]()
|
Hm, thinking about it more, this really shows that it would be much better to have the structs like C_MatrixXd have the same sizeof() and alignment attributes as the Eigen types that they're representing, so that they can be created on the stack. This would allow good performance with tiny matrices (though one would still lose the benefit of expression templates).
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
![]() Registered Member ![]()
|
Hi!
First off, let me say that I really like Eigen, and I think it's a fantastic lib! It has really nice syntax, and all the flexibility (and more) that I need ![]() Best regards, -jonas-
|
![]() Registered Member ![]()
|
Thanks!
Yes, we know ![]() ![]()
I'd question that. In our experience, GCC has the best performance out there. But the GCC version is extremely important: use GCC >= 4.2, and ideally, use GCC 4.4. GCC 4.1 and older are very slow with Eigen. Make sure that you pass -O2 -msse2. Play with -DNDEBUG and -mfpmath=sse. I don't know what icpc's -fast does, but try gcc's -ffast-math.
... and with recent GCC properly used...
Yes, this is already the top entry on our todo, http://eigen.tuxfamily.org/index.php?title=Todo
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
![]() Registered Member ![]()
|
Sweet! And thanks a thousand! Part of the problem is probably my old g++ on OSX...
|
Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]