Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:16:17+00:00 2026-06-02T04:16:17+00:00

I am using openMP to parallelize a few statements. I am using the parallel

  • 0

I am using openMP to parallelize a few statements. I am using the parallel for construct. The parallelized for loop looks like:

double solverFunction::apply(double* parameters_ , Model* varModel_)  
{
     double functionEvaluation = 0;
     Command* command_ = 0;
     Model* model_ = 0;

     #pragma omp parallel for  shared (functionEvaluation) private (model_,command_)
     for (int i=rowStart;i<rowEnd+1;i++)
     {
         model_ = new Model(varModel_);
         model_->addVariable("i", i);
         model_->addVariable("j", 1);
         command_ = formulaCommand->duplicate(model_);
         functionEvaluation += command_->execute().toDouble();
     }
}

It is workly on average. Execution time is dramatically reduced, and result is as expected. However, from time to time, especially for big problems (big number of iterations over i, big number of data to copy in copy constructor call

 model_ = new Model(varModel_);

, others?), it crashed. Call stack ends in classes such as qAtomicBasic (it is a program written in C++/Qt), QHash, and I have an idea it crashes because of concurrent read/write access in memory.

HOWEVER, model_ and command_ are private, so that each thread deals with a copy of each. In the variable model_, I copy varModel_, so that the pointer passed in argument is not altered by the threads. Alike, command_ is a copy of the member variable formulaCommand (duplicate is roughtly a copy constructer).

The possible flaws in my code I identified are

  • functionEvaluation may be modified by several threads simultaneously

  • copy constructor in statement

    model_ = new Model(varModel_);

reads the members for varModel_ in memory to construct the new (model_) instance. Concurrent access to varModel_ data members could occur, althought this not about altering their value here, but only reading them (affecting them to other variables).

Also, I see two improvements only (which I cannot test until a few days, but I ask for advice anyway):

  • add atomic clause, so that functionEvalution is not concurrently written in

  • add operator reduction(+,functionEvaluation), so that concurrency regarding access to functionEvaluation is dealt with automatically

Do these solutions seem to accuratly solve the problem and which is more efficient in general? Where does the problem can lie with the code I wrote? What are solutions?

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:16:18+00:00Added an answer on June 2, 2026 at 4:16 am

    The first observation is that, as you’ve noticed yourself, modifying functionEvaluation concurrently is a bad idea. It will fail.

    The read-only access of varModel_, on the other hand, is not a problem. Neither is the copy constructor call (but where is it? Your code doesn’t show it).

    Unrelatedly, using the private clause in C++ is a bad idea. Just declare the thread-private variables inside the parallel block (in this case, the for loop).

    I also don’t see why you are using pointers here. Their use doesn’t make immediate sense – use stack-allocated objects instead.

    The following modified code should work (I’ve also taken the liberty of unifying the coding style … why the trailing underscores?):

    double solverFunction::apply(double parameters, Model const& varModel)
    {
         double result = 0;
    
         #pragma omp parallel for reduction(+:result)
         for (int i = rowStart; i < rowEnd + 1; ++i)
         {
             Model model(varModel);
             mode.addVariable("i", i);
             mode.addVariable("j", i);
             Command command = formulaCommand->duplicate(model);
             result += command.execute().toDouble();
         }
    
         return result;
    }
    

    Note that, due to inherent floating point inaccuracies, this code may yield different results from the sequential code. This is unavoidable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using openmp and my program looks like as follows: \#pragma omp parallel
I have this function which I would like to parallelize using openmp: for(i=len-1;i>=0;i--){ if(bin[i]==49)
I have a program using OpenMP to parallelize a for-loop. Inside the loop, the
I'm wondering if it is feasible to make this loop parallel using openMP. Of
I am trying to parallelize my code using openmp. I have managed to parallelize
I have a serial application that I parallelized using OpenMP. I simply added the
I would like to do some transformation to the IplImage using OpenMP. It is
I'm trying to implement the distance matrix in parallel using openmp in which I
I am using OpenMP to parallelize loops. In normal case, one would use: #pragma
I am working with cuda and using openMP for parallel threading: #pragma omp parallel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.