|
| 1 | +# Iterative computation |
| 2 | +It is a common use-case to perform an iterative computation, e.g. run a randomized simulation until the results are |
| 3 | +stable/accurate enough, or train a machine learning model while the loss keeps dropping. |
| 4 | + |
| 5 | +While there is currently no built-in support in HQ for iteratively submitting new tasks to an existing job, you can perform |
| 6 | +an iterative computation relatively easily with the following approach: |
| 7 | + |
| 8 | +1. Submit a HQ job that performs a computation |
| 9 | +2. Wait for the job to finish |
| 10 | +3. Read the output of the job and decide if computation should continue |
| 11 | +4. If yes, go to 1. |
| 12 | + |
| 13 | +# Python API |
| 14 | +With the Python API, we can simply write the outermost iteration loop in Python, and repeatedly submit jobs, until some |
| 15 | +end criterion has been achieved: |
| 16 | + |
| 17 | +```python |
| 18 | +from hyperqueue import Job, Client |
| 19 | + |
| 20 | +client = Client() |
| 21 | + |
| 22 | +while True: |
| 23 | + job = Job() |
| 24 | + job.program(["my-program"], stdout="out.txt") |
| 25 | + |
| 26 | + # Submit a job |
| 27 | + submitted = client.submit(job) |
| 28 | + |
| 29 | + # Wait for it to complete |
| 30 | + client.wait_for_jobs([submitted]) |
| 31 | + |
| 32 | + # Read the output of the job |
| 33 | + with open("out.txt") as f: |
| 34 | + # Check some termination condition and eventually end the loop |
| 35 | + if f.read().strip() == "done": |
| 36 | + break |
| 37 | +``` |
| 38 | + |
| 39 | +# Command-line interface |
| 40 | +With the command-line interface, you can perform the iterative loop e.g. in Bash. |
| 41 | + |
| 42 | +```bash |
| 43 | +#!/bin/bash |
| 44 | + |
| 45 | +while : |
| 46 | +do |
| 47 | + # Submit a job and wait for it to complete |
| 48 | + ./hq submit --wait ./compute.sh |
| 49 | + |
| 50 | + # Read the output of the job |
| 51 | + output=$(./hq job cat last stdout) |
| 52 | + |
| 53 | + # Decide if we should end or continue |
| 54 | + if [ "${output}" -eq 0 ]; then |
| 55 | + break |
| 56 | + fi |
| 57 | +done |
| 58 | +``` |
0 commit comments