-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathslurm_example.py
61 lines (44 loc) · 1.57 KB
/
slurm_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import concurrent.futures
import subprocess
import time
import cluster_tools
# "Worker" functions.
def square(n):
return n * n
def hostinfo():
return subprocess.check_output("uname -a", shell=True)
def sleep(duration):
time.sleep(duration)
return True
def example_1():
"""Square some numbers on remote hosts!"""
executor = cluster_tools.get_executor("slurm", debug=True, keep_logs=True)
# executor = cluster_tools.get_executor("multiprocessing", 5)
# executor = cluster_tools.get_executor("sequential")
with executor:
job_count = 5
futures = [executor.submit(square, n) for n in range(job_count)]
for future in concurrent.futures.as_completed(futures):
print(future.result())
def example_2():
"""Get host identifying information about the servers running
our jobs.
"""
with cluster_tools.SlurmExecutor(False) as executor:
futures = [executor.submit(hostinfo) for n in range(15)]
for future in concurrent.futures.as_completed(futures):
print(future.result().strip())
def example_3():
with cluster_tools.SlurmExecutor(False) as exc:
print(list(exc.map(square, [5, 7, 11, 12, 13, 14, 15, 16, 17])))
def sleep_example():
executor = cluster_tools.get_executor("slurm")
# executor = cluster_tools.get_executor("multiprocessing", 5)
# executor = cluster_tools.get_executor("sequential")
with executor:
print(list(executor.map(sleep, [10, 10, 10])))
if __name__ == "__main__":
example_1()
example_2()
example_3()
sleep_example()