ThreadPrivate and extern Variables

General OpenMP discussion

ThreadPrivate and extern Variables

Postby prat777 » Sun Apr 01, 2012 6:58 am

Hi,
Iam new to OpenMP. Iam trying to parallelize a for loop which internally calls a function whose definition is in another file. Both files share a global variable. One file has extern declaration and other has actual declaration.

How to make the global variable private for each openmp threads?

I tried in the following way and its giving compilation error.

file1.c
-------
int a;
#pragma omp threadprivate(a)

int main()
{
#pragma omp parallel for
for(int i=0;i<n;i++)
SomeFunction(i);
}


file2.c
-------
extern int a;
#pragma omp threadprivate(a) //THROWING AN ERROR
SomeFunction(i)
{
//uses variable a
}

Thanks in Advance
Last edited by prat777 on Tue Apr 03, 2012 5:35 am, edited 1 time in total.
prat777
 
Posts: 3
Joined: Sun Apr 01, 2012 6:47 am

Re: ThreadPrivate and extern Variables

Postby ftinetti » Tue Apr 03, 2012 4:02 am

I think

Code: Select all
#pragma omp threadprivate(i) //THROWING AN ERROR


should be

Code: Select all
#pragma omp threadprivate(a)


HTH,

Fernando.
ftinetti
 
Posts: 558
Joined: Wed Feb 10, 2010 2:44 pm

Re: ThreadPrivate and extern Variables

Postby prat777 » Tue Apr 03, 2012 5:35 am

Hi Fernando,
Sorry for that.. its actually "a" only in my code.. not "i". I mistyped it while posting the thread.

Thanks.
prat777
 
Posts: 3
Joined: Sun Apr 01, 2012 6:47 am

Re: ThreadPrivate and extern Variables

Postby ftinetti » Tue Apr 03, 2012 11:44 am

I see. I do not have any problem using gcc 4.4.0 on Linux.

What compiler and OS are you using?

What's the actual error message?

Fernando.
ftinetti
 
Posts: 558
Joined: Wed Feb 10, 2010 2:44 pm

Re: ThreadPrivate and extern Variables

Postby prat777 » Tue Apr 03, 2012 10:54 pm

Hi Fernando,

Iam using Visual studio 2008 C++ compiler on Windows 7 32 bit OS.

The exact error message is
error C3053: 'a' : 'threadprivate' is only valid for global or static data items.

Thanks.
prat777
 
Posts: 3
Joined: Sun Apr 01, 2012 6:47 am

Re: ThreadPrivate and extern Variables

Postby ftinetti » Wed Apr 04, 2012 4:05 am

Hi again,

Maybe you should ask in some VS forum/to VS vendor ( 'a' is global)...

Just in case:
Code: Select all
// file1.c
// -------

int a;
#pragma omp threadprivate(a)

extern void SomeFunction(int);

int main()
{
  int i;

  a = -1;

  #pragma omp parallel for schedule(static) copyin(a)
  for(i=0; i<100; i++)
  {
    SomeFunction(i);
  }
}


Code: Select all
//file2.c
//-------

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

extern int a;
#pragma omp threadprivate(a)

void SomeFunction(int i)
{
    a++;

  if ((i == 0) || (i == 50))
  {
    printf("i = %d and a = %d on %d\n", i, a, omp_get_thread_num());
  }


  if ((i == 49) || (i == 99))
  {
    printf("i = %d and a = %d on %d\n", i, a, omp_get_thread_num());
  }
}


$ gcc -fopenmp file1.c file2.c -o global
$ export OMP_NUM_THREADS=2
$ ./global
i = 0 and a = 0 on 0
i = 49 and a = 49 on 0
i = 50 and a = 0 on 1
i = 99 and a = 49 on 1
ftinetti
 
Posts: 558
Joined: Wed Feb 10, 2010 2:44 pm


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 4 guests