Bug in simple parallel for loop

General OpenMP discussion

Bug in simple parallel for loop

Postby khuffenb » Wed May 12, 2010 8:13 am

I cannot figure out why this code produces inconsistent results, sometimes skipping steps in the inner loop. Maybe a simple bug, but I can't find it. I appreciate any input.

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

int main(int argc, char *argv[]) {
  int N = 4;
  int i,j;

  // Let's try a loop
#pragma omp parallel for
  for (j=0;j<N;j++) {
    for (i=0;i<N;i++) {   
      printf("id: %d; ij: %d %d; total: %2d, N: %d\n", omp_get_thread_num(), i, j, j*N+i, N);
    }
  }

  return 0;
}


I compile with: gcc -fopenmp -o test1 test1.c
using gcc version 4.4.3 20100127 (Red Hat 4.4.3-4) (GCC).

Sometime the output is normal, but often and unpredictably skips elements in the inner loop:

Code: Select all
$ ./test1 | sort -n -k7
id: 0; ij: 0 0; total:  0, N: 4
id: 0; ij: 2 0; total:  2, N: 4
id: 0; ij: 3 0; total:  3, N: 4
id: 0; ij: 0 1; total:  4, N: 4
id: 0; ij: 1 1; total:  5, N: 4
id: 1; ij: 0 2; total:  8, N: 4
id: 1; ij: 1 2; total:  9, N: 4
id: 1; ij: 2 2; total: 10, N: 4
id: 1; ij: 3 2; total: 11, N: 4
id: 1; ij: 0 3; total: 12, N: 4
id: 1; ij: 1 3; total: 13, N: 4
id: 1; ij: 2 3; total: 14, N: 4
id: 1; ij: 3 3; total: 15, N: 4


Here (i,j) = (1,0); (2,1); and (3,1) are missing. Which steps are skipped and by which threads varies in subsequent runs.
khuffenb
 

Re: Bug in simple parallel for loop

Postby ftinetti » Wed May 12, 2010 9:20 am

Try adding

private(j, i)

to the parallel for.
ftinetti
 
Posts: 603
Joined: Wed Feb 10, 2010 2:44 pm

Re: Bug in simple parallel for loop

Postby khuffenb » Wed May 12, 2010 9:33 am

This does seem to solve the problem. Thanks!

So I guess the issue was the i-loop in one thread was changing the i-value in the other, thus causing the loops to increment strangely.
khuffenb
 

Re: Bug in simple parallel for loop

Postby ftinetti » Wed May 12, 2010 10:20 am

Right, the only variable "assumed" as private is the one of the for loop distributed among threads. Other variables seem to be shared by default, I didn't check the specification/s, but maybe that is implementation dependent.
ftinetti
 
Posts: 603
Joined: Wed Feb 10, 2010 2:44 pm

Re: Bug in simple parallel for loop

Postby ejd » Thu May 13, 2010 6:02 pm

The answer to the question is in the OpenMP V3.0 spec, section 2.9.1.1 Data-sharing Attribute Rules for Variables Referenced in a Construct. Basically there are three types of variables: predetermined, explicitly determined, or implicitly determined. The code has no explicitly determined variables (i.e., variables listed in a data sharing attribute clause - private, shared, default, etc. - on the parallel for). That leaves predetermined variables which are covered by a number of rules - but only one applies:
• The loop iteration variable(s) in the associated for-loop(s) of a for or parallel for construct is(are) private.

This rule makes the variable "j" private.

The rest of the variables are implicitly determined and are covered by the following rules:
Variables with implicitly determined data-sharing attributes are those that are referenced in a given construct, do not have predetermined data-sharing attributes, and are not listed in a data-sharing attribute clause on the construct.

Rules for variables with implicitly determined data-sharing attributes are as follows:
• In a parallel or task construct, the data-sharing attributes of these variables are determined by the default clause, if present.
• In a parallel construct, if no default clause is present, these variables are shared.

As you can see, this makes the rest of the variables ("i" and "N") shared.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 4 guests