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 9291591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:41:03+00:00 2026-06-18T20:41:03+00:00

I wanted to optimize below code using openMP double val; double m_y = 0.0f;

  • 0

I wanted to optimize below code using openMP

double val;
double m_y = 0.0f;
double m_u = 0.0f;
double m_v = 0.0f;

#define _MSE(m, t) \
val = refData[t] - calData[t];  \
m += val*val; 

#pragma omp parallel 
 {
 #pragma omp for
for( i=0; i<(width*height)/2; i++ ) {  //yuv422: 2 pixels at a time
    _MSE(m_u, 0); 
    _MSE(m_y, 1); 
    _MSE(m_v, 2); 
    _MSE(m_y, 3); 

  #pragma omp reduction(+:refData) reduction(+:calData)
    refData += 4;
    calData += 4;
 // int id = omp_get_thread_num();
 //printf("Thread %d performed %d iterations of the loop\n",id ,i);
}

}

Any suggestion welcome for optimizing above code currently I have wrong output.

  • 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-18T20:41:04+00:00Added an answer on June 18, 2026 at 8:41 pm

    I think the easiest thing you can do is allow it to split into 4 threads, and calculate the UYVY errors in each of those. Instead of making them separate values, make them an array:

    double sqError[4] = {0};
    const int numBytes = width * height * 2;
    
    #pragma omp parallel for
    for( int elem = 0; elem < 4; elem++ ) {
        for( int i = elem; i < numBytes; i += 4 ) {
            int val = refData[i] - calData[i];
            sqError[elem] += (double)(val*val);
        }
    }
    

    This way, each thread operates exclusively on one thing and there is no contention.

    Maybe it’s not the most advanced use of OMP, but you should see a speedup.


    After your comment about performance hit, I did some experiments and found that indeed the performance was worse. I suspect this may be due to cache misses.

    You said:

    performance hit this time with openMP : Time :0.040637 with serial
    Time :0.018670

    So I reworked it using the reduction on each variable and using a single loop:

        #pragma omp parallel for reduction(+:e0) reduction(+:e1) reduction(+:e2) reduction(+:e3)
        for( int i = 0; i < numBytes; i += 4 ) {
            int val = refData[i] - calData[i];
            e0 += (double)(val*val);
            val = refData[i+1] - calData[i+1];
            e1 += (double)(val*val);
            val = refData[i+2] - calData[i+2];
            e2 += (double)(val*val);
            val = refData[i+3] - calData[i+3];
            e3 += (double)(val*val);
        }
    

    With my test case on a 4-core machine, I observed a little less than 4-fold improvement:

    serial:             2025 ms
    omp with 2 loops:   6850 ms
    omp with reduction: 455  ms
    

    [Edit] On the subject of why the first piece of code performed worse than the non-parallel version, Hristo Iliev said:

    Your first piece of code is a terrible example of what false sharing
    does in multithreaded codes. As sqError has only 4 elements of 8 bytes
    each, it fits in a single cache line (even in a half cache line on
    modern x86 CPUs). With 4 threads constantly writing to neighbouring
    elements, this would generate a massive amount of inter-core cache
    invalidation due to false sharing. One can get around this by using
    instead a structure like this struct _error { double val; double
    pad[7]; } sqError[4]; Now each sqError[i].val will be in a separate
    cache line, hence no false sharing.

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

Sidebar

Related Questions

I found this in some code I wanted to optimize. Here is the snipet:
Feeling like less-code-is-better-code so wanted to optimize how to enable several items at the
I wanted to see if I can better organize / optimize my code, and
Wanted to know if someone had a suggestion on code or maybe there's a
Wanted to understand the difference between undef and define a macro as 0. Thanks.
just wanted to ask where I define initial class properties? From other languages I
How do I optimize the following PHP code? <?php if(strlen($_POST['myName']) < 2 || isNumeric($_POST['myName']))
I am using Rcpp and RInside, to interconnect my R based code to c++.
I am using xdebug to trace some code to see how much memory it
I'm trying to optimize a procedure that has code like the following: CREATE TABLE

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.