768. Simulate a GPU cluster queue with FIFO and backfill policies
Training and eval jobs share a fixed pool of GPUs. The scheduling policy decides whether a small job may jump ahead of a big one that is waiting.
Write schedule_jobs(total_gpus, jobs, policy). jobs is a list of (name, arrival, gpus, duration). Return a dict mapping each name to its start time, or None if it can never start (it asks for more than total_gpus).
Simulation: time moves from event to event (arrivals and job completions). At each event time t, first free the GPUs of every job ending at or before t, then add every job arriving at or before t to the back of the queue in input order, then start jobs. Policy 'fifo': start the job at the head of the queue while it fits, stopping at the first one that does not. Policy 'backfill': scan the whole queue in order and start every job that fits right now. Started jobs leave the queue and run for duration time units.
def schedule_jobs(total_gpus, jobs, policy)Example 1
Input: total_gpus = 4, jobs = [('a', 0, 3, 5), ('b', 1, 2, 2), ('c', 1, 1, 1)], policy = 'fifo'
Output: {'a': 0, 'b': 5, 'c': 5}; with policy = 'backfill' it returns {'a': 0, 'b': 5, 'c': 1}
Example 2
Input: total_gpus = 4, jobs = J1, policy = 'fifo'
Output: {'a': 0, 'b': 5, 'c': 5}
Constraints
- At most 2,000 jobs.
- policy is 'fifo' or 'backfill'.
- Durations are positive integers.
Hint 1
Only arrival and completion times matter.
Hint 2
Free GPUs before you enqueue and start jobs at a time step.
Hint 3
FIFO uses a blocked flag; backfill never sets it.
Common mistakes
- Ticking one unit at a time, which is slow and error prone with large durations.
- Starting jobs before freeing GPUs from jobs that ended at the same time.
- Looping forever on a job that can never fit.
Understand the idea
A cluster scheduler is a queue plus a policy. Strict FIFO is fair but suffers head of line blocking: an 8 GPU job waiting for a full node keeps 4 free GPUs idle even when a 4 GPU job could run. Backfill lets later jobs start if they fit now, which raises utilization; production schedulers add reservations so the big job is not starved forever. In the example, with 4 GPUs and job a holding 3, the 1 GPU job c can run at time 1 under backfill but waits until 5 under FIFO. Simulate by jumping from event to event instead of ticking every time unit.