Skip to content

Readable CPlusPlus

Matt Norman edited this page Oct 12, 2019 · 29 revisions

What does readability mean for scientific codes?

C++ performance portable codes (Kokkos, RAJA, SYCL, HC) have a reputation for being "unreadable." In some cases, this reputation is justifiable, but I think there is a lot of sociology wrapped up in this statement. I also think that C++ portable codes can be written in a way that is easily read by traditional scientific code developers. A seasoned C++ programmer (which I certainly am not) will probably look at some of these codes and say they are trivially readable. Then again, C++ seems to have at least three main flavors:

  1. C with ampersands, basic type templating, and non-inheriting classes
  2. Complex inheritance
  3. Complex templates

Readability constraints only apply to code that is actively being developed by scientists. In some cases, projects can abstract these constructs away from the users. In many cases, though, the code cannot be easily abstracted away in a library because it is highly custom to the process being modeled. I'm talking about code that scientific developers have to be able to develop, read, and modify directly.

While readability is not an objective metric, for many science and engineering code developers, we are used to relatively simple looping structures and abstractions. The code is usually located in a set of adjacent loops inside simple functions. The call tree is straightforward, if a bit deep at times. If inheritance is used, it is straightforward, shallow, and clear. I would argue the first flavor of C++ in the previous numbered list is the style best suited for these kinds of developers. Departures from this style should be minimized and decorated with clear comments.

  1. I need to be able to find the code
  2. I need to be able to recognize a loop body when I see one
  3. I need to know what the effective looping is doing
  4. I need to have recognizable data structures

Everyone Needs Training

There's a natural tendency for each camp to point the finger at another and say, "they need training." A C++ metaprogramming expert will probably say, "scientists just need to study C++ and become more modern." Scientists will, in turn, say, "No, you need to write more readable code." As with most things, I think it makes most sense for the two to meet in the middle. C++ experts need to better understand what a domain scientists considers readable. Domain scientists need to take a few steps to understanding how to map what they're used to seeing onto a new context. It might be a difficult task, but it's not impossible.

For instance, C++ portability approaches tend to look like CUDA in the sense that they expose the code as a kernel that operates on a single thread. Domain scientists will find this confusing until we show them that it's actually identical to the code you find inside a set of loop nests. The goal for domain scientists is to take what they find confusing and express it in terms they're familiar with. The goal for C++ experts is to help them frame their code in simpler expressions that come closer to what a domain scientist is used to seeing.

Some Common Sense Steps Toward Readability

Clone this wiki locally