OPEN CATALOG

Best practices for performance

PWR024: Loop can be rewritten in OpenMP canonical form

Issue #

The loop is currently not in OpenMP canonical form but it can be made OpenMP compliant through refactoring.

Relevance #

OpenMP requires a loop to fulfill some requirements: it must be a for loop, have single statements in its loop headers, use only one iterator variable which must be an integer and use only integer increments. Failing to fulfill any of these requirements inhibits the loop parallelization with OpenMP. In many cases, a simple rewrite can fix this.

Actions #

Rewrite the loop to make it an OpenMP canonical form using single statements involving only the iterator variable in the loop header.

Code example #

The following loop is not in OpenMP canonical form because the iterator is initialized outside the loop:

int example(int **A, int n, int m) {
   int i = 0;
   for (; i < n; i++) {
       A[i] = m;
   }
}

This can be easily fixed by moving the iterator initializer inside the for statement.

int example(int **A, int n, int m) {
   int i;
   for (i = 0; i < n; i++) {
       A[i] = m;
   }
}

References #

Canonical Loop Form – OPENMP API Specification: Version 5.0 November 2018