Skip to content

Latest commit

 

History

History

PWD010

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

PWD010: Incorrect sharing in OpenMP parallel region

Issue

A variable is being incorrectly shared in the OpenMP datascoping clauses.

Actions

Change the data scope of the variable from shared to private.

Relevance

Specifying an invalid scope for a variable may introduce race conditions and produce incorrect results. For instance, when a variable is written from parallel threads and the specified scoping is shared instead of private.

Code example

In the following code no variable is privatized:

void example(int **result, unsigned rows, unsigned cols) {
  int i, j;

  // j is implicitly shared and it should be private!
  #pragma omp parallel for shared(result)
  for (i = 0; i < rows; i++) {
    for (j = 0; j < cols; j++) {
      result[i][j] = 0;
    }
  }
}

This introduced a data race due to j being shared among threads. It should be privatized:

void example(int **result, unsigned rows, unsigned cols) {
  int i, j;

  // j is implicitly shared and it should be private!
  #pragma omp parallel for shared(result) private(j)
  for (i = 0; i < rows; i++) {
    for (j = 0; j < cols; j++) {
      result[i][j] = 0;
    }
  }
}

Related resources

References