Skip to content

Commit b17228e

Browse files
authored
Make decorator that executes an agenda in parallel (#1117)
Now you can do ```python3 @pyarts.in_parallel(ws=ws) def create_agenda_parallel(ws: pyarts.Workspace): ws.abs_speciesSet(species=["H2O", "CO2"]) ws.atm_pointInit() ``` The two methods are executed in parallel. Note that this will come with overhead, and the example above is clearly faster to run serial.
2 parents 5a16f24 + b5e5fac commit b17228e

36 files changed

Lines changed: 646 additions & 295 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ heaptrack.*.gz
4141
qdrant_storage/
4242
.roo/
4343
tests/core/sensor/sensor_count.xml
44+
tests/core/xml/test.xml

python/src/pyarts3/__init__.py.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""This module contains functions to interact with ARTS."""
33

44
from pyarts3 import arts # noqa
5-
from pyarts3.workspace import Workspace, arts_agenda # noqa
5+
from pyarts3.workspace import Workspace, arts_agenda, in_parallel # noqa
66
from pyarts3.workspace.callback import callback_operator # noqa
77

88
def __getattr__(attr):

python/src/pyarts3/workspace/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
77
"""
88

9-
from pyarts3.workspace.workspace import Workspace, arts_agenda # noqa
9+
from pyarts3.workspace.workspace import Workspace, arts_agenda, in_parallel # noqa
1010
from pyarts3.workspace.callback import callback_operator # noqa
1111
from pyarts3.workspace.compatible import compat # noqa

python/src/pyarts3/workspace/workspace.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,21 @@ def parser(fn):
522522
return parser
523523
else:
524524
return _arts_agenda(func, ws, fix)
525+
526+
527+
def in_parallel(func=None, *, ws=None):
528+
"""
529+
Parses the decorated function into an Agenda and executes it immediately
530+
using the provided workspace.
531+
"""
532+
def execute(fn):
533+
if ws is None:
534+
raise ValueError("Workspace 'ws' must be provided to in_parallel")
535+
agenda = _arts_agenda(fn, None, False)
536+
agenda._set_name(getsource(fn))
537+
agenda.par_execute(ws)
538+
return agenda
539+
540+
if func is None:
541+
return execute
542+
return execute(func)

src/core/xml/xml_io.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ try {
6868
return xml_file;
6969
} catch (const std::exception& e) {
7070
throw std::runtime_error(
71-
std::format("Error reading file {}:\n{}", filename, e.what()));
71+
std::format("Cannot read file: \"{}\":\n{}", filename, e.what()));
7272
}
7373

7474
//! Extends data from XML file
@@ -91,10 +91,7 @@ try {
9191
return xml_file;
9292
} catch (const std::exception& e) {
9393
throw std::runtime_error(
94-
std::format("Error extending file {} containing {}:\n{}",
95-
filename,
96-
xml_io_stream_name_v<T>,
97-
e.what()));
94+
std::format("Cannot extend file: \"{}\" containing {}:\n{}", filename, xml_io_stream_name_v<T>, e.what()));
9895
}
9996

10097
//! Appends data from XML file
@@ -117,7 +114,7 @@ try {
117114
return xml_file;
118115
} catch (const std::exception& e) {
119116
throw std::runtime_error(
120-
std::format("Error appending file {} containing {}:\n{}",
117+
std::format("Cannot append file: \"{}\" containing {}:\n{}",
121118
filename,
122119
xml_io_stream_name_v<T>,
123120
e.what()));

src/core/xml/xml_io_base.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void XMLTag::get_attribute_value(const std::string_view& aname, Index& value) {
207207
strstr >> value;
208208
if (strstr.fail()) {
209209
xml_parse_error(std::format(
210-
"Error while parsing value of {0} from <{1}>", aname, name));
210+
"Cannot parse value of {0} from <{1}>", aname, name));
211211
}
212212
}
213213

@@ -220,7 +220,7 @@ void XMLTag::get_attribute_value(const std::string_view& aname, Size& value) {
220220
strstr >> value;
221221
if (strstr.fail()) {
222222
xml_parse_error(std::format(
223-
"Error while parsing value of {0} from <{1}>", aname, name));
223+
"Cannot parse value of {0} from <{1}>", aname, name));
224224
}
225225
}
226226

@@ -234,7 +234,7 @@ void XMLTag::get_attribute_value(const std::string_view& aname,
234234
strstr >> double_imanip() >> value;
235235
if (strstr.fail()) {
236236
xml_parse_error(std::format(
237-
"Error while parsing value of {0} from <{1}>", aname, name));
237+
"Cannot parse value of {0} from <{1}>", aname, name));
238238
}
239239
}
240240

