From a849b404cadac4b9d7fb7fad39d1a9ead6498bee Mon Sep 17 00:00:00 2001 From: Nils Homer Date: Sun, 22 Mar 2026 22:06:04 -0700 Subject: [PATCH] feat: add set_threads convenience method Expose a `set_threads` convenience method on `Model` that takes a `NonZeroU32`, following the existing `make_quiet()` pattern. You can already do this with `set_option("threads", 4)`, but the typed method makes it discoverable via IDE autocomplete and catches mistakes at compile time (eg. wrong type, zero value). --- src/lib.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0f7e8a7..45f56de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,7 +110,7 @@ use std::convert::{TryFrom, TryInto}; use std::ffi::{c_void, CString}; -use std::num::TryFromIntError; +use std::num::{NonZeroU32, TryFromIntError}; use std::ops::{Bound, Index, RangeBounds}; use std::os::raw::c_int; use std::ptr::null; @@ -391,6 +391,19 @@ impl Model { self.highs.set_option(option, value) } + /// Set the number of threads to use when solving the model. + /// + /// ``` + /// # use highs::ColProblem; + /// # use highs::Sense::Maximise; + /// # use std::num::NonZeroU32; + /// let mut model = ColProblem::default().optimise(Maximise); + /// model.set_threads(NonZeroU32::new(1).unwrap()); + /// ``` + pub fn set_threads(&mut self, threads: NonZeroU32) { + self.set_option("threads", threads.get() as i32); + } + /// Find the optimal value for the problem, panic if the problem is incoherent pub fn solve(self) -> SolvedModel { self.try_solve().expect("HiGHS error: invalid problem") @@ -1042,4 +1055,22 @@ mod test { let solved = model.solve(); assert_eq!(solved.objective_value(), 1.0); } + + #[test] + fn test_set_threads() { + // Verify that the option is accepted by the solver by reading it back + // via the raw C API after setting it. + use std::num::NonZeroU32; + let mut model = Model::new(RowProblem::default()); + model.set_threads(NonZeroU32::new(2).unwrap()); + let mut value: i32 = 0; + let option = std::ffi::CString::new("threads").unwrap(); + let status = unsafe { + highs_sys::Highs_getIntOptionValue(model.as_mut_ptr(), option.as_ptr(), &mut value) + }; + assert_eq!(status, highs_sys::STATUS_OK); + assert_eq!(value, 2); + } + + }