Repository navigation
Expand file tree
/
Copy pathdtlmod_python.cpp
More file actions
208 lines (184 loc) · 11.5 KB
/
Copy pathdtlmod_python.cpp
File metadata and controls
208 lines (184 loc) · 11.5 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
/* 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/DTL.hpp>
#include <dtlmod/DTLException.hpp>
#include <dtlmod/Engine.hpp>
#include <dtlmod/FileEngine.hpp>
#include <dtlmod/FileTransport.hpp>
#include <dtlmod/Metadata.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 <xbt/log.h>
namespace py = pybind11;
using dtlmod::DTL;
using dtlmod::Engine;
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::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");
/* 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<py::gil_scoped_release>(),
py::arg("filename") = "", "Create the DTL (no return)")
.def_static("connect", &DTL::connect, py::call_guard<py::gil_scoped_release>(), "Connect an Actor to the DTL")
.def_static("disconnect", &DTL::disconnect, py::call_guard<py::gil_scoped_release>(),
"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<py::gil_scoped_release>(), py::arg("name"),
"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",
[](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",
[](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_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("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")
// Engine factory
.def("open", &Stream::open, py::arg("name"), py::call_guard<py::gil_scoped_release>(), 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<py::gil_scoped_release>(), 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<py::gil_scoped_release>(), 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::arg("name"), "Retrieve a Variable information by name")
.def("remove_variable", &Stream::remove_variable, py::arg("name"), "Remove a Variable from this Stream");
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");
/* 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<py::gil_scoped_release>(),
"Begin a transaction on this Engine")
.def("put", py::overload_cast<const std::shared_ptr<Variable>&>(&Engine::put, py::const_), py::arg("var"),
py::call_guard<py::gil_scoped_release>(), "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<py::gil_scoped_release>(),
"Put a Variable in the DTL using this Engine")
.def("get", &Engine::get, py::arg("var"), py::call_guard<py::gil_scoped_release>(),
"Get a Variable from the DTL using this Engine")
.def("end_transaction", &Engine::end_transaction, py::call_guard<py::gil_scoped_release>(),
"End a transaction on this Engine")
.def_property_readonly("current_transaction", &Engine::get_current_transaction,
"The id of the current transaction on this Engine (read-only)")
.def("close", &Engine::close, py::call_guard<py::gil_scoped_release>(), "Close this Engine");
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);
}