-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples infrastructure and add example for iterative computation
- Loading branch information
Showing
5 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Examples | ||
Here you can find several examples of how HyperQueue can be used for various use-cases, both with the command-line | ||
interface and also with the Python API. | ||
|
||
You can view these examples either in the [documentation](https://it4innovations.github.io/hyperqueue/stable/examples/iterative-computation/) | ||
or on [GitHub](https://github.com/It4innovations/hyperqueue/tree/main/examples). | ||
|
||
- [Iterative computation](iterative-computation) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Iterative computation | ||
It is a common use-case to perform an iterative computation, e.g. run a randomized simulation until the results are | ||
stable/accurate enough, or train a machine learning model while the loss keeps dropping. | ||
|
||
While there is currently no built-in support in HQ for iteratively submitting new tasks to an existing job, you can perform | ||
an iterative computation relatively easily with the following approach: | ||
|
||
1. Submit a HQ job that performs a computation | ||
2. Wait for the job to finish | ||
3. Read the output of the job and decide if computation should continue | ||
4. If yes, go to 1. | ||
|
||
# Python API | ||
With the Python API, we can simply write the outermost iteration loop in Python, and repeatedly submit jobs, until some | ||
end criterion has been achieved: | ||
|
||
```python | ||
from hyperqueue import Job, Client | ||
|
||
client = Client() | ||
|
||
while True: | ||
job = Job() | ||
job.program(["my-program"], stdout="out.txt") | ||
|
||
# Submit a job | ||
submitted = client.submit(job) | ||
|
||
# Wait for it to complete | ||
client.wait_for_jobs([submitted]) | ||
|
||
# Read the output of the job | ||
with open("out.txt") as f: | ||
# Check some termination condition and eventually end the loop | ||
if f.read().strip() == "done": | ||
break | ||
``` | ||
|
||
# Command-line interface | ||
With the command-line interface, you can perform the iterative loop e.g. in Bash. | ||
|
||
```bash | ||
#!/bin/bash | ||
|
||
while : | ||
do | ||
# Submit a job and wait for it to complete | ||
./hq submit --wait ./compute.sh | ||
|
||
# Read the output of the job | ||
output=$(./hq job cat last stdout) | ||
|
||
# Decide if we should end or continue | ||
if [ "${output}" -eq 0 ]; then | ||
break | ||
fi | ||
done | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
Copy all files from the `examples` directory to `<built-docs>/examples`, so that they can be rendered in the | ||
documentation. | ||
""" | ||
import glob | ||
import os.path | ||
|
||
import mkdocs_gen_files | ||
|
||
|
||
for path in glob.glob("examples/**/*", recursive=True): | ||
if os.path.isfile(path): | ||
with open(path) as src_file: | ||
with mkdocs_gen_files.open(path, "w") as dest_file: | ||
dest_file.write(src_file.read()) |