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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:02:54+00:00 2026-05-26T16:02:54+00:00

I am using OpenMP to parallelize loops. In normal case, one would use: #pragma

  • 0

I am using OpenMP to parallelize loops. In normal case, one would use:

#pragma omp for schedule(static, N_CHUNK)
for(int i = 0; i < N; i++) {
    // ...
}

For nested loops, I can put pragma on the inner or outter loop

#pragma omp for schedule(static, N_CHUNK) // can be here...
for(int i = 0; i < N; i++) {
#pragma omp for schedule(static, N_CHUNK) // or here...
    for(int k = 0; k < N; k++) {
    // both loops have consant number of iterations
    // ...
    }
}

But! I have two loops, where number of iterations in 2nd loop depends on the 1st loop:

for(int i = 0; i < N; i++) {
    for(int k = i; k < N; k++) {
    // k starts from i, not from 0...
    }
}

What is the best way to balance CPU usage for this kind of loop?

  • 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-05-26T16:02:54+00:00Added an answer on May 26, 2026 at 4:02 pm

    As always:

    • it depends
    • profile.
    • In this case: see also OMP_NESTED environment variable

    The things that are going to make the difference here are not being shown:

    • (non)linear memory addressing (also watch the order of the loops
    • use of shared variables;

    As to your last scenario:

    for(int i = 0; i < N; i++) {
        for(int k = i; k < N; k++) {
        // k starts from i, not from 0...
        }
    }
    

    I suggest parallelizing the outer loop for the following reasons:

    • all other things being equal coarse grained parallelizing usually leads to better performance due to

      • increased cache locality
      • reduced frequency of locking required
        (note that this hinges on assumptions about the loop contents that I can’t really make; I’m basing it on my experience of /usual/ parallelized code)
    • the inner loop might become so short as to be inefficient to parallelize (IOW: the outer loop’s range is predictable, the inner loop less so, or doesn’t lend itself to static scheduling as well)

    • nested parallellism rarely scales well
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function which I would like to parallelize using openmp: for(i=len-1;i>=0;i--){ if(bin[i]==49)
I am working with cuda and using openMP for parallel threading: #pragma omp parallel
I have the following C/C++ code using OpenMP: int nProcessors=omp_get_max_threads(); if(argv[4]!=NULL){ printf(argv[4]: %s\n,argv[4]); nProcessors=atoi(argv[4]);
I am writing a multi-threaded program using OpenMP in C++. At one point my
I would like to do some transformation to the IplImage using OpenMP. It is
I have written a library in C using a variety of the #pragma omp
i am trying to use OpenMP in my program (i am newbie using OpenMP)
In a member function, I can parallelize using the shared member variable int *x
I have a program using OpenMP to parallelize a for-loop. Inside the loop, the
I am parallelizing several separated for-loops using OpenMP. While debugging in gdb, I found

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.