Classify Kernel Bottlenecks Using the Roofline Model
Arrays & HashingAuto-graded · 18 test casesLast updated
Determine whether computational kernels are limited by arithmetic throughput or memory bandwidth using roofline analysis.
Problem Statement
You are analyzing the performance characteristics of various computational kernels. Given hardware specifications and workload metrics, classify each kernel as either compute-bound or memory-bound using the roofline model.
The roofline model characterizes performance bottlenecks by comparing a workload's arithmetic intensity against the hardware's balance point:
- Arithmetic Intensity (AI): The ratio of floating-point operations to bytes transferred from memory (ops/bytes)
- Ridge Point: The critical arithmetic intensity where compute and memory constraints intersect, calculated as
peak_flops / sustainable_bandwidth - Classification Rule:
- If
AI > ridge_point: the kernel is compute-bound (limited by arithmetic throughput) - If
AI ≤ ridge_point: the kernel is memory-bound (limited by memory bandwidth)
- If
Input
workloads: A list of dictionaries, each containing:"phase": A string identifier for the kernel or workload phase"ops": Total number of arithmetic operations (integer or float)"bytes": Total bytes transferred from memory (integer or float)
peak_flops: Maximum theoretical arithmetic throughput in operations per second (float)sustainable_bandwidth: Achievable memory bandwidth in bytes per second (float)
Output
Return a list of dictionaries in the same order as the input workloads, each containing:
"phase": The original phase identifier (string)"bound": Either"compute-bound"or"memory-bound"(string)
Constraints
1 ≤ len(workloads) ≤ 1000ops > 0andbytes > 0for all workloadspeak_flops > 0andsustainable_bandwidth > 0- All numeric values fit in standard float precision
- Phase identifiers are non-empty strings
Examples
Example 1
Input: classify_kernel_bottleneck([{'phase': 'prefill', 'ops': 200000000000000, 'bytes': 100000000000}, {'phase': 'decode', 'ops': 10000000000, 'bytes': 200000000000}], 1000000000000000, 1000000000000)
Output: [{'phase': 'prefill', 'bound': 'compute-bound'}, {'phase': 'decode', 'bound': 'memory-bound'}]
Example 2
Input: classify_kernel_bottleneck([{'phase': 'prefill', 'ops': 50000000000000, 'bytes': 100000000000}, {'phase': 'decode', 'ops': 1000000000000, 'bytes': 20000000000}], 800000000000000, 1600000000000)
Output: [{'phase': 'prefill', 'bound': 'memory-bound'}, {'phase': 'decode', 'bound': 'memory-bound'}]
Example 3
Input: classify_kernel_bottleneck([{'phase': 'custom-kernel', 'ops': 1000, 'bytes': 100}], 10000, 100)
Output: [{'phase': 'custom-kernel', 'bound': 'memory-bound'}]
Hints
Hint 1
Calculate the ridge point first—it's the hardware's characteristic ratio that divides compute-bound from memory-bound regimes.
Hint 2
Arithmetic intensity is simply operations divided by bytes for each workload.
Hint 3
Compare each workload's arithmetic intensity to the ridge point to determine the bottleneck.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The roofline model provides a framework for understanding whether a kernel's performance is constrained by arithmetic capabilities or memory transfer rates.
Step 1: Calculate the Ridge Point
The ridge point represents the arithmetic intensity threshold where hardware transitions from being memory-limited to compute-limited. It equals the ratio of peak computational throughput to memory bandwidth:
ridge_point = peak_flops / sustainable_bandwidth
Step 2: Compute Arithmetic Intensity for Each Workload
For each kernel, calculate how many operations are performed per byte of data transferred:
arithmetic_intensity = ops / bytes
Step 3: Compare and Classify
Compare the workload's arithmetic intensity against the ridge point:
- When arithmetic intensity exceeds the ridge point, the workload performs enough computation per byte that memory can keep up—the arithmetic units become the bottleneck (compute-bound)
- When arithmetic intensity is at or below the ridge point, data cannot be supplied fast enough to saturate the compute units—memory bandwidth becomes the bottleneck (memory-bound)
Why This Works
The roofline model captures the fundamental tradeoff in hardware: you can either be waiting for data (memory-bound) or waiting for computation to complete (compute-bound). The ridge point identifies the exact balance where both resources are equally stressed. Workloads with high data reuse (high AI) shift toward compute constraints, while workloads with poor reuse (low AI) are throttled by memory.
Complexity Analysis
- Time Complexity: O(n) where n is the number of workloads—we perform constant-time arithmetic for each workload
- Space Complexity: O(n) for storing the output list
Solution Code
from typing import List, Dict
def classify_kernel_bottleneck(workloads: List[Dict], peak_flops: float, sustainable_bandwidth: float) -> List[Dict]:
ridge_point = peak_flops / sustainable_bandwidth
results = []
for workload in workloads:
arithmetic_intensity = workload['ops'] / workload['bytes']
if arithmetic_intensity > ridge_point:
bound = "compute-bound"
else:
bound = "memory-bound"
results.append({
'phase': workload['phase'],
'bound': bound
})
return results