Lecture 7 - Machine-Level Programming 4 - Data
Lecture 7 - Machine-Level Programming 4 - Data
Lecture 7 - Machine-Level Programming 4 - Data
1
Carnegie Mellon
Today
Arrays
▪ One-dimensional
▪ Multi-dimensional (nested)
▪ Multi-level
Structures
▪ Allocation
▪ Access
2
Carnegie Mellon
Today
Procedures (x86-64)
Arrays
▪ One-dimensional
▪ Multi-dimensional (nested)
▪ Multi-level
Structures
3
Carnegie Mellon
Floating Point
▪ Stored & operated on in floating point registers
Intel ASM Bytes C
Single s 4 float
Double l 8 double
Extended t 10/12/16 long double
4
Carnegie Mellon
Array Allocation
Basic Principle
T A[L];
▪ Array of data type T and length L
▪ Contiguously allocated region of L * sizeof(T) bytes
char string[12];
x x + 12
int val[5];
x x+4 x+8 x + 12 x + 16 x + 20
double a[3];
x x+8 x + 16 x + 24
char *p[3]; IA32
x x+4 x+8 x + 12
x86-64
x x+8 x + 16 x + 24
5
Carnegie Mellon
Array Access
Basic Principle
T A[L];
▪ Array of data type T and length L
▪ Identifier A can be used as a pointer to array element 0: Type T*
int val[5]; 1 5 2 1 3
x x+4 x+8 x + 12 x + 16 x + 20
Array Example
#define ZLEN 5
typedef int zip_dig[ZLEN];
zip_dig cmu = { 1, 5, 2, 1, 3 };
zip_dig mit = { 0, 2, 1, 3, 9 };
zip_dig ucb = { 9, 4, 7, 2, 0 };
zip_dig cmu; 1 5 2 1 3
16 20 24 28 32 36
zip_dig mit; 0 2 1 3 9
36 40 44 48 52 56
zip_dig ucb; 9 4 7 2 0
56 60 64 68 72 76
int get_digit
(zip_dig z, int dig)
{
return z[dig];
} ◼ Register %edx contains
starting address of array
IA32 ◼ Register %eax contains
# %edx = z array index
# %eax = dig ◼ Desired digit at
movl (%edx,%eax,4),%eax # z[dig] 4*%eax + %edx
◼ Use memory reference
(%edx,%eax,4)
8
Carnegie Mellon
int A[R][C];
A A A A A A
[0] • • • [0] [1] • • • [1] • • • [R-1] • • • [R-1]
[0] [C-1] [0] [C-1] [0] [C-1]
4*R*C Bytes
9
Carnegie Mellon
Today
Structures
▪ Allocation
▪ Access
10
Carnegie Mellon
Structure Allocation
struct rec {
int a[3]; Memory Layout
int i; a i n
struct rec *n;
}; 0 12 16 20
Concept
▪ Contiguously-allocated region of memory
▪ Refer to members within structure by names
▪ Members may be of different types
11
Carnegie Mellon
Structure Access
r r+12
struct rec {
int a[3];
int i; a in
struct rec *n;
}; 0 12 16 20
12
Carnegie Mellon
13
Carnegie Mellon
struct rec {
int a[3];
Following Linked List int i;
struct rec *n;
C Code };
void set_val
(struct rec *r, int val) a i n
{ 0 12 16 20
while (r) {
int i = r->i; Element i
r->a[i] = val;
r = r->n; Register Value
} %edx r
}
%ecx val
.L17: # loop:
movl 12(%edx), %eax # r->i
movl %ecx, (%edx,%eax,4) # r->a[i] = val
movl 16(%edx), %edx # r = r->n
testl %edx, %edx # Test r
jne .L17 # If != 0 goto loop
14
Carnegie Mellon
Summary
Arrays
▪ One-dimensional
▪ Multi-dimensional (nested)
Structures
▪ Allocation
▪ Access
15
Carnegie Mellon
16
Carnegie Mellon
Today
Storage technologies and trends
Locality of reference
Caching in the memory hierarchy
17
Carnegie Mellon
19
Carnegie Mellon
16 x 8 DRAM chip
Cols
0 1 2 3
RAS = 2
2
/ 0
addr
1
Rows
Memory
controller 2
8 3
/
data
supercell 3
8
(2,1) /
data
supercell
Internal row buffer
(2,1)
22
Carnegie Mellon
Memory Modules
addr (row = i, col = j)
: supercell (i,j)
DRAM 0
64 MB
memory module
DRAM 7
consisting of
eight 8Mx8 DRAMs
63 56 55 48 47 40 39 32 31 24 23 16 15 8 7 0
Memory
controller
64-bit doubleword at main memory address A
64-bit doubleword
23
Carnegie Mellon
Enhanced DRAMs
Basic DRAM cell has not changed since its invention in 1966.
▪ Commercialized by Intel in 1970.
DRAM cores with better interface logic and faster I/O :
▪ Synchronous DRAM (SDRAM)
▪ Uses a conventional clock signal instead of asynchronous control
▪ Allows reuse of the row addresses (e.g., RAS, CAS, CAS, CAS)
24
Carnegie Mellon
Nonvolatile Memories
DRAM and SRAM are volatile memories
▪ Lose information if powered off.
Nonvolatile memories retain value even if powered off
▪ Read-only memory (ROM): programmed during production
▪ Programmable ROM (PROM): can be programmed once
▪ Eraseable PROM (EPROM): can be bulk erased (UV, X-Ray)
▪ Electrically eraseable PROM (EEPROM): electronic erase capability
▪ Flash memory: EEPROMs with partial (sector) erase capability
▪ Wears out after about 100,000 erasings.
Uses for Nonvolatile Memories
▪ Firmware programs stored in a ROM (BIOS, controllers for disks,
network cards, graphics accelerators, security subsystems,…)
▪ Solid state disks (replace rotating disks in thumb drives, smart
phones, mp3 players, tablets, laptops,…)
▪ Disk caches
25
Carnegie Mellon
CPU chip
Register file
ALU
I/O Main
Bus interface
bridge memory
26
Carnegie Mellon
ALU
%eax
Main memory
I/O bridge 0
A
Bus interface x A
27
Carnegie Mellon
ALU
%eax
Main memory
I/O bridge x 0
Bus interface x A
28
Carnegie Mellon
ALU
%eax x
Main memory
I/O bridge 0
Bus interface x A
29
Carnegie Mellon
ALU
%eax y
Main memory
I/O bridge 0
A
Bus interface A
30
Carnegie Mellon
ALU
%eax y
Main memory
I/O bridge 0
y
Bus interface A
31
Carnegie Mellon
ALU
%eax y
main memory
I/O bridge 0
bus interface y A
32
Carnegie Mellon
Actuator
Electronics
(including a
processor
SCSI and memory!)
connector
Image courtesy of Seagate Technology
33
Carnegie Mellon
Disk Geometry
Disks consist of platters, each with two surfaces.
Each surface consists of concentric rings called tracks.
Each track consists of sectors separated by gaps.
Tracks
Surface
Track k Gaps
Spindle
Sectors
34
Carnegie Mellon
Surface 0
Platter 0
Surface 1
Surface 2
Platter 1
Surface 3
Surface 4
Platter 2
Surface 5
Spindle
35
Carnegie Mellon
Disk Capacity
Capacity: maximum number of bits that can be stored.
▪ Vendors express capacity in units of gigabytes (GB), where
1 GB = 109 Bytes (Lawsuit pending! Claims deceptive advertising).
Capacity is determined by these technology factors:
▪ Recording density (bits/in): number of bits that can be squeezed
into a 1 inch segment of a track.
▪ Track density (tracks/in): number of tracks that can be squeezed
into a 1 inch radial segment.
▪ Areal density (bits/in2): product of recording and track density.
Modern disks partition tracks into disjoint subsets called
recording zones
▪ Each track in a zone has the same number of sectors, determined
by the circumference of innermost track.
▪ Each zone has a different number of sectors/track
36
Carnegie Mellon
37
Carnegie Mellon
spindle
spindle
spindle
spindle
38
Carnegie Mellon
Arm
Spindle
39
Carnegie Mellon
40
Carnegie Mellon
Disk Access
41
Carnegie Mellon
Disk Access
Rotation is counter-clockwise
42
Carnegie Mellon
43
Carnegie Mellon
44
Carnegie Mellon
45
Carnegie Mellon
46
Carnegie Mellon
47
Carnegie Mellon
After BLUE read Seek for RED Rotational latency After RED read
48
Carnegie Mellon
After BLUE read Seek for RED Rotational latency After RED read
49
Carnegie Mellon
50
Carnegie Mellon
52
Carnegie Mellon
I/O Bus
CPU chip
Register file
ALU
System bus Memory bus
I/O Main
Bus interface
bridge memory
Main
Bus interface
memory
I/O bus
Main
Bus interface
memory
I/O bus
Main
Bus interface
memory
I/O bus
Sequential read tput 250 MB/s Sequential write tput 170 MB/s
Random read tput 140 MB/s Random write tput 14 MB/s
Rand read access 30 us Random write access 300 us
58
Carnegie Mellon
Disadvantages
▪ Have the potential to wear out
▪ Mitigated by “wear leveling logic” in flash translation layer
▪ E.g. Intel X25 guarantees 1 petabyte (1015 bytes) of random
writes before they wear out
▪ In 2010, about 100 times more expensive per byte
Applications
▪ MP3 players, smart phones, laptops
▪ Beginning to appear in desktops and servers
59
Carnegie Mellon
Storage Trends
SRAM
Metric 1980 1985 1990 1995 2000 2005 2010 2010:1980
DRAM
Metric 1980 1985 1990 1995 2000 2005 2010 2010:1980
Disk
Metric 1980 1985 1990 1995 2000 2005 2010 2010:1980
Clock
rate (MHz) 1 20 150 600 3300 2000 2500 2500
Cycle
time (ns) 1000 50 6 1.6 0.3 0.50 0.4 2500
Cores 1 1 1 1 1 2 4 4
Effective
cycle 1000 50 6 1.6 0.3 0.25 0.1 10,000
time (ns)
61
Carnegie Mellon
1,000,000.0
SSD
100,000.0
Disk seek time
10,000.0
Flash SSD access time
DRAM access time
1,000.0
ns
100.0
DRAM CPU cycle time
Effective CPU cycle time
10.0
1.0
0.1 CPU
0.0
1980 1985 1990 1995 2000 2003 2005 2010
Year 62
Carnegie Mellon
63
Carnegie Mellon
Today
Storage technologies and trends
Locality of reference
Caching in the memory hierarchy
64
Carnegie Mellon
Locality
Principle of Locality: Programs tend to use data and
instructions with addresses near or equal to those they
have used recently
Temporal locality:
▪ Recently referenced items are likely
to be referenced again in the near future
Spatial locality:
▪ Items with nearby addresses tend
to be referenced close together in time
65
Carnegie Mellon
Locality Example
sum = 0;
for (i = 0; i < n; i++)
sum += a[i];
return sum;
Data references
▪ Reference array elements in succession
(stride-1 reference pattern). Spatial locality
▪ Reference variable sum each iteration. Temporal locality
Instruction references
▪ Reference instructions in sequence. Spatial locality
▪ Cycle through loop repeatedly. Temporal locality
66
Carnegie Mellon
Locality Example
Question: Does this function have good locality with
respect to array a?
68
Carnegie Mellon
Memory Hierarchies
Some fundamental and enduring properties of hardware
and software:
▪ Fast storage technologies cost more per byte, have less capacity,
and require more power (heat!).
▪ The gap between CPU and main memory speed is widening.
▪ Well-written programs tend to exhibit good locality.
69
Carnegie Mellon
Today
Storage technologies and trends
Locality of reference
Caching in the memory hierarchy
70
Carnegie Mellon
L1: L1 cache
Smaller, (SRAM) L1 cache holds cache lines retrieved
from L2 cache
faster,
costlier L2:
L2 cache
per byte L2 cache holds cache lines
(SRAM)
retrieved from main memory
L3:
Larger, Main memory
(DRAM) Main memory holds disk blocks
slower, retrieved from local disks
cheaper
per byte L4: Local secondary storage Local disks hold files
(local disks) retrieved from disks on
remote network servers
71
Carnegie Mellon
Caches
Cache: A smaller, faster storage device that acts as a staging
area for a subset of the data in a larger, slower device.
Fundamental idea of a memory hierarchy:
▪ For each k, the faster, smaller device at level k serves as a cache for the
larger, slower device at level k+1.
Why do memory hierarchies work?
▪ Because of locality, programs tend to access the data at level k more
often than they access the data at level k+1.
▪ Thus, the storage at level k+1 can be slower, and thus larger and
cheaper per bit.
Big Idea: The memory hierarchy creates a large pool of
storage that costs as much as the cheap storage near the
bottom, but that serves data to programs at the rate of the
fast storage near the top.
72
Carnegie Mellon
4 5 6 7
8 9 10 11
12 13 14 15
73
Carnegie Mellon
Block b is in cache:
Cache 8 9 14 3
Hit!
Memory 0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
74
Carnegie Mellon
75
Carnegie Mellon
Capacity miss
▪ Occurs when the set of active cache blocks (working set) is larger than
the cache.
76
Carnegie Mellon
Web cache Web pages Remote server disks 1,000,000,000 Web proxy
server
77
Carnegie Mellon
Summary
The speed gap between CPU, memory and mass storage
continues to widen.
78
Carnegie Mellon
Cache Memories
79
Carnegie Mellon
Today
Cache memory organization and operation
Performance impact of caches
▪ The memory mountain
▪ Rearranging loops to improve spatial locality
▪ Using blocking to improve temporal locality
80
Carnegie Mellon
Cache Memories
Cache memories are small, fast SRAM-based memories
managed automatically in hardware.
▪ Hold frequently accessed blocks of main memory
CPU looks first for data in caches (e.g., L1, L2, and L3),
then in main memory.
Typical system structure:
CPU chip
Register file
Cache
ALU
memories
System bus Memory bus
I/O Main
Bus interface
bridge memory
81
Carnegie Mellon
set
line
S = 2s sets
Cache size:
v tag 0 1 2 B-1 C = S x E x B data bytes
valid bit
B = 2b bytes per cache block (the data)
82
Carnegie Mellon
• Locate set
Cache Read • Check if any line in set
has matching tag
E = 2e lines per set • Yes + line valid: hit
• Locate data starting
at offset
Address of word:
t bits s bits b bits
S = 2s sets
tag set block
index offset
v tag 0 1 2 B-1
valid bit
B = 2b bytes per cache block (the data)
83
Carnegie Mellon
84
Carnegie Mellon
L1 L1 L1 L1 L2 unified cache:
d-cache i-cache
… d-cache i-cache 256 KB, 8-way,
Access: 11 cycles
Main memory
85
Carnegie Mellon
86
Carnegie Mellon
87
Carnegie Mellon
88
Carnegie Mellon
89
Carnegie Mellon
90
Carnegie Mellon
Concluding Observations
Programmer can optimize for cache performance
▪ How data structures are organized
▪ How data are accessed
▪ Nested loop structure
▪ Blocking is a general technique
All systems favor “cache friendly code”
▪ Getting absolute optimum performance is very platform specific
▪Cache sizes, line sizes, associativities, etc.
▪ Can get most of the advantage with generic code
▪ Keep working set reasonably small (temporal locality)
▪ Use small strides (spatial locality)
91