[Omp] VLAs in private clause

Marina Kraeva kraeva at iastate.edu
Wed Nov 5 09:57:50 PST 2003


Thank you all for your replies!
FYI - using a VLA object (not a parameter) in private clause works fine on 2
out of 3 platforms, but fails on IBM. There the object seems to be shared. See
the example below.

Also I recommend to ARB to better explain this case in API. For example it can
be mentioned in the section 2.7.2 of the C/C++ OpenMP API, where it says about
heap allocated memory being shared.
(Yes, I know that the API version 2.5 will be both for Fortran and C, so the
numeration will be different).

Marina.

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

int *check;

void func(int n, int m) {
   int b[n][m];

     #pragma omp parallel private(b) shared(check)
     {
          b[1][1]=omp_get_thread_num();
          #pragma omp barrier
          check[omp_get_thread_num()] = b[1][1];
     }
}

int main()
{
   int i, j;

   omp_set_dynamic(0);
   omp_set_num_threads(2);

   check = (int *)malloc(2*sizeof(int));
   for (i=0; i<2; i++)
         check[i] = -1;

   func(2, 2);

   for (i=0; i<2; i++){
       if (check[i] != i) printf("FAIL in thread %d b[1][1] was %d, should be
%d\n",i,check[i],i);
       else printf("in thread %d b[1][1] was %d, should be
%d\n",i,check[i],i);
   }

   return 0;
}
----------------------------------------------
Program output:
FAIL in thread 0 b[1][1] was 1, should be 0
in thread 1 b[1][1] was 1, should be 1



"Fedor V. Sergeev" wrote:

> On Tue, 4 Nov 2003, Shah, Sanjiv wrote:
>
> > KSG,
> >
> > This isn't quite correct.  VLA's are an ANSI C 99 feature, and
> > privatizing them is allowed, and it is the implementation's
> > responsibility to allocate the storage for them.
>
> Well, the testcase presented here is somewhat more tricky than that.
>
> Generally VLAs are objects (C standard notion) and thus it would be quite
> logical to privatize them as a single array object, reserving a space and
> having no problems to access it.
>
> Thus the testcase like this one:
>
> void func(int n, int m) {
>         int a[n][m];
>         #pragma omp parallel private(a)
>         {
>                 a[1][1]=1;
>         }
> }
>
> should work fine.
>
> However original testcase is
>
> void func(int n, int m, int a[n][m])
>
> and here we get a VLA parameter. According to c99 (ISO/IEC 9899:1999),
> "6.7.5.3 Function declarators" an array-type parameter is actually being
> adjusted to pointer-type.
>
> Thus "a" here is not int[n][m], it is int (*)[m], which is a pointer.
> Attempt to access privatized pointer can lead to disasters. Segmentation
> fault in Marina's case.
>
> That, btw, explains why things start working with firstprivate.
>
> regards,
>   Fedor.
>
> >
> > Marina, your example looks like legal OpenMP to me, and the compilers
> > you are using appear to not support the mix of OpenMP and ANSI C99
> > VLA's.  This isn't surprising, since the OpenMP spec isn't crystal clear
> > on this point.
> >
> > Regards,
> > Sanjiv
> >
> > -----Original Message-----
> > From: Omp-bounces at openmp.org [mailto:Omp-bounces at openmp.org] On Behalf
> > Of Kang Su
> > Sent: Monday, November 03, 2003 8:05 PM
> > To: Marina Kraeva; omp at openmp.org
> > Subject: Re: [Omp] VLAs in private clause
> >
> >
> > The problem with your code is that 'private' variables
> > have an indeterminate value.  Thus the address of 'a'
> > is pointing to "who knows where".  With 'firstprivate'
> > this is fixed due to the fact that 'firstprivate' will
> > construct the object.
> >
> > Also you use non-const array sizes, but I guess that
> > works on a lot compilers.
> >
> > Thanks,
> >
> > KSG
> >
> >
> > --- Marina Kraeva <kraeva at iastate.edu> wrote:
> > > Hi,
> > >
> > > I have a question on listing VLAs (Variable Length
> > > Arrays) as private.
> > > The following example fails on 3 different platforms
> > > (segmentation fault). One
> > > of the compilers gave a warning that "Variable "a"
> > > is used before it is
> > > defined".
> > > I don't understand where and how I was supposed to
> > > define this "a". If I list
> > > "a" as firstprivate (like in the example A.27 of the
> > > C/C++ OpenMP API 2.0), then
> > > everything works fine. Can anyone comment on this
> > > example?
> > >
> > > Thanks!
> > > Marina.
> > >
> > > ======================================
> > > #include <omp.h>
> > > #include <stdlib.h>
> > >
> > > int N = 10;
> > > int M = 10;
> > >
> > > void func(int n, int m, int a[n][m]);
> > >
> > > void func(int n, int m, int a[n][m]) {
> > >      #pragma omp parallel private(a)
> > >      {
> > >         a[1][1]=1;
> > >      }
> > > }
> > >
> > > int main()
> > > {
> > >    int i, j;
> > >    int a[N][M];
> > >
> > >    omp_set_dynamic(0);
> > >    omp_set_num_threads(2);
> > >
> > >    for (i=0; i<N; i++)
> > >        for (j=0; j<M; j++)
> > >             a[i][j] = 1;
> > >
> > >    func(N, M, a);
> > >
> > >    return 0;
> > > }
> > >
> > >
> > >
> > >
> > >
> > > _______________________________________________
> > > Omp mailing list
> > > Omp at openmp.org
> > > http://openmp.org/mailman/listinfo/omp_openmp.org
> >
> >
> > __________________________________
> > Do you Yahoo!?
> > Exclusive Video Premiere - Britney Spears
> > http://launch.yahoo.com/promos/britneyspears/
> >
> > _______________________________________________
> > Omp mailing list
> > Omp at openmp.org
> > http://openmp.org/mailman/listinfo/omp_openmp.org
> >
> > _______________________________________________
> > Omp mailing list
> > Omp at openmp.org
> > http://openmp.org/mailman/listinfo/omp_openmp.org
> >






More information about the Omp mailing list