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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:41:15+00:00 2026-06-15T14:41:15+00:00

I have the below snippet which is a hotspot in an application. The for

  • 0

I have the below snippet which is a hotspot in an application.
The for loop is not vectorized due to vector dependance.
Is there a way to rewrite this loop to make it run faster.

#define  NUM_KEYS  (1L << 20)
#define  NUM_BUCKETS (1L << 10)    
int i,k;
int shift = (1L << 11);
int key_array[NUM_KEYS],key_buff[NUM_KEYS];
int bucket_ptrs[NUM_BUCKETS];

for( i=0; i<NUM_KEYS; i++ )  
{
    k = key_array[i];
    key_buff[bucket_ptrs[k >> shift]++] = k;
}

One approach I tried was to create a temporary array to hold the shifted values of key_array.

for( i=0; i<NUM_KEYS; i++ )  
{
        key_arrays[i] = key_array[i] >> shift;
}
for( i=0; i<NUM_KEYS; i++ )  
{
    k = key_array[i];
    j = key_arrays[i];
    key_buff[bucket_ptrs[j]++] = k;
}

Here the first loop gets vectorized. But overall there is no improvement in performance.

  • 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-15T14:41:16+00:00Added an answer on June 15, 2026 at 2:41 pm

    Why is the loop not vectorizable?

    This is because you have non-sequential memory access here:

    key_buff[bucket_ptrs[k >> shift]++] = k;
    

    bucket_ptrs determines the index for accessing key_buff. Since these indices are all over the place, the memory access is non-sequential.

    Currently, x86 processors only support SIMD loads/stores to contiguous chunks of memory. (ideally aligned as well)

    If you want it to vectorize, you will need AVX2’s gather/scatter instructions. Those don’t exist it, but should be coming out in the next generation of Intel processors.

    Here the first loop gets vectorized. But overall there is no
    improvement in performance.

    This is because you’re adding additional loop-overhead. So you’re now making two passes over key_array. If anything, I’m surprised that it isn’t slower.

    Is there a way to rewrite this loop to make it run faster.

    I doubt it – at least not without altering the algorithm. At the very least, you will want key_buff to fit comfortably into your L1 cache.

    AVX2 will let it vectorize, but the problem is that key_buff is 4MB. That won’t fit into the lower level caches. So even AVX2 might not help much. You will be bound completely by memory access.

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

Sidebar

Related Questions

I have below snippet of code in which TestClass is extending jPanel which is
I have the following snippet in my pom.xml (Full pom attached below which can
I have the code snippet below in C++ which basically calculates the pi using
I have been pulling my hair above below snippet of code - which is
I have stumbled upon the following problem. The below code snippet does not link
I have the below code snippet, which runs fine. But the problem is it
Below is the code snippet with comments which describes the problem statement. We have
I have the below jquery snippet which filters for any links which has a
I have wrote a class (a snippet is below) which have some data members
I have a html snippet below which renders perfectly in all browsers. However in

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.