In the first article of the series on latest best practises in parallel programming in C, we pay attention to current features and limitations of compilers that support the OpenMP standard. We present the following two case studies. First, we suggest to declare program variables at the beginning of the smallest loop scope, instead of declaring them at the beginning of the program or function/procedure. Second, we study how to overcome that struct variables cannot be passed as arguments to OpenMP directives and clauses.
1. Declare program variables at the innermost loop scope
Modern programming languages allow to declare program variables at the beginning of the function/procedure, but also in the middle of the source code, for instance. From the point of view of parallelization, we are very interested in loops because they typically are the most time-consuming parts of the program. Thus, we address the following questions:
- When is it valid to declare a program variable within a loop scope, i.e., at the beginning of a loop body?
- What are the advantages of this programming practice for the parallelization of sequential codes with OpenMP?
For illustrative purposes, consider the source code of the function parallware__root() shown below. It mainly consists of a loop for_i that computes the sum of a set of values given by auxiliary variables aux and matrix. The OpenMP-enabled parallel implementation computes a parallel reduction operation on variable reduction, privatizing the auxiliary variables aux and matrix.
In large real programs, there may be a large list of privatizable variables that may be difficult to maintain. So, how can we simplify the OpenMP pragmas and avoid programming errors when deciding what variables have to be declared as private within the parallel region?
The answer is: Declare loop-level temporary variables within loop scopes, i.e., at the beginning of the loop body. A variable (either scalar, array,…) is said to be temporary within a loop if it is fully initialized at the beginning of each loop iteration, and there is not any use of the variable between the loop entry and the initialization of the variable. In the example source code shown above, the variables j, k, aux and matrix are temporary in the loop for_i. The equivalent OpenMP-enabled source code shown below declares temporaries within the loop body, which leads to simpler OpenMP pragmas because loop-level temporaries are automatically assumed to be thread-private.
Finally note that in the last example, an OpenMP clause PRIVATE(i,j,k,aux,matrix) is not necessary and, indeed, it is not valid because the variables don’t exist yet and OpenMP compiler do not compile the source code successfully.
2. Avoid the use of struct variables in OpenMP clauses
OpenMP does not support a field of a struct in a clause. Thus, if the struct variable is a loop-level temporary you can declare it inside of the corresponding loop scope. Otherwise, it is a good programming practice to declare each struct field as an independent variable.
In the following example source code we can see a struct variable named a declared inside the external loop for_i. The OpenMP code compiles successfully.
In the following example, the struct variable a is declared at the beginning of the procedure, outside the loop for_i. Within the loop for_i the field a.x is private in the scope of the loop. However, there is also a reduction on the field a.y, which is not loop-level temporary and thus cannot be privatized. Thus, this source code raises a compilation error due to lack of support for struct variables in OpenMP clauses.
A valid solution is to declared independent variables for struct fields and use them within the loop body. Such an implementation is shown below and compiles successfully as OpenMP clauses are free of struct variables.
It’s a good practice to follow these recommendation from the beginning when doing parallel programming in C with OpenMP. In addition, following these programming suggestions you will naturally increase the effectiveness of Parallware to automatically discover parallelism in your sequential code and auto-generating equivalent OpenMP-enabled parallel code.
Have you already seen the live demo of Parallware? If not, just remember that Parallware guarantees correctness and performance in your OpenMP-enabled parallel code. When you are starting to develop your code, it might be beneficial to compile it with Parallware as it automatically checks many of the best programming practices in C presented in this articles.
If you like this post, please share!