-3
$\begingroup$

I want to understand how important it is that I learn to write decent C++ code, for the sake of a career in scientific computing (FEM, fast solvers, etc). By this I mean that I want to acquire skills that are in demand and that are reusable.

From what I hear, C++ is difficult to master, and I imagine that once I know how to do good C++ code, then it will be easier to switch to some modern fortran version, more than the other way around. But I am no expert, so I ask the question to the community.

What major difficulties are there in learning C++ after fortran>=90 (and the other way around)?

Moreover, I am currently developing in fortran77.

What about the question above with fortran77 as a starting point?

$\endgroup$
6
  • 6
    $\begingroup$ There are a number of C++ vs Fortran questions on this forum already. Have you looked at those for answers to at least some of your questions? $\endgroup$ Commented Jun 12, 2023 at 20:37
  • 1
    $\begingroup$ Here's an example: scicomp.stackexchange.com/questions/304/c-vs-fortran-for-hpc $\endgroup$ Commented Jun 12, 2023 at 20:38
  • $\begingroup$ @WolfgangBangerth I have seen them but I couldn't specifically find a place where one talks about the difficulties in going from F to C or the other way around, and not in detail. $\endgroup$
    – Lilla
    Commented Jun 14, 2023 at 18:30
  • 1
    $\begingroup$ There may be two issues for Fortran programmers that hold them back: (i) Fortran as a language was designed before modern programming languages came around. For example, it does not generally operate on the "keywords" principle, and every statement has its own individually-designed syntax. The syntax of C and all other modern languages just looks and feels different, and so there is a mismatch for experts in Fortran and C to learn the respective other language. $\endgroup$ Commented Jun 14, 2023 at 23:09
  • 2
    $\begingroup$ (ii) Fortran is a vastly smaller language. For example, the core language descriptions for Fortran 2018 (~450 pages) and C++20 (incidentally also ~450 pages) are almost equally long. But C++23 then has another ~1150 pages of standard library whereas Fortran 2018 does not actually have any standard library. It just takes anyone quite a long time to really learn what C++ has to offer. Conversely, it will take anyone a long time to learn how much stuff one actually has to write oneself in Fortran because the language does not provide anything. $\endgroup$ Commented Jun 14, 2023 at 23:19

2 Answers 2

2
$\begingroup$

I'd generally prefer C++ for the simple reason that it is better suited to writing complex and dynamic data structures, and because it is used much more outside the domain of scientific computing.

That said, a legitimate case could be made for Fortran, which was designed to max out performance (it is actually hard to write slow code in Fortran). Fortran also has native support for constructs you may find useful in scientific computing - multidimensional arrays, complex numbers, and also stuff such as array comprehensions (though C99 added native complex numbers).

Whatever you pick, popular HPC APIs such as OpenMP and OpenMPI have support for C/C++ and Fortran. There's also CUDA Fortran, besides the better-known CUDA C/C++, if you're into GPGPU. Ultimately, the language is more a medium for expressing your ideas. Pick one, stick to it, and use it as a medium to focus on scientific computing concepts (numerical methods, algorithm design, scalability analysis etc.).

$\endgroup$
4
  • 7
    $\begingroup$ A point that has been made many times on this forum is that the claims that Fortran is faster than C/C++ may have been true in the 1990s when C++ compilers were new, but that that has not been the case for a long time. $\endgroup$ Commented Jun 13, 2023 at 16:02
  • 6
    $\begingroup$ Separately, while OpenMP for Fortran and CUDA Fortran exist, those who try to use them pretty consistently report that there are many compiler bugs and that these bugs are not or are only very slowly fixed -- primarily because these extensions are not widely used, and they don't get to the top of compiler authors' priority lists. $\endgroup$ Commented Jun 13, 2023 at 16:47
  • $\begingroup$ Valid point from @WolfgangBangerth , but I mention it given the amount of bad C/C++ I've seen. You need a bit more work to write efficient C/C++ (register, restrict etc.). Speaking of restrict, the example I had in mind was that C/C++ has an aliasing problem with pointers, which stops compilers from doing some optimisations. I have primarily used OpenMP and CUDA C/C++, so I cannot say about the other comment (bugs, 'second-class' status in fixes) but they're definitely valid concerns the OP should consider. $\endgroup$ Commented Jun 15, 2023 at 20:48
  • $\begingroup$ register has not been used by compilers in decades. It is simply ignored. In fact, in newer C++ standards, register is no longer accepted by the compiler. restrict is the same: The compiler accepts it, and then ignores it in C++. In other words, neither is used in modern C++ codes these days. $\endgroup$ Commented Jun 16, 2023 at 14:43
2
$\begingroup$

In the past I was a FORTRAN77 programmer. Currently I am using C++. I would like to add some pros and cons that concern me.

Pros:

The size of the C++ user community is larger than the FORTRAN community, so there is a lot of useful information about C++ on the Internet. The amount of information about FORTRAN on the Internet is a bit less compared to C++ information.

A notable feature of C++ is that there are many useful data structures such as vector, map, ..., together with the object-oriented class concept, which are already predefined in C++. These data structures and class features make coding easier and more fun.

Cons:

I suppose you probably want to get into HPC programming in the near future. But, for example, in the article you see the following statement: "The C++ language bindings to MPI had been deleted since MPI 3.0 (circa 2009) because it reportedly added only minimal functionality over the existing C bindings relative to modern C++ practice while incurring significant amount of maintenance to the MPI standard specification." If you use a lot of useful data structures and classes, you may find that useful features of C++ are sometimes barriers to HPC programming.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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