a3mlord
a3mlord

Reputation: 1060

OpenMP scoping - seg. fault with shared()

I have the following code:

[...]

#pragma omp parallel
{
   #pragma omp single firstprivate(nodes)
   {

      [...] /* Do some calculation to find out the value of "nodes" */

      double *tForTasks[nodes];

      for(node=0; node < nodes ; node++){

         #pragma omp task private(l, k ,j) shared(tForTasks)
         {
            [...] /* Piece of iterative kernel that writes on tForTasks and on private data structures */
         }
    }
}

[...]

and I found out that defining tForTasks as shared causes a segmentation fault, but taking shared(tForTasks) out of the task declaration works just fine. Is there anything that I am doing wrong?

I didn't provide more code because it seems irrelevant for the problem (please let me know if something is needed). I have the segmentation fault for any number of threads between 1 and 16. Without shared(tForTasks), I never have it.

Thanks

Upvotes: 1

Views: 270

Answers (1)

Anton
Anton

Reputation: 6587

The task clause is not part of region of single according to OpenMP specs (link?). Thus, it can outlive the tForTasks array and run after its destruction. Thus when you declare it shared, tasks can refer to uninitialized or deallocated memory raising segmentation fault. When you omit the shared specification, the array is copied to each task as firstprivate since it was declared in the parallel region. But in this case a thread will not see the writes to tForTasks from other threads.

One solution is to allocate the array dynamically and release it after the tasks completion.

Upvotes: 1

Related Questions