Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openMP - parallelization with for loop and private

I wrote a function to evaluate a given function at points in a set (set_). Code with no parallelization is like that:

void Method::evaluateSet(double* funcEvals_, double** set_)
{
    for(int j= 0;j<m_npts;j++)
    {
        if(!(isInsideConstraints(set_[j])) || qIsNaN(funcEvals_[j]) || !qIsFinite(funcEvals_[j])) 
        {
            funcEvals_[j] = DBL_MAX;                     
        }
        else
        {
            solverInput input_(m_input);
            input_.setFunParameters(simplex_[j]);
            funcEvals_[j]=input_.apply(simplex_[j]);
        }
    }           
}

and this is working properly.

I then parallelize using openMP, with a parallel construct, and a private copy of the variable set_ for each thread. Loop is

#pragma omp parallel for  private (set_)
for(int j= 0;j<m_npts;j++)
{
    if(!(isInsideConstraints(set_[j])) || qIsNaN(funcEvals_[j]) || !qIsFinite(funcEvals_[j])) 
    {
        funcEvals_[j] = DBL_MAX;
    }
    else
    {
        solverInput input_(m_input);
        input_.setFunParameters(set_[j]);
        funcEvals_[j]=input_.apply(set_[j]);
    }
}
#pragma omp barrier

It crashes, and error occurs at if evaluation, with set_ is being used without been initialized. I don't understand. Since I set the set_ variable private, shouldn't there be a copy of original set_ in each thread?

What is wrong with the code and how to improve?

Thanks and regards.

like image 425
kiriloff Avatar asked Apr 16 '12 10:04

kiriloff


People also ask

Is nested parallelism possible in OpenMP?

OpenMP parallel regions can be nested inside each other. If nested parallelism is disabled, then the new team created by a thread encountering a parallel construct inside a parallel region consists only of the encountering thread. If nested parallelism is enabled, then the new team may consist of more than one thread.

How can you distinguish between shared and private variables in a thread in a multithreaded environment?

If a variable is shared, then there exists one instance of this variable which is shared among all threads. If a variable is private, then each thread in a team of threads has its own local copy of the private variable.

What is the difference between OMP for and OMP parallel for?

#pragma omp parallel spawns a group of threads, while #pragma omp for divides loop iterations between the spawned threads.


1 Answers

When you use private for a variable a private copy starts with no value, I mean it is not initialized at that time. Therefore, the value passed by parameter do not set set_ variable. You need to use firstprivate instead, which first initializes the private copy with the current value.

like image 55
mert Avatar answered Oct 19 '22 09:10

mert