I am trying to understand why a program using arrays (such as matrix multiplication) can be written in some way to take advantage of spatial locality of the cache.
Does spatial locality provided by a cache refer to locality within virtual memory, physical memory or both? When a computer system brings a block of data from main memory to CPU cache, does it bring virtually or physically contiguous data objects into CPU cache?
When we define an array or an object of a structure either non-dynamically or dynamically (via malloc()), is it correct that such an array or object is allocated contiguously? Does "contiguous" refer to virtual memory or physical memory or both?
If spacial locality of cache is for physical memory and not necessarily virtual memory, and OS can allocate to a C program virtually not necessarily physically contiguous arrays, how can we write a program to take advantage of the spatial locality of a cache?
Thanks.