21
$\begingroup$

I am looking for books or articles, or blog-posts, or any published material in general, that address specifically the uses of C++ modern features (move semantics, the STL, iterators, lazy evaluation, etc.) in scientific computing. Can you suggest any?

I think that these new features will make it easier to write efficient code, but I haven't found real examples. Most references I've read are about generic uses of C++, and do not contain examples of scientific computing. So I am looking for examples (do not have to be production code examples, just pedagogical examples, at the level of, say, Numerical Recipes) of scientific computing code using C++ modern features.

Note that I am not asking about libraries that use these features. I am asking about articles/books/etc explaining how I can exploit these features in scientific computing.

$\endgroup$
  • 2
    $\begingroup$ Are you asking about "modern" in the sense of what is understood to be the best practice nowadays (vs 20 years ago), or "modern" in the sense of specifically C++11/14? $\endgroup$ – Kirill Jul 24 '15 at 18:52
  • 2
    $\begingroup$ @Kirill I guess it's both. Mostly something that uses C++11/14, but following best practices. $\endgroup$ – becko Jul 24 '15 at 19:12
11
$\begingroup$

Two examples of libraries that use modern C++ constructs:

  • Both the eigen and armadillo libraries (linear algebra) use several modern C++ constructs. For instance, they use both expression templates to simplify arithmetic expressions and can sometimes eliminate some temporaries:

http://eigen.tuxfamily.org

http://arma.sourceforge.net/

http://hpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf (presentation on expression templates in Armadillo)

  • The CGAL library (computational geometry) uses many modern C++ features (it heavily uses templates and specializations):

http://www.cgal.org

Note:

modern C++ constructs are very elegant and can be very fun to use. It is both a strong point and a weakness: when using them, it is so tempting to add several layers of templates / specializations / lambdas that in the end you sometimes get more "administration" than effective code in the program (in other words, your program "talks" more about the problem than describing the solution). Finding the right balance is very subtle. Conclusion: one needs to track the evolution of the "signal/noise" ratio in the code by measuring:

  • how many lines of code in the program ?
  • how many classes/templates ?
  • running time ?
  • memory consumption ?

Everything that increases the first two ones may be considered as a cost (because it may make the program harder to understand and to maintain), everything that decreases the last two ones is a gain.

For instance, introducing an abstraction (a virtual class or a template) can factor code and make the program simpler (gain), but if it is never derivated / instanced once only, then it introduces a cost for no associated gain (again it is subtle because the gain may come later in the future evolution of the program, therefore there is no "golden rule").

Programmer's comfort is also an important factor to be taken into account in the cost/gain balance: with too many templates, compilation time may increase significantly, and error messages become difficult to parse.

See also

To what extent is generic and meta-programming using C++ templates useful in computational science?

$\endgroup$
  • 2
    $\begingroup$ Same goes for Armadillo as well as Eigen, no? $\endgroup$ – dr.blochwave Jul 27 '15 at 13:58
  • $\begingroup$ Yes, you are right (I just checked in Armadillo's documentation, I'm less familiar with it). I'm editing the post. $\endgroup$ – BrunoLevy Jul 27 '15 at 14:03
  • 1
    $\begingroup$ Longer compilation times are another cost worth mentioning. $\endgroup$ – Kirill Jul 28 '15 at 8:46
  • $\begingroup$ @Kirill, yes good point (edited the post to mention it), thanks. $\endgroup$ – BrunoLevy Aug 31 '15 at 7:14
10
$\begingroup$

I would suggest taking a look at Deal.II. It uses the STL, it's own iterators, shared pointers, etc.

The various linear solvers can use the various matrices because of how it was designed. I haven't come across any use of move semantics, but that doesn't mean they aren't there. Here is a link.

$\endgroup$
  • $\begingroup$ Also OpenFOAM -- pretty extensive use of templates. $\endgroup$ – tpg2114 Jul 24 '15 at 17:34
  • 1
    $\begingroup$ deal.II doesn't use move semantics (because it refrains from using C++11 language features -- but it uses C++11 library features for which there are replacements in BOOST for older compilers). It uses pretty much every other C++ language feature, though, including all of the ones asked in the original question. $\endgroup$ – Wolfgang Bangerth Jul 25 '15 at 3:16
  • $\begingroup$ It seems not easy to look at a large library in details. $\endgroup$ – Michael Feb 5 at 14:55
7
$\begingroup$

The HPX library makes heavy use of a range of C++11 features such as move constructors and is also aiming to be a complete implementation of N4409 (Working Draft, Technical Specification for C++ Extensions for Parallelism).

They have a list of publications on their site which includes a number of example of using the library to speed up scientific computation. There is also so interesting discussion of the library and it's use of modern C++ in this CppCast episode.

$\endgroup$
  • $\begingroup$ Welcome to scicomp.se! If you add links or cite some articles/books/blog-posts that discuss this library for scientific computation, I'll happily upvote your Answer! $\endgroup$ – hardmath Jul 25 '15 at 3:00
5
$\begingroup$

I suggest taking a look at Scientific and Engineering C++: An Introduction with Advanced Techniques and Examples by Barton and Nackmann.

The fact that this book was published in 1994 makes it appear to violate your criterion of "modern techniques." However, Barton and Nackmann were on the cutting edge of what was possible with C++ templates at the time and the innovative techniques they devised for achieving good performance are still used in the latest C++ class libraries.

$\endgroup$
4
$\begingroup$

Besides deal.ii (which was already suggested here) you can also have a look at the Dune library which makes extensive use of some advanced C++ features like template metaprogramming, iterator ranges, smart pointers, and so on. There is also a recent preprint by Joachim Schöberl, which comments on the use of C++ 11 features, like for instance lambda functions, for simplifying the implementation of the finite element methods in NGSolve. Boost also has some libraries related to scientific programming like uBLAS, Graph, etc. I guess in most of these libraries you will find good examples of modern C++ usage. However, be aware that you may encounter also bad examples of using advanced/modern C++. In some cases, while reading the code/documentation, I got the feeling that occasionally things are vastly over-generalized for the sake of showing off advanced skills like TMP, where for 99% of all potential applications a more straightforward implementation would also do the job.

$\endgroup$
3
+50
$\begingroup$

The book "Guide to Scientific Computing in C++" by Pitt-Francis & Whiteley was written to answer exactly this sort of thing (use of STL, iterators etc.) it is available via Amazon, or as an e-Book from publisher.

Disclosure - I work in the same research group as the authors, but still think it's a very good resource for this!

$\endgroup$
1
$\begingroup$

I think that this book is perfect for you, as it did for me: Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers (C++ In-Depth) by Peter Gottschling especially if used in conjunction with Programming Principles and Practice Using C++ 2nd Edition Bjarne Stroustrup. The inventor of C++ himself. Both should provide a solid ground to stand on.

$\endgroup$
0
$\begingroup$

The Blaze library for linear algebra makes heavy use of C++14 in the form of deduced and trailing return types. Other modern C++ features in use are constexpr, alias templates, and a whole lot of template metaprogramming with expression SFINAE.

You can also use initializer lists for your vectors and matrices, e.g.

blaze::DynamicVector<int> x{ 4, -1, 3 };

For more details see their getting started page.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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