#FT0000135. Mô phỏng bộ nhớ đệm (Cache Simulator)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Mô phỏng bộ nhớ đệm (Cache Simulator)
Cache Simulator
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
A processor accesses nonnegative integer memory addresses. There are independent caches with capacities distinct addresses. Initially all caches are empty. Each cache follows LRU (Least Recently Used) replacement: accessing a resident address is a hit and makes that address most recently used; accessing an absent address is a miss, inserts that address, and evicts the least recently used one if necessary.
You are given commands:
ADDR x: access the single address .RANGE b y k: access addresses in exactly that order. The stride may be negative or zero.STAT: print the number of misses since the precedingSTATfor every cache in the initial capacity order, then reset all miss counters to zero.STATdoes not clear or otherwise alter cache contents.END: marks the end of input and is not a memory access.
The last command before END is guaranteed to be STAT. Every cache sees the same sequence of references, but cache capacities differ. All accessed addresses, including every address generated by RANGE, are valid 24-bit addresses.
Input
The first line contains the number of caches . The second line lists strictly increasing capacities . Subsequent input contains ADDR x, RANGE b y k, and STAT commands. The argument of ADDR may appear on the next line because whitespace separates tokens. The final line is END.
Output
For every STAT, print one line of exactly integers: miss counts since the previous STAT, in the cache order .
Subtasks
- Subtask 1 (20 points): ; ; every accessed address lies in ; for every
RANGE; where counts all accesses generated byADDRandRANGE; at most commands excludingEND; at least oneSTAT, and the last command beforeENDisSTAT. - Subtask 2 (30 points): ; ; every accessed address lies in ; for every
RANGE; where counts all accesses generated byADDRandRANGE; at most commands excludingEND; at least oneSTAT, and the last command beforeENDisSTAT. - Subtask 3 (50 points): ; ; every accessed address lies in ; for every
RANGE; where counts all accesses generated byADDRandRANGE; at most commands excludingEND; at least oneSTAT, and the last command beforeENDisSTAT.
Examples
Example 1
Input
2
4 8
RANGE 1 1 5
RANGE 2 1 2
ADDR
99
STAT
ADDR
2
RANGE 5 -1 2
STAT
RANGE 0 10000 10
RANGE 0 20000 5
RANGE 0 30000 4
STAT
END
Output
6 6
1 0
18 13
Explanation
The first RANGE accesses five new addresses , causing five misses per cache. RANGE 2 1 2 revisits , both hits, and ADDR 99 adds one miss, so the first STAT prints 6 6. Caches are not cleared. Next, ADDR 2 hits in the capacity-8 cache and RANGE 5 -1 2 accesses ; the capacity-4 cache incurs one miss while capacity-8 incurs none, giving 1 0. Across the final three RANGE commands, the capacity-4 cache incurs , , and misses, totalling . The capacity-8 cache incurs , , and misses, totalling . These are counts since the most recent STAT, so the third line is 18 13.
Example 2
Input
2
2 3
ADDR 1
ADDR 2
ADDR 3
ADDR 1
STAT
ADDR 2
STAT
END
Output
4 3
1 0
Explanation
The first four accesses are . The size-2 cache has only the two most recently used addresses, so the last access to misses, for misses total. The size-3 cache holds all three addresses, so that access hits, for misses. After STAT, cache contents remain unchanged. The next access to misses in the size-2 cache (which contains ) but hits in size 3, hence 1 0.