[Tutor] On learning Fortran and C++ for scientific computing

Laura Creighton lac at openend.se
Sat Apr 11 18:56:32 CEST 2015


These days, most important scientific libraries are wrapped so that you
call call them directly from python. Google for "python bindings <name of
the library you want>" and see if you get any hits.  If you have a library
that doesn't have python bindings, you can probably make them.  Start
reading here:
http://intermediate-and-advanced-software-carpentry.readthedocs.org/en/latest/c++-wrapping.html
http://scicomp.stackexchange.com/questions/2283/which-package-should-i-use-to-wrap-modern-fortran-code-with-python

to see about wrapping C++ and Fortran code.

But chances are somebody has already done it.
And for astronomical calculations, I just use pyephem.
http://rhodesmill.org/pyephem/index.html

It is difficult, and usually pointless to argue with somebody who prefers to
use Fortran or C++ about using some other language.  People like what they
are used to.  Moreover, in the case of Fortran and C++, these languages are
significantly harder to use than Python, where harder, in this case,
means -- the code you write is more prone to errors.

Also, it is very easy to write inefficient programs in C++ or Fortran,
and scientists, who are not professional programmers are extremely prone
to doing this.  So while a Fortran or C++ library may be fast (if the
library author knew what he or she was doing, or the community has fixed
inefficiencies over the years) that doesn't mean that your own C++ or
Fortran code is guaranteed to be fast.

And, of course, if it takes you 5 times as long in your personal time to
write the code, do you actually care how fast the result runs?  For very
long running calculations, where time is money, the answer may be yes.
But if all you want to know is where Uranus is going to be next Thursday
so you can point your telescope at it, not a bit.

If you need something that runs faster than the standard version of
python, you may be able to get it with numpy and the other scientific
tools here: http://www.scipy.org/ or with pypy http://speed.pypy.org/ .
The number of problems where you can legitimately claim that you badly
need the speed that C++ and Fortran provide is shrinking all the time,
as machines grow faster.  Plus, in modern computing the new challenge is
to use multiple cores.

It's a very hard problem, and all the old C++ and Fortran libraries
were created with the assumption that there is only one core.  Right now
we don't actually know how to use multiple cores efficiently -- it is hot
research in computer science right now, with no particularly good answers.
But once we figure out how to do this, all the old C++ and Fortran
libraries are probably going to have to be rewritten with this in mind.
(A solution that doesn't require such re-writing would, of course be
highly desirable, but things are not looking very good on that front.)

If your real question is 'would it be desirable to learn C++ and Fortran
from a historical perspective, to get an understanding of how important
computational libraries in my field are structured', then I am the wrong
person to ask.  I knew both of these languages before there was a Python.
I think learning these langauges will indeed make understanding the
old libraries easier, but _using_ these languages to do real work isn't
needed.  Or rather, if you are one of the people who needs to use them,
then you would already know that you are, and why, and wouldn't be asking
the question.  The world is full of things to learn, so you need to do
your own prioritisation of Fortran vs everything else you haven't learned
yet. :)

Best of luck,
Laura Creighton

p.s. I gave this topic a more descriptive subject line than 'Hi' but it should
still thread with the original.


More information about the Tutor mailing list