In recent years, newer CPUs do not have significantly higher frequencies any more. However, CPUs are being manufactured with more cores, enabling more than one operation (thread) at each instant. This can be very useful to speed up many aspects of processing and in particular image processing.
Most of the programs in Gnuastro utilize multi-threaded programming for the CPU intensive processing steps. This can potentially lead to a significant decrease in the running time of a program, see A note on threads. In terms of reading the code, you do not need to know anything about multi-threaded programming. You can simply follow the case where only one thread is to be used. In these cases, threads are not used and can be completely ignored.
When the C language was defined (the K&R’s book was written), using threads was not common, so C’s threading capabilities are not introduced there. Gnuastro uses POSIX threads for multi-threaded programming, defined in the pthread.h system wide header. There are various resources for learning to use POSIX threads. An excellent tutorial is provided by the Lawrence Livermore National Laboratory, with abundant figures to better understand the concepts, it is a very good start. The book ‘Advanced programming in the Unix environment’258, by Richard Stevens and Stephen Rago, Addison-Wesley, 2013 (Third edition) also has two chapters explaining the POSIX thread constructs which can be very helpful.
An alternative to POSIX threads was OpenMP, but POSIX threads are low level, allowing much more control, while being easier to understand, see Why C programming language?. All the situations where threads are used in Gnuastro currently are completely independent with no need of coordination between the threads. Such problems are known as “embarrassingly parallel” problems. They are some of the simplest problems to solve with threads and are also the ones that benefit most from them, see the LLNL introduction259.
One very useful POSIX thread concept is
pthread_barrier
. Unfortunately, it is only an optional feature in
the POSIX standard, so some operating systems do not include it. Therefore
in Implementation of pthread_barrier
, we introduce our own
implementation. This is a rather technical section only necessary for more
technical readers and you can safely ignore it. Following that, we describe
the helper functions in this header that can greatly simplify writing a
multi-threaded program, see Gnuastro’s thread related functions for
more.
Do not let the title scare you! The two chapters on Multi-threaded programming are very self-sufficient and do not need any more knowledge than K&R.
GNU Astronomy Utilities 0.23 manual, July 2024.