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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:02:57+00:00 2026-05-14T15:02:57+00:00

How would I unroll the following nested loops? for(k = begin; k != end;

  • 0

How would I unroll the following nested loops?

for(k = begin; k != end; ++k) {
 for(j = 0; j < Emax; ++j) {
  for(i = 0; i < N; ++i) { 
   if (j >= E[i]) continue; 
   array[k] += foo(i, tr[k][i], ex[j][i]);
  }
 }
}

I tried the following, but my output isn’t the same, and it should be:

for(k = begin; k != end; ++k) {
 for(j = 0; j < Emax; ++j) {
  for(i = 0; i+4 < N; i+=4) { 
   if (j >= E[i]) continue; 
   array[k] += foo(i, tr[k][i], ex[j][i]);
   array[k] += foo(i+1, tr[k][i+1], ex[j][i+1]);
   array[k] += foo(i+2, tr[k][i+2], ex[j][i+2]);
   array[k] += foo(i+3, tr[k][i+3], ex[j][i+3]);
  }
  if (i < N) {
   for (; i < N; ++i) {
    if (j >= E[i]) continue; 
    array[k] += foo(i, tr[k][i], ex[j][i]);
   }
  }
 }
}

I will be running this code in parallel using Intel’s TBB so that it takes advantage of multiple cores. After this is finished running, another function prints out what is in array[] and right now, with my unrolling, the output isn’t identical. Any help is appreciated.

Update: I fixed it. I used the answer for this question to do the unrolling… the output wasn’t matching because I wasn’t doing array[k] = 0; after the first for loop.

Thanks,
Hristo

  • 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-14T15:02:57+00:00Added an answer on May 14, 2026 at 3:02 pm
       if (j >= E[i]) continue; 
       array[k] += foo(i, tr[k][i], ex[j][i]);
       array[k] += foo(i+1, tr[k][i+1], ex[j][i+1]);
       array[k] += foo(i+2, tr[k][i+2], ex[j][i+2]);
       array[k] += foo(i+3, tr[k][i+3], ex[j][i+3]);
    

    versus

    if (j >= E[i]) continue; 
    array[k] += foo(i, tr[k][i], ex[j][i]);
    

    Screening conditions are not identical

    a better approach to screening (eliminate branching):

    array[k] += (j < E[i])*foo(i, tr[k][i], ex[j][i]);
    

    also, you need to guarantee N is divisible by 4 otherwise you may overshoot. alternatively, truncate N to be divisible by four (N – N%4)

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

Sidebar

Related Questions

Would it be possible to extract the following information from logs? Start up/Shut down
Would appreciate some assistance i have a txt file witht he following contents: 1234|dog|apartment|two
Would there be any problems if I did the following to imitate a liked
Would it be easy to convert the following function so that instead of just
would it be possible to set an int value to begin with a zero?
Would like to sort the following XML by joining the fsxml/formula/ingrrow where itemcode =
Would someone kindly assist me with the following? I have two DataGridView objects that
Would like to get step-by-step explanation of the following function in Haskell fibs =
Would the following be the correct way to implement a fairly straightforward thread-safe logging
Would the following SQL remove also the index - or does it have to

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.