laddu_python/lib.rs
1#![warn(clippy::perf, clippy::style)]
2#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
3use pyo3::prelude::*;
4use pyo3::types::PyDict;
5
6#[cfg_attr(coverage_nightly, coverage(off))]
7pub mod amplitudes;
8#[cfg_attr(coverage_nightly, coverage(off))]
9pub mod data;
10#[cfg_attr(coverage_nightly, coverage(off))]
11pub mod utils;
12
13#[cfg_attr(coverage_nightly, coverage(off))]
14pub mod mpi {
15 #[cfg(not(feature = "mpi"))]
16 use pyo3::exceptions::PyModuleNotFoundError;
17
18 use super::*;
19 /// Check if ``laddu`` was compiled with MPI support (returns ``True`` if it was).
20 ///
21 /// Since ``laddu-mpi`` has the same namespace as ``laddu`` (they both are imported with
22 /// ``import laddu``), this method can be used to check if MPI capabilities are available
23 /// without actually running any MPI code. While functions in the ``laddu.mpi`` module will
24 /// raise an ``ModuleNotFoundError`` if MPI is not supported, its sometimes convenient to have
25 /// a simple boolean check rather than a try-catch block, and this method provides that.
26 ///
27 #[pyfunction]
28 pub fn is_mpi_available() -> bool {
29 #[cfg(feature = "mpi")]
30 return true;
31 #[cfg(not(feature = "mpi"))]
32 return false;
33 }
34 /// Use the Message Passing Interface (MPI) to run on a distributed system
35 ///
36 /// Parameters
37 /// ----------
38 /// trigger: bool, default=True
39 /// An optional parameter which allows MPI to only be used under some boolean
40 /// condition.
41 ///
42 /// Notes
43 /// -----
44 /// You must have MPI installed for this to work, and you must call the program with
45 /// ``mpirun <executable>``, or bad things will happen.
46 ///
47 /// MPI runs an identical program on each process, but gives the program an ID called its
48 /// "rank". Only the results of methods on the root process (rank 0) should be
49 /// considered valid, as other processes only contain portions of each dataset. To ensure
50 /// you don't save or print data at other ranks, use the provided ``laddu.mpi.is_root()``
51 /// method to check if the process is the root process.
52 ///
53 /// Once MPI is enabled, it cannot be disabled. If MPI could be toggled (which it can't),
54 /// the other processes will still run, but they will be independent of the root process
55 /// and will no longer communicate with it. The root process stores no data, so it would
56 /// be difficult (and convoluted) to get the results which were already processed via
57 /// MPI.
58 ///
59 /// Additionally, MPI must be enabled at the beginning of a script, at least before any
60 /// other ``laddu`` functions are called. For this reason, it is suggested that you use the
61 /// context manager ``laddu.mpi.MPI`` to ensure the MPI backend is used properly.
62 ///
63 /// If ``laddu.mpi.use_mpi()`` is called multiple times, the subsequent calls will have no
64 /// effect.
65 ///
66 /// You **must** call ``laddu.mpi.finalize_mpi()`` before your program exits for MPI to terminate
67 /// smoothly.
68 ///
69 /// See Also
70 /// --------
71 /// laddu.mpi.MPI
72 /// laddu.mpi.using_mpi
73 /// laddu.mpi.is_root
74 /// laddu.mpi.get_rank
75 /// laddu.mpi.get_size
76 /// laddu.mpi.finalize_mpi
77 ///
78 #[pyfunction]
79 #[pyo3(signature = (*, trigger=true))]
80 pub fn use_mpi(trigger: bool) -> PyResult<()> {
81 #[cfg(feature = "mpi")]
82 {
83 laddu_core::mpi::use_mpi(trigger);
84 Ok(())
85 }
86 #[cfg(not(feature = "mpi"))]
87 return Err(PyModuleNotFoundError::new_err(
88 "`laddu` was not compiled with MPI support! Please use `laddu-mpi` instead.",
89 ));
90 }
91
92 /// Drop the MPI universe and finalize MPI at the end of a program
93 ///
94 /// This should only be called once and should be called at the end of all ``laddu``-related
95 /// function calls. This **must** be called at the end of any program which uses MPI.
96 ///
97 /// See Also
98 /// --------
99 /// laddu.mpi.use_mpi
100 ///
101 #[pyfunction]
102 pub fn finalize_mpi() -> PyResult<()> {
103 #[cfg(feature = "mpi")]
104 {
105 laddu_core::mpi::finalize_mpi();
106 Ok(())
107 }
108 #[cfg(not(feature = "mpi"))]
109 return Err(PyModuleNotFoundError::new_err(
110 "`laddu` was not compiled with MPI support! Please use `laddu-mpi` instead.",
111 ));
112 }
113
114 /// Check if MPI is enabled
115 ///
116 /// This can be combined with ``laddu.mpi.is_root()`` to ensure valid results are only
117 /// returned from the root rank process on the condition that MPI is enabled.
118 ///
119 /// See Also
120 /// --------
121 /// laddu.mpi.use_mpi
122 /// laddu.mpi.is_root
123 ///
124 #[pyfunction]
125 pub fn using_mpi() -> PyResult<bool> {
126 #[cfg(feature = "mpi")]
127 return Ok(laddu_core::mpi::using_mpi());
128 #[cfg(not(feature = "mpi"))]
129 return Err(PyModuleNotFoundError::new_err(
130 "`laddu` was not compiled with MPI support! Please use `laddu-mpi` instead.",
131 ));
132 }
133
134 /// Check if the current MPI process is the root process
135 ///
136 /// This can be combined with ``laddu.mpi.using_mpi()`` to ensure valid results are only
137 /// returned from the root rank process on the condition that MPI is enabled.
138 ///
139 /// See Also
140 /// --------
141 /// laddu.mpi.use_mpi
142 /// laddu.mpi.using_mpi
143 ///
144 #[pyfunction]
145 pub fn is_root() -> PyResult<bool> {
146 #[cfg(feature = "mpi")]
147 return Ok(laddu_core::mpi::is_root());
148 #[cfg(not(feature = "mpi"))]
149 return Err(PyModuleNotFoundError::new_err(
150 "`laddu` was not compiled with MPI support! Please use `laddu-mpi` instead.",
151 ));
152 }
153
154 /// Get the rank of the current MPI process
155 ///
156 /// Returns ``None`` if MPI is not enabled
157 ///
158 /// See Also
159 /// --------
160 /// laddu.mpi.use_mpi
161 ///
162 #[pyfunction]
163 pub fn get_rank() -> PyResult<Option<i32>> {
164 #[cfg(feature = "mpi")]
165 return Ok(laddu_core::mpi::get_rank());
166 #[cfg(not(feature = "mpi"))]
167 return Err(PyModuleNotFoundError::new_err(
168 "`laddu` was not compiled with MPI support! Please use `laddu-mpi` instead.",
169 ));
170 }
171
172 /// Get the total number of MPI processes (including the root process)
173 ///
174 /// Returns ``None`` if MPI is not enabled
175 ///
176 /// See Also
177 /// --------
178 /// laddu.mpi.use_mpi
179 ///
180 #[pyfunction]
181 pub fn get_size() -> PyResult<Option<i32>> {
182 #[cfg(feature = "mpi")]
183 return Ok(laddu_core::mpi::get_size());
184 #[cfg(not(feature = "mpi"))]
185 return Err(PyModuleNotFoundError::new_err(
186 "`laddu` was not compiled with MPI support! Please use `laddu-mpi` instead.",
187 ));
188 }
189}
190
191pub trait GetStrExtractObj {
192 fn get_extract<T>(&self, key: &str) -> PyResult<Option<T>>
193 where
194 T: for<'py> FromPyObject<'py>;
195}
196
197#[cfg_attr(coverage_nightly, coverage(off))]
198impl GetStrExtractObj for Bound<'_, PyDict> {
199 fn get_extract<T>(&self, key: &str) -> PyResult<Option<T>>
200 where
201 T: for<'py> FromPyObject<'py>,
202 {
203 self.get_item(key)?
204 .map(|value| value.extract::<T>())
205 .transpose()
206 }
207}