Skip to main content

deq_runtime/
lib.rs

1//! deq runtime (software)
2//!
3//! This module provides a distributed decoding system runtime.
4
5#[cfg(feature = "cli")]
6pub mod benchmark;
7#[cfg(feature = "cli")]
8pub mod cli;
9pub mod controller;
10pub mod coordinator;
11pub mod decoder;
12pub mod jit;
13pub mod misc;
14#[cfg(all(feature = "python_binding", feature = "cli"))]
15pub mod python;
16#[cfg(feature = "cli")]
17pub mod server;
18pub mod signal_checker;
19pub mod simulator;
20pub mod visualizer;
21
22pub use crate::signal_checker::SIGNAL_CHECKER;
23
24#[allow(clippy::large_enum_variant)]
25pub mod bin {
26    include!("proto/deq.bin.rs");
27}
28pub mod util {
29    include!("proto/deq.util.rs");
30}
31
32#[cfg(feature = "python_binding")]
33use pyo3::prelude::*;
34
35#[cfg(feature = "python_binding")]
36#[pymodule]
37fn deq_runtime(m: &Bound<'_, PyModule>) -> PyResult<()> {
38    m.add_class::<decoder::python_decoder::PyDecodingHypergraph>()?;
39    m.add_class::<decoder::python_decoder::PyHyperedge>()?;
40    m.add_class::<decoder::python_decoder::PyLossSite>()?;
41    m.add_class::<decoder::python_decoder::PyLossInfo>()?;
42    #[cfg(feature = "cli")]
43    {
44        m.add_function(wrap_pyfunction!(cli_run, m)?)?;
45        python::register(m)?;
46    }
47    m.add_function(wrap_pyfunction!(jit::py_static_jit_compile, m)?)?;
48    Ok(())
49}
50
51#[cfg(all(feature = "python_binding", feature = "cli"))]
52#[pyfunction]
53#[pyo3(signature = (*args, **kwargs))]
54fn cli_run(
55    py: Python<'_>,
56    args: &Bound<'_, pyo3::types::PyTuple>,
57    kwargs: Option<&Bound<'_, pyo3::types::PyDict>>,
58) -> PyResult<()> {
59    let mut parameters: Vec<String> = vec!["deq".to_string()];
60    for arg in args.iter() {
61        let arg_str: String = arg.extract()?;
62        parameters.push(arg_str);
63    }
64    if let Some(kwargs) = kwargs {
65        for (key, value) in kwargs.iter() {
66            let key_str: String = key.extract()?;
67            // Convert Python-style underscores to CLI-style dashes
68            let key_str = key_str.replace('_', "-");
69            let value_str: String = value.extract()?;
70            parameters.push(format!("--{key_str}"));
71            parameters.push(value_str);
72        }
73    }
74    use crate::cli::Cli;
75    use clap::Parser;
76    use tokio::runtime::Runtime;
77
78    let cli = Cli::try_parse_from(parameters).map_err(|e| {
79        // Print the error/help message but don't exit the process
80        let _ = e.print();
81        pyo3::exceptions::PyValueError::new_err(e.to_string())
82    })?;
83    // Release the GIL so the signal checker can acquire it for Ctrl+C handling
84    py.detach(move || {
85        let rt = Runtime::new().unwrap();
86        rt.block_on(async {
87            cli.run().await;
88        });
89    });
90    Ok(())
91}