Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 993c511

Browse files
committed
Add examples infrastructure and add example for iterative computation
1 parent 26ecedb commit 993c511

5 files changed

Lines changed: 88 additions & 0 deletions

File tree

docs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mkdocs==1.2.3
22
mkdocs-material==7.3.2
33
mkdocs-minify-plugin==0.5.0
44
mkdocs-git-revision-date-localized-plugin==0.10.0
5+
mkdocs-gen-files==0.5.0
56
mike==1.1.2
67
requests==2.31.0
78
jinja2==3.0.3

examples/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Examples
2+
Here you can find several examples of how HyperQueue can be used for various use-cases, both with the command-line
3+
interface and also with the Python API.
4+
5+
You can view these examples either in the [documentation](https://it4innovations.github.io/hyperqueue/stable/examples/iterative-computation/)
6+
or on [GitHub](https://github.com/It4innovations/hyperqueue/tree/main/examples).
7+
8+
- [Iterative computation](iterative-computation)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
```

mkdocs.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ nav:
1313
- Getting Started:
1414
- Quickstart: quickstart.md
1515
- Cheatsheet: cheatsheet.md
16+
- Examples:
17+
- examples/README.md
18+
- Iterative computation: examples/iterative-computation/README.md
1619
- Deployment:
1720
- deployment/index.md
1821
- Server: deployment/server.md
@@ -81,6 +84,9 @@ plugins:
8184
canonical_version: stable
8285
- nedoc:
8386
path: python/apidoc
87+
- gen-files:
88+
scripts:
89+
- scripts/doc_copy_examples.py
8490

8591
extra:
8692
analytics:

scripts/doc_copy_examples.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Copy all files from the `examples` directory to `<built-docs>/examples`, so that they can be rendered in the
3+
documentation.
4+
"""
5+
import glob
6+
import os.path
7+
8+
import mkdocs_gen_files
9+
10+
11+
for path in glob.glob("examples/**/*", recursive=True):
12+
if os.path.isfile(path):
13+
with open(path) as src_file:
14+
with mkdocs_gen_files.open(path, "w") as dest_file:
15+
dest_file.write(src_file.read())

0 commit comments

Comments
 (0)