Modern computers have a lot of memory. Each CPU has its cache, and each running program gets assigned its portion of system RAM. There are many different ways this memory can be used and assigned. Shared memory is a concept that affects both CPU caches and the use of system RAM in different ways.
Shared Memory in Hardware
Most modern CPUs have three cache tiers, referred to as L1, L2, and L3. L1 is the smallest and fastest cache, while L3 is the largest and slowest. However, all of them are faster than accessing the main memory, making the hit rate critical to performance. Many factors make the L1 cache faster than L3. First of all, L1 memory cells are physically bigger. There are also fewer of them, and they’re located much closer to the CPU core, often within it.
Placing a cache block within the die area of a core comes with extra complexity on multicore CPUs. At this point, you can choose to have a cache in each core or outside of the cores, specifically between them. Each has its benefits and drawbacks.
Placing a cache in a core minimizes access latency, but it also means that each core needs its cache. This means that you may end up with duplicated cache lines in the L1 caches of different cores, reducing cache space efficiency. This is called a local cache, and while it’s great for access time, it’s less efficient in cache space usage and requires extra overhead in terms of cache coherency.
A shared cache is a cache that is available to multiple or all cores in a multicore CPU. A shared cache means multiple cores can access one instance of specific data, limiting wasted space due to duplication. It also means that one core can temporarily claim more than its fair share of the cache space if it needs it, while the other cores do not. It does come at the cost of increased access time.
Shared Caches in Practice
Modern CPUs use both concepts, with each core having a local L1 cache. L3 cache tends to be shared between many cores, though sometimes not all. L2 varies but can be local or shared depending on the specific CPU generation architecture.
Tip: For chiplet CPUs like high-end AMD Ryzen models, caches may be shared between all cores on a chiplet rather than with all cores in the whole CPU. It doesn’t matter how many cores a cache is shared between; even if it’s just two, it is still a shared cache, though it can be worth highlighting that it’s only partially shared.
Note: System RAM can also be shared between multiple physical CPUs on a single motherboard or between nodes in a multi-CPU system.
Shared Memory in Software
In modern computers, the software doesn’t get to address physical memory directly. Instead, it is assigned a virtual address segment, and the computer translates these virtual addresses to the physical addresses as needed. This helps to isolate memory for individual processes, which is helpful for security.
In some cases, it may be desirable to transfer data in memory from one process to another. The most efficient way to do this is to allow the two processes to share memory space. In this way, both processes can read the same data and communicate with each other. This also helps to use system RAM efficiently as the data isn’t duplicated.
Software-shared memory will typically be achieved by keeping one physical copy of the data and mapping access to it via virtual memory for each process that needs access to it.
Shared memory is the concept of having one section of memory accessible by multiple things. This can be implemented in both hardware and software. CPU cache may be shared between multiple processor cores. This is especially the case for higher tiers of CPU cache. The system memory may also be shared between various physical CPUs in a single larger system.
In software, shared memory can allow IPC Inter-Process Communication. One process allocates memory as shared with one or more specific processes. Those other processes can then access that memory location via virtual memory mapping. Shared memory helps ensure efficient use of memory space by avoiding data duplication in a limited space.
Did this help? Let us know!