Unsigned int Loop Control Variable for C and C++

Unsigned int Loop Control Variable for C and C++ in OpenMP 3.0

In OpenMP 3.0, the control variable of a loop associated with a for directive can be of signed or unsigned integer type. In OpenMP 2.5, only signed integer type was allowed.  SeeSection 2.5.1 on p. 38 of the OpenMP 3.0 Specification.

Example (C/C++):

#include <stdio.h>
#include <omp.h>

#define N 20

int main ()
{
  float a[N];

  omp_set_dynamic(0);
  omp_set_num_threads(10);

  for (int i=0; i<N; ++i) {
       a[i] = i;
  }

#pragma omp parallel shared(a)
  {
    #pragma omp for
    for (unsigned int i=0; i<N; ++i) {
         a[i] = a[i] * 2.0;
    }

    #pragma omp for
    for (unsigned long j=0; j<N; ++j) {
         a[j] = a[j] * 2.0;
    }
  }

  for (int i=0; i<N; i++) {
       printf ("%i: %f\n", i, a[i]);
  }
}
 

% cc -xopenmp -xO3 test.c 
% a.out
0: 0.000000
1: 4.000000
2: 8.000000
3: 12.000000
4: 16.000000
5: 20.000000
6: 24.000000
7: 28.000000
8: 32.000000
9: 36.000000
10: 40.000000
11: 44.000000
12: 48.000000
13: 52.000000
14: 56.000000
15: 60.000000
16: 64.000000
17: 68.000000
18: 72.000000
19: 76.000000
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact