Professional Documents
Culture Documents
(CS 526)
Muhammad Awais,
multi-core chip
Main memory
x=15213
35
The cache coherence problem
Core 1 reads x
multi-core chip
Main memory
x=15213
36
The cache coherence problem
Core 2 reads x
multi-core chip
Main memory
x=15213
The cache coherence problem
Core 1 writes to x, setting it to 21660
multi-core chip
Main memory
x= 15213
The cache coherence problem
Core 2 attempts to read x… gets a stale copy
multi-core chip
Main memory
x=15213
In the MESI protocol:
• Modified (M): The cache line is only present in the current cache and
has been modified. It needs to be written back to main memory.
• Exclusive (E): The cache line is only present in the current cache and
has not been modified.
• Shared (S): The cache line is present in multiple caches, and it's not
modified.
• Invalid (I): The cache line is invalid or not present in the current
cache.
Modified (M):
• This state occurs when a processor has a cache line that has been
modified, meaning it's different from the corresponding data in the
main memory.
• When a processor writes to a cache line in the M state, it must
eventually write it back to main memory to keep it coherent.
• Other caches cannot have this line in any state (E, S, or I) because it's
modified and they would have stale data.
Exclusive (E):
• This state occurs when a cache line is present in only one cache, and
it hasn't been modified.
• In this state, the cache has the most up-to-date copy of the data
compared to other caches.
• The data can be safely read or modified because it's not being used
elsewhere.
Shared (S):
• This state occurs when multiple caches have a copy of the same data,
and none of them have modified it.
• The data is consistent among all caches in the shared state
Invalid (I):
• If a cache line is marked as invalid, it means that it must be fetched
from main memory or another cache before it can be used.
some scenarios to see how MESI protocol
works:
• Read Miss:
• If a processor tries to read a memory location and it's not in the cache
(I state), it must fetch it from main memory. It will transition to the E
or S state depending on whether other caches also request the same
data.
Write
• Write Operation:
• If a processor wants to write to a memory location, it first checks its
cache. If the data is in the E or M state, the write can proceed directly.
If it's in the S state, it must issue an invalidate request to the other
caches holding the data
Level 1 to level 2
• Cache-to-Cache Transfer:
• If a cache wants to read a memory location that's already in another
cache (E or M state), it sends a request to the owning cache. The
owning cache responds by sending the data. The requesting cache
then transitions to the S state.
Eviction
• Cache Eviction:
• When a cache line is evicted (replaced) from a cache, it might need to
be written back to main memory if it's in the M state.
Other Protocols
• MSI protocol (Modified, Shared, Invalid)
• MOSI protocol (Modified, Owned, Shared, Invalid)
• MESI protocol (Modified, Exclusive, Shared, Invalid)
• MOESI protocol (Modified, Owned, Exclusive, Shared, Invalid)
Programming for Multi-core
• Programmers must use threads or processes
1 1 0 1
#include <sched.h>
#include <bits/stdc++.h>
#include <chrono>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <bits/stdc++.h>
#include <chrono>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <bits/stdc++.h>
#include <chrono>
#include <pthread.h>
#include <unistd.h>
CPU_ZERO(&cores);
CPU_SET(i, &cores);
pthread_attr_t attr;
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cores);
pthread_create(th1, &attr, ChildThread, NULL);
Kernel scheduler
API
#include <sched.h>
int sched_getaffinity(pid_t pid,
unsigned int len, unsigned long * mask);
51
Flynn’s Taxonomy
• Michael Flynn (from Stanford)
– Made a characterization of computer systems
which became known as Flynn’s Taxonomy
Computer
Instructions Data
Multiple Processor
Organization
Flynn’s Taxonomy:
1.Single Instruction, Single Data stream - SISD
2.Single Instruction, Multiple Data stream - SIMD
3.Multiple Instruction, Single Data stream - MISD
4.Multiple Instruction, Multiple Data stream- MIMD
1. Single Instruction, Single Data Stream – SISD
• Single processor
• Single instruction stream
• Data stored in single memory
• Example: Uni-processor Systems
SI SISD SD
2. Single Instruction, Multiple Data Stream - SIMD
SI SISD SD
SISD SD
3. Multiple Instruction, Single Data Stream - MISD
• Sequence of data
• Transmitted to set of processors
• Each processor executes different instruction
sequence, using same Data
• Few examples: Systolic array Processors
SI SISD
SI SISD SD
SI SISD
4. Multiple Instruction, Multiple Data Stream- MIMD
• Set of processors
• Simultaneously execute different instruction
sequences
• Different sets of data
• Examples: Multi-cores, SMPs, Clusters
SI SISD SD
SI SISD SD
SI SISD SD
MIMD - Overview
• General purpose processors
• Each can process all instructions necessary
• Further classified by method of processor
communication:
1. Via Shared Memory
2. Message Passing (Distributed Memory)
Taxonomy of Processor
Architectures
Taxonomy of Processor
Architectures
Tightly Coupled - SMP
• Processors share memory
• Communicate via that shared memory
• Executed in parallel
• Process:Definitions of Threads and Processes
– An instance of program running on computer
– A unit of resource ownership:
• virtual address space to hold process image
– Process switch
• Thread switch
– Switching processor between threads within same process
– Typically less costly than process switch
Implicit and Explicit
Multithreading
• All commercial processors use explicit
multithreading:
– Concurrently execute instructions from different
explicit threads
– Interleave instructions from different threads on shared
pipelines OR parallel execution on parallel pipelines