Repository navigation
Expand file tree
/
Copy pathdtlmod_python.cpp
More file actions
291 lines (263 loc) · 18.3 KB
/
Copy pathdtlmod_python.cpp
File metadata and controls
291 lines (263 loc) · 18.3 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
/* Copyright (c) 2025-2026. The SWAT Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#include <pybind11/pybind11.h> // Must come before our own stuff
#include <pybind11/functional.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <dtlmod/CompressionReductionMethod.hpp>
#include <dtlmod/DTL.hpp>
#include <dtlmod/DTLException.hpp>
#include <dtlmod/Engine.hpp>
#include <dtlmod/FileEngine.hpp>
#include <dtlmod/FileTransport.hpp>
#include <dtlmod/Metadata.hpp>
#include <dtlmod/ReductionMethod.hpp>
#include <dtlmod/StagingEngine.hpp>
#include <dtlmod/StagingMboxTransport.hpp>
#include <dtlmod/StagingMqTransport.hpp>
#include <dtlmod/StagingTransport.hpp>
#include <dtlmod/Stream.hpp>
#include <dtlmod/Transport.hpp>
#include <dtlmod/Variable.hpp>
#include <dtlmod/version.hpp>
#include <simgrid/python.hpp>
#include <xbt/log.h>
namespace py = pybind11;
using dtlmod::DTL;
using dtlmod::Engine;
using dtlmod::ReductionMethod;
using dtlmod::Stream;
using dtlmod::Transport;
using dtlmod::Variable;
namespace {
std::string get_dtlmod_version()
{
int major;
int minor;
int patch;
dtlmod_version_get(&major, &minor, &patch);
return simgrid::xbt::string_printf("%i.%i.%i", major, minor, patch);
}
} // namespace
PYBIND11_MODULE(dtlmod, m)
{
m.doc() = "DTLMod userspace API";
m.attr("dtlmod_version") = get_dtlmod_version();
py::register_exception<dtlmod::UnknownEngineTypeException>(m, "UnknownEngineTypeException");
py::register_exception<dtlmod::UndefinedEngineTypeException>(m, "UndefinedEngineTypeException");
py::register_exception<dtlmod::MultipleEngineTypeException>(m, "MultipleEngineTypeException");
py::register_exception<dtlmod::UnknownTransportMethodException>(m, "UnknownTransportMethodException");
py::register_exception<dtlmod::UndefinedTransportMethodException>(m, "UndefinedTransportMethodException");
py::register_exception<dtlmod::MultipleTransportMethodException>(m, "MultipleTransportMethodException");
py::register_exception<dtlmod::InvalidEngineAndTransportCombinationException>(
m, "InvalidEngineAndTransportCombinationException");
py::register_exception<dtlmod::OpenStreamFailureException>(m, "OpenStreamFailureException");
py::register_exception<dtlmod::InvalidOpenOverheadException>(m, "InvalidOpenOverheadException");
py::register_exception<dtlmod::UnknownOpenModeException>(m, "UnknownOpenModeException");
py::register_exception<dtlmod::UnknownVariableException>(m, "UnknownVariableException");
py::register_exception<dtlmod::MultipleVariableDefinitionException>(m, "MultipleVariableDefinitionException");
py::register_exception<dtlmod::InconsistentVariableDefinitionException>(m, "InconsistentVariableDefinitionException");
py::register_exception<dtlmod::IncorrectPathDefinitionException>(m, "IncorrectPathDefinitionException");
py::register_exception<dtlmod::GetWhenNoTransactionException>(m, "GetWhenNoTransactionException");
py::register_exception<dtlmod::UnknownReductionMethodException>(m, "UnknownReductionMethodException");
py::register_exception<dtlmod::InconsistentDecimationStrideException>(m, "InconsistentDecimationStrideException");
py::register_exception<dtlmod::InconsistentDecimationInterpolationException>(
m, "InconsistentDecimationInterpolationException");
py::register_exception<dtlmod::UnknownDecimationOptionException>(m, "UnknownDecimationOptionException");
py::register_exception<dtlmod::UnknownDecimationInterpolationException>(m, "UnknownDecimationInterpolationException");
py::register_exception<dtlmod::DoubleReductionException>(m, "DoubleReductionException");
py::register_exception<dtlmod::UnknownCompressionOptionException>(m, "UnknownCompressionOptionException");
py::register_exception<dtlmod::InconsistentCompressionRatioException>(m, "InconsistentCompressionRatioException");
py::register_exception<dtlmod::SubscriberSideCompressionException>(m, "SubscriberSideCompressionException");
py::register_exception<dtlmod::TransactionCanceledException>(m, "TransactionCanceledException");
py::register_exception<dtlmod::EndOfStreamException>(m, "EndOfStreamException");
/* Class Engine */
py::class_<Engine, std::shared_ptr<Engine>> engine(
m, "Engine", "An Engine defines how data is transferred between the applications and the DTL");
engine.def_property_readonly("name", &Engine::get_name, "The name of the Engine (read-only)")
.def("begin_transaction", &Engine::begin_transaction, py::call_guard<simgrid::SimGridGilGuard>(),
"Begin a transaction on this Engine. On the subscriber side, raises an EndOfStreamException if all the "
"publishers have closed the Engine and the requested transaction will never be produced")
.def("put", py::overload_cast<const std::shared_ptr<Variable>&>(&Engine::put, py::const_), py::arg("var"),
py::call_guard<simgrid::SimGridGilGuard>(), "Put a Variable in the DTL using this Engine")
.def("put", py::overload_cast<const std::shared_ptr<Variable>&, size_t>(&Engine::put, py::const_), py::arg("var"),
py::arg("simulated_size_in_bytes"), py::call_guard<simgrid::SimGridGilGuard>(),
"Put a Variable in the DTL using this Engine")
.def("get", &Engine::get, py::arg("var"), py::call_guard<simgrid::SimGridGilGuard>(),
"Get a Variable from the DTL using this Engine")
.def("end_transaction", &Engine::end_transaction, py::call_guard<simgrid::SimGridGilGuard>(),
"End a transaction on this Engine. Raises a TransactionCanceledException if the transaction has been "
"canceled, on the subscriber side, and also on the publisher side with a Staging Engine")
.def_property_readonly("current_transaction", &Engine::get_current_transaction,
"The id of the current transaction on this Engine (read-only)")
.def("cancel_transaction", &Engine::cancel_transaction, py::call_guard<simgrid::SimGridGilGuard>(),
py::arg("transaction_id"),
"Cancel all in-flight activities of a specific transaction (must be called from an external actor). A "
"simulated memory copy in progress before a put is not interrupted")
.def("close", &Engine::close, py::call_guard<simgrid::SimGridGilGuard>(),
"Close this Engine. Publishers block until every publisher has closed it, and so do subscribers once they "
"have started a transaction");
py::enum_<Engine::Type>(engine, "Type", "The type of Engine")
.value("Undefined", Engine::Type::Undefined)
.value("Staging", Engine::Type::Staging)
.value("File", Engine::Type::File);
/* Class Transport */
py::class_<Transport> transport(m, "Transport", "The transport method used by an Engine to transfer data");
py::enum_<Transport::Method>(transport, "Method", "The transport method used by the Engine")
.value("Undefined", Transport::Method::Undefined)
.value("MQ", Transport::Method::MQ)
.value("Mailbox", Transport::Method::Mailbox)
.value("File", Transport::Method::File);
/* Class DTL */
py::class_<DTL, std::shared_ptr<DTL>>(m, "DTL", "Data Transport Layer")
.def_static("create", py::overload_cast<std::string_view>(&DTL::create),
py::call_guard<simgrid::SimGridGilGuard>(), py::arg("filename") = "", "Create the DTL (no return)")
.def_static("connect", &DTL::connect, py::call_guard<simgrid::SimGridGilGuard>(), "Connect an Actor to the DTL")
.def_static("disconnect", &DTL::disconnect, py::call_guard<simgrid::SimGridGilGuard>(),
"Disconnect an Actor from the DTL")
.def_property_readonly("has_active_connections", &DTL::has_active_connections,
"Check whether some simulated actors are currently connected to the DTL (read-only)")
.def("add_stream", &DTL::add_stream, py::call_guard<simgrid::SimGridGilGuard>(), py::arg("name"),
py::arg("type") = Engine::Type::Undefined, py::arg("method") = Transport::Method::Undefined,
"Add a data stream to the DTL")
.def_property_readonly("all_streams", &DTL::get_all_streams,
"Retrieve all streams declared in the DTL (read-only)")
.def(
"stream_by_name",
[](const DTL& self, std::string_view name) { return self.get_stream_by_name(name).value_or(nullptr); },
py::arg("name"), "Retrieve a data stream from the DTL by its name (returns None if not found)");
/* Class Stream */
py::class_<Stream, std::shared_ptr<Stream>> stream(
m, "Stream", "A Stream defines the connection between the applications that produce or consume data and the DTL");
stream.def_property_readonly("name", &Stream::get_name, "The name of the Stream (read-only)")
.def_property_readonly(
"engine_type_str",
[](const Stream& self) {
auto result = self.get_engine_type_str();
return result ? py::cast(*result) : py::cast<py::none>(Py_None);
},
"Print out the engine type of this Stream (read-only, returns None if invalid)")
.def_property_readonly(
"transport_method_str",
[](const Stream& self) {
auto result = self.get_transport_method_str();
return result ? py::cast(*result) : py::cast<py::none>(Py_None);
},
"Print out the transport method of this Stream (read-only, returns None if invalid)")
.def(
"reduction_method",
[](const Stream& self, std::string_view name) -> py::object {
auto result = self.get_reduction_method(name);
return result ? py::cast(*result) : py::cast<py::none>(Py_None);
},
py::arg("name"), "Retrieve a reduction method by name, or None if not found")
.def_property_readonly("engine_type", &Stream::get_engine_type, "Get the engine type of this Stream (read-only)")
.def_property_readonly("transport_method", &Stream::get_transport_method,
"Get the transport method of this Stream (read-only)")
.def_property_readonly("access_mode", &Stream::get_access_mode_str,
"Print out the access mode of this Stream (read-only)")
.def_property_readonly("metadata_export", &Stream::does_export_metadata,
"Does the stream export metadata (read only)")
.def_property_readonly("simulate_memory_copy", &Stream::should_simulate_memory_copy,
"Do Engines opened on this Stream simulate a local memory copy overhead before a put "
"(read only)")
.def_property_readonly("open_overhead", &Stream::get_open_overhead,
"Fixed delay (in seconds) simulated each time an actor opens this Stream (read only)")
.def("set_engine_type", &Stream::set_engine_type, py::arg("type"),
"Set the engine type associated to this Stream")
.def("set_transport_method", &Stream::set_transport_method, py::arg("method"),
"Set the transport method associated to this Stream")
.def("set_metadata_export", &Stream::set_metadata_export,
"Specify that metadata must be exported for that stream")
.def("unset_metadata_export", &Stream::unset_metadata_export,
"Specify that metadata must not be exported for that stream")
.def("set_simulate_memory_copy", &Stream::set_simulate_memory_copy, py::arg("value"),
"Specify whether Engines opened on this Stream should simulate a local memory copy overhead before a "
"put. Must be called before Stream.open")
.def("set_open_overhead", &Stream::set_open_overhead, py::arg("seconds"),
"Specify a fixed delay (in seconds) to simulate each time an actor opens this Stream, whatever its engine "
"type and access mode. Must be called before Stream.open")
// Engine factory
.def("open", &Stream::open, py::arg("name"), py::call_guard<simgrid::SimGridGilGuard>(), py::arg("mode"),
"Open a Stream and create an Engine")
.def_property_readonly("num_publishers", &Stream::get_num_publishers,
"The number of actors connected to this Stream in Mode::Publish (read-only)")
.def_property_readonly("num_subscribers", &Stream::get_num_subscribers,
"The number of actors connected to this Stream in Mode::Subscribe (read-only)")
// Variable factory
.def(
"define_variable",
[](Stream& self, std::string_view name, size_t element_size) {
return self.define_variable(name, element_size);
},
py::call_guard<simgrid::SimGridGilGuard>(), py::arg("name"), py::arg("element_size"),
"Define a scalar variable for this Stream")
.def(
"define_variable",
[](Stream& self, std::string_view name, const std::vector<size_t>& shape, const std::vector<size_t>& start,
const std::vector<size_t>& count,
size_t element_size) { return self.define_variable(name, shape, start, count, element_size); },
py::call_guard<simgrid::SimGridGilGuard>(), py::arg("name"), py::arg("shape"), py::arg("start"),
py::arg("count"), py::arg("element_size"), "Define a variable for this Stream")
.def_property_readonly("all_variables", &Stream::get_all_variables, "Retrieve the list of Variables by names")
.def_property_readonly("metadata_file_name", &Stream::get_metadata_file_name,
"The name of the file in which the stream stores metadata (read-only)")
.def("inquire_variable", &Stream::inquire_variable, py::call_guard<simgrid::SimGridGilGuard>(), py::arg("name"),
py::arg("timeout") = 0.0,
"Retrieve a Variable information by name. If the Variable is not defined yet, wait for at most 'timeout' "
"seconds (0: do not wait, negative: wait forever) before raising an UnknownVariableException. A negative "
"timeout is not released by the end of stream")
.def("remove_variable", &Stream::remove_variable, py::arg("name"), "Remove a Variable from this Stream")
.def("define_reduction_method", &Stream::define_reduction_method, py::arg("name"),
"Define a reduction method for this Stream (e.g. 'decimation' or 'compression')");
py::enum_<Stream::Mode>(stream, "Mode", "The access mode for a Stream")
.value("Publish", Stream::Mode::Publish)
.value("Subscribe", Stream::Mode::Subscribe);
/* Class Variable */
py::class_<Variable, std::shared_ptr<Variable>>(
m, "Variable", "A Variable defines a data object that can be injected into or retrieved from a Stream")
.def_property_readonly("name", &Variable::get_name, "The name of the Variable (read-only)")
.def_property_readonly("shape", &Variable::get_shape, "The shape of the Variable (read-only)")
.def_property_readonly("element_size", &Variable::get_element_size,
"The element size of the Variable (read-only)")
.def_property_readonly("local_size", &Variable::get_local_size,
"The local size of the Variable for the current actor (read-only)")
.def_property_readonly("global_size", &Variable::get_global_size, "The global size of the Variable (read-only)")
.def(
"set_transaction_selection",
[](Variable& self, unsigned int transaction_id) { self.set_transaction_selection(transaction_id); },
py::arg("transaction_id"), "Set the selection of transactions to consider for this Variable")
.def(
"set_transaction_selection",
[](Variable& self, unsigned int begin, unsigned int count) { self.set_transaction_selection(begin, count); },
py::arg("begin"), py::arg("count"), "Set the selection of transactions to consider for this Variable")
.def("set_selection", &Variable::set_selection, py::arg("start"), py::arg("count"),
"Set the selection of elements to consider for this Variable")
.def("set_reduction_operation", &Variable::set_reduction_operation, py::arg("method"), py::arg("parameters"),
"Set a reduction operation on this Variable with the given method and parameters")
.def_property_readonly("is_reduced", &Variable::is_reduced,
"Whether this Variable has a reduction method applied (read-only)")
.def_property_readonly("is_reduced_by_publisher", &Variable::is_reduced_by_publisher,
"Whether this Variable was reduced on the publisher side (read-only)")
.def_property_readonly("is_reduced_by_subscriber", &Variable::is_reduced_by_subscriber,
"Whether this Variable was reduced on the subscriber side (read-only)")
.def_property_readonly("reduction_method", &Variable::get_reduction_method,
"The reduction method applied to this Variable, or None (read-only)");
/* Class ReductionMethod */
py::class_<ReductionMethod, std::shared_ptr<ReductionMethod>>(m, "ReductionMethod",
"A reduction method applied to Variables in a Stream")
.def_property_readonly("name", &ReductionMethod::get_name, "The name of the ReductionMethod (read-only)")
.def("get_reduced_variable_global_size", &ReductionMethod::get_reduced_variable_global_size, py::arg("var"),
py::arg("transaction_id") = 0, "Get the reduced global size of a Variable")
.def("get_reduced_variable_local_size", &ReductionMethod::get_reduced_variable_local_size, py::arg("var"),
py::arg("transaction_id") = 0, "Get the reduced local size of a Variable")
.def("get_reduced_variable_shape", &ReductionMethod::get_reduced_variable_shape, py::arg("var"),
"Get the reduced shape of a Variable")
.def("get_fidelity", &ReductionMethod::get_fidelity, py::arg("var"), py::arg("transaction_id") = 0,
"Get the fraction of the original information retained after reduction, in [0, 1] (1.0 = lossless)")
.def("get_flop_amount_to_reduce_variable", &ReductionMethod::get_flop_amount_to_reduce_variable, py::arg("var"),
"Get the flop cost to reduce a Variable")
.def("get_flop_amount_to_decompress_variable", &ReductionMethod::get_flop_amount_to_decompress_variable,
py::arg("var"), "Get the flop cost to decompress a Variable");
}