A problem with threadprivate directive.

General OpenMP discussion

A problem with threadprivate directive.

Postby Keres » Fri Dec 14, 2007 8:27 am

In advance, sorry for my english. ;)
I made simple example, using directive threadprivate. Like example A.23 from 2.5 specification.
Code: Select all
#include <iostream>
#include <omp.h>
#include "classT.h"
using namespace std;

const T a_aux(1);
T a(a_aux);

#pragma omp threadprivate(a)

int main()
{
return 0;
}


classT.h:
Code: Select all
int numCallsCopyConstructor = 0;
int numThreads = 0;

class T {
private:
  int innerValue;
public:
  T ();
  T (int);
  T (const T&);
  int getValue ();
  void setValue (int);
};

T :: T ()
{
  innerValue = 0;
}

T :: T (int val)
{
  innerValue = val;
}

T :: T (const T& t)
{
  numCallsCopyConstructor++;
  innerValue = t.innerValue;
}

int T :: getValue()
{
  return innerValue;
}

void T :: setValue(int val)
{
  innerValue = val;
}



When I compiled this code with gcc 4.3.0, i get error
'a' declared 'threadprivate' after first use

I can't understand, what's wrong with this code. Help me pls.
Keres
 
Posts: 4
Joined: Fri Dec 14, 2007 8:08 am
Location: Russia, Moscow

Re: A problem with threadprivate directive.

Postby lfm » Sat Dec 15, 2007 1:02 am

It seems OK to me and compiles OK with the Intel compiler. I don't have a recent gcc handy right now. You can try a private message to Jakub (look at the 3.0 draft discussion board on this site).

-- Larry
lfm
 
Posts: 135
Joined: Sun Oct 21, 2007 4:58 pm
Location: OpenMP ARB

Re: A problem with threadprivate directive.

Postby jakub » Wed Dec 19, 2007 1:21 pm

GCC ATM doesn't support any non-POD threadprivate vars (even if you got through this error you'd get a sorry that this isn't implemented).
GCC uses internally ELF TLS for threadprivate vars, but there is no support for constructors/destructors of TLS data (and it is actually very hard problem to do it properly, as soon as dlopen/dlclose is also involved,
because if you e.g. do dlopen from within parallel region and the dlopened library uses threadprivate non-POD vars, the expectation would be to let the TLS vars constructed in all currently running OpenMP managed threads. And if you dlclose within parallel region, all TLS non-PODs need to be destructed, otherwise it will crash when the threads are destroyed).
So for the time being, with G++ use pointers to non-POD vars if you need threadprivate non-PODs.
jakub
 
Posts: 74
Joined: Fri Oct 26, 2007 3:19 am

Re: A problem with threadprivate directive.

Postby Keres » Thu Dec 20, 2007 5:11 am

jakub wrote:GCC ATM doesn't support any non-POD threadprivate vars (even if you got through this error you'd get a sorry that this isn't implemented).
GCC uses internally ELF TLS for threadprivate vars, but there is no support for constructors/destructors of TLS data (and it is actually very hard problem to do it properly, as soon as dlopen/dlclose is also involved,
because if you e.g. do dlopen from within parallel region and the dlopened library uses threadprivate non-POD vars, the expectation would be to let the TLS vars constructed in all currently running OpenMP managed threads. And if you dlclose within parallel region, all TLS non-PODs need to be destructed, otherwise it will crash when the threads are destroyed).

jakub, thx for your explanation. It's very useful.
So for the time being, with G++ use pointers to non-POD vars if you need threadprivate non-PODs.

If we use pointer to class, what will happen when we enter to parallel section? It seems like we'll get copies of pointer to one object in each thread, and get error in result. Am I right?
Keres
 
Posts: 4
Joined: Fri Dec 14, 2007 8:08 am
Location: Russia, Moscow


Return to Using OpenMP

Who is online

Users browsing this forum: MarkB, Yahoo [Bot] and 11 guests