BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / GPU kernels (simulated)22 of 24 in topic

693. Simulate a multi-stage load and compute pipeline

HardGPU kernels (simulated) Pro

Why it matters Choosing the number of pipeline stages trades shared memory for latency hiding in modern matmul and attention kernels.

Fast kernels overlap loading the next tile with computing the current one, using several shared-memory buffers (stages). With too few buffers, the loader must wait for compute to free a buffer.

Write pipeline_time(load, compute, stages). load[i] and compute[i] are the times for tile i, processed in order. One loader and one compute unit run in parallel. Tile i uses buffer i % stages. Its load starts when the loader has finished load i - 1 and its buffer is free, meaning compute of tile i - stages has finished (no wait for i < stages). Its compute starts when its own load has finished and compute i - 1 has finished. Return (total_time, compute_idle) where total_time is when the last compute ends and compute_idle is total_time minus the sum of compute times. For no tiles return (0.0, 0.0).

Functiondef pipeline_time(load, compute, stages)

Example 1

Input: load [2, 2, 2], compute [3, 3, 3], stages 1

Output: (15.0, 6.0); with stages 2 it gives (11.0, 2.0)

Example 2

Input: load = [2, 2, 2], compute = [3, 3, 3], stages = 2

Output: (11.0, 2.0)

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Track load_end[i] and comp_end[i]; each depends on a few earlier entries.

Hint 2

load_end[i] = max(load_end[i - 1], comp_end[i - stages]) + load[i].

Hint 3

comp_end[i] = max(load_end[i], comp_end[i - 1]) + compute[i].

Hint 4

Idle compute time = total minus the sum of compute times.

Common mistakes
  • Forgetting the buffer-reuse dependency.
  • Letting compute start before its own load is done.
  • Counting idle time from the loader instead of the compute unit.
Understand the idea

A pipeline with buffers is like a cook with plates. A helper fetches ingredients onto a plate, the cook cooks from the plate, then the plate is free again. With one plate, the helper waits for the cook and the cook waits for the helper, so nothing overlaps. With two plates, the helper fills the next plate while the cook works.

Worked example

loads 2, computes 3, three tiles. One stage: load 0 to 2, compute 2 to 5, load 5 to 7, compute 7 to 10, load 10 to 12, compute 12 to 15. Two stages: load tile 1 runs 2 to 4 during compute, compute ends at 5, 8, 11.

load_end[i] = max(load_end[i-1], comp_end[i-stages]) + load[i]; comp_end[i] = max(load_end[i], comp_end[i-1]) + compute[i]

Real kernels pick 2 to 4 stages, limited by shared memory size.

Topics
GPU kernels (simulated)
Similar questions
  • Online softmax, the trick inside FlashAttentionHard
  • Count shared-memory bank conflict passes per warpHard
  • Simulate a work-efficient parallel prefix sumHard
  • Compute dK and dV tile by tile from the saved log-sum-expHard
All GPU kernels (simulated) problems
Report
693 / 903
Python 3solution.pyPython idle
1
2
3
4
def pipeline_time(load, compute, stages):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS