1
$\begingroup$

This question already has an answer here:

I am following a course of computational material physics. The professor uses fortran to code and uses lapack to solve eigenvalue problems. So far I just know c++. There is an equivalent library that make the same as LAPACK but in c++? Which is the best one ? Should I prefer to use fortran rather than c++ for my scope? Thank you

$\endgroup$

marked as duplicate by Richard, GertVdE, Anton Menshov, LedHead, Bill Greene Mar 13 at 0:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ You should prefer C++ of you ever intend to code outside of a legacy academic context. It's much more widely used in other domains and most new scientific software is written in it. However, Fortran does linear algebra style calculations well and is still used in style scientific applications, especially if there's a large legacy code base. $\endgroup$ – Richard Mar 11 at 19:56
  • 1
    $\begingroup$ @Richard To be fair, there is a reason, apart from legacy code, why Fortran is still used. It's performance is great due to the absence of pointer aliasing. It won't hurt to know Fortran and C++. $\endgroup$ – Nox Mar 11 at 20:37
  • 1
    $\begingroup$ Fortran has 9.5k questions on StackOverflow versus 608k questions for C++. SO Jobs shows 1 offering for Fortran and 534 for C++. It wouldn't hurt to know both, if knowing was free. But the benefit of Fortran has to be balanced against the cost (and difficulty) of learning a specialized tool with limited career prospects versus developing a deeper knowledge of a more general tool with similar performance characteristics. Unless OP already knows C++ well or explicitly plans on a career working with Fortran, I couldn't advise them to invest time on it. $\endgroup$ – Richard Mar 11 at 21:48
1
$\begingroup$

Definitely use C++ and Eigen. Reasons:

  • Sounds like it fits best for your case.
  • Eigen beats LAPACK using optimization flags (-O3) and a good compiler (GCC, Clang). At least for most tests I recently performed (dense linear algebra)
  • Once you debug your application use the -ffast-math compiler flag for a huge speedup, but test your output to see if it remains correct. I've noticed this is specially useful with Eigen.
  • C++ is an actively evolving high-performance language. Eigen takes advantage of its advanced features to optimize your code and make most things compile-time and SIMD/vectorized.
  • Virtually the only disadvantage of Eigen over LAPACK is that LAPACK has a standardized API. You can switch to another LAPACK library for higher performance in some cases.
  • Still, keep an eye on LAPACK for any routines from Eigen that are not serving you well.
  • Many production code in my field (computer vision, augmented reality, etc) that I've seen is using Eigen, and previously used LAPACK.
  • You can always use LAPACK as well with your C++ code when you want to follow your professor more closely, and compare with Eigen to be a good student. But if you just wanted to follow the professor, without regard to the best alternative, you might as well just learn Fortran.
  • See this: How does Eigen compare to BLAS/LAPACK

That said, Fortran can be simpler and faster than C++ in common usage. See the beginning of this video and the first answer to this stackoverflow post.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.