-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
214 lines (196 loc) · 5.82 KB
/
cli.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding: utf-8 -*-
from aiida.cmdline.utils.decorators import with_dbenv
import click
@click.group(context_settings={"help_option_names": ["-h", "--help"]})
def main():
"""The command line interface for aiida-sleep."""
@main.command("calc")
@click.option(
"-n", "--number", type=int, default=1, help="The number of calculations to launch."
)
@click.option("-c", "--code", default="sleep@slurm", help="The code to run.")
@click.option("-t", "--time", type=int, default=1, help="The time to sleep (seconds).")
@click.option(
"-p",
"--payload",
type=int,
default=100,
help="The number of attributes for the input payload.",
)
@click.option(
"-o",
"--output-dict",
type=int,
default=100,
help="The number of attributes for the output Dict.",
)
@click.option(
"-a",
"--output-array",
type=int,
default=100,
help="The size of the numpy array for the output ArrayData.",
)
@click.option(
"-f",
"--fail",
is_flag=True,
help="Parse the calculation with a non-zero exit code.",
)
@click.option("-s", "--submit", is_flag=True, help="Submit the calculation (or run).")
@with_dbenv()
def run_calcs_cli(number, code, time, payload, fail, output_dict, output_array, submit):
"""Run the `SleepCalculation`"""
for i in range(number):
print(
f"setting up and {'submitting' if submit else 'running'} calculation {i+1}"
)
node = run_calc(
code=code,
time=time,
payload=payload,
output_dict=output_dict,
output_array=output_array,
fail=fail,
submit=submit,
)
click.echo(str(node))
def run_calc(
code="sleep@slurm",
time=1,
payload=100,
output_dict=100,
output_array=100,
fail=False,
submit=False,
):
"""Run the `SleepCalculation`
:param code: code label
:param time: time the `SleepCalculation` runs sleep for (seconds)
:param payload: Size of input dict
:param output_dict: Size of output dict
:param output_array: Size of output array
:param fail: Intentionally fail the `SleepCalculation`
:param submit: whether to submit to daemon, otherwise run
:return: workchain node
"""
from aiida.engine import run_get_node
from aiida.engine import submit as submit_func
from aiida.orm import load_code
builder = load_code(code).get_builder()
builder.time = time
builder.payload = {f"input_key_{i}": f"value_{i}" for i in range(payload)}
builder.metadata.options.fail_calcjob = fail
builder.metadata.options.output_dict_size = output_dict
builder.metadata.options.output_array_size = output_array
if submit:
node = submit_func(builder)
else:
node = run_get_node(builder).node
return node
@main.command("workchain")
@click.option(
"-nw",
"--number-work",
type=int,
default=1,
help="The number of workchains to launch.",
)
@click.option(
"-nc",
"--number-calc",
type=int,
default=1,
help="The number of calculations per workchain.",
)
@click.option("-c", "--code", default="sleep@slurm", help="The code to run.")
@click.option("-t", "--time", type=int, default=1, help="The time to sleep (seconds).")
@click.option(
"-p",
"--payload",
type=int,
default=100,
help="The number of attributes for the input payload.",
)
@click.option(
"-o",
"--output-dict",
type=int,
default=100,
help="The number of attributes for the output Dict.",
)
@click.option(
"-a",
"--output-array",
type=int,
default=100,
help="The number of rows for the output ArrayData.",
)
@click.option(
"-f", "--fail", is_flag=True, help="Parse calculations with a non-zero exit code."
)
@click.option("-s", "--submit", is_flag=True, help="Submit the workchain (or run).")
@with_dbenv()
def run_workchains_cli(
number_work,
number_calc,
code,
time,
payload,
output_dict,
output_array,
fail,
submit,
):
"""Run a set of `SleepWorkChain`s"""
for i in range(number_work):
print(f"setting up and {'submitting' if submit else 'running'} workchain {i+1}")
node = run_workchain(
number=number_calc,
code=code,
time=time,
payload=payload,
output_dict=output_dict,
output_array=output_array,
fail=fail,
submit=submit,
)
click.echo(str(node))
def run_workchain(
number=1,
code="sleep@slurm",
time=1,
payload=100,
output_dict=100,
output_array=100,
fail=False,
submit=False,
):
"""Run the `SleepWorkChain`
:param number: Number of children `SleepCalculation`
:param code: code label
:param time: seconds for which each `SleepCalculation` runs `sleep`
:param payload: number of fields in `payload` input dictionary of CalcJob
:param output: number of fields in output dictionary of CalcJob
:param output_array: Size of output array
:param fail: Intentionally fail all `SleepCalculation`
:param submit: whether to submit to daemon, otherwise run
:return: workchain node
"""
from aiida.engine import run_get_node
from aiida.engine import submit as submit_func
from aiida.orm import load_code
from aiida.plugins import WorkflowFactory
builder = WorkflowFactory("sleep").get_builder()
builder.children = number
builder.calcjob.code = load_code(code)
builder.calcjob.time = time
builder.calcjob.payload = {f"input_key_{i}": f"value_{i}" for i in range(payload)}
builder.calcjob.metadata.options.fail_calcjob = fail
builder.calcjob.metadata.options.output_dict_size = output_dict
builder.calcjob.metadata.options.output_array_size = output_array
if submit:
node = submit_func(builder)
else:
node = run_get_node(builder).node
return node