KatInterview

Classify Kernel Bottlenecks Using the Roofline Model

Arrays & HashingAuto-graded · 18 test cases
MediumAuto-gradedArrays & Hashingroofline-modelperformance-analysismemory-bandwidth

Last 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)

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) ≤ 1000
  • ops > 0 and bytes > 0 for all workloads
  • peak_flops > 0 and sustainable_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.