曾謙文
A
Given that we are developing an operating system kernel for RISC-V, implement mutex routines to handle locking and unlocking.
Complete the above implementation to ensure the mutex functions correctly using the RISC-V Atomic extension.
A01 = ?
amoswap.w.aq
A02 = ?
old != MUTEX_LOCKED
ORold != 1
A03 = ?
amoswap.w.rl
A01 amoswap.w.aq
The amoswap.w.aq
instruction performs an atomic swap of a word in memory with a register value. The .aq
(acquire) suffix ensures memory ordering semantics such that no subsequent memory accesses are reordered before this atomic operation. In the context of mutex_trylock
, this ensures that the attempt to lock the mutex is visible to all threads/processes and enforces proper memory synchronization.
This instruction atomically swaps the value of the mutex with MUTEX_LOCKED
, which attempts to acquire the lock. The previous value (old
) is stored to determine if the lock was already held.
A02(old != MUTEX_LOCKED)
The return value of mutex_trylock
depends on whether the lock was successfully acquired. The variable old
holds the mutex's previous value:
old == MUTEX_UNLOCKED
(or 0), it indicates that the lock was not held, and the function can acquire the lock by setting it to MUTEX_LOCKED
.old == MUTEX_LOCKED
(or 1), it indicates the lock is already held, and the function returns false
.Thus, the return condition old != MUTEX_LOCKED
(or equivalently old != 1
) reflects whether the lock acquisition was successful.
A03amoswap.w.rl
The amoswap.w.rl
instruction atomically swaps a word in memory with a register value and enforces release semantics. The .rl
(release) suffix ensures memory ordering semantics such that no previous memory accesses are reordered after this atomic operation. In the context of mutex_unlock
, this ensures that all updates made while holding the lock are visible before the mutex is unlocked.
This instruction sets the mutex back to MUTEX_UNLOCKED
(or 0) atomically, ensuring that no thread observes the lock in an inconsistent state.
Next, we will implement a condition variable based on the mutex implementation provided earlier.
Complete the above implementation to ensure the condition variable function-like macros correctly.
A04 = ?
(cond)->waiting++
A05 = ?
mutex_lock(&(cond)->mutex)
A06 = ?
(cond)->status = 0
A07 = ?
mutex_unlock(&(cond)->mutex)
A08 = ?
mutex_unlock(&(cond)->mutex)
A04 (cond)->waiting++
When a thread waits on a condition variable, it must indicate that it is waiting by incrementing the waiting
counter. This ensures that the condition variable tracks how many threads are currently blocked and waiting for a broadcast signal.
By incrementing (cond)->waiting
, the thread signals that it has joined the queue of waiting threads. This step is essential for proper synchronization and allows the cond_broadcast
function to determine when all waiting threads have been notified.
A05 mutex_lock(&(cond)->mutex)
To check or modify the status
of the condition variable, the thread must acquire the condition variable's internal mutex. This ensures that only one thread can access or modify the condition variable's state at a time.
This line locks the internal mutex of the condition variable, ensuring thread-safe access to its status
field during the waiting loop.
A06 (cond)->status = 0
After all waiting threads have been signaled, the status
of the condition variable should be reset to 0
to indicate that no further notifications are pending. This allows the condition variable to be reused in subsequent synchronization operations.
Resetting the status
ensures that the condition variable does not incorrectly retain a "signaled" state after all waiting threads have been handled.
A07 mutex_unlock(&(cond)->mutex)
After modifying the condition variable's state, the thread releases the condition variable's internal mutex. This allows other threads to access the condition variable and ensures proper synchronization.
This line unlocks the internal mutex, ensuring that the condition variable remains available for other threads to use or modify.
A08 mutex_unlock(&(cond)->mutex)
If the thread finds that the condition variable's status
is not set to 1
, it releases the internal mutex and waits for the next opportunity to check the condition. This prevents a deadlock and allows other threads to operate on the condition variable.
This ensures that the thread does not block the condition variable's internal mutex while it waits for a broadcast signal.
Lastly, we aim to implement pthread_spin_lock using the RISC-V Atomic extension. Below is the C implementation:
And the RISC-V implementation:
A09 = ?
bnez t0, lock_held
A10 = ?
amoswap.w.aq t0, t1, (a0)
A09 bnez t0, lock_held
The instruction bnez t0, lock_held
checks whether the lock is already held by testing if the value in t0
is non-zero. If t0
is non-zero (indicating that the lock is held), the function jumps to the lock_held
label to return the value of EBUSY
. This avoids attempting to acquire the lock when it is already held.
bnez
(branch if not equal to zero) checks whether the lock's current value (t0
) is non-zero.lock_held
label to handle the case where the lock cannot be acquired.A10 amoswap.w.aq t0, t1, (a0)
The amoswap.w.aq
instruction performs an atomic swap between a memory location (*lock
) and a register value (t1
, which holds EBUSY
). This atomic operation ensures that the attempt to acquire the lock is thread-safe. The .aq
(acquire) suffix ensures proper memory ordering so that no subsequent memory operations are reordered before the lock is acquired.
aq t1, (a0) swaps the value of the lock (
*lock) with
t1(which is
EBUSY`).
t0
, which is later returned to indicate whether the lock was successfully acquired (t0 == 0
) or already held (t0 != 0
).Rewrite Problem A from Quiz 6: Operating in a bare-metal environment (using QEMU/RV32). The program should implement a mutex lock and condition variable, and include a test program.
Implementation Flowchart:
Result:
B
Let's assume we are developing an operating system kernel, and now it's time to implement the context switching mechanism for RISC-V.
The generic code:
IRQ code:
B01 = ?
sw t5, -13*4(sp)
B02 = ?
-13*4
B03 = ?
13*4
B04 = ?
mscratch
B05 = ?
sp
B06 = ?
-13*4(sp)
B01 sw t5, -13*4(sp)
The program counter (PC) of the current task is saved to the stack using sw t5, -13*4(sp)
. Since t5
holds the value of the program counter (mepc
), this step ensures the task's execution state is stored and can be restored later when the task is rescheduled.
This saves the program counter at the -13*4
offset from the current stack pointer, ensuring proper ordering in the task's saved context.
B02 -13*4
After saving the registers and program counter to the stack, the stack pointer (sp
) needs to be adjusted to account for the 13 saved values (12 callee-saved registers plus the program counter). The adjustment is negative because the stack grows downwards.
The value -13*4
reduces the stack pointer to reserve space for the saved context.
B03 13*4
When restoring the stack pointer for the next task, it must be incremented by the same amount that was subtracted during the save process (13*4
). This adjustment ensures the stack pointer is restored to the correct position for the new task's context.
The value 13*4
accounts for the saved registers and program counter when restoring the new task's stack pointer.
B04 mscratch
The mscratch
register is used as a temporary storage location for saving the system stack pointer (sp
). Writing the current stack pointer (sp
) to mscratch
ensures that it can be restored later when switching back to the system stack.
The mscratch
register serves as a scratchpad for saving/restoring the system stack pointer during the context switch.
B05 sp
The current system stack pointer (sp
) is written to the mscratch
register. This ensures that the system stack pointer can be restored when exiting the interrupt or performing subsequent context switches.
Writing sp
to mscratch
ensures the system stack pointer is preserved during the context switch.
B06 -13*4(sp)
To restore the program counter for the next task, the value is loaded from the stack at the offset -13*4
relative to the stack pointer (sp
). This corresponds to the position where the program counter was saved during the context save process.
This retrieves the program counter of the new task from the saved context on its stack, enabling the resumption of its execution.
Rewrite Problem B from Quiz 6: Operating in a bare-metal environment (using QEMU/RV32). The program should implement a mutex lock and condition variable, and include a test program.
Implementation:
System Initialization
Task Management
Interrupt Handling
Scheduling
Debug Features
Implementation Flowchart:
Code
I try to use gdb set the break point and find the it will jump directly to handle_trap and loop.
Possible questions:
Consider a complex task divided into four segments: PartA, PartB, PartC, and PartD. Each segment is assigned to a core in a quad-core processor system, where each core has its own dedicated cache. Cache coherence is maintained using a snoopy-based, write-invalidate MSI (Modified, Shared, Invalid) protocol, as depicted. The objective is to analyze the performance of this system and explore potential optimizations for improvement.
Each core begins by accessing and updating its local copy of the population data, stored under a designated label (e.g., pop_A for the population of PartA). This process is illustrated in the pseudocode below:
The sequence of memory accesses for this process is as follows:
(1) lw a1, pop_A |
(2) lw a1, pop_B |
(3) lw a1, pop_C |
(4) lw a1, pop_D |
---|---|---|---|
(5) sw a1, pop_A |
(6) sw a1, pop_B |
(7) sw a1, pop_C |
(8) sw a1, pop_D |
Our focus is to closely analyze the activities occurring on Core A. Since the memory accesses are independent (i.e., pop_A
, pop_B
, pop_C
, and pop_D
correspond to distinct memory locations), we can concentrate solely on Core A's memory interactions to understand its behavior. Please complete the table below by filling in columns C01 and C02. For each entry, describe the bus transaction(s) triggered by each access and the resulting state of the cache line for pop_A
after the access.
Access | Shared bus transactions | Cache A |
---|---|---|
Initial state | - | pop_A: I |
lw a1, pop_A | BusRd | pop_A: C01 |
sw a1, pop_A | BusRdX | pop_A: C02 |
C01 = ?
S
C02 = ?
M
The MSI protocol defines three cache states:
Actions and State Transitions:
lw a1, pop_A
), it issues a BusRd request to load the data from memory, moving the cache line from the I state to the S state.pop_A
(sw a1, pop_A
), it issues a BusRdX request to gain exclusive access to the data, transitioning the cache line to the M state.We are interested in exploring whether adopting the MESI protocol, as illustrated, could improve the performance of this processor. Assuming the four cores utilize a snoopy-based, write-invalidate MESI protocol, what would be the sequence of bus transactions and the cache line states for pop_A on Core A? Please complete the table below by filling in columns C03 and C04.
Access | Shared bus transactions | Cache A |
---|---|---|
Initial state | - | pop_A: I |
lw a1, pop_A | BusRd | pop_A: C03 |
sw a1, pop_A | - | pop_A: C04 |
C03 = ?
E
C04 = ?
M
The MESI protocol improves multi-core processor performance by efficiently managing cache line states across cores. Here's how Core A interacts with its cache line pop_A
under the MESI protocol:
Initial State:
pop_A
starts in the Invalid (I) state, meaning Core A's cache does not have the data or the data is outdated.After Load Operation (lw a1, pop_A
):
pop_A
is not in the Modified (M) or Exclusive (E) state.pop_A
transitions to the Exclusive (E) state in Core A's cache. This means Core A now has the data and can read it without intending to modify it, assuming no other core has the same data cached.After Store Operation (sw a1, pop_A
):
pop_A
is already in the Exclusive (E) state, no additional bus transaction is required.pop_A
changes to Modified (M), indicating Core A has updated the data, and the changes are not yet written back to memory.Each core then updates the overall census count, stored at a shared location labeled sum
. This process is illustrated in the following pseudocode:
The resulting sequence of memory accesses is as follows:
(1) lw a2, sum |
(3) lw a2, sum |
(4) lw a2, sum |
(6) lw a2, sum |
---|---|---|---|
(2) sw a2, sum |
(7) sw a2, sum |
(5) sw a2, sum |
(8) sw a2, sum |
We observe a critical issue in the current census counting implementation: there is a possibility that two processors may simultaneously read sum
, each add their local population count to it independently, and then write back to sum
, resulting in inaccuracies.
To ensure the final value of sum
accurately reflects the combined a0
values from all processors, semaphores should be integrated strategically to enforce mutual exclusion. These semaphores should minimize instruction blocking while preserving data correctness. Define and initialize any necessary semaphores, then insert WAIT
and SIGNAL
commands into the pseudocode below to ensure accurate updates to sum
. It is not required to include ecall
instructions for this modification.
Semaphore Initialization:
Pseudocode:
Specify the exact instruction, labeled as L1
to L7
, after which the WAIT(sum_sem)
command should be inserted to ensure exclusive access to sum
during critical updates, thus maintaining data accuracy.
C05 = ?
L4or
sw a1, <pop_label>
Identify the exact instruction, labeled as L1
to L7
, after which the SIGNAL(sum_sem)
command should be placed. This ensures exclusive access to sum
during critical updates, maintaining data accuracy.
C06 = ?
L7 or
sw a2, sum
To ensure mutual exclusion when updating the global variable sum, the WAIT and SIGNAL semaphore operations should enclose the critical section where sum is accessed. This guarantees that only one processor at a time can read and update sum, preventing race conditions.
C05 L4
Reason: After completing updates to the local population (L4), but before retrieving the global sum value at L5, we must ensure mutual exclusion. The WAIT(sum_sem) ensures that no other processor can access sum until the current processor finishes its update.
C06 L7
Reason: After writing the updated global population count (L7), the critical section is complete, and the semaphore can be released. The SIGNAL(sum_sem) allows other processors to access sum.
We are writing code to compute the greatest common divisor (GCD) using popcount.
Complete the above GCD implementation to make it work as expected.
Reference: Binary GCD algorithm
C07 = ?
(x & (-x)) - 1
C08 = ?
a | b
C09 = ?
a << k
The diagram below illustrates a classic fully-bypassed 5-stage pipeline, enhanced with an unpipelined divider operating in parallel with the ALU. While bypass paths are not depicted, the iterative divider generates 2 bits per cycle until producing a complete 32-bit result.
In this pipeline design, asynchronous interrupts are handled during the MEM
stage and trigger a jump to a dedicated interrupt trap handler address. The interrupt latency is defined as the number of cycles from when an interrupt request is raised in the MEM
stage to when the first instruction of the interrupt handler reaches the MEM
stage.
Consider the following code execution scenario. An interrupt is raised during cycle 8, causing a jump to isr
. The label isr
refers to an Interrupt Service Routine, which increments a counter at a fixed memory address before resuming the original execution context. The architectural guarantee of precise interrupts must be preserved. Assume all memory accesses take one cycle in the MEM
stage.
Notably, the div
instruction in RISC-V does not raise data-dependent exceptions. To prevent pipeline stalls during a multi-cycle divide operation, the pipeline control logic permits subsequent instructions that are independent of the divide result to proceed and complete before the divide finishes execution.
The RISC-V instructions MRET
, HRET
, SRET
, and URET
are used to return from traps in M-mode, H-mode, S-mode, and U-mode, respectively. When executing an xRET
instruction:
xPP
.yIE
) is updated with the value in xPIE
.xPIE
field is set to 1.xPP
field is reset to U.Trap handlers typically save the current system state:
And restore the system state upon executing the MRET
instruction:
Reference: The RISC-V Instruction Set Manual: Volume II - Privileged Architecture
What is the interrupt latency for the code above? __ D01 __ cycles
14
cycles 8 to 21, inclusive.
The interrupt is raised in cycle 4. The earliest uncommitted instruction when the interrupt is taken is labeled with "EPC" (exception PC), while "MTVEC" denotes the first instruction of the interrupt trap handler.
According to problem statement, the div
instruction does not trigger exceptions that depend on the specific data being processed. During the execution of a division, which may take multiple cycles to complete, the processor's pipeline control logic allows other instructions that do not depend on the division result to continue and finish execution without waiting for the division to complete.
The div generates 2 bits at a time, and it takes 16 cycles to complete the 32-bit quotient; if it has run exactly 6 cycles at the moment of detecting the interrupt, there are still 10 cycles left.
The design assumes that the multi-period div cannot be "safely rolled back" midway, and can only wait until it reaches the W stage.
Waiting 10 more cycles between detecting the interrupt (MEM) and being able to actually "flush + redirect to isr".
To ensure the program continues executing correctly, which instruction should the isr
return to? Answer in one of the instructions shown above.
D02 = ?
lui x4, 0x100
The EPC (exception PC) should point to the earliest uncommitted instruction.
When the external interrupt request arrives, it is detected during the MEM stage of the instruction lw x2, 0(x1). Because RISC‑V mandates precise interrupts, this lw is allowed to complete and commit in the following cycle, ensuring that it has written its result correctly. Meanwhile, the processor must identify which subsequent instructions have not yet safely committed by the time the interrupt is taken. Any such instructions must be either flushed or rolled back so that the system can restart from a well‐defined architectural state.
A closer inspection of the pipeline timing reveals that the earliest instruction still in a partially executed (i.e., uncommitted) state is lui x4, 0x100. Although instructions such as div x1, x2, x3 or slli x3, x2, 1 may have entered the pipeline already, by the time the interrupt is recognized and the pipeline is flushed, those instructions are deemed safely retired or do not need re‐execution. Therefore, the privilege architecture sets the EPC (Exception Program Counter) to the first instruction that has not fully committed—namely lui x4, 0x100. When the ISR finishes and executes mret, control flow resumes at that instruction
To reduce interrupt latency, we propose constraining the destination register for a div
instruction. The ABI could be modified to reserve a specific x
register to always store the divide result, allowing the interrupt handler to avoid using it. In a more generalized exception handler, reading this register could be deferred until the end of the context save, potentially hiding the divide latency.
Does this approach require any hardware modifications? __ D03 __ (Answer in Yes
or No
)
What is the corresponding limitation? __ D04 __ (Explain!)
No
It relies on code voluntarily complying with the ABI. (意思相近就給分)
The main limitation is that a general‐purpose register must be “sacrificed” to hold the div result. This reduces flexibility: other code cannot freely use that register, and compilers must insert additional move instructions if the result is needed in a different register.
Suppose you have been tasked with evaluating the performance of two processor designs:
You plan to benchmark both processors using the following assembly sequence:
Processor1 is a 4-stage pipelined processor with full bypassing, designed to reduce the number of cycles required to execute each instruction. It combines the EXE
and MEM
stages, with load requests sent in the EXE
stage and received in the WB
stage one cycle later. The IF
stage speculatively fetches the instruction at PC+4 and annuls incorrectly fetched instructions after a branch misprediction. Branches are resolved in the EXE
stage.
Processor2 is a 6-stage pipelined processor with full bypassing, designed to improve performance by increasing the clock speed. The EXE
stage is divided into two stages (EXE1
and EXE2
) to reduce the critical path through the ALU. The IF
stage always fetches the instruction at PC+4 and annuls incorrectly fetched instructions after a taken branch. ALU and Branch ALU results become available in the EXE2
stage.
How many cycles does it take Processor1 to execute one iteration of the loop? __ E01 __ cycle(s)
8
Tshe loop instruction:
In this 4-stage pipeline (IF, ID, EX, WB), the lw
instruction computes the address in the EX stage and retrieves the data in the WB stage, and the following instruction immediately uses the load result (such as add
) , because the data is not valid until WB, an extra bubble of 1 cycle must be inserted to avoid using invalid register values. In addition, the conditional branch (blt x10, x14, L1
) can only determine whether to jump in the EX stage, so if the branch is established, the next sequential instruction has been fetched in advance and must be canceled, resulting in a 1-cycle branch penalty. Expect. In summary, from the time the first loop instruction slli x11, x10, 2
is fetched (IF) to the time the next loop fetches slli x11, x10, 2
again, there will be a lot of time due to load-use dependencies and The branch penalty adds a total of 2 cycles, bringing the total number of full loop cycles to 8.
How many cycles does it take Processor2 to execute one iteration of the loop? __ E02 __ cycle(s)
12
Cycle 3 NOP:
Inserted to delay the add instruction because the result of lw is not available yet (waiting for lw to proceed to MEM).
Cycle 5 NOP:
Inserted because the add instruction still cannot execute in EXE2 without the result from lw, which only becomes available in WB.
These NOPs are necessary to resolve data hazards caused by the pipeline's inability to forward/load data earlier in this particular processor design.
What is the clock period for Processor1? __ E03 __ ns per loop iteration
48
8 cycles (6 ns/cycle) = 48 ns per loop iteration
Loop Time=Cycles per Loop×Clock Period per Cycle
Don't do this! You don't have to copy and paste from the original reference solution. Instead, annotate along with your thoughts and experiments.
B
General Matrix Multiply (GEMM) is a widely used algorithm in linear algebra, machine learning, statistics, and various other fields. It offers a more complex trade-off space compared to the previous tutorial, as there are multiple ways to structure the computation. These include techniques such as blocking, inner products, outer products, and systolic arrays. Below is the reference implementation:
Take note of the variables i
, j
, and p
used in the nested loops, as they play a key role in iterating over the computation.
Adjusting the order of i
, j
, and p
does not affect the result, but it can impact performance. Considering the cache, what is the optimal order for best performance? Answer in the form "i-j-p".
j-p-i
Thej-p-i
order is well-suited for cache prefetching, resulting in a higher L1 cache hit rate compared to theipj
order.
column‐major storage
i is the fastest‐varying (row) index in memory.
j is effectively the column index for C and B.
p is the “summation” index over the shared dimension k.
In other words, from outer to inner the optimal order is j → p → i
What order results in the worst performance?
i-p-j
The worst performance occurs when the loop order disrupts data locality, causing frequent cache misses due to non-contiguous memory access. For column-major storage (as used in the reference implementation), the worst order is:
Access to C[j * ldc + i]
In this order, j
is in the innermost loop. Since j
corresponds to the column index, changing j
results in large strides of ldc
in memory. This results in poor locality when writing to C
.
Access to A[p * lda + i]
With p
in the middle loop, i
is fixed while iterating over p
. Since p
varies, the memory access to A[p * lda + i]
skips rows in memory, causing poor cache utilization.
Access to B[j * ldb + p]
With j
in the innermost loop, p
is fixed while iterating over j
. This results in large memory strides for B[j * ldb + p]
due to ldb
.
Every other loop order maintains some degree of locality for at least one array. For instance:
j → i → p
keeps access to C
contiguous in the innermost loop.p → i → j
keeps access to A
contiguous for the innermost loop.But in i → p → j
, none of the arrays benefit from good locality, leading to the worst cache performance.
Test the performance of a specific order across different data sizes. From the graph, it can be observed that as the data size increases, overall performance decreases (the fluctuations in the middle may be related to cache evictions and reloads).
It can be observed that as the data size increases, FLOPS (Floating-point operations per second) will decline. Why does FLOPS decrease rapidly as the matrix size grows larger?
When matrices A and B are smaller than the L2 cache, GEMM only needs to read memory equivalent to the size of A and B from DDR. However, when A and B exceed the size of the L2 cache, due to the row-major layout of B or the column-major layout of A being non-contiguous in memory, GEMM reads more memory from DDR than the size of A and B. This increases cache misses, leading to degraded performance.
As the matrix size grows larger, the rapid decline in FLOPS (floating-point operations per second) can be explained by memory hierarchy effects, specifically cache inefficiencies:
Key Reasons for Performance Decrease:
Cache Misses
Small matrices often fit entirely within the CPU's L1 or L2 cache, allowing data reuse with minimal memory access overhead. However, as the matrix size grows, the working set (data required for computation) exceeds cache capacity. This leads to:
Increased cache evictions.
More frequent memory accesses to slower levels of the memory hierarchy (e.g., L3 cache or main memory).
Poor locality due to non-contiguous memory access patterns, especially for column-major storage.
Memory Bandwidth Limitation
Once the data no longer fits in the cache, the CPU becomes memory-bound rather than compute-bound. The performance becomes limited by the bandwidth of the memory subsystem, which cannot supply data to the CPU fast enough to sustain peak floating-point throughput.
Cache Associativity Conflicts
For large matrices, access patterns may cause conflicts in cache lines due to limited associativity, resulting in frequent cache line invalidations or evictions.
DRAM Latency
Accessing main memory (DRAM) introduces significant latency compared to accessing data in the cache. The rapid performance decline is often due to the processor idling while waiting for memory accesses to complete.
To make GEMM faster, let's divide a large matrix into smaller submatrices so that each submatrix can fit entirely in the cache.
First, partition the matrix (along the - and -dimensions) without blocking the -dimension, and then expand:
As shown in the diagram:
The multiplication of the submatrix from and the submatrix from produces a submatrix in (the left one).
Use optimization techniques such as loop unrolling and register caching (reusing register results). Next, divide the large matrix into smaller submatrices to ensure they fit entirely in the cache, thereby improving access performance. The process involves splitting the large matrix into smaller submatrices that can fully reside in the cache for efficient access.
The parameters must meet certain constraints: must be less than half the size of the L2 cache.
Consider the corresponding code below:
jc * ldb + pc
pc * lda + ic
jc * ldc + ic
This corresponds to the starting address of the submatrix of (B) that is being packed. The starting position is determined by the current block column ((jc)) and the current position in the (k)-dimension ((pc)):
jc * ldb
: Shifts to the (jc)-th column block of (B).pc
: Shifts to the current position in the (k)-dimension.This corresponds to the starting address of the submatrix of (A) that is being packed. The starting position is determined by the current block row ((ic)) and the current position in the (k)-dimension ((pc)):
pc * lda
: Shifts to the (pc)-th row block of (A).ic
: Shifts to the current position in the (m)-dimension.This corresponds to the starting address of the submatrix of (C) that is being updated. The starting position is determined by the current block row ((ic)) and the current block column ((jc)):
jc * ldc
: Shifts to the (jc)-th column block of (C).ic
: Shifts to the current position in the (m)-dimension.Matrix (B):
Matrix (A):
Matrix (C):
These adjustments ensure the correct blocks of data are passed into the packing functions and the macro kernel.
C
Determining the center of a peak in discrete data, such as a series of bins, can be useful for identifying the "center" of a signal, particularly in contexts like discrete Fourier transform (DFT) bins, touch sensors, or detecting impulses. This method works especially well with normal distributions but can also be effective for any symmetric or nearly symmetric distribution.
To locate the center, identify the highest peak in the dataset and examine the values of the bins immediately to its left and right. Using these values, you can calculate the center's position relative to the peak using the formula below, which estimates the offset from the highest cell:
This approach does not handle cases involving multiple peaks or multipath effects. In such scenarios, it is necessary to identify other peaks, account for their contributions, and iteratively refine the data to isolate and analyze additional peaks. Additionally, this method only applies to sources with a distinct hill-shaped distribution, as mathematically such centers can be detected.
For computational efficiency, consider using fixed-point arithmetic instead of floating-point. Fixed-point math provides higher accuracy with the same bit-width and is generally faster, particularly in embedded systems, even when a floating-point unit (FPU) is available. This makes it a practical choice for resource-constrained environments where performance and precision are critical.
Consider the code below:
This example illustrates the use of fixed-point arithmetic with 16 fractional bits, a common technique in embedded systems for efficient numerical computation. The variables a
and b
are represented in a fixed-point format, where the fractional part is scaled by . For instance, the value a = 132345
corresponds to , and b = 7491
corresponds to .
The addition operation a + b
combines the fixed-point representations directly without any conversion, keeping the result in the same fixed-point format. To display the result in a human-readable floating-point format, the program divides the sum by , which is the scaling factor for 16 fractional bits.
The program outputs the sum as 2.133728
, demonstrating how fixed-point arithmetic preserves computational efficiency while allowing precise representation of fractional numbers. This approach is particularly valuable in resource-constrained environments where performance and accuracy are critical.
The code below shows a method for computing the product while avoiding overflows.
Complete the code to ensure it works as expected. The variable a
must be defined before the variable big_b
. You should use shifts.
a >> 8
or equivalent
big_b>> 8
or equivalent
In fixed-point arithmetic, multiplication can cause the result to overflow, especially when the values of the two numbers are large. Therefore, it is necessary to reduce the amplitude of the value through "shifting and scaling" to avoid overflow. The specific steps are:
Shift-scale operands: Shift one or both operands right before multiplication to reduce their values.
Shift the result to restore it: After multiplication, shift the result right appropriately to adjust it back to the fixed decimal point ratio.
To prevent overflow:
a
and big_b
down by shifting each of them by 8 bits (dividing by .C01: a >> 8
Scale down a
by dividing it by (2^8 = 256) to reduce its magnitude.
C02: big_b >> 8
Similarly, scale down big_b
by dividing it by (2^8 = 256).
By scaling both operands equally:
Let's perform division in fixed-point arithmetic, reversing the approach of multiplication.
The use of shifts achieves three goals:
Complete the code to ensure it works as expected. You should use shifts.
a << 12
or equivalent
b >> 4
or equivalent
The result, when converted back to floating-point by dividing by , is approximately , which is close to the expected value. This approach demonstrates how to manage precision and avoid overflow in fixed-point division while adhering to the constraints of the representation.
In fixed-point arithmetic, it's common to adjust the scaling of operands to maintain precision and prevent overflow during operations. This often involves shifting operations, which are efficient in binary systems. For instance, left-shifting a number by 12 bits (a << 12
) effectively multiplies it by , and right-shifting by 4 bits (b >> 4
) divides it by . These adjustments help align the operands to a common scaling factor, facilitating accurate division. For a comprehensive understanding of fixed-point arithmetic and the role of scaling and shifting.
D
You are expected to calculate an approximate square root by determining the next power of 2 using the __builtin_clz
function provided by GCC/Clang.
Complete the code to ensure it works as expected. You should invoke the __builtin_clz
function.
(32 - __builtin_clz(i)) / 2
or equivalent
Using 32 - __builtin_clz(i)
ensures better precision and logical consistency when calculating the initial approximation of the square root in a 32-bit integer system. Here's the reasoning:
32-bit Integer Length:
__builtin_clz(i)
calculates the leading zero count in the binary representation of i
. Subtracting this from 32 gives the number of significant bits (or bit length) of i
, which accurately reflects its magnitude in binary.
Purpose of the Approximation:
The formula 1 << (32 - __builtin_clz(i)) / 2
estimates the square root of i
by calculating a power of 2 near the actual square root. For example, if i = 16
(binary 10000
), its bit length is 5 (32 - 27
leading zeros), leading to an initial approximation of , close to the actual square root.
Here is the extracted description with LaTeX annotations:
The Fast Fourier Transform (FFT) is a computational method used to efficiently compute the discrete Fourier transform of a sequence. This implementation also includes a function for convolution, defined as:
This convolution function calculates the resulting sequence by summing the product of elements from the input sequences and , with one sequence reversed and shifted. It is particularly useful in signal processing and applications requiring fast computation of polynomial or data convolutions. Consider the corresponding code below:
The reference output:
Complete the code to ensure it works as expected. You should invoke the __builtin_clz
function.
32 - __builtin_clz(s)
av[i] * bv[i]
or equivalent
s
Represents the Convolution Size:
s = na + nb - 1
is the total number of elements in the convolution result.s
.Using __builtin_clz
:
__builtin_clz(s)
calculates the number of leading zeros in the binary representation of s
. For a 32-bit integer, subtracting this count from 32 gives the number of bits required to represent s
.s
is not already a power of 2), we don't subtract 1 from s
.Updated Calculation:
L = 32 - __builtin_clz(s)
calculates the number of bits required.n = 1 << L
ensures n
is a power of 2 that accommodates s
.D03
should perform the point-wise multiplication of the two frequency-domain arrays av
and bv
to compute the convolution in the frequency domain.
D03
is responsible for multiplying each corresponding element of the arrays av
and bv
.av
and bv
contain the FFT-transformed values of a
and b
, respectively.E
The core concept of data-level parallelism is vectorized computation, which involves performing operations on multiple elements of a single vector simultaneously.
Consider the following function, which computes the product of all elements in an array:
Your task is to vectorize this function using the RISC-V Vector extension to leverage parallel processing.
Complete the code to ensure it works as expected.
vsetvli
vlw.v
vsw.v
vsetvli
: Configures the vector register length based on the element type (e32
for 32-bit integers) and group multiplier (m1
for single register per element). The register t0
stores the calculated vector length.vlw.v
: Loads 4 consecutive 32-bit elements from memory into the vector register v0
.vsw.v
: Stores the result in v1
back into the memory location pointed to by result
.F
Your task is to implement a simulator for the RISC-V instruction set. Below is a partial C source code snippet for instruction decoding and execution.
CSR(imm) = t | R(rs1)
CSR(imm) = R(rs1)
R(rs1) + IMM(I)
16
2
4
0x3f
or equivalent
0x3f
or equivalent
NPC = PC + IMM(J)
or equivalent
0b111
or equivalent
0b110
or equivalent
0b000
or equivalent
0b1110011
or equivalent
0b1110011
or equivalent
0b010000
or equivalent
0b000000
or equivalent
0b1100111
or equivalent
0b0010111
or equivalent
0b0110111
or equivalent
0b1101111
or equivalent
R(0) = 0
or equivalent
F01 = CSR(imm) = t | R(rs1)
This instruction (csrrs
) reads a value from a special register (CSR), does an OR operation with rs1
, saves the result to rd
, and updates the CSR with the new value.
F02 = CSR(imm) = R(rs1)
For csrrw
, it writes the value of rs1
into a special register (CSR) and saves the old value of the CSR into rd
.
F03 = R(rs1) + IMM(I)
This is for jalr
, which calculates the target address by adding the value in rs1
and an immediate value. It ensures the target address is even (aligned).
F04 = 16
The lh
instruction reads 16 bits (2 bytes) of data from memory and extends it to 32 or 64 bits with the same sign.
F05 = 2
The lhu
instruction reads 16 bits (2 bytes) from memory, but it treats the data as positive (unsigned).
F06 = 4
The lw
instruction reads 32 bits (4 bytes) of data from memory.
F07 = 0x3f
This ensures the shift for slli
does not exceed the maximum allowed (63 for 64-bit registers).
F08 = 0x3f
Similar to slli
, this ensures srli
shifts within a valid range (up to 63 for 64-bit).
F09 = NPC = PC + IMM(J)
The jal
instruction jumps to a new program counter (PC) calculated by adding the immediate value to the current PC.
F10 = 0b111
This is the funct3
value for the and
instruction, which does a bitwise AND operation.
F11 = 0b110
This is the funct3
value for the or
instruction, which does a bitwise OR operation.
F12 = 0b000
This funct3
value is for addi
, which adds an immediate value to rs1
and saves the result to rd
.
F13 = 0b1110011
This is the opcode for the ebreak
instruction, which triggers a break (for debugging).
F14 = 0b1110011
This is the opcode for the ecall
instruction, which makes a system call or software interrupt.
F15 = 0b010000
This is the funct7
value for srai
, which does an arithmetic right shift (keeps the sign).
F16 = 0b000000
This is the funct7
value for srli
, which does a logical right shift (fills with zeros).
F17 = 0b1100111
This is the opcode for jalr
, a jump instruction based on a register value.
F18 = 0b0010111
This is the opcode for auipc
, which calculates an address by adding an immediate value to the PC.
F19 = 0b0110111
This is the opcode for lui
, which loads an immediate value into the upper bits of a register.
F20 = 0b1101111
This is the opcode for jal
, which performs an unconditional jump to a new PC.
F21 = R(0) = 0
The x0
register in RISC-V is always zero. This makes sure its value does not change.
G
Our processor features an 8-entry direct-mapped TLB, indexed using the lowest-order bits of the virtual page number. The program below operates on a matrix , which consists of 4 rows and 256 columns. Each element in is a 32-bit integer, laid out in row-major order (i.e., consecutive elements in the same row occupy contiguous memory locations). The program computes the sum of the entries in a top-right submatrix of matrix . Assume starts at virtual address 0x0000, and the result of the sum is stored in a register. Instruction fetches can be ignored.
How many TLB misses will this program encounter when and (i.e., when )?
32
Matrix spans 32 contiguous 128B pages. Since all elements in are accessed sequentially in the order they are laid out in memory, there will be a total of 32 TLB misses.
Calculate the sum of all elements in matrix .
Virtual Memory and TLB (Translation Lookaside Buffer):
When the program accesses matrix , which spans multiple virtual pages,32 TLB Misses occur in total.
What is the highest possible TLB hit rate that this program can achieve?
Keep in mind that each page contains 32 elements of matrix . The maximum TLB hit rate occurs when there is 1 miss to load a page into the TLB, followed by 31 hits, resulting in a hit rate of .
To achieve this, must be , where , and can be any value.
When matrix is accessed, the program processes its elements row by row in memory order. Each page contains 32 elements, and due to the direct-mapped nature of the TLB, entering a new page causes 1 TLB miss. The remaining 31 accesses within that page result in TLB hits because the page is already loaded into the TLB. This access pattern ensures that the hit rate for a single page is .
To achieve the highest TLB hit rate, the matrix's width must be a multiple of , where . This ensures that the accesses align perfectly with page boundaries, avoiding unnecessary TLB conflicts. The number of rows does not affect the hit rate, as long as meets the alignment condition. With this optimized access pattern, the program can achieve the maximum TLB hit rate of .
It has been suggested that increasing the page size could significantly reduce TLB misses in the direct-mapped TLB for this program. For which values of (G03, range) and (G04, range) would doubling the page size fail to improve the TLB hit rate?
or equivalent
any
With the larger page size, each page now contains 64 elements, and a single row of spans 4 pages. For and any , the number of hits remains unchanged compared to smaller pages, as increasing the page size does not reduce compulsory misses.
When the number of rows , increasing the page size does not improve the TLB hit rate. This is because with a smaller number of rows, the program’s access pattern does not span enough pages for the larger page size to reduce the number of compulsory misses. However, whether impacts the hit rate largely depends on whether the matrix width aligns well with the current page size.
With the larger page size, each page holds 64 elements, so one row of takes up 4 pages. If , no matter the value of , the number of hits will be the same as with the smaller pages. This is because making the pages bigger does not reduce the required misses.
What is the smallest page size, as a power of 2, that ensures our program incurs at least one TLB hit for all values of and where ?
2048B or equivalent
To determine the minimum page size, consider the scenario with the lowest hit rate (0%), where accessing one element per column resulted in no reuse, as elements from different columns belonged to different pages. Therefore, the minimum page size required to guarantee at least one TLB hit must be greater than 1024B. Since page sizes must be powers of two, the smallest such size is 2048B.
To find the smallest page size, think about the situation where the TLB hit rate is the lowest (0%). This happens when each element in a column is on a different page, so there is no reuse of pages. To make sure there is at least one TLB hit, the page size needs to be big enough to hold multiple columns in the same page. This means the page size must be larger than 1024 bytes. Since page sizes must be powers of two, the smallest size that works is 2048 bytes.
The program is now modified by swapping the order of the two nested loops, as shown below:
Assume . For which values of will doubling the page size enhance the TLB hit rate of the above program?
or equivalent
For , doubling the page size causes TLB entry conflicts between the first and third rows, as well as between the second and fourth rows.
When the loop order is swapped, the program accesses elements column by column instead of row by row. With , doubling the page size improves the TLB hit rate only if . This is because, for , the larger page size reduces conflicts in the TLB that occur when rows share the same TLB entry. For example, without doubling the page size, conflicts happen between the first and third rows, as well as between the second and fourth rows. By increasing the page size, these conflicts are reduced, leading to better TLB performance.
H
This question involves analyzing an out-of-order machine. A program consisting of 50% integer and 50% floating-point instructions is profiled. The average latencies for these instructions, assuming infinite reservation station and commit queue sizes, are as follows:
Instruction Type | Decode to Issue | Decode to Commit | FU Latency |
---|---|---|---|
Integer | 3 cycles | 8 cycles | 2 cycles |
Floating-Point | 6 cycles | 20 cycles | 12 cycles |
If the processor commits an average of 0.5 instructions per cycle for this program, how many entries are occupied on average in the reservation stations and the commit queue?
Integer Reservation Station:
0.75
Floating-Point Reservation Station
1.5
Commit Queue:
7
We use Little's Law to calculate the occupancy ((N)) of each structure:
where is the throughput, and is the latency.First, the throughput for integer () and floating-point () instructions is 0.25 instructions per cycle each, as the total throughput is 0.5 instructions per cycle and the program consists of 50% integer and 50% floating-point instructions.
For the integer reservation station, the latency is 3 cycles. Using Little's Law, the occupancy is:
For the floating-point reservation station, the latency is 6 cycles. The occupancy is:
To compute the occupancy of the commit queue, we calculate the occupancy for each type of instruction separately and then sum them.For integer instructions, the latency is 8 cycles, so the occupancy is:
For floating-point instructions, the latency is 20 cycles, so the occupancy is:
The total commit queue occupancy is:
The core of this problem lies in combining Little's Law, CPU pipeline operations, and instruction type differences to calculate the average occupancy of the Reservation Station and Commit Queue using a weighted average method. It demonstrates how the instruction mix ratio and latency characteristics influence the operational efficiency of CPU resources.
I
Suppose you are designing a branch predictor for a pipelined in-order processor with the following stages.
The processor operates with the following characteristics:
Within this pipeline, control flow instructions are handled as follows:
BLE
/BNE
) consult the branch predictor. If a branch is predicted to be taken, all subsequent instructions in the pipeline are flushed, and the PC is redirected to the calculated branch target address.JAL
instructions execute the jump to the target address in the B stage, flushing all subsequent instructions in the pipeline. The implementation of JALR
does not need to be considered.For the following questions, consider the given C code and its corresponding RISC-V assembly equivalent. Note that there are no branches other than those explicitly included in the assembly code.
This setup forms the basis for analyzing the control flow and behavior of the program.
Assume that branch B1
is always Not Taken, while the branch predictor predicts all branches as Taken. On average, how many instructions will the processor flush per iteration of the loop?
13
Encountering 2 conditional branches. The first one is incorrectly predicted as taken, leading to 7 flushed instructions. Additionally, the unconditional JAL is taken, causing 3 more instructions to be flushed. In total, the number of flushed instructions is cycles.
The branch predictor uses a single branch history table (BHT) indexed by the lower 10 bits of the PC. Each entry in the table is a 1-bit counter that predicts "taken" if the value is 1 and "not taken" if the value is 0. The table is updated with the actual branch outcome at the end of the execute stage (1 for "taken," 0 for "not taken"). Given that the elements of array are uniformly distributed between , will the predictor achieve high or low accuracy for branches and ?
Branch B1
will have a prediction accuracy of approximately __ I02 __ (rate) since the branch direction is completely random.
50%
B2
is __ I03 __ (condition).
always taken or equivalent
B2 is always taken. B1, however, will only be predicted correctly about 50% of the time since its branch direction is completely random.
J
To design a directory-based MSI coherence protocol, the directory uses a bit vector to represent the sharer set for each cache line. In an -core system, the directory maintains an -bit wide bit vector for each line it tracks. If the -th bit of the bit vector is set, it indicates that core 's cache holds a copy of the line in either the Shared (S) or Modified (M) state.
Assume the processor has cores, each with a 1KB private cache using 32B cache lines. How many entries must the directory contain to track all the cache lines in the private caches?
or equivalent
Each core has 32 private cache lines, and with ( N ) cores, the total number of cache lines is $32 \times N $.
This problem asks how many entries the directory must have to track all cache lines in a system using the MSI coherence protocol.
Cache Line Calculation:
Each core has a private 1 KB cache, and each cache line is 32 bytes. So, the number of cache lines per core is:
Total Cache Lines:
With cores, each having 32 cache lines, the total number of cache lines across all cores is:
Directory Entries:
The directory needs to track each cache line in the system. So, the number of directory entries is equal to the total number of cache lines:
The design of the sharer sets is modified to replace the bit vector with a set of sharer pointers. Each pointer stores the core ID of one sharer. For instance, if cores 10 and 12 hold a cache line in the Shared (S) state, the sharer set in the directory will consist of two pointers, "10" and "12." Each sharer set can hold a maximum of 16 sharer pointers.
In an -core system, where is a power of 2, how many total bits are required to represent all sharer pointers in a sharer set?
or equivalent
With 16 pointers and each pointer requiring bits to represent the core ID, the total number of bits needed is .
The bit vector is replaced with a set of sharer pointers.
Each pointer represents the core ID of a sharer holding a cache line in the Shared (S) state.
The maximum number of sharer pointers in the sharer set is 16.
is the number of cores in the system, and is a power of 2.
Each sharer set can hold a maximum of 16 sharer pointers.
The total bits required to represent all sharer pointers in a set is:
K
The following questions examine memory accesses from multiple cores in a cache-coherent shared memory system. Each question evaluates the possible outcomes under the following memory consistency models:
Assume all registers (e.g., r1
, r2
, …) and memory locations (e.g., a
, b
, …) are initialized to 0.
Consider a cache-coherent shared-memory system executing the following two threads on two different cores. Assume that memory locations a
, b
, and c
are initialized to 0.
Thread 1 (T1) | Thread 2 (T2) |
---|---|
ST (a) <- 1 |
LD r1 <- (a) |
LD r2 <- (b) |
ST (b) <- 1 |
ST (a) <- 2 |
Identify the memory consistency models where the final values r1 = 1
, r2 = 0
, and (a) = 1
are possible. (Answer in SC, TSO, and RMO)
TSO, RMO
Multiple cores in a cache-coherent shared memory system:
This trade-off provides flexibility and higher efficiency.
Thread 1 (T1) | Thread 2 (T2) |
---|---|
ST (a) <- 1 |
LD r1 <- (a) |
LD r2 <- (b) |
ST (b) <- 1 |
ST (a) <- 2 |
T1 Operation Sequence:
ST (a) <- 1
: Store the value 1 into .LD r2 <- (b)
: Load the value from into .ST (a) <- 2
: Store the value 2 into .T2 Operation Sequence:
LD r1 <- (a)
: Load the value from into .ST (b) <- 1
: Store the value 1 into .Final values r1 = 1, r2 = 0, and (a) = 1
ST (a) <- 2
must happen after ST (a) <- 1
in program order. All threads must see this order, so a = 2
should be the final value.a = 1
is observed, it means ST (a) <- 2
was not applied globally, which violates SC.ST (a) <- 2
has not been committed yet).ST (a) <- 1
has not yet been committed.ST (a) <- 2
is still in the write buffer and has not been written to the shared memory.Consider the following four threads executing on a cache-coherent shared-memory system:
Thread 1 (T1) | Thread 2 (T2) | Thread 3 (T3) | Thread 4 (T4) |
---|---|---|---|
ST (a) <- 1 |
ST (a) <- 2 |
LD r1 <- (a) |
LD r2 <- (a) |
LD r3 <- (a) |
LD r4 <- (a) |
Determine the memory consistency models where the final values r1 = 1
, r2 = 2
, r3 = 2
, and r4 = 1
are possible. (Answer in SC, TSO, and RMO)
RMO
r1 = 1
, r2 = 2
, r3 = 2
, and r4 = 1
, we can make a possible execution order is as follows:
ST (a) <- 1
LD r1 <- (a)
(reads a = 1
)ST (a) <- 2
LD r2 <- (a)
(reads a = 2
)LD r3 <- (a)
(reads a = 2
)LD r4 <- (a)
(reads a = 2
)ST (a) <- 1
LD r1 <- (a)
(reads a = 1
)ST (a) <- 2
LD r2 <- (a)
(reads a = 2
)LD r3 <- (a)
(reads a = 2
)LD r4 <- (a)
(reads a = 2
)Consider the following three threads executing on a cache-coherent shared-memory system:
Thread 1 (T1) | Thread 2 (T2) | Thread 3 (T3) |
---|---|---|
ST (a) <- 1 |
LD r1 <- (a) |
LD r3 <- (b) |
LD r2 <- (b) |
LD r4 <- (a) |
|
ST (b) <- 1 |
Determine the memory consistency models where the final values r1 = 1
, r2 = 0
, r3 = 1
, and r4 = 0
are possible. (Answer in SC, TSO, and RMO)
TSO, RMO
If final values r1 = 1
, r2 = 0
, r3 = 1
, and r4 = 0
, we can make a possible execution order is as follows:
ST (a) <- 1
(a = 1
)LD r1 <- (a)
( r1 = 1
)ST (b) <- 1
(b = 1
)LD r3 <- (b)
(r3 = 1
)LD r2 <- (b)
(r2 = 1
)LD r4 <- (a)
(r4 = 1
)SC cannot satisfy the given conditions
If the final values are r1 = 1
, r2 = 0
, r3 = 1
, and r4 = 0
, a possible execution order under TSO is as follows:
ST (a) <- 1
1
to a
. This value is placed in the write buffer of T1 but has not yet been propagated to shared memory.LD r1 <- (a)
a
and sees the value 1
because TSO allows store-to-load forwarding. T2 can see the value in T1's write buffer.LD r3 <- (b)
b
and sees the value 1
, assuming ST (b) <- 1
from T1 has been propagated to shared memory.LD r2 <- (b)
b
and sees the value 0
, as ST (b) <- 1
might still be in T1's write buffer and not yet visible in shared memory.ST (b) <- 1
1
to b
and eventually propagates it to shared memory.LD r4 <- (a)
a
and sees the value 0
, as ST (a) <- 1
may still be in T1's write buffer and not yet propagated to shared memory.TSO can satisfy the given conditions
A sequence lock is a synchronization primitive used to allow a single writer thread to "publish" data that can be read by multiple reader threads. Its key property is that the writer is never blocked, though readers may be temporarily blocked. The code provided below is correct when executed on a system with sequential memory consistency but is not guaranteed to be correct under weaker memory consistency models.
The writer thread increments the sequence number twice—once before writing and once after. An odd sequence number indicates that a write operation is ongoing. The writer ensures that readers see either the previous consistent state or the fully updated state.
Readers check the sequence number before and after reading the object. If the sequence number changes during the read or is odd at the start, the reader retries. This ensures that the reader observes a consistent view of the data.
Notes
memcpy
is not atomic, readers can observe partial updates if the sequence lock's guarantees are violated.atomic_add
) do not act as memory barriers, which may lead to reordering of instructions under weaker memory consistency models.object_t
is assumed to be an opaque structure larger than 16 bytes, making it unsuitable for atomic memory operations. The sequence lock ensures that each reader observes a complete and consistent version of the object.If this sequence lock is used on a multicore system with TSO (Total Store Order) consistency, would the code remain correct? If yes, briefly explain why. If no, clearly specify the memory barriers that need to be added to ensure correctness.
Yes
(意思相近就給分)
The code would still be correct under TSO. TSO only allows stores to be re-ordered after loads.get_obj()
does not have any stores to shared data, so it is clearly correct.
publish()
is also correct, but it requires more justification.
The code remains correct under Total Store Order (TSO). TSO permits the reordering of stores only after loads. The get_obj()
function has no stores to shared data, so it is clearly valid.
For the publish()
function, it is also correct but requires more detailed justification. Analyzing the loads and stores to shared data, with atomics split into load/store pairs, we observe the following:
If this sequence lock is used on a multicore system with RMO (Relaxed Memory Order) consistency, would the code remain correct? If yes, briefly explain why. If no, clearly specify the memory barriers that must be added to ensure correctness.
No
(意思相近就給分)
With RMO, we need to add fences in bothpublish()
andget_obj()
. Specifically, we need RR fences both before and after thememcpy()
inget_obj()
and WW fences before and after thememcpy()
inpublish()
.
Under Relaxed Memory Order (RMO), fences are required in both publish()
and get_obj()
. Specifically:
get_obj()
: Read-Read (RR) fences must be added both before and after the memcpy()
operation.publish()
: Write-Write (WW) fences must be added both before and after the memcpy()
operation.Writer needs:
Reader needs:
These barriers ensure proper ordering of sequence number operations relative to data access.
The code above uses two atomic additions in the publish
function. Assuming a correct implementation on either TSO or RMO (with the necessary barriers in place), would replacing the atomic increments with ordinary non-atomic increments be sufficient? Why or why not?
Yes
Non-atomic instructions are fine.
The problem states that only a single thread will call
publish()
. Therefore, there will never be data races on&seqlock->seq
. We can simply regard the store to&seqlock->seq
as the serialization point.
non-atomic increments would be sufficient because:
publish()
), eliminating write-write race conditionsL
This problem evaluates a virtual memory system with the following characteristics:
The page table structure is summarized below, including the sizes of the page tables and data pages (not drawn to scale).
The processor includes a data TLB with 64 entries, and each entry can map either a 4KB page or a 1MB page. After a TLB miss, a hardware engine walks the page table to reload the TLB. The TLB operates using a first-in/first-out (FIFO) replacement policy.
The following program adds the elements from two 1MB arrays and stores the results in a third 1MB array. Assume that , and the starting addresses of the arrays are as follows:
Assume this program is the only process running on the system, and ignore instruction memory or operating system overheads. The data TLB is initially empty. The system has no cache, and each memory lookup has a latency of 100 cycles.
If all data pages are 4KB, compute the ratio of cycles for address translation to cycles for data access.
Address translation cycles
300
100 + 100 +100 (for L1, L2 and L3 PTE)
Data access cycles
400K or equivalent
4K * 100
(there is no cache, this assumes that memory access is byte-wise)
If all data pages are 1MB, compute the ratio of cycles for address translation to cycles for data access.
Address translation cycles
200
100 + 100 (for L1, L2 PTE)
Data access cycles
100M or equivalent
1M * 100
(there is no cache, this assumes that memory access is byte-wise)
Assume the system includes a PTE cache with a latency of one cycle. The PTE cache stores page table entries and has unlimited capacity. Calculate the ratio of cycles spent on address translation to cycles spent on data access for the case where data pages are 4KB.
Address translation cycles
77200
(256*3 + 3 + 1) * 100
(Note that the arrays are contiguous and share some PTE entries. 256 L3 PTEs per array * 3 arrays, 1 L2 PTE per array * 3 arrays, 1 L1 PTE)
Data access cycles
300M or equivalent
3M*100
What is the minimum number of entries required in the PTE cache to achieve the same performance as an unlimited PTE cache? Assume the PTE cache does not store L3 PTE entries and all data pages are 4KB.
4
(1 for L1 and 3 for L2)
Page Table Hierarchy:
Translation Lookaside Buffer (TLB):
Memory Access Latency:
Page Sizes:
PTE Cache:
Address Translation Cycles (L01):
Each address translation requires 3 memory accesses (L1, L2, and L3 PTE lookups), each taking 100 cycles:
Data Access Cycles (L02):
Each 4KB page requires a single memory access of 100 cycles. A 1MB array has (256) pages, so:
Address Translation Cycles (L03):
Each address translation requires 2 memory accesses (L1 and L2 PTE lookups), each taking 100 cycles:
Data Access Cycles (L04):
A 1MB page requires a single memory access of 100 cycles. For 1MB:
Address Translation Cycles (L05):
Data Access Cycles (L06):
Each data access takes 100 cycles. For 3MB (3 arrays of 1MB each):
Assumptions:
Calculation:
M
Suppose we have the following two functions, each running in its own thread:
thread1
:thread2
:Processor Design
The program runs on an in-order, two-way coarse-grained multithreaded processor with the following characteristics:
If each of these threads were executed individually on this processor, without multithreading, which thread would achieve a higher IPC?
thread2
Explain your reasoning.
(意思相近就給分)
Cache misses (100 cycle penalty) are much more expensive than branch mispredictions. So, even though thread2 can mispredict ~2 branches per iteration, the penalty is much smaller, resulting in higher IPC.
Suppose the core switches between threads upon encountering a cache miss. When a data cache miss occurs, the processor flushes the pipeline, switches to the other thread, and continues executing that thread until it encounters a miss. The pipeline flush discards the load instruction and all subsequent instructions, up to 6 instructions in total (including the load).
Would this design enable both threads to execute close to their peak single-threaded IPC in steady state?
No. thread2
has no memory instructions that could miss in cache. Therefore, once we switch to thread2, we would never have another cache miss again, starving thread1 entirely.
To fix this, we could switch on:
100
Now, consider running multiple instances of thread1
on this CPU. To achieve maximum IPC, more than 2-way multithreading will be required. What is the minimum number of thread1
instances that must run concurrently on this pipeline to maximize IPC?
M04 = ?
9
Each time a cache miss occurs, 6 instructions are flushed from the pipeline. These instructions must be re-executed when the thread is switched back, assuming the load will now hit in the cache.
In each inner-loop iteration, there are 6 instructions to execute, plus an additional 6 instructions that were flushed, resulting in a total of 12 instructions per loop iteration. To maximize IPC, at least ceil(100 / 12) = 9 threads are required.
Suppose we implement a policy of switching on branch instructions. In a coarse-grained multithreaded implementation, generating a "switch" signal for the multithreading control logic can occur at various stages of the pipeline. If the switch occurs at stage , stages through must be flushed.
Now, consider running two instances of thread2
. Which of the following switching points would result in the highest overall IPC? Select one of them.
a. Switch on every branch (as predicted by the BTB)
b. Switch on every branch misprediction (detected in the ALU)
c. Switch on every branch (as determined during decode)
d. Switch on every branch predicted taken (as determined by the branch predictor)
a
Instructions Per Cycle (IPC):
Memory Hierarchy (Cache) and Latency:
Branch Prediction and Mispredictions:
Multithreading Switching Policies:
Basic Math for IPC Calculation:
Multitasking is a fundamental concept in computer systems, allowing multiple tasks or processes to run concurrently, improving resource utilization and system responsiveness. Traditional multitasking in operating systems relies on threads and processes managed by the kernel. These kernel-level threads are preemptively scheduled, meaning the operating system decides when to switch between tasks. While effective, this approach introduces significant overhead due to context switching, where the CPU saves and restores registers, memory mappings, and other state information.
In scenarios where lightweight concurrency is needed, kernel-level multitasking often proves excessive. For instance, tasks such as cooperative multitasking, event-driven programming, or managing independent components within a simulation do not require preemption. Here, user-level constructs like coroutines or userspace threads become powerful tools.
Coroutines provide a mechanism for cooperative multitasking, where tasks explicitly yield control to other tasks at well-defined points. Unlike kernel-level threads, coroutines operate entirely in user space, requiring minimal overhead. This makes them an excellent choice for applications like asynchronous programming, where tasks need to pause while waiting for external events such as I/O completion, without blocking other operations. Coroutines maintain their execution state, including the program counter and local variables, allowing them to resume from where they left off seamlessly. This feature makes them particularly useful in game development, where behaviors like AI or animations require frequent context switching within a single-threaded environment.
Userspace threads, on the other hand, are an abstraction similar to coroutines but with a broader scope. They simulate traditional threads but are scheduled and managed entirely in user space, independent of the kernel. Userspace threads offer greater control over scheduling policies and avoid the overhead of kernel-mode context switches. However, unlike coroutines, userspace threads can preempt one another, making them closer in behavior to kernel threads but much more efficient for many workloads.
The trade-off between coroutines and userspace threads lies in complexity and flexibility. Coroutines are simpler, focusing on cooperative multitasking, while userspace threads provide additional features like preemption. In both cases, these constructs shine in environments where performance and fine-grained control are critical. They reduce the reliance on the kernel, avoid heavy context-switching overhead, and enable developers to implement concurrency with precision tailored to the application's needs.
For example, a high-performance web server might use coroutines to handle thousands of simultaneous connections without creating thousands of threads, which would strain the system's resources. Similarly, scientific simulations with many interacting entities might leverage userspace threads to model their behavior efficiently. Both approaches showcase how user-level multitasking constructs can outperform kernel-level alternatives in specific domains, enabling scalable and efficient multitasking solutions.
Implementing general-purpose coroutines typically requires a second call stack, a feature not natively supported by the C language. A practical, though platform-specific, approach involves using inline assembly to manipulate the stack pointer during the coroutine's initial setup. After obtaining a second call stack, the setjmp and longjmp functions from the standard C library can be used to switch between coroutines.
These functions work by saving and restoring the stack pointer, program counter, callee-saved registers, and other internal state required by the ABI. This ensures that returning to a coroutine after yielding restores its full state, as if resuming from a paused function call. Alternatively, minimalist implementations can achieve similar results by using inline assembly to swap only the stack pointer and program counter, ignoring other registers. While this approach clobbers all other registers, it can be much faster than relying on setjmp
and longjmp
, which must conservatively save all registers specified by the ABI. By contrast, the minimalist method allows the compiler to manage register spilling, saving only what is actively in use.
We now aim to implement a minimalist coroutine system on the RV32I architecture, assuming a 32-bit system without floating-point operations.
main.c
context.S
Build the above using the command:
Complete the code to ensure it works as expected.
-16
or equivalent~(ALIGN - 1)
56
60
N01 = -16 (or equivalent)
main.cTo make sure that the stack_end is 16-byte aligned, the pointer(stackpointer, sp)
Alignment is usually done with bitwise operations (bitwise AND).In binary, -16 is represented as 111…11110000 (32-bit). This creates a mask that clears the lowest 4 bits of any address.
By performing the operationstack_end &= -16;
, the lowest 4 bits of stack_end are set to 0, making it a multiple of 16.
N02 = ~(ALIGN - 1)
main.cTo align an address, we clear the lower 𝑁 bits, where 𝑁 is the power of 2 represented by ALIGN
N03 = 56
N04 = 60
I think the answers should switch.
context.SIn structure of context_t :
Thus, these offsets are used in the assembly file (context.S) to retrieve the values of entry and data during a context switch or function call.