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

  • Home
  • SEARCH
  • 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 8901859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:27:56+00:00 2026-06-15T01:27:56+00:00

I’m having issues understanding how OpenMP works with nested loops. Please help! I got

  • 0

I’m having issues understanding how OpenMP works with nested loops. Please help!

I got the following code to run in parallel:

#pragma omp parallel private(i)
{
for(i=0; i<n; i++)
{
    #pragma omp for
    for(j=0; j<n; j++)
    {
        if(asubsref(seed,j) > 0)
            asubsref(bin,j) = asubsref(bin,j) + 1;
    }
    #pragma omp for
    for(j=0; j<n; j++)
        asubsref(seed,j) = asubsref(seed,j) - asubsref(w,i);
}
}

However, I’m not quite sure how this code works (I just got it by luck). Here’s what I think it’s doing…

So for(i=0; i<n; i++) is split off into different threads and run in parallel. Because i was declared to be private, each instance of the loop is “sandboxed”; that is, any changes to j remain in that thread (at least until all the threads are done?). I’m confused because not declaring #pragma omp for causes the code to break…I’m not sure why this is.

If someone could walk me through what this code is doing, I’d appreciate it very much! Thank you!

  • 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-15T01:27:58+00:00Added an answer on June 15, 2026 at 1:27 am

    This is what one typically does in order to reduce the overhead of multiple enters and exists into and out of a parallel region. Compare the code from your question to the following equivalent code:

    for (i=0; i<n; i++)
    {
        #pragma omp parallel for
        for (j=0; j<n; j++)
        {
            if (asubsref(seed,j) > 0)
                asubsref(bin,j) = asubsref(bin,j) + 1;
        }
        #pragma omp parallel for
        for (j=0; j<n; j++)
            asubsref(seed,j) = asubsref(seed,j) - asubsref(w,i);
    }
    

    It runs the outer loop over i n times. In each iteration of the outer loop, it runs two parallel loops over j, splitting the n iterations among the threads. The problem here is that you have two parallel regions inside, each of which is activated n times. This could add significant overhead for a certain interval of values of n (the actual interval would depend on many factors).

    In order to reduce the overhead, one puts the whole code inside a parallel region. Thus each thread would execute all the iterations of the outer loop and the inner iterations would still be split among the threads. If you remove the worksharing constructs, then the inner loop would not get distributed among the threads. Instead, each thread would execute both the full range of outer and inner iterations, so for example asubsref(bin,j) would get incremented up to num_threads times instead of just once (why “up to”? hint: unprotected concurrent data access).

    Most of the times when one runs an outer loop inside a parallel construct, one wants different threads to keep ticking on the same iterations. This could be achieved by a barrier at the end of the loop body:

    #pragma omp parallel private(i)
    {
        for (i = 0; i < n; i++)
        {
            ...
            #pragma omp barrier
        }
    }
    

    In your case an explicit barrier is not necessary since the for worksharing construct has an implicit barrier at its end.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.