src/core/xml/xml_io_base.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ void xml_read_from_file_base(const String& filename, T& type) try {
227227
xml_io_stream<T>::read(buffer, type, xsh);
228228
xml_read_footer_from_stream(buffer);
229229
} catch (const std::runtime_error& e) {
230-
throw std::runtime_error(
231-
std::format("Error reading file {}:\n{}", filename, e.what()));
230+
throw std::runtime_error(std::format(
231+
"Cannot read file with full filename: \"{}\":\n{}", filename, e.what()));
232232
}
233233

234234
//! Extends data from XML file
@@ -249,8 +249,8 @@ void xml_extend_from_file_base(const String& filename, T& type) try {
249249
xml_io_stream<T>::extend(buffer, type, xsh);
250250
xml_read_footer_from_stream(buffer);
251251
} catch (const std::runtime_error& e) {
252-
throw std::runtime_error(
253-
std::format("Error reading file {}:\n{}", filename, e.what()));
252+
throw std::runtime_error(std::format(
253+
"Cannot extend file with full filename: \"{}\":\n{}", filename, e.what()));
254254
}
255255

256256
//! Appends data from XML file
@@ -271,8 +271,8 @@ void xml_append_from_file_base(const String& filename, T& type) try {
271271
xml_io_stream<T>::append(buffer, type, xsh);
272272
xml_read_footer_from_stream(buffer);
273273
} catch (const std::runtime_error& e) {
274-
throw std::runtime_error(
275-
std::format("Error reading file {}:\n{}", filename, e.what()));
274+
throw std::runtime_error(std::format(
275+
"Cannot append file with full filename: \"{}\":\n{}", filename, e.what()));
276276
}
277277

278278
//! Write data to XML file
@@ -321,9 +321,8 @@ void xml_write_to_file_base(const String& filename,
321321

322322
xml_write_footer_to_stream(*ofs);
323323
} catch (const std::runtime_error& e) {
324-
std::ostringstream os;
325-
os << "Error writing file: " << filename << '\n' << e.what();
326-
throw std::runtime_error(os.str());
324+
throw std::runtime_error(std::format(
325+
"Cannot write file with full filename: \"{}\":\n{}", filename, e.what()));
327326
}
328327
}
329328

@@ -332,7 +331,7 @@ void xml_read_from_stream(std::istream& i, T& v, bifstream* b = nullptr) try {
332331
xml_io_stream<T>::read(i, v, b);
333332
} catch (const std::exception& e) {
334333
throw std::runtime_error(std::format(
335-
"Error streaming {}:\n{}", xml_io_stream<T>::type_name, e.what()));
334+
"Cannot stream {}:\n{}", xml_io_stream<T>::type_name, e.what()));
336335
}
337336

338337
template <arts_xml_ioable T>

src/core/xml/xml_io_stream_aggregate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,6 @@ struct xml_io_stream<T> {
263263
tag.check_end_name(type_name);
264264
} catch (const std::exception& e) {
265265
throw std::runtime_error(
266-
std::format("Error reading {}:\n{}", type_name, e.what()));
266+
std::format("Cannot read {}:\n{}", type_name, e.what()));
267267
}
268268
};

src/core/xml/xml_io_stream_array.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct xml_io_stream<Array<T>> {
8181
tag.check_end_name(type_name);
8282
} catch (const std::exception& e) {
8383
throw std::runtime_error(std::format(
84-
"Error extending {}<{}>:\n{}", type_name, inner::type_name, e.what()));
84+
"Cannot extend {}<{}>:\n{}", type_name, inner::type_name, e.what()));
8585
}
8686

8787
static void append(std::istream& is,
@@ -92,7 +92,7 @@ struct xml_io_stream<Array<T>> {
9292
n.emplace_back(std::move(x));
9393
} catch (const std::exception& e) {
9494
throw std::runtime_error(std::format(
95-
"Error appending {}<{}>:\n{}", type_name, inner::type_name, e.what()));
95+
"Cannot append {}<{}>:\n{}", type_name, inner::type_name, e.what()));
9696
}
9797

9898
static void read(std::istream& is,
@@ -102,7 +102,7 @@ struct xml_io_stream<Array<T>> {
102102
extend(is, n, pbifs);
103103
} catch (const std::exception& e) {
104104
throw std::runtime_error(std::format(
105-
"Error reading {}<{}>:\n{}", type_name, inner::type_name, e.what()));
105+
"Cannot read {}<{}>:\n{}", type_name, inner::type_name, e.what()));
106106
}
107107
};
108108

src/core/xml/xml_io_stream_enum_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ struct xml_io_stream<T> {
5151
tag.check_end_name(type_name);
5252
} catch (const std::exception& e) {
5353
throw std::runtime_error(
54-
std::format("Error reading enum {}:\n{}", type_name, e.what()));
54+
std::format("Cannot read enum {}:\n{}", type_name, e.what()));
5555
}
5656
};

0 commit comments

Comments
 (0)