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

798. Pack instructions into VLIW bundles with a greedy list scheduler

HardGPU kernels (simulated) Pro

A VLIW machine issues one bundle per cycle, and a bundle can hold several instructions as long as each functional unit (for example load, alu, vector) has a free slot. The compiler, or you, decides which instructions go together. Better packing means fewer cycles for the same program.

Write schedule_bundles(instrs, slots, latency). instrs is a list of (name, unit, deps) in program order, where deps lists names of earlier instructions whose results this one needs. slots maps each unit to how many instructions of that unit fit in one bundle. latency maps each unit to the number of cycles until its result can be used: an instruction issued in cycle c on unit u makes its result ready at cycle c + latency[u].

Schedule greedily, cycle by cycle starting at cycle 0. In each cycle, walk the not yet issued instructions in program order and issue an instruction if every dependency is ready by this cycle and its unit still has a free slot in this cycle.

Return the list of bundles from cycle 0 to the last cycle that issued something. Each bundle is the list of names issued in that cycle, in program order; an idle cycle is an empty list.

Functiondef schedule_bundles(instrs, slots, latency)

Example 1

Input: instrs = [('ld1', 'load', []), ('ld2', 'load', []), ('add', 'alu', ['ld1', 'ld2']), ('st', 'load', ['add'])], slots = {'load': 1, 'alu': 2}, latency = {'load': 2, 'alu': 1}

Output: [['ld1'], ['ld2'], [], ['add'], ['st']]. ld2 issues in cycle 1 and is ready at cycle 3

Explanation: So add waits until cycle 3.

Example 2

Input: instrs = P1, slots = {'load': 1, 'alu': 2}, latency = {'load': 2, 'alu': 1}

Output: [['ld1'], ['ld2'], [], ['add'], ['st']]

Example 3

Input: instrs = P2, slots = {'alu': 2}, latency = {'alu': 1}

Output: [['a', 'b'], ['c', 'd']]

Constraints

  • At most 2,000 instructions.
  • Every latency is at least 1 and every unit used has at least one slot.
  • deps only name earlier instructions.
Hint 1

Keep a dict name -> ready cycle.

Hint 2

Reset the per-unit slot counts at the start of every cycle.

Hint 3

A dependency issued this cycle is never ready this cycle because latency is at least 1.

Common mistakes
  • Letting an instruction use a result in the same cycle it was produced.
  • Stopping the scan at the first blocked instruction (that is in-order issue, a different machine).
  • Dropping idle cycles in the middle of the schedule.
Understand the idea

On VLIW and SIMD hardware, speed comes from keeping every slot busy every cycle. Two things stop you: dependencies (a result is not ready yet) and resources (no free slot of the right unit). A list scheduler is the classic greedy answer. When optimising a kernel by hand you do the same thing: interleave independent work, such as loads for the next item while the ALU works on the current one, so latency hides behind useful work.

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
  • Simulate a multi-stage load and compute pipelineHard
All GPU kernels (simulated) problems
Report
798 / 903
Python 3solution.pyPython idle
1
2
3
4
def schedule_bundles(instrs, slots, latency):
# return a list of bundles, one per cycle
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