Solutions
A
Hit ratio = __ 0.95 __(A01)
AMAT = HitTime + MissPenalty * MissRatio
1.5 cycles = 1 cycle + (10 cycles) * (1 – HitRatio)
0.05 = 1 - HitRatio
HitRatio = 0.95
10
additional cycles to retrieve the word from main memory, store it in the cache, and return it to the CPU. If we want an average memory access time of 1.4
cycles, what is the minimum possible value for the cache’s hit ratio?Minimum possible value of hit ratio: __ 0.96 __(A02)
AMAT = HitTime + MissRatio * MissPenalty
1.4 = 1 + (1 - HitRatio) * 10
HitRatio = 0.96
B
Consider that P1
is a five-stage pipelined RISC-V processor (IF, DEC, EXE, MEM, WB), where:
Assume that the loop shown to the right has been running for a while and will be re-executed after this iteration.
Cycles per iteration on this processor
P1
: __ 6 __(B01)
Data Hazards will happen between
add x3, x2, x1
andxor x4, x3, x4
,slli x2, x2, 1
andbnez x2, loop
.
There is no load-use hazard, so we do not stall the pipeline.
Forwarding
Flushing
Now consider that P2
is a variant of the previous five-stage pipelined RISC-V processor (IF, DEC, EXE, MEM, WB), where:
Assume that our example loop (same as the previous program) has been running for a while and will be re-executed after this iteration. How many cycles does it take to execute one iteration of the loop on this processor?
Cycles per iteration on processor
P2
: __ 8 __ (B02)
Cycle 1 2 3 4 5 6 7 8 9 IF add slli xor benz benz banz and or add ID add slli xor xor xor benz and flush EX add slli NOP NOP xor benz flush MEM add slli NOP NOP xor benz WB add slli NOP NOP xor In the 5-th cycle, because no bypass path, so the
x3
value of add need first write back to Register, so insert twoNOP
to wait it to actually write back, also in cycle.
C
You are designing a four stage RISC-V processor (IF, DEC, EX, WB) that:
This processor has a tight area constraint, and you only have enough area for one bypass path for read-after-write hazards. You are trying to decide whether to put the bypass path from EX to DEC or from WB to DEC. As part of this evaluation, you construct two processors:
P1
: Bypassing from WB to DEC.P2
: Bypassing from EX to DEC.These processors ensure that if the instruction in the DEC stage does not have all of its operands ready to read from the register file or read from a bypass path, the instruction in the decode stage is stalled.
You are using the following loop of a program to evaluate the performance of the processor:
For the following questions, assume this loop has been running for a long time, and that the processor assumes that the branch will be taken.
How many cycles does this loop take to execute in each of the processors?
P1
cycles per iteration: __ ? __P2
cycles per iteration: __ ? __Processor
P1
cycles per iteration: __ 7 __(C01)
ProcessorP2
cycles per iteration: __ 8 __(C02)
P1
cycles per iteration:
Data Hazards is betweenlw t0, 0(a0)
andaddi t1, t0, 1
, and also betweenaddi t1, t0, 1
andxor t3, t1, a0
.
So we can stall a cycle to wait thelw
instruction and pass the data toDEC
stage.
P2 cycles per iteration:
Only the forwarding fromEX
toDEC
, we need to wait the data memory responses untilWB
stage.
D
Consider the execution of the following code sequence on a 5-stage pipelined RISC-V processor, which is fully bypassed. The code below iterates over the array shown below to find the index of the element whose value is 0x104. Thus, on the second iteration of the loop, it branches to label done. The instruction unimp signals the end of the program. This processor:
The program:
N/A
.
Instructions stalled in DEC stage: __ beq __(D01)
This program's behavior is similar to the following c code, find the index of element 0x104.
- There has the data hazard between
lw x14, 0(x13)
andbeq x14, x10, done
, and also betweenaddi x13, x13, 4
andsub x13, x13, x11
,sub x13, x13, x11
.- The load-use hazard between
lw x14, 0(x13)
andbeq x14, x10, done
, so we need to stall2
cycles to wait the data write-back.- Because we always speculates that the next instruction should come from address PC+4, so the instruction in IF stage for the cycle 7 is
sub
.- Therefore, only
beq
instruction stalled in the DEC stage.
Number of NOPs added to handle stalls: __ 2 __(D02)
We need to stall
2
cycle for the load-use hazard in the one iteration of the loop, we can see the2
NOP instruction called at the cycle5
.
Number of cycles for one iteration of the loop: __ 8 __(D03)
The number of cycles between two
lw
instruction is 8.
Number of NOPs added to handle stalls: __ 2 __(D04)
Cycle 9 10 11 12 13 14 15 16 17 IF lw beq addi addi addi j sub srli unimp DEC NOP lw beq beq beq addi NOP sub srli EXE NOP NOP lw NOP NOP beq NOP NOP sub MEM j NOP NOP lw NOP NOP beq NOP NOP WB addi j NOP NOP lw NOP NOP beq NOP
- The NOPs instruction at the cycle 9 and the cycle 15 are for branch hazard to flush the mistake instruction.
- The same as the problem 2, we need to insert 2
NOP
instructions to stall for the load-use hazards at the cycle 13.
Instructions stalled due to removal of EXE bypass path: __ srli __(D05)
Cycle 9 10 11 12 13 14 15 16 17 18 IF lw beq addi addi addi j sub srli unimp unimp DEC NOP lw beq beq beq addi NOP sub srli srli EXE NOP NOP lw NOP NOP beq NOP NOP sub NOP MEM j NOP NOP lw NOP NOP beq NOP NOP sub WB addi j NOP NOP lw NOP NOP beq NOP NOP
Number of NOPs required to handle removal of EXE bypass path: __ 1 __(D06)
We need to stall
1
cycle to wait the dependant data calculated and through forwarding from MEM to DEC.
E
Consider a direct-mapped cache with 64 total data words with 1 word/cache line.
The program shown below repeatedly executes an inner loop that sums the 16 elements of an array that is
stored starting in location 0x310
.
The program is executed for many iterations, then a measurement of the cache statistics is made during one iteration through all the code, i.e., starting with the execution of the instruction labeled outer_loop:
until just before the next time that instruction is executed.
In total, how many instruction fetches occur during one complete iteration of the outer loop? How many data reads?
Number of instruction fetches: __ 83 __(E01)
Number of data reads: __ 16 __(E02)
Instruction fetch = 2 + 5 * 16 + 1 = 83
2 instructions before loop label are executed once. The 5 instructions in the loop are executed 16 times, andj outer_loop
is executed once.The inner loop runs for 16 iterations.
lw
is called once per iteration.
The memory address has 32 bits total which will be divided up into bits for the tag, index, block offset and byte offset.Since there are 64 total words with 1 word/cache line, there must be 64 cache lines. The index bits must fully index into these 64 lines. This requires 6 bits. Each cache line holds one block which is specified to be one word (32 bits or 4 bytes). Since there is only one word in each block, we need no bits (i.e. 0 bits) for this offset. Since each byte has its own address, we need four possible offsets into each block. Thus, the two bits are sufficient for the byte offset. Tag: 24 bits, Index: 6 bits, Block offset: 0 bits, Byte offset: 2 bits. For each memory access, we look at bits [7:2]
of the address to determine which index in the cache that particular memory address maps to. Remember that both instructions and the array are stored in memory. Thus, each instruction fetch is a memory access and is also cached along with all "normal" memory accesses in the form of lw
instructions. Also note that this program has been executing for multiple cycles already. Thus, many slots in the cache are already populated. We need to figure out which ones will result in cache misses. As we walk through the program for the first time, we will not count initializing the cache contents as cache misses. How many instruction fetch misses occur during one complete iteration of the outer loop? How many data read misses? Hint: remember that the array starts at address 0x310.
Number of instruction fetch misses: __ 4 __(E03)
Number of data read misses: __ 4 __(E04)
We fetch and cache the lw instruction next at index 0b0100
. The lw instruction accesses address 0x310
(the first element of the array). Bits [7:2]
of 0x310
are 0b100
. But the cache line at index 0b0100
already holds the lw instruction which must be evicted. This is the first data read miss. The next two add and bne
instructions are cached in indices 0b0101
and 0b0110
respectively.
On the next iteration of the inner loop, the subi and slli instructions are already cached. The lw
instruction is not. This is the first instruction fetch miss. We fetch the instruction and cache it into index 0b0100
, evicting the previously cached memory contents at address 0x310
from the last iteration. Next, we look at the next element of the array at address 0x314
. This gets cached at index 0b101
, evicting the cached add instruction (the second data read miss). Almost immediately, we need to fetch the add instruction again (the second instruction fetch miss). This evicts the memory contents of 0x314
which we just cached. At this point, observe the caching pattern. All cached instructions in indices 0b0100
through 0b10011
(the cache indices containing the elements of the array) will be evicted once, replaced with array elements, and ultimately restored in a subsequent iteration. This leads to 4 instruction fetch misses and 4 data read misses.
Index | Content |
---|---|
0000 | addi |
0001 | mv |
0010 | subi |
0011 | slli |
0100 | lw Mem[0x310] lw |
0101 | add Mem[0x314] add |
0110 | bne Mem[0x318] bne |
0111 | j outer_loop Mem[0x31C] j outer_loop | |
1000 | Mem[0x320] |
1001 | Mem[0x324] |
1010 | Mem[0x328] |
… | … |
As a final note, notice that on the next iteration of the outer loop, most of the cache indices will already contain the correct values which is the case if this code has been running for a while. This is why we did not treat all the cache initializations as cache misses –- because previous runs had already set the cache.
Hit ratio: __ 91/99 __(E05)
The total number of memory accesses is the sum of the instruction fetches and data reads (). There were 4 instruction fetch misses and four data read misses, so the total number of cache hits is . The hit ratio is therefore .
F
Assume, the program shown below is being run on a RISC-V processor with a cache with the following parameters:
The cache will divide the 32-bit address supplied by the processor into four fields: 2 bits of byte offset, B bits of block offset, L bits of cache line index, and T bits of tag field. Based on the cache parameters given above, what are the appropriate values for B, L, and T?
value for B: __ 1 __(F01)
value for L: __ 3 __(F02)
value for T: __ 26 __(F03)
B: 2 words per line;
L: 8 lines per ways
T: 32 - 6 = 26
If the SLLI
instruction is resident in a cache line, what will be its cache line index? the value of the tag field for the cache?
Cache line index for SLLI when resident in cache: __ 1 __(F04)
Tag field for SLLI when resident in cache: __ 0x9 __(F05)
Instruction address = base + instruction_index * 4 = 0x240 + 3*4 = 0x24C
0x24C = 0b00000000000000000000001001_001_100
Given that the code begins at address 0x240
and the array begins at address 0x420
, and that there are 16 elements in the array as shown in the code above, list all the values j () where the location holding the value A[j]
will map to the same cache line index as the SLLI
instruction in the program.
List all
j
whereA[j]
have the same cache line index asSLLI
: __ 10, 11 ___(F06)
The array addresses that overlap are:
0x448 = 0b00000000000000000000010001_001_000
and
0x44C = 0b00000000000000000000010001_001_100
These correspond to the 10th and 11th elements of the array.
G
Consider a 2-way set-associative cache where each way has 4 cache lines with a block size of 2 words. Each cache line includes a valid bit (V) and a dirty bit (D), which is used to implement a write-back strategy. The replacement policy is least-recently-used (LRU). The cache is used for both instruction fetch and data (LD,ST) accesses. Please use this cache when answering questions (A) through (D).
Using this cache, a particular benchmark program experiences an average memory access time (AMAT) of 1.3 cycles. The access time on a cache hit is 1 cycle; the miss penalty (i.e., additional access time) is 10 cycles. What is the hit ratio when running the benchmark program?
Hit ratio for benchmark program: __ 0.97 __(G01)
AMAT = hit_time + (1 - hit_ratio) * miss_penalty
1.3 = 1 + (1 - HR) * 10
This cache is used to run the following benchmark program. The code starts at memory address 0
; the array referenced by the code has its first element at memory address 0x200
. First determine the number of memory accesses (both instruction and data) made during each iteration through the loop. Then estimate the steady-state average hit ratio for the program, i.e., the average hit ratio after many iterations through the loop.
Number of memory accesses made during each iteration of the loop: __ 8 __(G02)
Estimated steady-state average hit ratio: __ 15/16 __(G03)
In steady state:
0b1000
The other way of our cache can be used to load data from our array starting at address 0x200
. Every time we encounter a new element of the array not in the cache, our cache load request will actually fetch two words (i.e. to contiguous elements in our array); thus, in the next iteration of our loop, we will not need to fetch again, as the next element is already in our cache.
Thus, the data fetch instruction only (lw) misses every other iteration of the while loop. Since there are 8 total fetches per while loop iteration, we see that one out of every 2 * 8 = 16 instructions is a miss.
Steady-state hit ratio = (8 - 0.5) / 8 =
H
Consider the diagram below for a 2-way set associative cache to be used with our RISC-V processor. Each cache line holds a single 32-bit word of data along with its associated tag and valid bit (0
when the cache line is invalid, 1
when the cache line is valid).
The RISC-V produces 32-bit byte addresses, A[31:0]. To ensure the best cache performance, which address bits should be used for the cache index? For the tag field?
address bits used for cache index: A[ __ 4:2 __] (H01)
address bits used for tag field: A[ __ 31:5 __] (H02)
2 bits for byte offset (both 0)
1 word per block implies block offset = 0 bits (no need to select)
8 lines/way implies index = 3 bits
Remaining 27 bits are tag bits.
Suppose the processor does a read of location 0x5678
. Identify which cache location(s) would be checked to see if that location is in the cache. For each location specify the cache section (P
or Q
) and row number (0 through 7). E.g., 3P
for row 3
, section P
. If there is a cache hit on this access what would be the contents of the tag data for the cache line that holds the data for this location?
cache location(s) checked on access to
0x5678
: __ 6P and 6Q __(H03)
cache tag data on hit for location0x5678
(hex): __ 0x2B3 __(H04)
0x5678
in binary:
Line: 3’b110 = 6
Tag: 0b0010_1011_0011 = 0x2B3
Remember, all ways are checked in parallel when searching a set-associative cache.
Estimate the approximate cache hit ratio for the following program. Assume the cache is empty before execution begins (all the valid bits are 0) and that an LRU replacement strategy is used. Remember the cache is used for both instruction and data (LD) accesses.
approximate hit ratio: __ 5/6 __(H05)
The first instruction in the loop (lw) is at 0xC = 0b1000. Extracting the index bits from this address, we see that the instructions within the loop body (lw - bnez) occupy one of the ways in the cache.
Thus, one way is always free for data. Each iteration causes a data fetch that is a cache miss (since there is only a single word per cache line no spatial locality). Thus, each iteration there are 5 instruction fetches (hits) and 1 data fetch (always a miss).