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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:14:48+00:00 2026-06-02T19:14:48+00:00

I am learning cache operations with regard to spatial locality. (My references so far

  • 0

I am learning cache operations with regard to spatial locality. (My references so far are Principles of Parallel Programming by Lin and Snyder, this tutorial, and of course Wikipedia.)

Take the following example, compiled with gcc, running on Windows 7 Professional, using Intel Core2 Duo CPU (L7500).

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int *array;
    int length;
    int count;
    int range;
    int i;

    // generate an array of a million integers between 0 and 99
    length = 1000000;
    range = 100;
    array = calloc(length, sizeof(int));
    srand(time(NULL));
    for(i = 0; i < length; i++)
    {
        array[i] = rand() % range;
        // printf("%d\n", array[i]);
    }

    // count the number of occurrences of 3 in the array
    count=0;
    for(i=0; i<length; i++)
    {
        if(array[i]==3)
        {
            count++;
        }
    }
    printf("count = %6d\n", count);

    return 0;
}

Now in the second half of the routine, the entire array of integers is going to be read, so per spatial locality the CPU should load them into the cache in advance. But how much of the array can/does/should it load into the cache at any one time during the loop? One cache line at a time (64 bytes / 4 bytes per int = 16 integers), large blocks of it, or the entire array in one fell swoop?

Also, from what I understand, the latency involved in loading data from RAM into the cache (or per the textbook, from non-local to local memory) can be much more significant than the time required to actually run the routine. True?

Now say we move this code to a multiprocessor/multicore machine, and the counting portion of the code is changed to run in 4, 8, 16, etc. parallel threads (using pthreads), counting separate portions of the array, then adding the private counts together at the end. Could this cause multiple separate occurrences of RAM-to-cache latency, making the parallel version run slower than the serial one?

  • 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-02T19:14:49+00:00Added an answer on June 2, 2026 at 7:14 pm

    Yes, memory speed and latency do dominate many algorithms and it is necessary to use the memory cache as efficiently as possible to speed these up.

    Running in parallel can hurt your performance, but not usually. Figuring this out requires a lot of testing and tweaking.

    For example, take a quad core chip connected to one bank of RAM. If the algorithm requires maximum speed memory reads and the computation is always faster than the RAM speed, running in parallel will not gain anything and will probably slow things down.

    But if you have a dual socket system, each CPU will have its own RAM and the algorithm will speed up.

    Or, the system might upgrade from 1 bank of RAM to 4 and switch from a single channel to quad-channel RAM configuration. At that point, RAM speed might exceed the computation speed and the quad core will see benefit from running more threads.

    In my opinion, running a thread per core will usually benefit you and will take advantage of system upgrades. Running a single thread may avoid a small amount of synchronization overhead, but will always limit the program in the future.

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

Sidebar

Related Questions

I am playing with my Xcode, following tutorial, learning this stuff. I added an
Learning a lot in my few years of programming that the best projects are
Still learning Objective-C / iPhone SDK here. I think I know why this wasn't
Learning Ruby. Needed to create a hash of arrays. This works... but I don't
I'm learning Scala and trying to use javax.cache in Scala code and can't find
Here is the actual line of code I'm looking at: ContentVersionCache cvc = ((PageBase)this.Page).cache;
This one is killing me... I'm learning to develop SOAP servers in PHP at
I started learning Django recently and am having a strange problem with the tutorial.
Years ago I was learning about x86 assembler, CPU pipelining, cache misses, branch prediction,
I started my intrepid journey into learning objective-c for ios, and got as far

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.