OpenMP firstprivate Question

Born2bwire

Diamond Member
Oct 28, 2005
9,840
6
71
I am playing around with using OpenMP to speed up the execution of some for loops in a main and I need to use firstprivate on a class object. firstprivate requires that:

A variable specified in a firstprivate clause must not have an incomplete type or a reference type.

A variable with a class type that is specified as firstprivate must have an accessible, unambiguous copy constructor.

Variables that are private within a parallel region or that appear in the reduction clause of a parallel directive cannot be specified in a firstprivate clause on a work-sharing directive that binds to the parallel construct.

http://msdn.microsoft.com/en-us/library/d179faxw.aspx

What I was wondering about is in regards to the first rule. The class object that I am specifying in firstprivate is a class object that contains functions that pass by reference. The variable itself is not a reference type but the class itself passes reference types, is this still valid or not? The program is wigging out when I run it in parallel and I think it maybe due to problems arising in the portions that are passing by reference.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
* Member functions that are pass-by-reference should be OK.
* Make sure everything that is passed into those functions (by reference) is either private, firstprivate, or shared with appropriate synchronization.
* Make sure your copy constructor works.
* Make sure all the functions called on the object by the dynamic owner thread are thread-safe. Watch for external dependencies: libraries, memory allocation, or something explicit.

If you're still worried about firstprivate and objects after trying the above, move your object to the stack, give all threads a private pointer to it, and enclose accesses to the object in a pragma omp single (is there a pragma omp master or something like that?). You'll still have to make the object functions thread safe if they have external dependencies.