How can I make static variables and functions (from a templatised class) private in OpenMP to have different values in each thread?
1 Answer
Depending on the scenario, you should be able to use:
#pragma omp threadprivate(theVariable)
For details, see this article on using Thread-local Storage in OpenMP.
6 Comments
The Quantum Physicist
As a matter of fact I tried that already. But I got an error. error: 'SysBase<double>::_dt' declared 'threadprivate' after first use
Reed Copsey
@SamerAfach You typically can't use a static variable as both static + thread local. You have to make a new thread local, then update the static (which is shared) afterwards.
Hristo Iliev
Note also that
threadprivate local static variables are only supported since OpenMP 3.0, which means no support in Visual C++ (if you develop on Windows or intend to port to later)The Quantum Physicist
@HristoIliev How can I know the version of OpenMP I'm using? I use Ubuntu actually with g++ for my programs. Should I give some extra directive to use OpenMP 3.0?
Hristo Iliev
From the GCC Wiki: "As of GCC 4.2, the compiler implements version 2.5 of the OpenMP standard and as of 4.4 it implements version 3.0 of the OpenMP standard. The OpenMP 3.1 is supported since GCC 4.7." Standards are (kind of) backwards compatible and there is no way to force conformance to specific OMP version in GCC (and also in the other compilers that I use).
|