See More

/* 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 // Must come before our own stuff #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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<:unknownenginetypeexception>(m, "UnknownEngineTypeException"); py::register_exception<:undefinedenginetypeexception>(m, "UndefinedEngineTypeException"); py::register_exception<:multipleenginetypeexception>(m, "MultipleEngineTypeException"); py::register_exception<:unknowntransportmethodexception>(m, "UnknownTransportMethodException"); py::register_exception<:undefinedtransportmethodexception>(m, "UndefinedTransportMethodException"); py::register_exception<:multipletransportmethodexception>(m, "MultipleTransportMethodException"); py::register_exception<:invalidengineandtransportcombinationexception>( m, "InvalidEngineAndTransportCombinationException"); py::register_exception<:openstreamfailureexception>(m, "OpenStreamFailureException"); py::register_exception<:unknownopenmodeexception>(m, "UnknownOpenModeException"); py::register_exception<:unknownvariableexception>(m, "UnknownVariableException"); py::register_exception<:multiplevariabledefinitionexception>(m, "MultipleVariableDefinitionException"); py::register_exception<:inconsistentvariabledefinitionexception>(m, "InconsistentVariableDefinitionException"); py::register_exception<:incorrectpathdefinitionexception>(m, "IncorrectPathDefinitionException"); py::register_exception<:getwhennotransactionexception>(m, "GetWhenNoTransactionException"); /* Class DTL */ py::class_>(m, "DTL", "Data Transport Layer") .def_static("create", py::overload_cast<:string_view>(&DTL::create), py::call_guard<:gil_scoped_release>(), py::arg("filename") = "", "Create the DTL (no return)") .def_static("connect", &DTL::connect, py::call_guard<:gil_scoped_release>(), "Connect an Actor to the DTL") .def_static("disconnect", &DTL::disconnect, py::call_guard<: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<: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( 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<: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<: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<: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<: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& shape, const std::vector& start, const std::vector& count, size_t element_size) { return self.define_variable(name, shape, start, count, element_size); }, py::call_guard<: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_<:mode>(stream, "Mode", "The access mode for a Stream") .value("Publish", Stream::Mode::Publish) .value("Subscribe", Stream::Mode::Subscribe); /* Class Variable */ py::class_>( 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( 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<:gil_scoped_release>(), "Begin a transaction on this Engine") .def("put", py::overload_cast&>(&Engine::put, py::const_), py::arg("var"), py::call_guard<:gil_scoped_release>(), "Put a Variable in the DTL using this Engine") .def("put", py::overload_cast&, size_t>(&Engine::put, py::const_), py::arg("var"), py::arg("simulated_size_in_bytes"), py::call_guard<:gil_scoped_release>(), "Put a Variable in the DTL using this Engine") .def("get", &Engine::get, py::arg("var"), py::call_guard<:gil_scoped_release>(), "Get a Variable from the DTL using this Engine") .def("end_transaction", &Engine::end_transaction, py::call_guard<: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<:gil_scoped_release>(), "Close this Engine"); py::enum_<: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(m, "Transport", "The transport method used by an Engine to transfer data"); py::enum_<: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); }