-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathsimulation_execution_options.py
More file actions
333 lines (274 loc) · 12.7 KB
/
simulation_execution_options.py
File metadata and controls
333 lines (274 loc) · 12.7 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import os
import warnings
from logging import getLogger
from pathlib import Path
from typing import Optional, Union
from kwave import BINARY_DIR, PLATFORM
from kwave.ksensor import kSensor
logger = getLogger(__name__)
class SimulationExecutionOptions:
"""
A class to manage and configure the execution options for k-Wave simulations.
"""
def __init__(
self,
is_gpu_simulation: bool = False,
binary_path: Optional[str] = None,
binary_dir: Optional[str] = None,
binary_name: Optional[str] = None,
kwave_function_name: Optional[str] = "kspaceFirstOrder3D",
delete_data: bool = True,
device_num: Optional[int] = None,
num_threads: Optional[int] = None,
thread_binding: Optional[bool] = None,
system_call: Optional[str] = None,
verbose_level: int = 0,
auto_chunking: Optional[bool] = True,
show_sim_log: bool = True,
checkpoint_interval: Optional[int] = None, # [seconds]
checkpoint_timesteps: Optional[int] = None, # [timestep integer]
checkpoint_file: Optional[Path | str] = None, # [path to hdf5 file]
):
self.is_gpu_simulation = is_gpu_simulation
self._binary_path = binary_path
self._binary_name = binary_name
self._binary_dir = binary_dir
self.kwave_function_name = kwave_function_name
self.delete_data = delete_data
self.device_num = device_num
self.num_threads = num_threads
self.thread_binding = thread_binding
self.system_call = system_call
self.verbose_level = verbose_level
self.auto_chunking = auto_chunking
self.show_sim_log = show_sim_log
self.checkpoint_interval = checkpoint_interval
self.checkpoint_timesteps = checkpoint_timesteps
self.checkpoint_file = checkpoint_file
self.validate()
def _validate_checkpoint_options(self):
# Checkpointing parameters are set
if self.checkpoint_interval is not None or self.checkpoint_timesteps is not None:
# No checkpoint file set
if self.checkpoint_file is None:
raise ValueError("`checkpoint_file` must be set when `checkpoint_interval` or `checkpoint_timesteps` is set.")
# Both checkpointing parameters are set
if self.checkpoint_interval is not None and self.checkpoint_timesteps is not None:
raise ValueError("`checkpoint_interval` and `checkpoint_timesteps` cannot be set at the same time.")
# Checkpoint file is set but no checkpointing parameters are set
if self.checkpoint_file is not None and self.checkpoint_interval is None and self.checkpoint_timesteps is None:
raise ValueError("`checkpoint_interval` or `checkpoint_timesteps` must be set when `checkpoint_file` is set.")
def validate(self):
"""Validate all simulation options before running a simulation.
This method should be called before running a simulation to ensure all options
are in a valid state. It validates:
1. Checkpoint configuration (if any checkpoint options are set)
Raises:
ValueError: If any option configuration is invalid
"""
# Validate checkpoint configuration if any checkpoint options are set
if any(x is not None for x in [self.checkpoint_interval, self.checkpoint_timesteps, self.checkpoint_file]):
self._validate_checkpoint_options()
@property
def num_threads(self) -> Union[int, str]:
return self._num_threads
@num_threads.setter
def num_threads(self, value: Union[int, str]):
cpu_count = os.cpu_count()
if cpu_count is None:
raise RuntimeError("Unable to determine the number of CPUs on this system. Please specify the number of threads explicitly.")
if value == "all":
warnings.warn(
"The 'all' option is deprecated. The value of None sets the maximal number of threads (excluding Windows).",
DeprecationWarning,
)
value = cpu_count
if value is None:
value = cpu_count
if not isinstance(value, int):
raise ValueError("Got {value}. Number of threads must be 'all' or a positive integer")
if value <= 0 or value > cpu_count:
raise ValueError(f"Number of threads {value} must be a positive integer and less than total threads on the system {cpu_count}.")
self._num_threads = value
@property
def verbose_level(self) -> int:
return self._verbose_level
@verbose_level.setter
def verbose_level(self, value: int):
if not (isinstance(value, int) and 0 <= value <= 2):
raise ValueError("Verbose level must be between 0 and 2")
self._verbose_level = value
@property
def is_gpu_simulation(self) -> Optional[bool]:
return self._is_gpu_simulation
@is_gpu_simulation.setter
def is_gpu_simulation(self, value: Optional[bool]):
"Set the flag to enable default GPU simulation. This option will supersede custom binary paths."
self._is_gpu_simulation = value
# Automatically update the binary name based on the GPU simulation flag
if value is not None:
self._binary_name = None
@property
def binary_name(self) -> str:
valid_binary_names = ["kspaceFirstOrder-OMP", "kspaceFirstOrder-CUDA"]
if self._binary_name is None:
# set default binary name based on GPU simulation value
if self.is_gpu_simulation is None:
raise ValueError("`is_gpu_simulation` must be set to either True or False before determining the binary name.")
if self.is_gpu_simulation:
self._binary_name = "kspaceFirstOrder-CUDA"
else:
self._binary_name = "kspaceFirstOrder-OMP"
if PLATFORM == "windows":
self._binary_name += ".exe"
valid_binary_names = [name + ".exe" for name in valid_binary_names]
elif self._binary_name not in valid_binary_names:
warnings.warn("Custom binary name set. Ignoring `is_gpu_simulation` state.")
return self._binary_name
@binary_name.setter
def binary_name(self, value: str):
self._binary_name = value
@property
def binary_path(self) -> Path:
if self._binary_path is not None:
return self._binary_path
binary_dir = BINARY_DIR if self._binary_dir is None else self._binary_dir
if binary_dir is None:
raise ValueError("Binary directory is not specified.")
path = Path(binary_dir) / self.binary_name
if PLATFORM == "windows" and not path.name.endswith(".exe"):
path = path.with_suffix(".exe")
return path
@binary_path.setter
def binary_path(self, value: str):
# check if the binary path is a valid path
if not os.path.exists(value):
raise FileNotFoundError(
f"Binary path {value} does not exist. If you are trying to set `binary_dir`, use the `binary_dir` attribute instead."
)
self._binary_path = value
@property
def binary_dir(self) -> str:
return BINARY_DIR if self._binary_dir is None else self._binary_dir
@binary_dir.setter
def binary_dir(self, value: str):
# check if binary_dir is a directory
if not os.path.isdir(value):
raise NotADirectoryError(
f"{value} is not a directory. If you are trying to set the `binary_path`, use the `binary_path` attribute instead."
)
self._binary_dir = Path(value)
@property
def device_num(self) -> Optional[int]:
return self._device_num
@device_num.setter
def device_num(self, value: Optional[int]):
if value is not None and value < 0:
raise ValueError("Device number must be non-negative")
self._device_num = value
@property
def checkpoint_interval(self) -> Optional[int]:
return self._checkpoint_interval
@checkpoint_interval.setter
def checkpoint_interval(self, value: Optional[int]):
if value is not None:
if not isinstance(value, int) or value <= 0:
raise ValueError("Checkpoint interval must be a positive integer in seconds")
self._checkpoint_interval = value
@property
def checkpoint_timesteps(self) -> Optional[int]:
return self._checkpoint_timesteps
@checkpoint_timesteps.setter
def checkpoint_timesteps(self, value: Optional[int]):
if value is not None:
if not isinstance(value, int) or value < 0:
raise ValueError("Checkpoint timesteps must be a positive integer")
self._checkpoint_timesteps = value
@property
def checkpoint_file(self) -> Optional[Path]:
if self._checkpoint_file is None:
return None
return self._checkpoint_file
@checkpoint_file.setter
def checkpoint_file(self, value: Optional[Path | str]):
if value is not None:
if not isinstance(value, (str, Path)):
raise ValueError("Checkpoint file must be a string or Path object.")
if isinstance(value, str):
value = Path(value)
if not value.parent.is_dir():
raise FileNotFoundError(f"Checkpoint folder {value.parent} does not exist.")
if value.suffix != ".h5":
raise ValueError(f"Checkpoint file {value} must have .h5 extension.")
self._checkpoint_file = value
def as_list(self, sensor: kSensor) -> list[str]:
options_list = []
if self.device_num is not None:
options_list.append("-g")
options_list.append(str(self.device_num))
if self._num_threads is not None and PLATFORM != "windows":
options_list.append("-t")
options_list.append(str(self._num_threads))
if self.verbose_level > 0:
options_list.append("--verbose")
options_list.append(str(self.verbose_level))
if (self.checkpoint_interval is not None or self.checkpoint_timesteps is not None) and self.checkpoint_file is not None:
if self.checkpoint_timesteps is not None:
options_list.append("--checkpoint_timesteps")
options_list.append(str(self.checkpoint_timesteps))
if self.checkpoint_interval is not None:
options_list.append("--checkpoint_interval")
options_list.append(str(self.checkpoint_interval))
options_list.append("--checkpoint_file")
options_list.append(str(self.checkpoint_file))
record_options_map = {
"p": "p_raw",
"p_max": "p_max",
"p_min": "p_min",
"p_rms": "p_rms",
"p_max_all": "p_max_all",
"p_min_all": "p_min_all",
"p_final": "p_final",
"u": "u_raw",
"u_max": "u_max",
"u_min": "u_min",
"u_rms": "u_rms",
"u_max_all": "u_max_all",
"u_min_all": "u_min_all",
"u_final": "u_final",
}
if sensor.record is not None:
matching_keys = sorted(set(sensor.record).intersection(record_options_map.keys()))
options_list.extend([f"--{record_options_map[key]}" for key in matching_keys])
if "u_non_staggered" in sensor.record or "I_avg" in sensor.record or "I" in sensor.record:
options_list.append("--u_non_staggered_raw")
if ("I_avg" in sensor.record or "I" in sensor.record) and ("p" not in sensor.record):
options_list.append("--p_raw")
else:
options_list.append("--p_raw")
if sensor.record_start_index is not None:
options_list.append("-s")
options_list.append(f"{sensor.record_start_index}")
return options_list
def get_options_string(self, sensor: kSensor) -> str:
# raise a deprecation warning
warnings.warn("This method is deprecated. Use `as_list` method instead.", DeprecationWarning)
options_list = self.as_list(sensor)
return " ".join(options_list)
@property
def env_vars(self) -> dict:
env = os.environ
if PLATFORM != "darwin":
env.update({"OMP_PLACES": "cores"})
if self.thread_binding is not None:
if PLATFORM == "darwin":
raise ValueError("Thread binding is not supported in MacOS.")
# read the parameters and update the system options
if self.thread_binding:
env.update({"OMP_PROC_BIND": "SPREAD"})
else:
env.update({"OMP_PROC_BIND": "CLOSE"})
else:
if PLATFORM != "darwin":
env.update({"OMP_PROC_BIND": "SPREAD"})
return env