ArticleslgStudy

computer science

Cache prefetching

Cache prefetching is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Cache prefetching rather than just read about it. In short: Cache prefetching is a technique used by central processing units (CPUs) to boost execution performance by fetching instructions or data from their primary or main storage in slower memory to a faster local memory before it is actually needed. Most modern CPUs have fast and local cache memory in which prefetched data is held until it is required.

Cache prefetching — main illustration
Cache prefetching — illustration

Key takeaways

  • Cache prefetching belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Cache prefetching to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Cache prefetching from memory before moving on to harder problems.

Reference excerpt

Cache prefetching is a technique used by central processing units (CPUs) to boost execution performance by fetching instructions or data from their primary or main storage in slower memory to a faster local memory before it is actually needed. Most modern CPUs have fast and local cache memory in which prefetched data is held until it is required. The source for the prefetch operation is usually main memory. Because of their design, accessing cache memories is typically much faster than accessing main memory. Prefetching can be done with non-blocking cache control instructions. Prefetching is based on the principle of data locality.

Data vs. instruction cache prefetching Cache prefetching can either fetch data or instructions into cache.

Data prefetching fetches data before it is needed. Because data access patterns show less regularity than instruction patterns, accurate data prefetching is generally more challenging than instruction prefetching. Instruction prefetching fetches instructions before they need to be executed. The first mainstream microprocessors to use some form of instruction prefetch were the Intel 8086 (six bytes) and the Motorola 68000 (four bytes). In recent years, all high-performance processors use prefetching techniques.

Hardware vs. software cache prefetching Cache prefetching can be accomplished either by hardware or by software.

Hardware-based prefetching is typically accomplished by having a dedicated hardware mechanism in the processor that watches the stream of instructions or data being requested by the executing program, recognizes the next few elements that the program might need based on this stream, and prefetches into the processor's cache. Software-based prefetching is typically accomplished by having the compiler analyze the code and insert additional "prefetch" instructions in the program during compilation itself.

Methods of hardware prefetching

Stream buffers Stream buffers were developed based on the concept of "one block lookahead (OBL) scheme" proposed by Alan Jay Smith. Stream buffers are one of the most common hardware based prefetching techniques in use. This technique was originally proposed by Norman Jouppi in 1990, and many variations of this method have been developed since. The basic idea is that the cache miss address (and k subsequent addresses) are fetched into a separate buffer of depth k. This buffer is called a stream buffer and is separate from the cache. The processor then consumes data/instructions from the stream buffer if the address associated with the prefetched blocks matches the requested address generated by the program executing on the processor. The figure below illustrates this setup: Whenever the prefetch mechanism detects a miss on a memory block, say A, it allocates a stream to begin prefetching successive blocks from the missed block onward. If the stream buffer can hold 4 blocks, then the processor would prefetch A+1, A+2, A+3, A+4 and hold those in the allocated stream buffer. If the processor consumes A+1 next, then it shall be moved "up" from the stream buffer to the processor's cache. The first entry of the stream buffer would now be A+2 and so on. This pattern of prefetching successive blocks is called Sequential Prefetching. It is mainly used when contiguous locations are to be prefetched. For example, it is used when prefetching instructions. This mechanism can be scaled up by adding multiple such stream buffers, each of which would maintain a separate prefetch stream. For each new miss, there would be a new stream buffer allocated, and it would operate in a similar way as described above. The ideal depth of the stream buffer is subject to experimentation against various benchmarks and depends on the rest of the microarchitecture involved.

Strided prefetching This type of prefetching monitors the delta between the addresses of the memory accesses and looks for patterns within it.

Regular strides In this pattern, consecutive memory accesses are made to blocks that are s addresses apart. In this case, the prefetcher calculates the s and uses it to compute the memory address for prefetching. For example, if s = 4, the address to be prefetched would A+4.

Irregular spatial strides In this case, the delta between the addresses of consecutive memory accesses is variable but still follows a pattern. Some prefetcher designs exploit this property to predict and prefetch for future accesses.

Irregular temporal prefetching This class of prefetchers looks for memory access streams that repeat over time. For example, in the stream of memory accesses N, A, B, C, E, G, H, A, B, C, I, J, K, A, B, C, L, M, N, O, A, B, C, ...; the stream A, B, C is repeating over time. Other design variations have tried to provide more efficient implementations.

Collaborative prefetching Computer applications generate a variety of access patterns. The processor and memory subsystem architectures used to execute these applications further disambiguate the memory access patterns they generate. Hence, the effectiveness and efficiency of prefetching schemes often depends on the application and the architectures used to execute them. Recent research has focused on building collaborative mechanisms to synergistically use multiple prefetching schemes for better prefetching coverage and accuracy.

Methods of software prefetching

Compiler-directed prefetching Compiler-directed prefetching is widely used within loops with a large number of iterations. In this technique, the compiler predicts future cache misses and inserts a prefetch instruction based on the miss penalty and execution time of the instructions. These prefetches are non-blocking memory operations; that is, these memory accesses do not interfere with actual memory accesses. They do not change the state of the processor or cause page faults. One main advantage of software prefetching is that it reduces the number of compulsory cache misses. The following example shows the addition of a prefetch instruction into code to improve cache performance. In the following iteration,

the ith element of the array array1 is accessed. The system can prefetch the elements that are presumably accessed in future iterations by inserting a prefetch instruction as shown below:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Cache prefetching

Start with the simplest possible case. Write down what Cache prefetching claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Cache prefetching before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Cache prefetching ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Cache prefetching

In research
Cache prefetching appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Cache prefetching in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Cache prefetching is common in secondary-school and first-year university syllabi. It links to neighbouring topics Cache (computing), so understanding it makes those chapters shorter.
In everyday life
Look for Cache prefetching outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Cache prefetching in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Cache prefetching means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Cache prefetching out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Cache prefetching in simple terms?

Cache prefetching is a technique used by central processing units (CPUs) to boost execution performance by fetching instructions or data from their primary or main storage in slower memory to a faster local memory before it is actually needed. Most modern CPUs have fast and local cache memory in wh…

Why does Cache prefetching matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Cache prefetching?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Cache prefetching.

Tags

  • Cache (computing)

Keep exploring