The storage system of the calculation group: 5. Cache (cache function, cache working principle, cache performance analysis)

mind Mapping

Insert picture description here

Why cache?

Insert picture description here

1. When the program is executed, it is necessary to load the program execution code and data from the auxiliary memory into the memory, and then the CPU fetches instructions from the memory one by one.
2. But the memory and CPU speed do not match, which will greatly affect the efficiency of the CPU. So we need a middleware that can alleviate the speed problem: cache. Usually, the speed of cache can reach dozens of times of main memory.

How Cache works

Insert picture description here

1. After adding the cache, according to the principle of locality, some code will be copied to the cache, and the CPU will fetch instructions directly from the cache.
2. Due to hardware integration and cost issues, the cache capacity is generally very small

Principle of locality

Insert picture description here

For program A and program B in the above figure, when program A accesses a two-dimensional array sequentially, its physical addresses are also continuous. Therefore, according to the principle of spatial locality, the cache hit rate will be very high, and the program execution speed will be fast . For program B, when program A accesses a two-dimensional array sequentially, its physical addresses are not continuous. Therefore, according to the principle of spatial locality, its cache hit rate is low and the program execution efficiency is not high.

Performance analysis (add cache)

Insert picture description here
Insert picture description here

How to define "nearby" in spatial locality?

Insert picture description here

For example, when you access a[1][0], you need to determine which address space it is in, and then copy the entire address space to the cache according to the principle of locality.

Problems to be solved?

Insert picture description here

Question 1: See the storage system of the calculation group: 6. Cache- main memory mapping method
Question 2: See the storage system of the calculation group: 7. Cache replacement algorithm
Question 3: See the storage system of the calculation group: 8. Cache write strategy

Guess you like

Origin blog.csdn.net/qq_41587740/article/details/109104586