/* 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
#include
#include
#include
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<: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<:invalidopenoverheadexception>(m, "InvalidOpenOverheadException");
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");
py::register_exception<:unknownreductionmethodexception>(m, "UnknownReductionMethodException");
py::register_exception<:inconsistentdecimationstrideexception>(m, "InconsistentDecimationStrideException");
py::register_exception<:inconsistentdecimationinterpolationexception>(
m, "InconsistentDecimationInterpolationException");
py::register_exception<:unknowndecimationoptionexception>(m, "UnknownDecimationOptionException");
py::register_exception<:unknowndecimationinterpolationexception>(m, "UnknownDecimationInterpolationException");
py::register_exception<:doublereductionexception>(m, "DoubleReductionException");
py::register_exception<:unknowncompressionoptionexception>(m, "UnknownCompressionOptionException");
py::register_exception<:inconsistentcompressionratioexception>(m, "InconsistentCompressionRatioException");
py::register_exception<:subscribersidecompressionexception>(m, "SubscriberSideCompressionException");
py::register_exception<:transactioncanceledexception>(m, "TransactionCanceledException");
py::register_exception<:endofstreamexception>(m, "EndOfStreamException");
/* 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<: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&>(&Engine::put, py::const_), py::arg("var"),
py::call_guard<:simgridgilguard>(), "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<:simgridgilguard>(),
"Put a Variable in the DTL using this Engine")
.def("get", &Engine::get, py::arg("var"), py::call_guard<:simgridgilguard>(),
"Get a Variable from the DTL using this Engine")
.def("end_transaction", &Engine::end_transaction, py::call_guard<: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<: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<:simgridgilguard>(),
"Close this Engine. Publishers block until every publisher has closed it, and so do subscribers once they "
"have started a transaction");
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);
/* Class DTL */
py::class_>(m, "DTL", "Data Transport Layer")
.def_static("create", py::overload_cast<:string_view>(&DTL::create),
py::call_guard<:simgridgilguard>(), py::arg("filename") = "", "Create the DTL (no return)")
.def_static("connect", &DTL::connect, py::call_guard<:simgridgilguard>(), "Connect an Actor to the DTL")
.def_static("disconnect", &DTL::disconnect, py::call_guard<: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<: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(
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<: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<: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<: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<: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<: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& 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<: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<: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_<: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")
.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_>(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");
}