From 19eec23f70fcab4ec3bbe2c6c2f6d08a7ea7eb8f Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Thu, 16 Oct 2025 18:58:10 +0000 Subject: [PATCH 01/22] Cache when resolving entry modes --- .../instruction_mode_to_global_mode/mod.rs | 319 +++++++++++------- 1 file changed, 206 insertions(+), 113 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index b4bc0f27..ebfe43d0 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -20,6 +20,7 @@ use rustc_hash::FxHashSet; use std::hash::Hash; use std::iter; use std::mem; +use std::ops::ControlFlow; use strum::EnumCount; use strum_macros::{EnumCount, VariantArray}; use unwrap_or::unwrap_some_or; @@ -400,70 +401,135 @@ impl ResolvedControlFlowGraph { f32_rounding_kernels: &FxHashMap, f16f64_rounding_kernels: &FxHashMap, ) -> Result { - fn get_incoming_mode( + fn get_exit_mode( cfg: &ControlFlowGraph, kernels: &FxHashMap, - node: NodeIndex, - mut exit_getter: impl FnMut(&Node) -> Option>, - ) -> Result, TranslateError> { - let mut mode: Option = None; - let mut visited = iter::once(node).collect::>(); - let mut to_visit = cfg - .graph - .neighbors_directed(node, Direction::Incoming) - .map(|x| x) - .collect::>(); - while let Some(node) = to_visit.pop() { - if !visited.insert(node) { - continue; + index: NodeIndex, + node: &Node, + getter: &mut impl FnMut(&Node) -> Mode, + exit_cache: &mut FxHashMap>, + unknown: &mut FxHashSet, + ) -> Option> { + let mode = getter(node); + let exit = match mode.exit { + Some(ExtendedMode::Entry(kernel)) => { + Resolved::Value(kernels.get(&kernel).copied().unwrap_or_default()) } - let node_data = &cfg.graph[node]; - match (mode, exit_getter(node_data)) { - (_, None) => { - for next in cfg.graph.neighbors_directed(node, Direction::Incoming) { - if !visited.contains(&next) { - to_visit.push(next); - } - } + Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), + None => { + if let Some(mode) = exit_cache.get(&index) { + return Some(*mode); } - (existing_mode, Some(new_mode)) => { - let new_mode = match new_mode { - ExtendedMode::BasicBlock(new_mode) => new_mode, - ExtendedMode::Entry(kernel) => { - kernels.get(&kernel).copied().unwrap_or_default() + unknown.insert(index); + let mode = cfg + .graph + .neighbors_directed(index, Direction::Incoming) + .try_fold(None, |prevailing_mode: Option, predecessor| { + if unknown.contains(&predecessor) { + return ControlFlow::Continue(prevailing_mode); } - }; - if let Some(existing_mode) = existing_mode { - if existing_mode != new_mode { - return Ok(Resolved::Conflict); + let mode = get_exit_mode( + cfg, + kernels, + predecessor, + &cfg.graph[predecessor], + getter, + exit_cache, + unknown, + ); + match (prevailing_mode, mode) { + (_, None) => { + return ControlFlow::Continue(prevailing_mode); + } + (_, Some(Resolved::Conflict)) => { + ControlFlow::Break(Resolved::::Conflict) + } + (None, Some(Resolved::Value(x))) => ControlFlow::Continue(Some(x)), + (Some(prevailing_mode), Some(Resolved::Value(x))) => { + if prevailing_mode == x { + ControlFlow::Continue(Some(prevailing_mode)) + } else { + ControlFlow::Break(Resolved::::Conflict) + } + } } + }); + let result = match mode { + ControlFlow::Break(x) => x, + ControlFlow::Continue(None) => { + return None; } - mode = Some(new_mode); - } + ControlFlow::Continue(Some(x)) => Resolved::Value(x), + }; + exit_cache.insert(index, result); + unknown.remove(&index); + result } - } - // This should happen only for orphaned basic blocks - mode.map(Resolved::Value).ok_or_else(error_unreachable) + }; + Some(exit) } - fn resolve_mode( + fn get_entry_mode( cfg: &ControlFlowGraph, kernels: &FxHashMap, - node: NodeIndex, - exit_getter: impl FnMut(&Node) -> Option>, - mode: &Mode, - ) -> Result, TranslateError> { + index: NodeIndex, + node: &Node, + getter: &mut impl FnMut(&Node) -> Mode, + exit_cache: &mut FxHashMap>, + unknown: &mut FxHashSet, + ) -> Resolved { + let mode = getter(node); let entry = match mode.entry { Some(ExtendedMode::Entry(kernel)) => { Resolved::Value(kernels.get(&kernel).copied().unwrap_or_default()) } Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), - None => get_incoming_mode(cfg, kernels, node, exit_getter)?, - }; - let exit = match mode.entry { - Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), - Some(ExtendedMode::Entry(_)) | None => entry, + None => { + unknown.clear(); + unknown.insert(index); + let mode = cfg + .graph + .neighbors_directed(index, Direction::Incoming) + .try_fold(None, |prevailing_mode: Option, predecessor| { + if unknown.contains(&predecessor) { + return ControlFlow::Continue(prevailing_mode); + } + let mode = get_exit_mode( + cfg, + kernels, + predecessor, + &cfg.graph[predecessor], + getter, + exit_cache, + unknown, + ); + match (prevailing_mode, mode) { + (_, None) => { + return ControlFlow::Continue(prevailing_mode); + } + (_, Some(Resolved::Conflict)) => { + ControlFlow::Break(Resolved::::Conflict) + } + (None, Some(Resolved::Value(x))) => ControlFlow::Continue(Some(x)), + (Some(prevailing_mode), Some(Resolved::Value(x))) => { + if prevailing_mode == x { + ControlFlow::Continue(Some(prevailing_mode)) + } else { + ControlFlow::Break(Resolved::::Conflict) + } + } + } + }); + let result = match mode { + ControlFlow::Break(x) => x, + ControlFlow::Continue(None) => { + panic!("Should never happen for {:?}", index) + } + ControlFlow::Continue(Some(x)) => Resolved::Value(x), + }; + result + } }; - Ok(ResolvedMode { entry, exit }) + entry } fn resolve_node_impl( cfg: &ControlFlowGraph, @@ -473,91 +539,107 @@ impl ResolvedControlFlowGraph { f16f64_rounding_kernels: &FxHashMap, index: NodeIndex, node: &Node, + visited: &mut FxHashSet, + denormal_f32_cache: &mut FxHashMap>, + denormal_f16f64_cache: &mut FxHashMap>, + rounding_f32_cache: &mut FxHashMap>, + rounding_f16f64_cache: &mut FxHashMap>, ) -> Result { - let denormal_f32 = resolve_mode( + visited.clear(); + let denormal_f32_entry = get_entry_mode( cfg, f32_denormal_kernels, index, - |node| node.denormal_f32.exit, - &node.denormal_f32, - )?; - let denormal_f16f64 = resolve_mode( + node, + &mut |node| node.denormal_f32, + denormal_f32_cache, + visited, + ); + let denormal_f32_exit = match node.denormal_f32.exit { + Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), + Some(ExtendedMode::Entry(_)) | None => denormal_f32_entry, + }; + visited.clear(); + let denormal_f16f64_entry = get_entry_mode( cfg, f16f64_denormal_kernels, index, - |node| node.denormal_f16f64.exit, - &node.denormal_f16f64, - )?; - let rounding_f32 = resolve_mode( + node, + &mut |node| node.denormal_f16f64, + denormal_f16f64_cache, + visited, + ); + let denormal_f16f64_exit = match node.denormal_f16f64.exit { + Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), + Some(ExtendedMode::Entry(_)) | None => denormal_f16f64_entry, + }; + visited.clear(); + let rounding_f32_entry = get_entry_mode( cfg, f32_rounding_kernels, index, - |node| node.rounding_f32.exit, - &node.rounding_f32, - )?; - let rounding_f16f64 = resolve_mode( + node, + &mut |node| node.rounding_f32, + rounding_f32_cache, + visited, + ); + let rounding_f32_exit = match node.rounding_f32.exit { + Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), + Some(ExtendedMode::Entry(_)) | None => rounding_f32_entry, + }; + visited.clear(); + let rounding_f16f64_entry = get_entry_mode( cfg, f16f64_rounding_kernels, index, - |node| node.rounding_f16f64.exit, - &node.rounding_f16f64, - )?; + node, + &mut |node| node.rounding_f16f64, + rounding_f16f64_cache, + visited, + ); + let rounding_f16f64_exit = match node.rounding_f16f64.exit { + Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), + Some(ExtendedMode::Entry(_)) | None => rounding_f16f64_entry, + }; Ok(ResolvedNode { label: node.label, - denormal_f32, - denormal_f16f64, - rounding_f32, - rounding_f16f64, + denormal_f32: ResolvedMode { + entry: denormal_f32_entry, + exit: denormal_f32_exit, + }, + denormal_f16f64: ResolvedMode { + entry: denormal_f16f64_entry, + exit: denormal_f16f64_exit, + }, + rounding_f32: ResolvedMode { + entry: rounding_f32_entry, + exit: rounding_f32_exit, + }, + rounding_f16f64: ResolvedMode { + entry: rounding_f16f64_entry, + exit: rounding_f16f64_exit, + }, }) } - fn resolve_node( - cfg: &ControlFlowGraph, - f32_denormal_kernels: &FxHashMap, - f16f64_denormal_kernels: &FxHashMap, - f32_rounding_kernels: &FxHashMap, - f16f64_rounding_kernels: &FxHashMap, - index: NodeIndex, - node: &Node, - error: &mut bool, - ) -> ResolvedNode { - match resolve_node_impl( - cfg, - f32_denormal_kernels, - f16f64_denormal_kernels, - f32_rounding_kernels, - f16f64_rounding_kernels, - index, - node, - ) { - Ok(node) => node, - Err(_) => { - *error = true; - ResolvedNode { - label: SpirvWord(u32::MAX), - denormal_f32: ResolvedMode { - entry: Resolved::Conflict, - exit: Resolved::Conflict, - }, - denormal_f16f64: ResolvedMode { - entry: Resolved::Conflict, - exit: Resolved::Conflict, - }, - rounding_f32: ResolvedMode { - entry: Resolved::Conflict, - exit: Resolved::Conflict, - }, - rounding_f16f64: ResolvedMode { - entry: Resolved::Conflict, - exit: Resolved::Conflict, - }, - } - } - } - } let mut error = false; + // { + // use std::io::Write; + // let mut file = std::fs::File::create("/slowhome/andrzej/dev/zluda/graph.dot").unwrap(); + // write!( + // &mut file, + // "{:?}", + // petgraph::dot::Dot::with_config(&cfg.graph, &[petgraph::dot::Config::EdgeNoLabel]), + // ) + // .unwrap(); + // } + let mut visited = FxHashSet::default(); + let mut denormal_f32_cache = FxHashMap::default(); + let mut denormal_f16f64_cache = FxHashMap::default(); + let mut rounding_f32_cache = FxHashMap::default(); + let mut rounding_f16f64_cache = FxHashMap::default(); let graph = cfg.graph.map( |index, node| { - resolve_node( + resolve_node_impl( &cfg, f32_denormal_kernels, f16f64_denormal_kernels, @@ -565,8 +647,13 @@ impl ResolvedControlFlowGraph { f16f64_rounding_kernels, index, node, - &mut error, + &mut visited, + &mut denormal_f32_cache, + &mut denormal_f16f64_cache, + &mut rounding_f32_cache, + &mut rounding_f16f64_cache, ) + .unwrap() }, |_, ()| (), ); @@ -702,6 +789,7 @@ fn compute_full_mode_insertions( rounding_f32: MandatoryModeInsertions, rounding_f16f64: MandatoryModeInsertions, ) -> Result { + let start = std::time::Instant::now(); let cfg = ResolvedControlFlowGraph::new( cfg, &denormal_f32.kernels, @@ -709,6 +797,11 @@ fn compute_full_mode_insertions( &rounding_f32.kernels, &rounding_f16f64.kernels, )?; + let duration = start.elapsed(); + println!( + " Pass \"compute_full_mode_insertions::new\" took {:?}", + duration + ); join_modes( flat_resolver, directives, From e2eae2d38d5e91b6a664b38c35fbabca4c7f6877 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Thu, 16 Oct 2025 23:41:51 +0000 Subject: [PATCH 02/22] Clean up changes --- .../instruction_mode_to_global_mode/mod.rs | 201 +++++++++--------- 1 file changed, 95 insertions(+), 106 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index ebfe43d0..4ddb141c 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -401,6 +401,56 @@ impl ResolvedControlFlowGraph { f32_rounding_kernels: &FxHashMap, f16f64_rounding_kernels: &FxHashMap, ) -> Result { + fn get_exit_mode_from_dependencies( + cfg: &ControlFlowGraph, + kernels: &FxHashMap, + index: NodeIndex, + getter: &mut impl FnMut(&Node) -> Mode, + exit_cache: &mut std::collections::HashMap< + NodeIndex, + Resolved, + rustc_hash::FxBuildHasher, + >, + unknown: &mut std::collections::HashSet, + ) -> Option> { + unknown.insert(index); + let mode = cfg + .graph + .neighbors_directed(index, Direction::Incoming) + .try_fold(None, |prevailing_mode: Option, predecessor| { + if unknown.contains(&predecessor) { + return ControlFlow::Continue(prevailing_mode); + } + let mode = get_exit_mode( + cfg, + kernels, + predecessor, + &cfg.graph[predecessor], + getter, + exit_cache, + unknown, + ); + match (prevailing_mode, mode) { + (_, None) => { + return ControlFlow::Continue(prevailing_mode); + } + (_, Some(Resolved::Conflict)) => ControlFlow::Break(()), + (None, Some(Resolved::Value(x))) => ControlFlow::Continue(Some(x)), + (Some(prevailing_mode), Some(Resolved::Value(x))) => { + if prevailing_mode == x { + ControlFlow::Continue(Some(prevailing_mode)) + } else { + ControlFlow::Break(()) + } + } + } + }); + match mode { + ControlFlow::Break(_) => Some(Resolved::Conflict), + ControlFlow::Continue(None) => None, + ControlFlow::Continue(Some(x)) => Some(Resolved::Value(x)), + } + } fn get_exit_mode( cfg: &ControlFlowGraph, kernels: &FxHashMap, @@ -420,47 +470,10 @@ impl ResolvedControlFlowGraph { if let Some(mode) = exit_cache.get(&index) { return Some(*mode); } - unknown.insert(index); - let mode = cfg - .graph - .neighbors_directed(index, Direction::Incoming) - .try_fold(None, |prevailing_mode: Option, predecessor| { - if unknown.contains(&predecessor) { - return ControlFlow::Continue(prevailing_mode); - } - let mode = get_exit_mode( - cfg, - kernels, - predecessor, - &cfg.graph[predecessor], - getter, - exit_cache, - unknown, - ); - match (prevailing_mode, mode) { - (_, None) => { - return ControlFlow::Continue(prevailing_mode); - } - (_, Some(Resolved::Conflict)) => { - ControlFlow::Break(Resolved::::Conflict) - } - (None, Some(Resolved::Value(x))) => ControlFlow::Continue(Some(x)), - (Some(prevailing_mode), Some(Resolved::Value(x))) => { - if prevailing_mode == x { - ControlFlow::Continue(Some(prevailing_mode)) - } else { - ControlFlow::Break(Resolved::::Conflict) - } - } - } - }); - let result = match mode { - ControlFlow::Break(x) => x, - ControlFlow::Continue(None) => { - return None; - } - ControlFlow::Continue(Some(x)) => Resolved::Value(x), - }; + let result = get_exit_mode_from_dependencies( + cfg, kernels, index, getter, exit_cache, unknown, + ); + let result = unwrap_some_or!(result, return None); exit_cache.insert(index, result); unknown.remove(&index); result @@ -476,7 +489,7 @@ impl ResolvedControlFlowGraph { getter: &mut impl FnMut(&Node) -> Mode, exit_cache: &mut FxHashMap>, unknown: &mut FxHashSet, - ) -> Resolved { + ) -> Result, TranslateError> { let mode = getter(node); let entry = match mode.entry { Some(ExtendedMode::Entry(kernel)) => { @@ -485,53 +498,15 @@ impl ResolvedControlFlowGraph { Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), None => { unknown.clear(); - unknown.insert(index); - let mode = cfg - .graph - .neighbors_directed(index, Direction::Incoming) - .try_fold(None, |prevailing_mode: Option, predecessor| { - if unknown.contains(&predecessor) { - return ControlFlow::Continue(prevailing_mode); - } - let mode = get_exit_mode( - cfg, - kernels, - predecessor, - &cfg.graph[predecessor], - getter, - exit_cache, - unknown, - ); - match (prevailing_mode, mode) { - (_, None) => { - return ControlFlow::Continue(prevailing_mode); - } - (_, Some(Resolved::Conflict)) => { - ControlFlow::Break(Resolved::::Conflict) - } - (None, Some(Resolved::Value(x))) => ControlFlow::Continue(Some(x)), - (Some(prevailing_mode), Some(Resolved::Value(x))) => { - if prevailing_mode == x { - ControlFlow::Continue(Some(prevailing_mode)) - } else { - ControlFlow::Break(Resolved::::Conflict) - } - } - } - }); - let result = match mode { - ControlFlow::Break(x) => x, - ControlFlow::Continue(None) => { - panic!("Should never happen for {:?}", index) - } - ControlFlow::Continue(Some(x)) => Resolved::Value(x), - }; - result + let result = get_exit_mode_from_dependencies( + cfg, kernels, index, getter, exit_cache, unknown, + ); + unwrap_some_or!(result, return Err(error_unreachable())) } }; - entry + Ok(entry) } - fn resolve_node_impl( + fn resolve_node( cfg: &ControlFlowGraph, f32_denormal_kernels: &FxHashMap, f16f64_denormal_kernels: &FxHashMap, @@ -554,7 +529,7 @@ impl ResolvedControlFlowGraph { &mut |node| node.denormal_f32, denormal_f32_cache, visited, - ); + )?; let denormal_f32_exit = match node.denormal_f32.exit { Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), Some(ExtendedMode::Entry(_)) | None => denormal_f32_entry, @@ -568,7 +543,7 @@ impl ResolvedControlFlowGraph { &mut |node| node.denormal_f16f64, denormal_f16f64_cache, visited, - ); + )?; let denormal_f16f64_exit = match node.denormal_f16f64.exit { Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), Some(ExtendedMode::Entry(_)) | None => denormal_f16f64_entry, @@ -582,7 +557,7 @@ impl ResolvedControlFlowGraph { &mut |node| node.rounding_f32, rounding_f32_cache, visited, - ); + )?; let rounding_f32_exit = match node.rounding_f32.exit { Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), Some(ExtendedMode::Entry(_)) | None => rounding_f32_entry, @@ -596,7 +571,7 @@ impl ResolvedControlFlowGraph { &mut |node| node.rounding_f16f64, rounding_f16f64_cache, visited, - ); + )?; let rounding_f16f64_exit = match node.rounding_f16f64.exit { Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), Some(ExtendedMode::Entry(_)) | None => rounding_f16f64_entry, @@ -621,17 +596,7 @@ impl ResolvedControlFlowGraph { }, }) } - let mut error = false; - // { - // use std::io::Write; - // let mut file = std::fs::File::create("/slowhome/andrzej/dev/zluda/graph.dot").unwrap(); - // write!( - // &mut file, - // "{:?}", - // petgraph::dot::Dot::with_config(&cfg.graph, &[petgraph::dot::Config::EdgeNoLabel]), - // ) - // .unwrap(); - // } + let mut error = None; let mut visited = FxHashSet::default(); let mut denormal_f32_cache = FxHashMap::default(); let mut denormal_f16f64_cache = FxHashMap::default(); @@ -639,7 +604,7 @@ impl ResolvedControlFlowGraph { let mut rounding_f16f64_cache = FxHashMap::default(); let graph = cfg.graph.map( |index, node| { - resolve_node_impl( + let maybe_node = resolve_node( &cfg, f32_denormal_kernels, f16f64_denormal_kernels, @@ -652,13 +617,37 @@ impl ResolvedControlFlowGraph { &mut denormal_f16f64_cache, &mut rounding_f32_cache, &mut rounding_f16f64_cache, - ) - .unwrap() + ); + match maybe_node { + Ok(node) => node, + Err(e) => { + error = Some(e); + ResolvedNode { + label: node.label, + denormal_f32: ResolvedMode { + entry: Resolved::Conflict, + exit: Resolved::Conflict, + }, + denormal_f16f64: ResolvedMode { + entry: Resolved::Conflict, + exit: Resolved::Conflict, + }, + rounding_f32: ResolvedMode { + entry: Resolved::Conflict, + exit: Resolved::Conflict, + }, + rounding_f16f64: ResolvedMode { + entry: Resolved::Conflict, + exit: Resolved::Conflict, + }, + } + } + } }, |_, ()| (), ); - if error { - Err(error_unreachable()) + if let Some(error) = error { + Err(error) } else { Ok(Self { basic_blocks: cfg.basic_blocks, From 6e028d30026b497c3ce0ba6a8a8d2af6f746e479 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Fri, 17 Oct 2025 20:17:01 +0000 Subject: [PATCH 03/22] Replace microlp with HiGHS --- Cargo.lock | 148 +++++++----------- ptx/Cargo.toml | 2 +- .../instruction_mode_to_global_mode/mod.rs | 91 +++++++---- 3 files changed, 115 insertions(+), 126 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8030f1cf..664038ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,6 +138,26 @@ dependencies = [ "syn 2.0.89", ] +[[package]] +name = "bindgen" +version = "0.71.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f58bf3d7db68cfbac37cfc485a8d711e87e064c3d0fe0435b92f7a407f9d6b3" +dependencies = [ + "bitflags 2.9.1", + "cexpr", + "clang-sys", + "itertools 0.13.0", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash 2.1.1", + "shlex", + "syn 2.0.89", +] + [[package]] name = "bit-vec" version = "0.8.0" @@ -420,7 +440,7 @@ version = "0.0.0" dependencies = [ "proc-macro2", "quote", - "rustc-hash 2.0.0", + "rustc-hash 2.1.1", "syn 2.0.89", ] @@ -1771,6 +1791,26 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "highs" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "122a64dffa53f13578cffd62fdf1cb2f83ac8a71a989dce5d9232049331fff28" +dependencies = [ + "highs-sys", + "log", +] + +[[package]] +name = "highs-sys" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "908a51a57402b0616cf0026c0e7bf908cbee7bac13bd45778ba4f2e2ac6bb6db" +dependencies = [ + "bindgen 0.71.1", + "cmake", +] + [[package]] name = "hip_runtime-sys" version = "0.0.0" @@ -2121,9 +2161,9 @@ checksum = "9374ef4228402d4b7e403e5838cb880d9ee663314b0a900d5a6aabf0c213552e" [[package]] name = "log" -version = "0.4.22" +version = "0.4.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" [[package]] name = "logos" @@ -2168,16 +2208,6 @@ dependencies = [ "libc", ] -[[package]] -name = "matrixmultiply" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9380b911e3e96d10c1f415da0876389aaf1b56759054eeb0de7df940c456ba1a" -dependencies = [ - "autocfg", - "rawpointer", -] - [[package]] name = "maybe-async" version = "0.2.10" @@ -2213,16 +2243,6 @@ dependencies = [ "libc", ] -[[package]] -name = "microlp" -version = "0.2.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51d1790c73b93164ff65868f63164497cb32339458a9297e17e212d91df62258" -dependencies = [ - "log", - "sprs", -] - [[package]] name = "migrations_internals" version = "2.2.1" @@ -2266,21 +2286,6 @@ dependencies = [ "hip_runtime-sys", ] -[[package]] -name = "ndarray" -version = "0.16.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "882ed72dce9365842bf196bdeedf5055305f11fc8c03dee7bb0194a6cad34841" -dependencies = [ - "matrixmultiply", - "num-complex", - "num-integer", - "num-traits", - "portable-atomic", - "portable-atomic-util", - "rawpointer", -] - [[package]] name = "no-std-compat" version = "0.4.1" @@ -2297,39 +2302,12 @@ dependencies = [ "minimal-lexical", ] -[[package]] -name = "num-complex" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" -dependencies = [ - "num-traits", -] - [[package]] name = "num-conv" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" -[[package]] -name = "num-integer" -version = "0.1.46" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" -dependencies = [ - "num-traits", -] - -[[package]] -name = "num-traits" -version = "0.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" -dependencies = [ - "autocfg", -] - [[package]] name = "num_enum" version = "0.4.3" @@ -2570,17 +2548,17 @@ dependencies = [ "cuda_macros", "cuda_types", "half", + "highs", "hip_runtime-sys", "int-enum", "libloading", "llvm_zluda", - "microlp", "paste", "petgraph", "pretty_assertions", "ptx_parser", "quick-error", - "rustc-hash 2.0.0", + "rustc-hash 2.1.1", "smallvec", "strum 0.26.3", "strum_macros 0.26.4", @@ -2597,7 +2575,7 @@ dependencies = [ "derive_more", "logos", "ptx_parser_macros", - "rustc-hash 2.0.0", + "rustc-hash 2.1.1", "strum 0.27.1", "thiserror 1.0.64", "winnow 0.6.20", @@ -2612,7 +2590,7 @@ dependencies = [ "proc-macro2", "ptx_parser_macros_impl", "quote", - "rustc-hash 2.0.0", + "rustc-hash 2.1.1", "syn 2.0.89", ] @@ -2622,7 +2600,7 @@ version = "0.0.0" dependencies = [ "proc-macro2", "quote", - "rustc-hash 2.0.0", + "rustc-hash 2.1.1", "syn 2.0.89", ] @@ -2658,12 +2636,6 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" -[[package]] -name = "rawpointer" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" - [[package]] name = "redox_syscall" version = "0.5.11" @@ -2738,9 +2710,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] name = "rustc-hash" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" [[package]] name = "rustc_version" @@ -2938,18 +2910,6 @@ version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" -[[package]] -name = "sprs" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bff8419009a08f6cb7519a602c5590241fbff1446bcc823c07af15386eb801b" -dependencies = [ - "ndarray", - "num-complex", - "num-traits", - "smallvec", -] - [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -3714,7 +3674,7 @@ dependencies = [ "paste", "ptx", "ptx_parser", - "rustc-hash 2.0.0", + "rustc-hash 2.1.1", "serde", "serde_json", "tempfile", @@ -3728,13 +3688,13 @@ dependencies = [ name = "zluda_bindgen" version = "0.1.0" dependencies = [ - "bindgen", + "bindgen 0.70.1", "cuda_types", "libloading", "prettyplease", "proc-macro2", "quote", - "rustc-hash 2.0.0", + "rustc-hash 2.1.1", "syn 2.0.89", ] @@ -3883,7 +3843,7 @@ dependencies = [ "ptx", "ptx_parser", "regex", - "rustc-hash 2.0.0", + "rustc-hash 2.1.1", "unwrap_or", "wchar", "winapi", diff --git a/ptx/Cargo.toml b/ptx/Cargo.toml index 7ee6e436..996b85fe 100644 --- a/ptx/Cargo.toml +++ b/ptx/Cargo.toml @@ -18,7 +18,7 @@ rustc-hash = "2.0.0" strum = "0.26" strum_macros = "0.26" petgraph = "0.7.1" -microlp = "0.2.11" +highs = "1.12.0" int-enum = "1.1" unwrap_or = "1.0.1" smallvec = "1.15.1" diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index 4ddb141c..5d122d72 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -7,9 +7,6 @@ use super::SpirvWord; use super::Statement; use super::TranslateError; use crate::pass::error_unreachable; -use microlp::OptimizationDirection; -use microlp::Problem; -use microlp::Variable; use petgraph::graph::NodeIndex; use petgraph::visit::IntoNodeReferences; use petgraph::Direction; @@ -740,9 +737,22 @@ pub(crate) fn run<'input>( flat_resolver: &mut GlobalStringIdentResolver2<'input>, directives: Vec, SpirvWord>>, ) -> Result, SpirvWord>>, TranslateError> { + let mut start = std::time::Instant::now(); let cfg = create_control_flow_graph(&directives)?; + let duration = start.elapsed(); + println!( + " Subpass \"create_control_flow_graph\" took {:?}", + duration + ); + let mut start = std::time::Instant::now(); let (denormal_f32, denormal_f16f64, rounding_f32, rounding_f16f64) = compute_minimal_mode_insertions(&cfg); + let duration = start.elapsed(); + println!( + " Subpass \"compute_minimal_mode_insertions\" took {:?}", + duration + ); + let mut start = std::time::Instant::now(); let temp = compute_full_mode_insertions( flat_resolver, &directives, @@ -752,7 +762,19 @@ pub(crate) fn run<'input>( rounding_f32, rounding_f16f64, )?; - apply_global_mode_controls(directives, temp) + let duration = start.elapsed(); + println!( + " Subpass \"compute_full_mode_insertions\" took {:?}", + duration + ); + let mut start = std::time::Instant::now(); + let result = apply_global_mode_controls(directives, temp); + let duration = start.elapsed(); + println!( + " Subpass \"apply_global_mode_controls\" took {:?}", + duration + ); + result } // For every basic block this pass computes: @@ -778,7 +800,6 @@ fn compute_full_mode_insertions( rounding_f32: MandatoryModeInsertions, rounding_f16f64: MandatoryModeInsertions, ) -> Result { - let start = std::time::Instant::now(); let cfg = ResolvedControlFlowGraph::new( cfg, &denormal_f32.kernels, @@ -786,11 +807,6 @@ fn compute_full_mode_insertions( &rounding_f32.kernels, &rounding_f16f64.kernels, )?; - let duration = start.elapsed(); - println!( - " Pass \"compute_full_mode_insertions::new\" took {:?}", - duration - ); join_modes( flat_resolver, directives, @@ -819,10 +835,17 @@ fn compute_minimal_mode_insertions( MandatoryModeInsertions, MandatoryModeInsertions, ) { + let start = std::time::Instant::now(); let rounding_f32 = compute_single_mode_insertions(cfg, |node| node.rounding_f32); let denormal_f32 = compute_single_mode_insertions(cfg, |node| node.denormal_f32); let denormal_f16f64 = compute_single_mode_insertions(cfg, |node| node.denormal_f16f64); let rounding_f16f64 = compute_single_mode_insertions(cfg, |node| node.rounding_f16f64); + let duration = start.elapsed(); + println!( + " Subsubpass \"compute_single_mode_insertions\" took {:?}", + duration + ); + let start = std::time::Instant::now(); let denormal_f32 = optimize_mode_insertions::(denormal_f32); let denormal_f16f64 = @@ -831,6 +854,11 @@ fn compute_minimal_mode_insertions( optimize_mode_insertions::(rounding_f32); let rounding_f16f64: MandatoryModeInsertions = optimize_mode_insertions::(rounding_f16f64); + let duration = start.elapsed(); + println!( + " Subsubpass \"optimize_mode_insertions\" took {:?}", + duration + ); (denormal_f32, denormal_f16f64, rounding_f32, rounding_f16f64) } @@ -1768,7 +1796,7 @@ fn optimize_mode_insertions< >( partial: PartialModeInsertion, ) -> MandatoryModeInsertions { - let mut problem = Problem::new(OptimizationDirection::Maximize); + let mut problem = highs::RowProblem::default(); let mut kernel_modes = FxHashMap::default(); let basic_block_variables = partial .bb_maybe_insert_mode @@ -1782,13 +1810,22 @@ fn optimize_mode_insertions< .or_insert_with(|| one_of::(&mut problem)); kernel_modes[value.into()] }) - .collect::>(); + .collect::>(); let bb = and(&mut problem, &*modes); (basic_block, bb) }) .collect::>(); // TODO: add fallback on Error - let solution = problem.solve().unwrap(); + let mut solver = problem.optimise(highs::Sense::Maximise); + solver.make_quiet(); + // Takes minutes for a problem that is solved sub-second + solver.set_option("presolve", "off"); + // Experimentally, the fastest mode, simplex with simplex_strategy = 0 is slightly slower + solver.set_option("solver", "pdlp"); + solver.set_option("parallel", "off"); + solver.set_option("threads", 1); + let solved_model = solver.solve(); + let solution = solved_model.get_solution(); let mut basic_blocks = partial.bb_must_insert_mode; for (basic_block, variable) in basic_block_variables { if solution[variable] < 0.5 { @@ -1810,30 +1847,22 @@ fn optimize_mode_insertions< } } -fn and(problem: &mut Problem, variables: &[Variable]) -> Variable { - let result = problem.add_binary_var(1.0); - for var in variables { - problem.add_constraint( - &[(result, 1.0), (*var, -1.0)], - microlp::ComparisonOp::Le, - 0.0, - ); +fn and(problem: &mut highs::RowProblem, variables: &[highs::Col]) -> highs::Col { + let result = problem.add_integer_column(1.0, 0..=1); + for var in variables.iter().copied() { + problem.add_row(..=0, &[(result, 1.0), (var, -1.0)]); } - problem.add_constraint( + let variables = variables.to_vec(); + problem.add_row( + -(variables.len() as i32 - 1).., iter::once((result, 1.0)).chain(variables.iter().map(|var| (*var, -1.0))), - microlp::ComparisonOp::Ge, - -((variables.len() - 1) as f64), ); result } -fn one_of(problem: &mut Problem) -> [Variable; N] { - let result = std::array::from_fn(|_| problem.add_binary_var(0.0)); - problem.add_constraint( - result.into_iter().map(|var| (var, 1.0)), - microlp::ComparisonOp::Eq, - 1.0, - ); +fn one_of(problem: &mut highs::RowProblem) -> [highs::Col; N] { + let result = std::array::from_fn(|_| problem.add_integer_column(0.0, 0..=1)); + problem.add_row(1.0..=1.0, result.into_iter().map(|var| (var, 1.0))); result } From 9b8493edbcd655013c933baaf4b7598cb66ad6c1 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Fri, 17 Oct 2025 20:23:22 +0000 Subject: [PATCH 04/22] Don't panic on HiGHS errors --- .../instruction_mode_to_global_mode/mod.rs | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index 5d122d72..92885bca 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -7,6 +7,7 @@ use super::SpirvWord; use super::Statement; use super::TranslateError; use crate::pass::error_unreachable; +use highs::HighsStatus; use petgraph::graph::NodeIndex; use petgraph::visit::IntoNodeReferences; use petgraph::Direction; @@ -746,7 +747,7 @@ pub(crate) fn run<'input>( ); let mut start = std::time::Instant::now(); let (denormal_f32, denormal_f16f64, rounding_f32, rounding_f16f64) = - compute_minimal_mode_insertions(&cfg); + compute_minimal_mode_insertions(&cfg)?; let duration = start.elapsed(); println!( " Subpass \"compute_minimal_mode_insertions\" took {:?}", @@ -829,12 +830,15 @@ fn compute_full_mode_insertions( // pass should use default value fn compute_minimal_mode_insertions( cfg: &ControlFlowGraph, -) -> ( - MandatoryModeInsertions, - MandatoryModeInsertions, - MandatoryModeInsertions, - MandatoryModeInsertions, -) { +) -> Result< + ( + MandatoryModeInsertions, + MandatoryModeInsertions, + MandatoryModeInsertions, + MandatoryModeInsertions, + ), + TranslateError, +> { let start = std::time::Instant::now(); let rounding_f32 = compute_single_mode_insertions(cfg, |node| node.rounding_f32); let denormal_f32 = compute_single_mode_insertions(cfg, |node| node.denormal_f32); @@ -847,19 +851,23 @@ fn compute_minimal_mode_insertions( ); let start = std::time::Instant::now(); let denormal_f32 = - optimize_mode_insertions::(denormal_f32); + optimize_mode_insertions::(denormal_f32) + .map_err(|_| error_unreachable())?; let denormal_f16f64 = - optimize_mode_insertions::(denormal_f16f64); + optimize_mode_insertions::(denormal_f16f64) + .map_err(|_| error_unreachable())?; let rounding_f32 = - optimize_mode_insertions::(rounding_f32); + optimize_mode_insertions::(rounding_f32) + .map_err(|_| error_unreachable())?; let rounding_f16f64: MandatoryModeInsertions = - optimize_mode_insertions::(rounding_f16f64); + optimize_mode_insertions::(rounding_f16f64) + .map_err(|_| error_unreachable())?; let duration = start.elapsed(); println!( " Subsubpass \"optimize_mode_insertions\" took {:?}", duration ); - (denormal_f32, denormal_f16f64, rounding_f32, rounding_f16f64) + Ok((denormal_f32, denormal_f16f64, rounding_f32, rounding_f16f64)) } // This function creates control flow graph for the whole module. This control @@ -1795,7 +1803,7 @@ fn optimize_mode_insertions< const N: usize, >( partial: PartialModeInsertion, -) -> MandatoryModeInsertions { +) -> Result, HighsStatus> { let mut problem = highs::RowProblem::default(); let mut kernel_modes = FxHashMap::default(); let basic_block_variables = partial @@ -1816,7 +1824,7 @@ fn optimize_mode_insertions< }) .collect::>(); // TODO: add fallback on Error - let mut solver = problem.optimise(highs::Sense::Maximise); + let mut solver = problem.try_optimise(highs::Sense::Maximise)?; solver.make_quiet(); // Takes minutes for a problem that is solved sub-second solver.set_option("presolve", "off"); @@ -1824,7 +1832,7 @@ fn optimize_mode_insertions< solver.set_option("solver", "pdlp"); solver.set_option("parallel", "off"); solver.set_option("threads", 1); - let solved_model = solver.solve(); + let solved_model = solver.try_solve()?; let solution = solved_model.get_solution(); let mut basic_blocks = partial.bb_must_insert_mode; for (basic_block, variable) in basic_block_variables { @@ -1841,10 +1849,10 @@ fn optimize_mode_insertions< } } } - MandatoryModeInsertions { + Ok(MandatoryModeInsertions { basic_blocks, kernels, - } + }) } fn and(problem: &mut highs::RowProblem, variables: &[highs::Col]) -> highs::Col { From a3ea6cd72bfdc47f5c1412f21ba558ffad565c45 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Mon, 20 Oct 2025 19:54:31 +0000 Subject: [PATCH 05/22] More performance improvements --- .../instruction_mode_to_global_mode/mod.rs | 238 +++++++++--------- 1 file changed, 126 insertions(+), 112 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index 92885bca..f16ec9eb 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -15,7 +15,6 @@ use petgraph::Graph; use ptx_parser as ast; use rustc_hash::FxHashMap; use rustc_hash::FxHashSet; -use std::hash::Hash; use std::iter; use std::mem; use std::ops::ControlFlow; @@ -404,12 +403,8 @@ impl ResolvedControlFlowGraph { kernels: &FxHashMap, index: NodeIndex, getter: &mut impl FnMut(&Node) -> Mode, - exit_cache: &mut std::collections::HashMap< - NodeIndex, - Resolved, - rustc_hash::FxBuildHasher, - >, - unknown: &mut std::collections::HashSet, + exit_cache: &mut FxHashMap>, + unknown: &mut FxHashSet, ) -> Option> { unknown.insert(index); let mode = cfg @@ -738,22 +733,9 @@ pub(crate) fn run<'input>( flat_resolver: &mut GlobalStringIdentResolver2<'input>, directives: Vec, SpirvWord>>, ) -> Result, SpirvWord>>, TranslateError> { - let mut start = std::time::Instant::now(); let cfg = create_control_flow_graph(&directives)?; - let duration = start.elapsed(); - println!( - " Subpass \"create_control_flow_graph\" took {:?}", - duration - ); - let mut start = std::time::Instant::now(); let (denormal_f32, denormal_f16f64, rounding_f32, rounding_f16f64) = compute_minimal_mode_insertions(&cfg)?; - let duration = start.elapsed(); - println!( - " Subpass \"compute_minimal_mode_insertions\" took {:?}", - duration - ); - let mut start = std::time::Instant::now(); let temp = compute_full_mode_insertions( flat_resolver, &directives, @@ -763,18 +745,7 @@ pub(crate) fn run<'input>( rounding_f32, rounding_f16f64, )?; - let duration = start.elapsed(); - println!( - " Subpass \"compute_full_mode_insertions\" took {:?}", - duration - ); - let mut start = std::time::Instant::now(); let result = apply_global_mode_controls(directives, temp); - let duration = start.elapsed(); - println!( - " Subpass \"apply_global_mode_controls\" took {:?}", - duration - ); result } @@ -840,10 +811,10 @@ fn compute_minimal_mode_insertions( TranslateError, > { let start = std::time::Instant::now(); - let rounding_f32 = compute_single_mode_insertions(cfg, |node| node.rounding_f32); - let denormal_f32 = compute_single_mode_insertions(cfg, |node| node.denormal_f32); - let denormal_f16f64 = compute_single_mode_insertions(cfg, |node| node.denormal_f16f64); - let rounding_f16f64 = compute_single_mode_insertions(cfg, |node| node.rounding_f16f64); + let rounding_f32 = compute_single_mode_insertions(cfg, |node| node.rounding_f32)?; + let denormal_f32 = compute_single_mode_insertions(cfg, |node| node.denormal_f32)?; + let denormal_f16f64 = compute_single_mode_insertions(cfg, |node| node.denormal_f16f64)?; + let rounding_f16f64 = compute_single_mode_insertions(cfg, |node| node.rounding_f16f64)?; let duration = start.elapsed(); println!( " Subsubpass \"compute_single_mode_insertions\" took {:?}", @@ -853,7 +824,7 @@ fn compute_minimal_mode_insertions( let denormal_f32 = optimize_mode_insertions::(denormal_f32) .map_err(|_| error_unreachable())?; - let denormal_f16f64 = + let denormal_f16f64: MandatoryModeInsertions = optimize_mode_insertions::(denormal_f16f64) .map_err(|_| error_unreachable())?; let rounding_f32 = @@ -1729,15 +1700,56 @@ impl<'a> Drop for BasicBlockState<'a> { } fn compute_single_mode_insertions( - graph: &ControlFlowGraph, + cfg: &ControlFlowGraph, mut getter: impl FnMut(&Node) -> Mode, -) -> PartialModeInsertion { - let mut must_insert_mode = FxHashSet::::default(); +) -> Result, TranslateError> { + fn get_exit_mode_reachability( + cfg: &ControlFlowGraph, + getter: &mut impl FnMut(&Node) -> Mode, + index: NodeIndex, + exit_cache: &mut FxHashMap>, + visited: &mut FxHashSet, + ) -> Option> { + if let Some(mode) = getter(cfg.graph.node_weight(index).unwrap()).exit { + return Some(match mode { + ExtendedMode::BasicBlock(value) => { + ModeReachability::Value(Some(value), FxHashSet::default()) + } + ExtendedMode::Entry(id) => { + ModeReachability::Value(None, FxHashSet::from_iter(iter::once(id))) + } + }); + } + if let Some(cached) = exit_cache.get(&index) { + return Some(cached.clone()); + } + if !visited.insert(index) { + return None; + } + let mode = cfg + .graph + .neighbors_directed(index, Direction::Incoming) + .try_fold(ModeReachability::empty(), |old_mode, predecessor| { + if visited.contains(&predecessor) { + return ControlFlow::Continue(old_mode); + } + let new_mode = + get_exit_mode_reachability(cfg, getter, predecessor, exit_cache, visited); + old_mode.fold(new_mode) + }); + let result = match mode { + ControlFlow::Continue(m) => m, + ControlFlow::Break(m) => m, + }; + exit_cache.insert(index, result.clone()); + Some(result) + } + let mut must_insert_mode: std::collections::HashSet = + FxHashSet::::default(); let mut maybe_insert_mode = FxHashMap::default(); - let mut remaining = graph + let mut remaining = cfg .graph .node_references() - .rev() .filter_map(|(index, node)| { getter(node) .entry @@ -1749,45 +1761,85 @@ fn compute_single_mode_insertions( .flatten() }) .collect::>(); - 'next_basic_block: while let Some((index, node_id, expected_mode)) = remaining.pop() { - let mut to_visit = - UniqueVec::new(graph.graph.neighbors_directed(index, Direction::Incoming)); - let mut visited = FxHashSet::default(); - while let Some(current) = to_visit.pop() { - if !visited.insert(current) { - continue; - } - let exit_mode = getter(graph.graph.node_weight(current).unwrap()).exit; - match exit_mode { - None => { - for predecessor in graph.graph.neighbors_directed(current, Direction::Incoming) - { - if !visited.contains(&predecessor) { - to_visit.push(predecessor); - } - } - } - Some(ExtendedMode::BasicBlock(mode)) => { - if mode != expected_mode { - maybe_insert_mode.remove(&node_id); - must_insert_mode.insert(node_id); - continue 'next_basic_block; - } - } - Some(ExtendedMode::Entry(kernel)) => match maybe_insert_mode.entry(node_id) { - std::collections::hash_map::Entry::Vacant(entry) => { - entry.insert((expected_mode, iter::once(kernel).collect::>())); - } - std::collections::hash_map::Entry::Occupied(mut entry) => { - entry.get_mut().1.insert(kernel); - } + let mut exit_cache = FxHashMap::default(); + let mut visited = FxHashSet::default(); + while let Some((index, node_id, expected_mode)) = remaining.pop() { + visited.clear(); + let folded_mode = cfg + .graph + .neighbors_directed(index, Direction::Incoming) + .try_fold( + ModeReachability::from_value(expected_mode), + |old_mode, predecessor| { + let new_mode = get_exit_mode_reachability( + cfg, + &mut getter, + predecessor, + &mut exit_cache, + &mut visited, + ); + old_mode.fold(new_mode) }, + ); + let result = match folded_mode { + ControlFlow::Continue(m) => m, + ControlFlow::Break(m) => m, + }; + match result { + ModeReachability::Conflict => { + must_insert_mode.insert(node_id); + } + ModeReachability::Value(None, _) => { + return Err(error_unreachable()); + } + ModeReachability::Value(Some(value), reachable_kernels) => { + maybe_insert_mode.insert(node_id, (value, reachable_kernels)); } } } - PartialModeInsertion { + Ok(PartialModeInsertion { bb_must_insert_mode: must_insert_mode, bb_maybe_insert_mode: maybe_insert_mode, + }) +} + +#[derive(Clone)] +enum ModeReachability { + Conflict, + Value(Option, FxHashSet), +} + +impl ModeReachability { + fn fold(self, other: Option) -> ControlFlow { + let other = match other { + Some(x) => x, + None => return ControlFlow::Continue(self), + }; + match (self, other) { + (_, ModeReachability::Conflict) => ControlFlow::Break(ModeReachability::Conflict), + (ModeReachability::Conflict, _) => ControlFlow::Break(ModeReachability::Conflict), + ( + ModeReachability::Value(old_value, mut old_kernels), + ModeReachability::Value(new_value, new_kernels), + ) => match (old_value, new_value) { + (Some(x), Some(y)) if x != y => ControlFlow::Break(ModeReachability::Conflict), + _ => { + old_kernels.extend(new_kernels); + ControlFlow::Continue(ModeReachability::Value( + old_value.or(new_value), + old_kernels, + )) + } + }, + } + } + + fn empty() -> Self { + ModeReachability::Value(None, FxHashSet::default()) + } + + fn from_value(t: T) -> Self { + ModeReachability::Value(Some(t), FxHashSet::default()) } } @@ -1887,44 +1939,6 @@ enum ExtendedMode { Entry(SpirvWord), } -struct UniqueVec { - set: FxHashSet, - vec: Vec, -} - -impl UniqueVec { - fn new(iter: impl Iterator) -> Self { - let mut set = FxHashSet::default(); - let mut vec = Vec::new(); - for item in iter { - if set.contains(&item) { - continue; - } - set.insert(item); - vec.push(item); - } - Self { set, vec } - } - - fn pop(&mut self) -> Option { - if let Some(t) = self.vec.pop() { - assert!(self.set.remove(&t)); - Some(t) - } else { - None - } - } - - fn push(&mut self, t: T) -> bool { - if self.set.insert(t) { - self.vec.push(t); - true - } else { - false - } - } -} - fn get_modes(inst: &ast::Instruction) -> InstructionModes { match inst { // TODO: review it when implementing virtual calls From 7b207fa49b63fb309b0b17a4363455541fdcf0ab Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Mon, 20 Oct 2025 19:56:52 +0000 Subject: [PATCH 06/22] Fix some unused perf measurements --- ptx/src/pass/instruction_mode_to_global_mode/mod.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index f16ec9eb..69cc1fb6 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -810,17 +810,10 @@ fn compute_minimal_mode_insertions( ), TranslateError, > { - let start = std::time::Instant::now(); let rounding_f32 = compute_single_mode_insertions(cfg, |node| node.rounding_f32)?; let denormal_f32 = compute_single_mode_insertions(cfg, |node| node.denormal_f32)?; let denormal_f16f64 = compute_single_mode_insertions(cfg, |node| node.denormal_f16f64)?; let rounding_f16f64 = compute_single_mode_insertions(cfg, |node| node.rounding_f16f64)?; - let duration = start.elapsed(); - println!( - " Subsubpass \"compute_single_mode_insertions\" took {:?}", - duration - ); - let start = std::time::Instant::now(); let denormal_f32 = optimize_mode_insertions::(denormal_f32) .map_err(|_| error_unreachable())?; @@ -833,11 +826,6 @@ fn compute_minimal_mode_insertions( let rounding_f16f64: MandatoryModeInsertions = optimize_mode_insertions::(rounding_f16f64) .map_err(|_| error_unreachable())?; - let duration = start.elapsed(); - println!( - " Subsubpass \"optimize_mode_insertions\" took {:?}", - duration - ); Ok((denormal_f32, denormal_f16f64, rounding_f32, rounding_f16f64)) } From 1ee16c8200ab5efd0af1e4e37f4d01e122ae13a9 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Mon, 20 Oct 2025 21:39:34 +0000 Subject: [PATCH 07/22] Vendor in HiGHS --- .gitmodules | 4 + Cargo.lock | 3 +- Cargo.toml | 4 + ext/HiGHS | 1 + ext/highs-sys/Cargo.toml | 25 + ext/highs-sys/README.md | 204 ++ ext/highs-sys/build.rs | 126 ++ ext/highs-sys/install-dependencies.sh | 11 + ext/highs-sys/src/c_bindings.rs | 2062 +++++++++++++++++++ ext/highs-sys/src/lib.rs | 35 + ext/highs-sys/tests/test_highs_call.rs | 115 ++ ext/highs-sys/tests/test_highs_functions.rs | 134 ++ ext/highs-sys/wrapper.h | 1 + 13 files changed, 2723 insertions(+), 2 deletions(-) create mode 160000 ext/HiGHS create mode 100644 ext/highs-sys/Cargo.toml create mode 100644 ext/highs-sys/README.md create mode 100644 ext/highs-sys/build.rs create mode 100755 ext/highs-sys/install-dependencies.sh create mode 100644 ext/highs-sys/src/c_bindings.rs create mode 100644 ext/highs-sys/src/lib.rs create mode 100644 ext/highs-sys/tests/test_highs_call.rs create mode 100644 ext/highs-sys/tests/test_highs_functions.rs create mode 100644 ext/highs-sys/wrapper.h diff --git a/.gitmodules b/.gitmodules index a4e5dc5e..5a1877cd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,3 +3,7 @@ url = https://github.com/llvm/llvm-project branch = release/17.x shallow = true +[submodule "ext/HiGHS"] + path = ext/HiGHS + url = https://github.com/ERGO-Code/HiGHS.git + shallow = true diff --git a/Cargo.lock b/Cargo.lock index 664038ef..a6eec346 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1804,11 +1804,10 @@ dependencies = [ [[package]] name = "highs-sys" version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "908a51a57402b0616cf0026c0e7bf908cbee7bac13bd45778ba4f2e2ac6bb6db" dependencies = [ "bindgen 0.71.1", "cmake", + "pkg-config", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 543024d1..692edcee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ "dark_api", "detours-sys", "ext/amd_comgr-sys", + "ext/highs-sys", "ext/hip_runtime-sys", "ext/hipblaslt-sys", "ext/miopen-sys", @@ -55,3 +56,6 @@ lto = true [profile.dev.package.xtask] opt-level = 2 + +[patch.crates-io] +highs-sys = { path = "ext/highs-sys" } diff --git a/ext/HiGHS b/ext/HiGHS new file mode 160000 index 00000000..2584a4d6 --- /dev/null +++ b/ext/HiGHS @@ -0,0 +1 @@ +Subproject commit 2584a4d6b8d9ac08c70757f4f21afa77c74b10cd diff --git a/ext/highs-sys/Cargo.toml b/ext/highs-sys/Cargo.toml new file mode 100644 index 00000000..69e95ad1 --- /dev/null +++ b/ext/highs-sys/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "highs-sys" +version = "1.11.0" +authors = ["Ophir LOJKINE"] +edition = "2018" +description = "Rust binding for the HiGHS linear programming solver. See http://highs.dev." +license = "MIT" +repository = "https://github.com/rust-or/highs-sys" +keywords = ["linear-programming", "optimization", "math", "solver"] +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[build-dependencies] +bindgen = "0.71.1" +cmake = { version = "0.1.49", optional = true } +pkg-config = { version = "0.3.27", optional = true } + +[features] +default = ["build", "highs_release"] +discover = ["dep:pkg-config"] +build = ["dep:cmake"] +highs_release = [] +ninja = [] +libz = [] diff --git a/ext/highs-sys/README.md b/ext/highs-sys/README.md new file mode 100644 index 00000000..6eb1c837 --- /dev/null +++ b/ext/highs-sys/README.md @@ -0,0 +1,204 @@ +# highs-sys + +[![docs.rs badge](https://docs.rs/highs-sys/badge.svg)](https://docs.rs/highs-sys) +[![rust crate](https://img.shields.io/crates/v/highs-sys.svg)](https://lib.rs/highs-sys) + +Rust binding for the HiGHS linear programming solver. +See http://highs.dev. + +This repository contains the source for HiGHS itself as a submodule. +You should clone it with + +``` +git clone --recursive git@github.com:rust-or/highs-sys.git +``` + +This crate can either use and link a version of HiGHS that is already installed and available on your system or build and statically link HiGHS itself. + +## Usage + +At runtime, HiGHS depends at the minimum on the C++ standard library. +It needs to be installed both on your system and any system you want to deploy your application to. + +How you install these depends on your operating system. + +#### Debian + +``` +sudo apt-get install libstdc++6 +``` + +(but it is probably already installed on your system) + +#### macOS + +libc++ comes by default when installing XCode. + +### Building HiGHS + +This crate can either build HiGHS itself and link it statically or [link against an already installed version](#using-a-pre-installed-version-of-highs). +To build HiGHS, you need at least a C++ compiler and cmake. +Enabling additional features may incur additional runtime dependencies. + +#### Linux + +These can be easily installed using your distribution's package manager. +For example, on Debian: `sudo apt install g++ cmake`. + +#### macOS + +To install a C++ compiler, run `xcode-select --install`. +The easiest way to obtain cmake is via brew: `brew install cmake`. + +If you enable the `libz` or `ninja` features, you should also install these via brew. + +#### Windows + +You need to install [CMake](https://cmake.org/download/) and [Clang (available in LLVM)](https://releases.llvm.org/download.html). + +They are available in [winget](https://winget.run/). + +```powershell +winget install -e --id Kitware.CMake +winget install -e --id LLVM.LLVM +``` + +If you enable the Ninja feature, you can also obtain Ninja from winget: + +```powershell +winget install -e --id Ninja-build.Ninja +``` + +If desired, libz needs to be installed and made discoverable by adding the `libz-sys` crate as a dependency in your project or manually setting up libz and setting the `ZLIB_ROOT` environment variable. + +### Using a pre-installed version of HiGHS + +Rather than building HiGHS, you can link against a version you have already installed on your system. +To do that, install pkg-config on your system and enable the `discover` feature on this crate. + +This will generally cause HiGHS to be linked dynamically, which means it also needs to be installed on the system you deploy to. + +Note that at the time of writing, HiGHS is packaged in few package managers, so you may need to build and install HiGHS from source. + +#### Feature Flags + +`build` (enabled by default): build HiGHS and link it statically +`highs_release`: set CMake profile to "Release" regardless of build profile; only takes effect when `build` is enabled. +`libz`: enable HiGHS libz linking to enable support for reading 'mps.gz'; only takes effect when `build` is enabled. +`ninja`: set CMake generator to Ninja; only takes effect when `build` is enabled. +`discover`: use pkg-config to discover and link against an already installed version of HiGHS; takes precedence over `build` if both are enabled + +## Example + +```rust + // This illustrates the use of Highs_call, the simple C interface to +// HiGHS. It's designed to solve the general LP problem +// +// Min c^Tx subject to L <= Ax <= U; l <= x <= u +// +// where A is a matrix with m rows and n columns +// +// The scalar n is numcol +// The scalar m is numrow +// +// The vector c is colcost +// The vector l is collower +// The vector u is colupper +// The vector L is rowlower +// The vector U is rowupper +// +// The matrix A is represented in packed column-wise form: only its +// nonzeros are stored +// +// * The number of nonzeros in A is nnz +// +// * The row indices of the nonnzeros in A are stored column-by-column +// in aindex +// +// * The values of the nonnzeros in A are stored column-by-column in +// avalue +// +// * The position in aindex/avalue of the index/value of the first +// nonzero in each column is stored in astart +// +// Note that astart[0] must be zero +// +// After a successful call to Highs_call, the primal and dual +// solution, and the simplex basis are returned as follows +// +// The vector x is colvalue +// The vector Ax is rowvalue +// The vector of dual values for the variables x is coldual +// The vector of dual values for the variables Ax is rowdual +// The basic/nonbasic status of the variables x is colbasisstatus +// The basic/nonbasic status of the variables Ax is rowbasisstatus +// +// The status of the solution obtained is modelstatus +// +// To solve maximization problems, the values in c must be negated +// +// The use of Highs_call is illustrated for the LP +// +// Min f = 2x_0 + 3x_1 +// s.t. x_1 <= 6 +// 10 <= x_0 + 2x_1 <= 14 +// 8 <= 2x_0 + x_1 +// 0 <= x_0 <= 3; 1 <= x_1 + +fn main() { + let numcol: usize = 2; + let numrow: usize = 3; + let nnz: usize = 5; + + // Define the column costs, lower bounds and upper bounds + let colcost: &mut [f64] = &mut [2.0, 3.0]; + let collower: &mut [f64] = &mut [0.0, 1.0]; + let colupper: &mut [f64] = &mut [3.0, 1.0e30]; + // Define the row lower bounds and upper bounds + let rowlower: &mut [f64] = &mut [-1.0e30, 10.0, 8.0]; + let rowupper: &mut [f64] = &mut [6.0, 14.0, 1.0e30]; + // Define the constraint matrix column-wise + let astart: &mut [c_int] = &mut [0, 2]; + let aindex: &mut [c_int] = &mut [1, 2, 0, 1, 2]; + let avalue: &mut [f64] = &mut [1.0, 2.0, 1.0, 2.0, 1.0]; + + let colvalue: &mut [f64] = &mut vec![0.; numcol]; + let coldual: &mut [f64] = &mut vec![0.; numcol]; + let rowvalue: &mut [f64] = &mut vec![0.; numrow]; + let rowdual: &mut [f64] = &mut vec![0.; numrow]; + + let colbasisstatus: &mut [c_int] = &mut vec![0; numcol]; + let rowbasisstatus: &mut [c_int] = &mut vec![0; numrow]; + + let modelstatus: &mut c_int = &mut 0; + + let status: c_int = unsafe { + Highs_call( + numcol.try_into().unwrap(), + numrow.try_into().unwrap(), + nnz.try_into().unwrap(), + colcost.as_mut_ptr(), + collower.as_mut_ptr(), + colupper.as_mut_ptr(), + rowlower.as_mut_ptr(), + rowupper.as_mut_ptr(), + astart.as_mut_ptr(), + aindex.as_mut_ptr(), + avalue.as_mut_ptr(), + colvalue.as_mut_ptr(), + coldual.as_mut_ptr(), + rowvalue.as_mut_ptr(), + rowdual.as_mut_ptr(), + colbasisstatus.as_mut_ptr(), + rowbasisstatus.as_mut_ptr(), + modelstatus + ) + }; + + assert_eq!(status, 0); + // The solution is x_0 = 2 and x_1 = 4 + assert_eq!(colvalue, &[2., 4.]); +} +``` + +For more examples, have a look at [`tests`](https://github.com/lovasoa/highs-sys/blob/master/tests). diff --git a/ext/highs-sys/build.rs b/ext/highs-sys/build.rs new file mode 100644 index 00000000..3d4c55d1 --- /dev/null +++ b/ext/highs-sys/build.rs @@ -0,0 +1,126 @@ +use std::env; +// use std::path::Path; + +/* +fn generate_bindings<'a>(include_paths: impl Iterator) { + // The bindgen::Builder is the main entry point + // to bindgen, and lets you build up options for + // the resulting bindings. + let builder = include_paths.fold(bindgen::Builder::default(), |builder, path| { + builder.clang_arg(format!("-I{}", path.to_string_lossy())) + }); + let c_bindings = builder + // The input header we would like to generate bindings for. + // This is a trivial wrapper header so that the HiGHS headers + // can be discovered from the include path. + .header("wrapper.h") + // Tell cargo to invalidate the built crate whenever any of the + // included header files changed. + .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) + // Finish the builder and generate the bindings. + .generate() + // Unwrap the Result and panic on failure. + .expect("Unable to generate bindings"); + + // Write the bindings to the $OUT_DIR/bindings.rs file. + c_bindings + .write_to_file("src/c_bindings.rs") + .expect("Couldn't write bindings!"); +} +*/ + +#[cfg(feature = "build")] +fn build() -> bool { + use cmake::Config; + let mut dst = Config::new("../HiGHS"); + try_use_ninja(&mut dst); + + // Avoid using downstream project's profile setting for HiGHS build. + if cfg!(feature = "highs_release") { + dst.profile("Release"); + } + + let dst = dst + .define("FAST_BUILD", "ON") + .define("BUILD_SHARED_LIBS", "OFF") + .define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreaded") + .define("ZLIB", if cfg!(feature = "libz") { "ON" } else { "OFF" }) + .build(); + + // let include_path = dst.join("include").join("highs"); + // generate_bindings(Some(include_path.as_path()).into_iter()); + + println!("cargo:rustc-link-search=native={}/lib", dst.display()); + println!("cargo:rustc-link-search=native={}/lib64", dst.display()); + println!("cargo:rustc-link-lib=static=highs"); + + if cfg!(feature = "libz") { + println!("cargo:rustc-link-lib=z"); + } + + let target = env::var("TARGET").unwrap(); + let apple = target.contains("apple"); + let linux = target.contains("linux"); + let mingw = target.contains("pc-windows-gnu"); + if apple { + println!("cargo:rustc-link-lib=dylib=c++"); + } else if linux || mingw { + println!("cargo:rustc-link-lib=dylib=stdc++"); + } + println!("cargo:rerun-if-changed=../HiGHS/src/interfaces/highs_c_api.h"); + + true +} + +fn try_use_ninja(cmake: &mut cmake::Config) { + let mut cmd = std::process::Command::new("ninja"); + cmd.arg("--version"); + if let Ok(status) = cmd.status() { + if status.success() { + cmake.generator("Ninja"); + } + } +} + +#[cfg(not(feature = "build"))] +fn build() -> bool { + false +} + +#[cfg(feature = "discover")] +fn discover() -> bool { + let lib = match pkg_config::Config::new() + .atleast_version("1.5.0") + .probe("highs") + { + Ok(lib) => lib, + Err(_e) => return false, + }; + + generate_bindings(lib.include_paths.iter().map(|p| p.as_path())); + + true +} + +#[cfg(not(feature = "discover"))] +fn discover() -> bool { + false +} + +fn main() { + if cfg!(all( + any( + feature = "highs_release", + feature = "libz", + feature = "ninja" + ), + not(feature = "build") + )) { + panic!("You have enabled features that control how HiGHS is built, but have not enabled the 'build' feature.\n\ + Thus, your features will never have any effect. Please enable the 'build' feature on highs-sys if you want to build HiGHS or disable the 'libz', 'ninja' and 'highs_release' features."); + } + + if !discover() && !build() { + panic!("Could neither discover nor build HiGHS"); + } +} diff --git a/ext/highs-sys/install-dependencies.sh b/ext/highs-sys/install-dependencies.sh new file mode 100755 index 00000000..737c9af3 --- /dev/null +++ b/ext/highs-sys/install-dependencies.sh @@ -0,0 +1,11 @@ +set -x +if test -x "$(which apt-get)"; then + sudo apt-get install libstdc++6 cmake +elif test -x "$(which dnf)"; then + sudo dnf install libstdc++ cmake +elif test -x "$(which brew)"; then + brew install cmake +else + echo "system not supported" + exit 1 +fi diff --git a/ext/highs-sys/src/c_bindings.rs b/ext/highs-sys/src/c_bindings.rs new file mode 100644 index 00000000..a2c514a0 --- /dev/null +++ b/ext/highs-sys/src/c_bindings.rs @@ -0,0 +1,2062 @@ +/* automatically generated by rust-bindgen 0.71.1 */ + +pub const _STDINT_H: u32 = 1; +pub const _FEATURES_H: u32 = 1; +pub const _DEFAULT_SOURCE: u32 = 1; +pub const __GLIBC_USE_ISOC2X: u32 = 0; +pub const __USE_ISOC11: u32 = 1; +pub const __USE_ISOC99: u32 = 1; +pub const __USE_ISOC95: u32 = 1; +pub const __USE_POSIX_IMPLICITLY: u32 = 1; +pub const _POSIX_SOURCE: u32 = 1; +pub const _POSIX_C_SOURCE: u32 = 200809; +pub const __USE_POSIX: u32 = 1; +pub const __USE_POSIX2: u32 = 1; +pub const __USE_POSIX199309: u32 = 1; +pub const __USE_POSIX199506: u32 = 1; +pub const __USE_XOPEN2K: u32 = 1; +pub const __USE_XOPEN2K8: u32 = 1; +pub const _ATFILE_SOURCE: u32 = 1; +pub const __WORDSIZE: u32 = 64; +pub const __WORDSIZE_TIME64_COMPAT32: u32 = 1; +pub const __SYSCALL_WORDSIZE: u32 = 64; +pub const __TIMESIZE: u32 = 64; +pub const __USE_MISC: u32 = 1; +pub const __USE_ATFILE: u32 = 1; +pub const __USE_FORTIFY_LEVEL: u32 = 0; +pub const __GLIBC_USE_DEPRECATED_GETS: u32 = 0; +pub const __GLIBC_USE_DEPRECATED_SCANF: u32 = 0; +pub const _STDC_PREDEF_H: u32 = 1; +pub const __STDC_IEC_559__: u32 = 1; +pub const __STDC_IEC_60559_BFP__: u32 = 201404; +pub const __STDC_IEC_559_COMPLEX__: u32 = 1; +pub const __STDC_IEC_60559_COMPLEX__: u32 = 201404; +pub const __STDC_ISO_10646__: u32 = 201706; +pub const __GNU_LIBRARY__: u32 = 6; +pub const __GLIBC__: u32 = 2; +pub const __GLIBC_MINOR__: u32 = 35; +pub const _SYS_CDEFS_H: u32 = 1; +pub const __glibc_c99_flexarr_available: u32 = 1; +pub const __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI: u32 = 0; +pub const __HAVE_GENERIC_SELECTION: u32 = 1; +pub const __GLIBC_USE_LIB_EXT2: u32 = 0; +pub const __GLIBC_USE_IEC_60559_BFP_EXT: u32 = 0; +pub const __GLIBC_USE_IEC_60559_BFP_EXT_C2X: u32 = 0; +pub const __GLIBC_USE_IEC_60559_EXT: u32 = 0; +pub const __GLIBC_USE_IEC_60559_FUNCS_EXT: u32 = 0; +pub const __GLIBC_USE_IEC_60559_FUNCS_EXT_C2X: u32 = 0; +pub const __GLIBC_USE_IEC_60559_TYPES_EXT: u32 = 0; +pub const _BITS_TYPES_H: u32 = 1; +pub const _BITS_TYPESIZES_H: u32 = 1; +pub const __OFF_T_MATCHES_OFF64_T: u32 = 1; +pub const __INO_T_MATCHES_INO64_T: u32 = 1; +pub const __RLIM_T_MATCHES_RLIM64_T: u32 = 1; +pub const __STATFS_MATCHES_STATFS64: u32 = 1; +pub const __KERNEL_OLD_TIMEVAL_MATCHES_TIMEVAL64: u32 = 1; +pub const __FD_SETSIZE: u32 = 1024; +pub const _BITS_TIME64_H: u32 = 1; +pub const _BITS_WCHAR_H: u32 = 1; +pub const _BITS_STDINT_INTN_H: u32 = 1; +pub const _BITS_STDINT_UINTN_H: u32 = 1; +pub const INT8_MIN: i32 = -128; +pub const INT16_MIN: i32 = -32768; +pub const INT32_MIN: i32 = -2147483648; +pub const INT8_MAX: u32 = 127; +pub const INT16_MAX: u32 = 32767; +pub const INT32_MAX: u32 = 2147483647; +pub const UINT8_MAX: u32 = 255; +pub const UINT16_MAX: u32 = 65535; +pub const UINT32_MAX: u32 = 4294967295; +pub const INT_LEAST8_MIN: i32 = -128; +pub const INT_LEAST16_MIN: i32 = -32768; +pub const INT_LEAST32_MIN: i32 = -2147483648; +pub const INT_LEAST8_MAX: u32 = 127; +pub const INT_LEAST16_MAX: u32 = 32767; +pub const INT_LEAST32_MAX: u32 = 2147483647; +pub const UINT_LEAST8_MAX: u32 = 255; +pub const UINT_LEAST16_MAX: u32 = 65535; +pub const UINT_LEAST32_MAX: u32 = 4294967295; +pub const INT_FAST8_MIN: i32 = -128; +pub const INT_FAST16_MIN: i64 = -9223372036854775808; +pub const INT_FAST32_MIN: i64 = -9223372036854775808; +pub const INT_FAST8_MAX: u32 = 127; +pub const INT_FAST16_MAX: u64 = 9223372036854775807; +pub const INT_FAST32_MAX: u64 = 9223372036854775807; +pub const UINT_FAST8_MAX: u32 = 255; +pub const UINT_FAST16_MAX: i32 = -1; +pub const UINT_FAST32_MAX: i32 = -1; +pub const INTPTR_MIN: i64 = -9223372036854775808; +pub const INTPTR_MAX: u64 = 9223372036854775807; +pub const UINTPTR_MAX: i32 = -1; +pub const PTRDIFF_MIN: i64 = -9223372036854775808; +pub const PTRDIFF_MAX: u64 = 9223372036854775807; +pub const SIG_ATOMIC_MIN: i32 = -2147483648; +pub const SIG_ATOMIC_MAX: u32 = 2147483647; +pub const SIZE_MAX: i32 = -1; +pub const WINT_MIN: u32 = 0; +pub const WINT_MAX: u32 = 4294967295; +pub const _INTTYPES_H: u32 = 1; +pub const ____gwchar_t_defined: u32 = 1; +pub const __PRI64_PREFIX: &[u8; 2] = b"l\0"; +pub const __PRIPTR_PREFIX: &[u8; 2] = b"l\0"; +pub const PRId8: &[u8; 2] = b"d\0"; +pub const PRId16: &[u8; 2] = b"d\0"; +pub const PRId32: &[u8; 2] = b"d\0"; +pub const PRId64: &[u8; 3] = b"ld\0"; +pub const PRIdLEAST8: &[u8; 2] = b"d\0"; +pub const PRIdLEAST16: &[u8; 2] = b"d\0"; +pub const PRIdLEAST32: &[u8; 2] = b"d\0"; +pub const PRIdLEAST64: &[u8; 3] = b"ld\0"; +pub const PRIdFAST8: &[u8; 2] = b"d\0"; +pub const PRIdFAST16: &[u8; 3] = b"ld\0"; +pub const PRIdFAST32: &[u8; 3] = b"ld\0"; +pub const PRIdFAST64: &[u8; 3] = b"ld\0"; +pub const PRIi8: &[u8; 2] = b"i\0"; +pub const PRIi16: &[u8; 2] = b"i\0"; +pub const PRIi32: &[u8; 2] = b"i\0"; +pub const PRIi64: &[u8; 3] = b"li\0"; +pub const PRIiLEAST8: &[u8; 2] = b"i\0"; +pub const PRIiLEAST16: &[u8; 2] = b"i\0"; +pub const PRIiLEAST32: &[u8; 2] = b"i\0"; +pub const PRIiLEAST64: &[u8; 3] = b"li\0"; +pub const PRIiFAST8: &[u8; 2] = b"i\0"; +pub const PRIiFAST16: &[u8; 3] = b"li\0"; +pub const PRIiFAST32: &[u8; 3] = b"li\0"; +pub const PRIiFAST64: &[u8; 3] = b"li\0"; +pub const PRIo8: &[u8; 2] = b"o\0"; +pub const PRIo16: &[u8; 2] = b"o\0"; +pub const PRIo32: &[u8; 2] = b"o\0"; +pub const PRIo64: &[u8; 3] = b"lo\0"; +pub const PRIoLEAST8: &[u8; 2] = b"o\0"; +pub const PRIoLEAST16: &[u8; 2] = b"o\0"; +pub const PRIoLEAST32: &[u8; 2] = b"o\0"; +pub const PRIoLEAST64: &[u8; 3] = b"lo\0"; +pub const PRIoFAST8: &[u8; 2] = b"o\0"; +pub const PRIoFAST16: &[u8; 3] = b"lo\0"; +pub const PRIoFAST32: &[u8; 3] = b"lo\0"; +pub const PRIoFAST64: &[u8; 3] = b"lo\0"; +pub const PRIu8: &[u8; 2] = b"u\0"; +pub const PRIu16: &[u8; 2] = b"u\0"; +pub const PRIu32: &[u8; 2] = b"u\0"; +pub const PRIu64: &[u8; 3] = b"lu\0"; +pub const PRIuLEAST8: &[u8; 2] = b"u\0"; +pub const PRIuLEAST16: &[u8; 2] = b"u\0"; +pub const PRIuLEAST32: &[u8; 2] = b"u\0"; +pub const PRIuLEAST64: &[u8; 3] = b"lu\0"; +pub const PRIuFAST8: &[u8; 2] = b"u\0"; +pub const PRIuFAST16: &[u8; 3] = b"lu\0"; +pub const PRIuFAST32: &[u8; 3] = b"lu\0"; +pub const PRIuFAST64: &[u8; 3] = b"lu\0"; +pub const PRIx8: &[u8; 2] = b"x\0"; +pub const PRIx16: &[u8; 2] = b"x\0"; +pub const PRIx32: &[u8; 2] = b"x\0"; +pub const PRIx64: &[u8; 3] = b"lx\0"; +pub const PRIxLEAST8: &[u8; 2] = b"x\0"; +pub const PRIxLEAST16: &[u8; 2] = b"x\0"; +pub const PRIxLEAST32: &[u8; 2] = b"x\0"; +pub const PRIxLEAST64: &[u8; 3] = b"lx\0"; +pub const PRIxFAST8: &[u8; 2] = b"x\0"; +pub const PRIxFAST16: &[u8; 3] = b"lx\0"; +pub const PRIxFAST32: &[u8; 3] = b"lx\0"; +pub const PRIxFAST64: &[u8; 3] = b"lx\0"; +pub const PRIX8: &[u8; 2] = b"X\0"; +pub const PRIX16: &[u8; 2] = b"X\0"; +pub const PRIX32: &[u8; 2] = b"X\0"; +pub const PRIX64: &[u8; 3] = b"lX\0"; +pub const PRIXLEAST8: &[u8; 2] = b"X\0"; +pub const PRIXLEAST16: &[u8; 2] = b"X\0"; +pub const PRIXLEAST32: &[u8; 2] = b"X\0"; +pub const PRIXLEAST64: &[u8; 3] = b"lX\0"; +pub const PRIXFAST8: &[u8; 2] = b"X\0"; +pub const PRIXFAST16: &[u8; 3] = b"lX\0"; +pub const PRIXFAST32: &[u8; 3] = b"lX\0"; +pub const PRIXFAST64: &[u8; 3] = b"lX\0"; +pub const PRIdMAX: &[u8; 3] = b"ld\0"; +pub const PRIiMAX: &[u8; 3] = b"li\0"; +pub const PRIoMAX: &[u8; 3] = b"lo\0"; +pub const PRIuMAX: &[u8; 3] = b"lu\0"; +pub const PRIxMAX: &[u8; 3] = b"lx\0"; +pub const PRIXMAX: &[u8; 3] = b"lX\0"; +pub const PRIdPTR: &[u8; 3] = b"ld\0"; +pub const PRIiPTR: &[u8; 3] = b"li\0"; +pub const PRIoPTR: &[u8; 3] = b"lo\0"; +pub const PRIuPTR: &[u8; 3] = b"lu\0"; +pub const PRIxPTR: &[u8; 3] = b"lx\0"; +pub const PRIXPTR: &[u8; 3] = b"lX\0"; +pub const SCNd8: &[u8; 4] = b"hhd\0"; +pub const SCNd16: &[u8; 3] = b"hd\0"; +pub const SCNd32: &[u8; 2] = b"d\0"; +pub const SCNd64: &[u8; 3] = b"ld\0"; +pub const SCNdLEAST8: &[u8; 4] = b"hhd\0"; +pub const SCNdLEAST16: &[u8; 3] = b"hd\0"; +pub const SCNdLEAST32: &[u8; 2] = b"d\0"; +pub const SCNdLEAST64: &[u8; 3] = b"ld\0"; +pub const SCNdFAST8: &[u8; 4] = b"hhd\0"; +pub const SCNdFAST16: &[u8; 3] = b"ld\0"; +pub const SCNdFAST32: &[u8; 3] = b"ld\0"; +pub const SCNdFAST64: &[u8; 3] = b"ld\0"; +pub const SCNi8: &[u8; 4] = b"hhi\0"; +pub const SCNi16: &[u8; 3] = b"hi\0"; +pub const SCNi32: &[u8; 2] = b"i\0"; +pub const SCNi64: &[u8; 3] = b"li\0"; +pub const SCNiLEAST8: &[u8; 4] = b"hhi\0"; +pub const SCNiLEAST16: &[u8; 3] = b"hi\0"; +pub const SCNiLEAST32: &[u8; 2] = b"i\0"; +pub const SCNiLEAST64: &[u8; 3] = b"li\0"; +pub const SCNiFAST8: &[u8; 4] = b"hhi\0"; +pub const SCNiFAST16: &[u8; 3] = b"li\0"; +pub const SCNiFAST32: &[u8; 3] = b"li\0"; +pub const SCNiFAST64: &[u8; 3] = b"li\0"; +pub const SCNu8: &[u8; 4] = b"hhu\0"; +pub const SCNu16: &[u8; 3] = b"hu\0"; +pub const SCNu32: &[u8; 2] = b"u\0"; +pub const SCNu64: &[u8; 3] = b"lu\0"; +pub const SCNuLEAST8: &[u8; 4] = b"hhu\0"; +pub const SCNuLEAST16: &[u8; 3] = b"hu\0"; +pub const SCNuLEAST32: &[u8; 2] = b"u\0"; +pub const SCNuLEAST64: &[u8; 3] = b"lu\0"; +pub const SCNuFAST8: &[u8; 4] = b"hhu\0"; +pub const SCNuFAST16: &[u8; 3] = b"lu\0"; +pub const SCNuFAST32: &[u8; 3] = b"lu\0"; +pub const SCNuFAST64: &[u8; 3] = b"lu\0"; +pub const SCNo8: &[u8; 4] = b"hho\0"; +pub const SCNo16: &[u8; 3] = b"ho\0"; +pub const SCNo32: &[u8; 2] = b"o\0"; +pub const SCNo64: &[u8; 3] = b"lo\0"; +pub const SCNoLEAST8: &[u8; 4] = b"hho\0"; +pub const SCNoLEAST16: &[u8; 3] = b"ho\0"; +pub const SCNoLEAST32: &[u8; 2] = b"o\0"; +pub const SCNoLEAST64: &[u8; 3] = b"lo\0"; +pub const SCNoFAST8: &[u8; 4] = b"hho\0"; +pub const SCNoFAST16: &[u8; 3] = b"lo\0"; +pub const SCNoFAST32: &[u8; 3] = b"lo\0"; +pub const SCNoFAST64: &[u8; 3] = b"lo\0"; +pub const SCNx8: &[u8; 4] = b"hhx\0"; +pub const SCNx16: &[u8; 3] = b"hx\0"; +pub const SCNx32: &[u8; 2] = b"x\0"; +pub const SCNx64: &[u8; 3] = b"lx\0"; +pub const SCNxLEAST8: &[u8; 4] = b"hhx\0"; +pub const SCNxLEAST16: &[u8; 3] = b"hx\0"; +pub const SCNxLEAST32: &[u8; 2] = b"x\0"; +pub const SCNxLEAST64: &[u8; 3] = b"lx\0"; +pub const SCNxFAST8: &[u8; 4] = b"hhx\0"; +pub const SCNxFAST16: &[u8; 3] = b"lx\0"; +pub const SCNxFAST32: &[u8; 3] = b"lx\0"; +pub const SCNxFAST64: &[u8; 3] = b"lx\0"; +pub const SCNdMAX: &[u8; 3] = b"ld\0"; +pub const SCNiMAX: &[u8; 3] = b"li\0"; +pub const SCNoMAX: &[u8; 3] = b"lo\0"; +pub const SCNuMAX: &[u8; 3] = b"lu\0"; +pub const SCNxMAX: &[u8; 3] = b"lx\0"; +pub const SCNdPTR: &[u8; 3] = b"ld\0"; +pub const SCNiPTR: &[u8; 3] = b"li\0"; +pub const SCNoPTR: &[u8; 3] = b"lo\0"; +pub const SCNuPTR: &[u8; 3] = b"lu\0"; +pub const SCNxPTR: &[u8; 3] = b"lx\0"; +pub const CMAKE_BUILD_TYPE: &[u8; 8] = b"Release\0"; +pub const HIGHS_GITHASH: &[u8; 10] = b"2584a4d6b\0"; +pub const HIGHS_VERSION_MAJOR: u32 = 1; +pub const HIGHS_VERSION_MINOR: u32 = 11; +pub const HIGHS_VERSION_PATCH: u32 = 0; +pub const HIGHSINT_FORMAT: &[u8; 2] = b"d\0"; +pub type __u_char = ::std::os::raw::c_uchar; +pub type __u_short = ::std::os::raw::c_ushort; +pub type __u_int = ::std::os::raw::c_uint; +pub type __u_long = ::std::os::raw::c_ulong; +pub type __int8_t = ::std::os::raw::c_schar; +pub type __uint8_t = ::std::os::raw::c_uchar; +pub type __int16_t = ::std::os::raw::c_short; +pub type __uint16_t = ::std::os::raw::c_ushort; +pub type __int32_t = ::std::os::raw::c_int; +pub type __uint32_t = ::std::os::raw::c_uint; +pub type __int64_t = ::std::os::raw::c_long; +pub type __uint64_t = ::std::os::raw::c_ulong; +pub type __int_least8_t = __int8_t; +pub type __uint_least8_t = __uint8_t; +pub type __int_least16_t = __int16_t; +pub type __uint_least16_t = __uint16_t; +pub type __int_least32_t = __int32_t; +pub type __uint_least32_t = __uint32_t; +pub type __int_least64_t = __int64_t; +pub type __uint_least64_t = __uint64_t; +pub type __quad_t = ::std::os::raw::c_long; +pub type __u_quad_t = ::std::os::raw::c_ulong; +pub type __intmax_t = ::std::os::raw::c_long; +pub type __uintmax_t = ::std::os::raw::c_ulong; +pub type __dev_t = ::std::os::raw::c_ulong; +pub type __uid_t = ::std::os::raw::c_uint; +pub type __gid_t = ::std::os::raw::c_uint; +pub type __ino_t = ::std::os::raw::c_ulong; +pub type __ino64_t = ::std::os::raw::c_ulong; +pub type __mode_t = ::std::os::raw::c_uint; +pub type __nlink_t = ::std::os::raw::c_ulong; +pub type __off_t = ::std::os::raw::c_long; +pub type __off64_t = ::std::os::raw::c_long; +pub type __pid_t = ::std::os::raw::c_int; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __fsid_t { + pub __val: [::std::os::raw::c_int; 2usize], +} +#[allow(clippy::unnecessary_operation, clippy::identity_op)] +const _: () = { + ["Size of __fsid_t"][::std::mem::size_of::<__fsid_t>() - 8usize]; + ["Alignment of __fsid_t"][::std::mem::align_of::<__fsid_t>() - 4usize]; + ["Offset of field: __fsid_t::__val"][::std::mem::offset_of!(__fsid_t, __val) - 0usize]; +}; +pub type __clock_t = ::std::os::raw::c_long; +pub type __rlim_t = ::std::os::raw::c_ulong; +pub type __rlim64_t = ::std::os::raw::c_ulong; +pub type __id_t = ::std::os::raw::c_uint; +pub type __time_t = ::std::os::raw::c_long; +pub type __useconds_t = ::std::os::raw::c_uint; +pub type __suseconds_t = ::std::os::raw::c_long; +pub type __suseconds64_t = ::std::os::raw::c_long; +pub type __daddr_t = ::std::os::raw::c_int; +pub type __key_t = ::std::os::raw::c_int; +pub type __clockid_t = ::std::os::raw::c_int; +pub type __timer_t = *mut ::std::os::raw::c_void; +pub type __blksize_t = ::std::os::raw::c_long; +pub type __blkcnt_t = ::std::os::raw::c_long; +pub type __blkcnt64_t = ::std::os::raw::c_long; +pub type __fsblkcnt_t = ::std::os::raw::c_ulong; +pub type __fsblkcnt64_t = ::std::os::raw::c_ulong; +pub type __fsfilcnt_t = ::std::os::raw::c_ulong; +pub type __fsfilcnt64_t = ::std::os::raw::c_ulong; +pub type __fsword_t = ::std::os::raw::c_long; +pub type __ssize_t = ::std::os::raw::c_long; +pub type __syscall_slong_t = ::std::os::raw::c_long; +pub type __syscall_ulong_t = ::std::os::raw::c_ulong; +pub type __loff_t = __off64_t; +pub type __caddr_t = *mut ::std::os::raw::c_char; +pub type __intptr_t = ::std::os::raw::c_long; +pub type __socklen_t = ::std::os::raw::c_uint; +pub type __sig_atomic_t = ::std::os::raw::c_int; +pub type int_least8_t = __int_least8_t; +pub type int_least16_t = __int_least16_t; +pub type int_least32_t = __int_least32_t; +pub type int_least64_t = __int_least64_t; +pub type uint_least8_t = __uint_least8_t; +pub type uint_least16_t = __uint_least16_t; +pub type uint_least32_t = __uint_least32_t; +pub type uint_least64_t = __uint_least64_t; +pub type int_fast8_t = ::std::os::raw::c_schar; +pub type int_fast16_t = ::std::os::raw::c_long; +pub type int_fast32_t = ::std::os::raw::c_long; +pub type int_fast64_t = ::std::os::raw::c_long; +pub type uint_fast8_t = ::std::os::raw::c_uchar; +pub type uint_fast16_t = ::std::os::raw::c_ulong; +pub type uint_fast32_t = ::std::os::raw::c_ulong; +pub type uint_fast64_t = ::std::os::raw::c_ulong; +pub type intmax_t = __intmax_t; +pub type uintmax_t = __uintmax_t; +pub type __gwchar_t = ::std::os::raw::c_int; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct imaxdiv_t { + pub quot: ::std::os::raw::c_long, + pub rem: ::std::os::raw::c_long, +} +#[allow(clippy::unnecessary_operation, clippy::identity_op)] +const _: () = { + ["Size of imaxdiv_t"][::std::mem::size_of::() - 16usize]; + ["Alignment of imaxdiv_t"][::std::mem::align_of::() - 8usize]; + ["Offset of field: imaxdiv_t::quot"][::std::mem::offset_of!(imaxdiv_t, quot) - 0usize]; + ["Offset of field: imaxdiv_t::rem"][::std::mem::offset_of!(imaxdiv_t, rem) - 8usize]; +}; +unsafe extern "C" { + pub fn imaxabs(__n: intmax_t) -> intmax_t; +} +unsafe extern "C" { + pub fn imaxdiv(__numer: intmax_t, __denom: intmax_t) -> imaxdiv_t; +} +unsafe extern "C" { + pub fn strtoimax( + __nptr: *const ::std::os::raw::c_char, + __endptr: *mut *mut ::std::os::raw::c_char, + __base: ::std::os::raw::c_int, + ) -> intmax_t; +} +unsafe extern "C" { + pub fn strtoumax( + __nptr: *const ::std::os::raw::c_char, + __endptr: *mut *mut ::std::os::raw::c_char, + __base: ::std::os::raw::c_int, + ) -> uintmax_t; +} +unsafe extern "C" { + pub fn wcstoimax( + __nptr: *const __gwchar_t, + __endptr: *mut *mut __gwchar_t, + __base: ::std::os::raw::c_int, + ) -> intmax_t; +} +unsafe extern "C" { + pub fn wcstoumax( + __nptr: *const __gwchar_t, + __endptr: *mut *mut __gwchar_t, + __base: ::std::os::raw::c_int, + ) -> uintmax_t; +} +pub type HighsInt = ::std::os::raw::c_int; +pub type HighsUInt = ::std::os::raw::c_uint; +#[doc = " Struct to handle callback output data\n"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct HighsCallbackDataOut { + pub cbdata: *mut ::std::os::raw::c_void, + pub log_type: ::std::os::raw::c_int, + pub running_time: f64, + pub simplex_iteration_count: HighsInt, + pub ipm_iteration_count: HighsInt, + pub pdlp_iteration_count: HighsInt, + pub objective_function_value: f64, + pub mip_node_count: i64, + pub mip_total_lp_iterations: i64, + pub mip_primal_bound: f64, + pub mip_dual_bound: f64, + pub mip_gap: f64, + pub mip_solution: *mut f64, + pub mip_solution_size: HighsInt, + pub cutpool_num_col: HighsInt, + pub cutpool_num_cut: HighsInt, + pub cutpool_num_nz: HighsInt, + pub cutpool_start: *mut HighsInt, + pub cutpool_index: *mut HighsInt, + pub cutpool_value: *mut f64, + pub cutpool_lower: *mut f64, + pub cutpool_upper: *mut f64, + pub user_solution_callback_origin: HighsInt, +} +#[allow(clippy::unnecessary_operation, clippy::identity_op)] +const _: () = { + ["Size of HighsCallbackDataOut"][::std::mem::size_of::() - 160usize]; + ["Alignment of HighsCallbackDataOut"][::std::mem::align_of::() - 8usize]; + ["Offset of field: HighsCallbackDataOut::cbdata"] + [::std::mem::offset_of!(HighsCallbackDataOut, cbdata) - 0usize]; + ["Offset of field: HighsCallbackDataOut::log_type"] + [::std::mem::offset_of!(HighsCallbackDataOut, log_type) - 8usize]; + ["Offset of field: HighsCallbackDataOut::running_time"] + [::std::mem::offset_of!(HighsCallbackDataOut, running_time) - 16usize]; + ["Offset of field: HighsCallbackDataOut::simplex_iteration_count"] + [::std::mem::offset_of!(HighsCallbackDataOut, simplex_iteration_count) - 24usize]; + ["Offset of field: HighsCallbackDataOut::ipm_iteration_count"] + [::std::mem::offset_of!(HighsCallbackDataOut, ipm_iteration_count) - 28usize]; + ["Offset of field: HighsCallbackDataOut::pdlp_iteration_count"] + [::std::mem::offset_of!(HighsCallbackDataOut, pdlp_iteration_count) - 32usize]; + ["Offset of field: HighsCallbackDataOut::objective_function_value"] + [::std::mem::offset_of!(HighsCallbackDataOut, objective_function_value) - 40usize]; + ["Offset of field: HighsCallbackDataOut::mip_node_count"] + [::std::mem::offset_of!(HighsCallbackDataOut, mip_node_count) - 48usize]; + ["Offset of field: HighsCallbackDataOut::mip_total_lp_iterations"] + [::std::mem::offset_of!(HighsCallbackDataOut, mip_total_lp_iterations) - 56usize]; + ["Offset of field: HighsCallbackDataOut::mip_primal_bound"] + [::std::mem::offset_of!(HighsCallbackDataOut, mip_primal_bound) - 64usize]; + ["Offset of field: HighsCallbackDataOut::mip_dual_bound"] + [::std::mem::offset_of!(HighsCallbackDataOut, mip_dual_bound) - 72usize]; + ["Offset of field: HighsCallbackDataOut::mip_gap"] + [::std::mem::offset_of!(HighsCallbackDataOut, mip_gap) - 80usize]; + ["Offset of field: HighsCallbackDataOut::mip_solution"] + [::std::mem::offset_of!(HighsCallbackDataOut, mip_solution) - 88usize]; + ["Offset of field: HighsCallbackDataOut::mip_solution_size"] + [::std::mem::offset_of!(HighsCallbackDataOut, mip_solution_size) - 96usize]; + ["Offset of field: HighsCallbackDataOut::cutpool_num_col"] + [::std::mem::offset_of!(HighsCallbackDataOut, cutpool_num_col) - 100usize]; + ["Offset of field: HighsCallbackDataOut::cutpool_num_cut"] + [::std::mem::offset_of!(HighsCallbackDataOut, cutpool_num_cut) - 104usize]; + ["Offset of field: HighsCallbackDataOut::cutpool_num_nz"] + [::std::mem::offset_of!(HighsCallbackDataOut, cutpool_num_nz) - 108usize]; + ["Offset of field: HighsCallbackDataOut::cutpool_start"] + [::std::mem::offset_of!(HighsCallbackDataOut, cutpool_start) - 112usize]; + ["Offset of field: HighsCallbackDataOut::cutpool_index"] + [::std::mem::offset_of!(HighsCallbackDataOut, cutpool_index) - 120usize]; + ["Offset of field: HighsCallbackDataOut::cutpool_value"] + [::std::mem::offset_of!(HighsCallbackDataOut, cutpool_value) - 128usize]; + ["Offset of field: HighsCallbackDataOut::cutpool_lower"] + [::std::mem::offset_of!(HighsCallbackDataOut, cutpool_lower) - 136usize]; + ["Offset of field: HighsCallbackDataOut::cutpool_upper"] + [::std::mem::offset_of!(HighsCallbackDataOut, cutpool_upper) - 144usize]; + ["Offset of field: HighsCallbackDataOut::user_solution_callback_origin"] + [::std::mem::offset_of!(HighsCallbackDataOut, user_solution_callback_origin) - 152usize]; +}; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct HighsCallbackDataIn { + pub user_interrupt: ::std::os::raw::c_int, + pub user_solution: *mut f64, + pub cbdata: *mut ::std::os::raw::c_void, + pub user_has_solution: ::std::os::raw::c_int, + pub user_solution_size: HighsInt, +} +#[allow(clippy::unnecessary_operation, clippy::identity_op)] +const _: () = { + ["Size of HighsCallbackDataIn"][::std::mem::size_of::() - 32usize]; + ["Alignment of HighsCallbackDataIn"][::std::mem::align_of::() - 8usize]; + ["Offset of field: HighsCallbackDataIn::user_interrupt"] + [::std::mem::offset_of!(HighsCallbackDataIn, user_interrupt) - 0usize]; + ["Offset of field: HighsCallbackDataIn::user_solution"] + [::std::mem::offset_of!(HighsCallbackDataIn, user_solution) - 8usize]; + ["Offset of field: HighsCallbackDataIn::cbdata"] + [::std::mem::offset_of!(HighsCallbackDataIn, cbdata) - 16usize]; + ["Offset of field: HighsCallbackDataIn::user_has_solution"] + [::std::mem::offset_of!(HighsCallbackDataIn, user_has_solution) - 24usize]; + ["Offset of field: HighsCallbackDataIn::user_solution_size"] + [::std::mem::offset_of!(HighsCallbackDataIn, user_solution_size) - 28usize]; +}; +pub type HighsCCallbackType = ::std::option::Option< + unsafe extern "C" fn( + arg1: ::std::os::raw::c_int, + arg2: *const ::std::os::raw::c_char, + arg3: *const HighsCallbackDataOut, + arg4: *mut HighsCallbackDataIn, + arg5: *mut ::std::os::raw::c_void, + ), +>; +pub const kHighsMaximumStringLength: HighsInt = 512; +pub const kHighsStatusError: HighsInt = -1; +pub const kHighsStatusOk: HighsInt = 0; +pub const kHighsStatusWarning: HighsInt = 1; +pub const kHighsVarTypeContinuous: HighsInt = 0; +pub const kHighsVarTypeInteger: HighsInt = 1; +pub const kHighsVarTypeSemiContinuous: HighsInt = 2; +pub const kHighsVarTypeSemiInteger: HighsInt = 3; +pub const kHighsVarTypeImplicitInteger: HighsInt = 4; +pub const kHighsOptionTypeBool: HighsInt = 0; +pub const kHighsOptionTypeInt: HighsInt = 1; +pub const kHighsOptionTypeDouble: HighsInt = 2; +pub const kHighsOptionTypeString: HighsInt = 3; +pub const kHighsInfoTypeInt64: HighsInt = -1; +pub const kHighsInfoTypeInt: HighsInt = 1; +pub const kHighsInfoTypeDouble: HighsInt = 2; +pub const kHighsObjSenseMinimize: HighsInt = 1; +pub const kHighsObjSenseMaximize: HighsInt = -1; +pub const kHighsMatrixFormatColwise: HighsInt = 1; +pub const kHighsMatrixFormatRowwise: HighsInt = 2; +pub const kHighsHessianFormatTriangular: HighsInt = 1; +pub const kHighsHessianFormatSquare: HighsInt = 2; +pub const kHighsSolutionStatusNone: HighsInt = 0; +pub const kHighsSolutionStatusInfeasible: HighsInt = 1; +pub const kHighsSolutionStatusFeasible: HighsInt = 2; +pub const kHighsBasisValidityInvalid: HighsInt = 0; +pub const kHighsBasisValidityValid: HighsInt = 1; +pub const kHighsPresolveStatusNotPresolved: HighsInt = -1; +pub const kHighsPresolveStatusNotReduced: HighsInt = 0; +pub const kHighsPresolveStatusInfeasible: HighsInt = 1; +pub const kHighsPresolveStatusUnboundedOrInfeasible: HighsInt = 2; +pub const kHighsPresolveStatusReduced: HighsInt = 3; +pub const kHighsPresolveStatusReducedToEmpty: HighsInt = 4; +pub const kHighsPresolveStatusTimeout: HighsInt = 5; +pub const kHighsPresolveStatusNullError: HighsInt = 6; +pub const kHighsPresolveStatusOptionsError: HighsInt = 7; +pub const kHighsPresolveStatusOutOfMemory: HighsInt = 8; +pub const kHighsModelStatusNotset: HighsInt = 0; +pub const kHighsModelStatusLoadError: HighsInt = 1; +pub const kHighsModelStatusModelError: HighsInt = 2; +pub const kHighsModelStatusPresolveError: HighsInt = 3; +pub const kHighsModelStatusSolveError: HighsInt = 4; +pub const kHighsModelStatusPostsolveError: HighsInt = 5; +pub const kHighsModelStatusModelEmpty: HighsInt = 6; +pub const kHighsModelStatusOptimal: HighsInt = 7; +pub const kHighsModelStatusInfeasible: HighsInt = 8; +pub const kHighsModelStatusUnboundedOrInfeasible: HighsInt = 9; +pub const kHighsModelStatusUnbounded: HighsInt = 10; +pub const kHighsModelStatusObjectiveBound: HighsInt = 11; +pub const kHighsModelStatusObjectiveTarget: HighsInt = 12; +pub const kHighsModelStatusTimeLimit: HighsInt = 13; +pub const kHighsModelStatusIterationLimit: HighsInt = 14; +pub const kHighsModelStatusUnknown: HighsInt = 15; +pub const kHighsModelStatusSolutionLimit: HighsInt = 16; +pub const kHighsModelStatusInterrupt: HighsInt = 17; +pub const kHighsBasisStatusLower: HighsInt = 0; +pub const kHighsBasisStatusBasic: HighsInt = 1; +pub const kHighsBasisStatusUpper: HighsInt = 2; +pub const kHighsBasisStatusZero: HighsInt = 3; +pub const kHighsBasisStatusNonbasic: HighsInt = 4; +pub const kHighsCallbackLogging: HighsInt = 0; +pub const kHighsCallbackSimplexInterrupt: HighsInt = 1; +pub const kHighsCallbackIpmInterrupt: HighsInt = 2; +pub const kHighsCallbackMipSolution: HighsInt = 3; +pub const kHighsCallbackMipImprovingSolution: HighsInt = 4; +pub const kHighsCallbackMipLogging: HighsInt = 5; +pub const kHighsCallbackMipInterrupt: HighsInt = 6; +pub const kHighsCallbackMipGetCutPool: HighsInt = 7; +pub const kHighsCallbackMipDefineLazyConstraints: HighsInt = 8; +pub const kHighsCallbackCallbackMipUserSolution: HighsInt = 9; +pub const kHighsCallbackDataOutLogTypeName: &[u8; 9] = b"log_type\0"; +pub const kHighsCallbackDataOutRunningTimeName: &[u8; 13] = b"running_time\0"; +pub const kHighsCallbackDataOutSimplexIterationCountName: &[u8; 24] = b"simplex_iteration_count\0"; +pub const kHighsCallbackDataOutIpmIterationCountName: &[u8; 20] = b"ipm_iteration_count\0"; +pub const kHighsCallbackDataOutPdlpIterationCountName: &[u8; 21] = b"pdlp_iteration_count\0"; +pub const kHighsCallbackDataOutObjectiveFunctionValueName: &[u8; 25] = + b"objective_function_value\0"; +pub const kHighsCallbackDataOutMipNodeCountName: &[u8; 15] = b"mip_node_count\0"; +pub const kHighsCallbackDataOutMipTotalLpIterationsName: &[u8; 24] = b"mip_total_lp_iterations\0"; +pub const kHighsCallbackDataOutMipPrimalBoundName: &[u8; 17] = b"mip_primal_bound\0"; +pub const kHighsCallbackDataOutMipDualBoundName: &[u8; 15] = b"mip_dual_bound\0"; +pub const kHighsCallbackDataOutMipGapName: &[u8; 8] = b"mip_gap\0"; +pub const kHighsCallbackDataOutMipSolutionName: &[u8; 13] = b"mip_solution\0"; +pub const kHighsCallbackDataOutCutpoolNumColName: &[u8; 16] = b"cutpool_num_col\0"; +pub const kHighsCallbackDataOutCutpoolNumCutName: &[u8; 16] = b"cutpool_num_cut\0"; +pub const kHighsCallbackDataOutCutpoolNumNzName: &[u8; 15] = b"cutpool_num_nz\0"; +pub const kHighsCallbackDataOutCutpoolStartName: &[u8; 14] = b"cutpool_start\0"; +pub const kHighsCallbackDataOutCutpoolIndexName: &[u8; 14] = b"cutpool_index\0"; +pub const kHighsCallbackDataOutCutpoolValueName: &[u8; 14] = b"cutpool_value\0"; +pub const kHighsCallbackDataOutCutpoolLowerName: &[u8; 14] = b"cutpool_lower\0"; +pub const kHighsCallbackDataOutCutpoolUpperName: &[u8; 14] = b"cutpool_upper\0"; +unsafe extern "C" { + #[doc = " Formulate and solve a linear program using HiGHS.\n\n @param num_col The number of columns.\n @param num_row The number of rows.\n @param num_nz The number of nonzeros in the constraint matrix.\n @param a_format The format of the constraint matrix as a\n `kHighsMatrixFormat` constant.\n @param sense The optimization sense as a `kHighsObjSense` constant.\n @param offset The objective constant.\n @param col_cost An array of length [num_col] with the column costs.\n @param col_lower An array of length [num_col] with the column lower bounds.\n @param col_upper An array of length [num_col] with the column upper bounds.\n @param row_lower An array of length [num_row] with the row lower bounds.\n @param row_upper An array of length [num_row] with the row upper bounds.\n @param a_start The constraint matrix is provided to HiGHS in compressed\n sparse column form (if `a_format` is\n `kHighsMatrixFormatColwise`, otherwise compressed sparse row\n form). The sparse matrix consists of three arrays,\n `a_start`, `a_index`, and `a_value`. `a_start` is an array\n of length [num_col] containing the starting index of each\n column in `a_index`. If `a_format` is\n `kHighsMatrixFormatRowwise` the array is of length [num_row]\n corresponding to each row.\n @param a_index An array of length [num_nz] with indices of matrix entries.\n @param a_value An array of length [num_nz] with values of matrix entries.\n\n @param col_value An array of length [num_col], to be filled with the\n primal column solution.\n @param col_dual An array of length [num_col], to be filled with the\n dual column solution.\n @param row_value An array of length [num_row], to be filled with the\n primal row solution.\n @param row_dual An array of length [num_row], to be filled with the\n dual row solution.\n @param col_basis_status An array of length [num_col], to be filled with the\n basis status of the columns in the form of a\n `kHighsBasisStatus` constant.\n @param row_basis_status An array of length [num_row], to be filled with the\n basis status of the rows in the form of a\n `kHighsBasisStatus` constant.\n @param model_status The location in which to place the termination\n status of the model after the solve in the form of a\n `kHighsModelStatus` constant.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_lpCall( + num_col: HighsInt, + num_row: HighsInt, + num_nz: HighsInt, + a_format: HighsInt, + sense: HighsInt, + offset: f64, + col_cost: *const f64, + col_lower: *const f64, + col_upper: *const f64, + row_lower: *const f64, + row_upper: *const f64, + a_start: *const HighsInt, + a_index: *const HighsInt, + a_value: *const f64, + col_value: *mut f64, + col_dual: *mut f64, + row_value: *mut f64, + row_dual: *mut f64, + col_basis_status: *mut HighsInt, + row_basis_status: *mut HighsInt, + model_status: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Formulate and solve a mixed-integer linear program using HiGHS.\n\n The signature of this method is identical to `Highs_lpCall`, except that it\n has an additional `integrality` argument, and that it is missing the\n `col_dual`, `row_dual`, `col_basis_status` and `row_basis_status` arguments.\n\n @param integrality An array of length [num_col], containing a\n `kHighsVarType` constant for each column.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_mipCall( + num_col: HighsInt, + num_row: HighsInt, + num_nz: HighsInt, + a_format: HighsInt, + sense: HighsInt, + offset: f64, + col_cost: *const f64, + col_lower: *const f64, + col_upper: *const f64, + row_lower: *const f64, + row_upper: *const f64, + a_start: *const HighsInt, + a_index: *const HighsInt, + a_value: *const f64, + integrality: *const HighsInt, + col_value: *mut f64, + row_value: *mut f64, + model_status: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Formulate and solve a quadratic program using HiGHS.\n\n The signature of this method is identical to `Highs_lpCall`, except that it\n has additional arguments for specifying the Hessian matrix.\n\n @param q_num_nz The number of nonzeros in the Hessian matrix.\n @param q_format The format of the Hessian matrix in the form of a\n `kHighsHessianStatus` constant. If q_num_nz > 0, this must\n be `kHighsHessianFormatTriangular`.\n @param q_start The Hessian matrix is provided to HiGHS as the lower\n triangular component in compressed sparse column form\n (or, equivalently, as the upper triangular component\n in compressed sparse row form). The sparse matrix consists\n of three arrays, `q_start`, `q_index`, and `q_value`.\n `q_start` is an array of length [num_col].\n @param q_index An array of length [q_num_nz] with indices of matrix\n entries.\n @param q_value An array of length [q_num_nz] with values of matrix entries.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_qpCall( + num_col: HighsInt, + num_row: HighsInt, + num_nz: HighsInt, + q_num_nz: HighsInt, + a_format: HighsInt, + q_format: HighsInt, + sense: HighsInt, + offset: f64, + col_cost: *const f64, + col_lower: *const f64, + col_upper: *const f64, + row_lower: *const f64, + row_upper: *const f64, + a_start: *const HighsInt, + a_index: *const HighsInt, + a_value: *const f64, + q_start: *const HighsInt, + q_index: *const HighsInt, + q_value: *const f64, + col_value: *mut f64, + col_dual: *mut f64, + row_value: *mut f64, + row_dual: *mut f64, + col_basis_status: *mut HighsInt, + row_basis_status: *mut HighsInt, + model_status: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Create a Highs instance and return the reference.\n\n Call `Highs_destroy` on the returned reference to clean up allocated memory.\n\n @returns A pointer to the Highs instance."] + pub fn Highs_create() -> *mut ::std::os::raw::c_void; +} +unsafe extern "C" { + #[doc = " Destroy the model `highs` created by `Highs_create` and free all\n corresponding memory. Future calls using `highs` are not allowed.\n\n To empty a model without invalidating `highs`, see `Highs_clearModel`.\n\n @param highs A pointer to the Highs instance."] + pub fn Highs_destroy(highs: *mut ::std::os::raw::c_void); +} +unsafe extern "C" { + #[doc = " Return the HiGHS version number as a string of the form \"vX.Y.Z\".\n\n @returns The HiGHS version as a `char*`."] + pub fn Highs_version() -> *const ::std::os::raw::c_char; +} +unsafe extern "C" { + #[doc = " Return the HiGHS major version number.\n\n @returns The HiGHS major version number."] + pub fn Highs_versionMajor() -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the HiGHS minor version number.\n\n @returns The HiGHS minor version number."] + pub fn Highs_versionMinor() -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the HiGHS patch version number.\n\n @returns The HiGHS patch version number."] + pub fn Highs_versionPatch() -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the HiGHS githash.\n\n @returns The HiGHS githash."] + pub fn Highs_githash() -> *const ::std::os::raw::c_char; +} +unsafe extern "C" { + #[doc = " Read a model from `filename` into `highs`.\n\n @param highs A pointer to the Highs instance.\n @param filename The filename to read.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_readModel( + highs: *mut ::std::os::raw::c_void, + filename: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Write the model in `highs` to `filename`.\n\n @param highs A pointer to the Highs instance.\n @param filename The filename to write.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_writeModel( + highs: *mut ::std::os::raw::c_void, + filename: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Write the presolved model in `highs` to `filename`.\n\n @param highs A pointer to the Highs instance.\n @param filename The filename to write.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_writePresolvedModel( + highs: *mut ::std::os::raw::c_void, + filename: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Reset the options and then call `clearModel`.\n\n See `Highs_destroy` to free all associated memory.\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_clear(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Remove all variables and constraints from the model `highs`, but do not\n invalidate the pointer `highs`. Future calls (for example, adding new\n variables and constraints) are allowed.\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_clearModel(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Clear all solution data associated with the model.\n\n See `Highs_destroy` to clear the model and free all associated memory.\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_clearSolver(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Presolve a model.\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_presolve(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Optimize a model. The algorithm used by HiGHS depends on the options that\n have been set.\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_run(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Postsolve a model using a primal (and possibly dual) solution.\n\n @param highs A pointer to the Highs instance.\n @param col_value An array of length [num_col] with the column solution\n values.\n @param col_dual An array of length [num_col] with the column dual\n values, or a null pointer if not known.\n @param row_dual An array of length [num_row] with the row dual values,\n or a null pointer if not known.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_postsolve( + highs: *mut ::std::os::raw::c_void, + col_value: *const f64, + col_dual: *const f64, + row_dual: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Write the solution information (including dual and basis status, if\n available) to a file.\n\n See also: `Highs_writeSolutionPretty`.\n\n @param highs A pointer to the Highs instance.\n @param filename The name of the file to write the results to.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_writeSolution( + highs: *const ::std::os::raw::c_void, + filename: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Write the solution information (including dual and basis status, if\n available) to a file in a human-readable format.\n\n The method identical to `Highs_writeSolution`, except that the\n printout is in a human-readable format.\n\n @param highs A pointer to the Highs instance.\n @param filename The name of the file to write the results to.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_writeSolutionPretty( + highs: *const ::std::os::raw::c_void, + filename: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Pass a linear program (LP) to HiGHS in a single function call.\n\n The signature of this function is identical to `Highs_passModel`, without the\n arguments for passing the Hessian matrix of a quadratic program and the\n integrality vector.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_passLp( + highs: *mut ::std::os::raw::c_void, + num_col: HighsInt, + num_row: HighsInt, + num_nz: HighsInt, + a_format: HighsInt, + sense: HighsInt, + offset: f64, + col_cost: *const f64, + col_lower: *const f64, + col_upper: *const f64, + row_lower: *const f64, + row_upper: *const f64, + a_start: *const HighsInt, + a_index: *const HighsInt, + a_value: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Pass a mixed-integer linear program (MILP) to HiGHS in a single function\n call.\n\n The signature of function is identical to `Highs_passModel`, without the\n arguments for passing the Hessian matrix of a quadratic program.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_passMip( + highs: *mut ::std::os::raw::c_void, + num_col: HighsInt, + num_row: HighsInt, + num_nz: HighsInt, + a_format: HighsInt, + sense: HighsInt, + offset: f64, + col_cost: *const f64, + col_lower: *const f64, + col_upper: *const f64, + row_lower: *const f64, + row_upper: *const f64, + a_start: *const HighsInt, + a_index: *const HighsInt, + a_value: *const f64, + integrality: *const HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Pass a model to HiGHS in a single function call. This is faster than\n constructing the model using `Highs_addRow` and `Highs_addCol`.\n\n @param highs A pointer to the Highs instance.\n @param num_col The number of columns.\n @param num_row The number of rows.\n @param num_nz The number of elements in the constraint matrix.\n @param q_num_nz The number of elements in the Hessian matrix.\n @param a_format The format of the constraint matrix to use in the form of\n a `kHighsMatrixFormat` constant.\n @param q_format The format of the Hessian matrix to use in the form of a\n `kHighsHessianFormat` constant.\n @param sense The optimization sense in the form of a `kHighsObjSense`\n constant.\n @param offset The constant term in the objective function.\n @param col_cost An array of length [num_col] with the objective\n coefficients.\n @param col_lower An array of length [num_col] with the lower column bounds.\n @param col_upper An array of length [num_col] with the upper column bounds.\n @param row_lower An array of length [num_row] with the upper row bounds.\n @param row_upper An array of length [num_row] with the upper row bounds.\n @param a_start The constraint matrix is provided to HiGHS in compressed\n sparse column form (if `a_format` is\n `kHighsMatrixFormatColwise`, otherwise compressed sparse\n row form). The sparse matrix consists of three arrays,\n `a_start`, `a_index`, and `a_value`. `a_start` is an array\n of length [num_col] containing the starting index of each\n column in `a_index`. If `a_format` is\n `kHighsMatrixFormatRowwise` the array is of length\n [num_row] corresponding to each row.\n @param a_index An array of length [num_nz] with indices of matrix\n entries.\n @param a_value An array of length [num_nz] with values of matrix entries.\n @param q_start The Hessian matrix is provided to HiGHS as the lower\n triangular component in compressed sparse column form\n (or, equivalently, as the upper triangular component\n in compressed sparse row form). The sparse matrix consists\n of three arrays, `q_start`, `q_index`, and `q_value`.\n `q_start` is an array of length [num_col]. If the model\n is linear, pass NULL.\n @param q_index An array of length [q_num_nz] with indices of matrix\n entries. If the model is linear, pass NULL.\n @param q_value An array of length [q_num_nz] with values of matrix\n entries. If the model is linear, pass NULL.\n @param integrality An array of length [num_col] containing a `kHighsVarType`\n constant for each column.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_passModel( + highs: *mut ::std::os::raw::c_void, + num_col: HighsInt, + num_row: HighsInt, + num_nz: HighsInt, + q_num_nz: HighsInt, + a_format: HighsInt, + q_format: HighsInt, + sense: HighsInt, + offset: f64, + col_cost: *const f64, + col_lower: *const f64, + col_upper: *const f64, + row_lower: *const f64, + row_upper: *const f64, + a_start: *const HighsInt, + a_index: *const HighsInt, + a_value: *const f64, + q_start: *const HighsInt, + q_index: *const HighsInt, + q_value: *const f64, + integrality: *const HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set the Hessian matrix for a quadratic objective.\n\n @param highs A pointer to the Highs instance.\n @param dim The dimension of the Hessian matrix. Should be [num_col].\n @param num_nz The number of non-zero elements in the Hessian matrix.\n @param format The format of the Hessian matrix as a `kHighsHessianFormat`\n constant. This must be `kHighsHessianFormatTriangular`.\n @param start The Hessian matrix is provided to HiGHS as the lower\n triangular component in compressed sparse column form\n (or, equivalently, as the upper triangular component\n in compressed sparse row form), using `q_start`, `q_index`,\n and `q_value`.The Hessian matrix is provided to HiGHS as the\n lower triangular component in compressed sparse column form.\n The sparse matrix consists of three arrays, `start`,\n `index`, and `value`. `start` is an array of length\n [num_col] containing the starting index of each column in\n `index`.\n @param index An array of length [num_nz] with indices of matrix entries.\n @param value An array of length [num_nz] with values of matrix entries.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_passHessian( + highs: *mut ::std::os::raw::c_void, + dim: HighsInt, + num_nz: HighsInt, + format: HighsInt, + start: *const HighsInt, + index: *const HighsInt, + value: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Passes multiple linear objective data to HiGHS, clearing any such\n data already in HiGHS\n\n @param highs A pointer to the Highs instance.\n @param weight A pointer to the weights of the linear objective, with\n its positive/negative sign determining whether it is\n minimized or maximized during lexicographic optimization\n @param offset A pointer to the objective offsets\n @param coefficients A pointer to the objective coefficients\n @param abs_tolerance A pointer to the absolute tolerances used when\n constructing objective constraints during lexicographic\n optimization\n @param rel_tolerance A pointer to the relative tolerances used when\n constructing objective constraints during lexicographic\n optimization\n @param priority A pointer to the priorities of the objectives during\n lexicographic optimization\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_passLinearObjectives( + highs: *const ::std::os::raw::c_void, + num_linear_objective: HighsInt, + weight: *const f64, + offset: *const f64, + coefficients: *const f64, + abs_tolerance: *const f64, + rel_tolerance: *const f64, + priority: *const HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Adds linear objective data to HiGHS\n\n @param highs A pointer to the Highs instance.\n @param weight The weight of the linear objective, with its\n positive/negative sign determining whether it is\n minimized or maximized during lexicographic\n optimization\n @param offset The objective offset\n @param coefficients A pointer to the objective coefficients\n @param abs_tolerance The absolute tolerance used when constructing an\n objective constraint during lexicographic optimization\n @param rel_tolerance The relative tolerance used when constructing an\n objective constraint during lexicographic optimization\n @param priority The priority of this objective during lexicographic\n optimization\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_addLinearObjective( + highs: *const ::std::os::raw::c_void, + weight: f64, + offset: f64, + coefficients: *const f64, + abs_tolerance: f64, + rel_tolerance: f64, + priority: HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Clears any multiple linear objective data in HiGHS\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_clearLinearObjectives(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Pass the name of a row.\n\n @param highs A pointer to the Highs instance.\n @param row The row for which the name is supplied.\n @param name The name of the row.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_passRowName( + highs: *const ::std::os::raw::c_void, + row: HighsInt, + name: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Pass the name of a column.\n\n @param highs A pointer to the Highs instance.\n @param col The column for which the name is supplied.\n @param name The name of the column.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_passColName( + highs: *const ::std::os::raw::c_void, + col: HighsInt, + name: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Pass the name of the model.\n\n @param highs A pointer to the Highs instance.\n @param name The name of the model.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_passModelName( + highs: *const ::std::os::raw::c_void, + name: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Read the option values from file.\n\n @param highs A pointer to the Highs instance.\n @param filename The filename from which to read the option values.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_readOptions( + highs: *const ::std::os::raw::c_void, + filename: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set a boolean-valued option.\n\n @param highs A pointer to the Highs instance.\n @param option The name of the option.\n @param value The new value of the option.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_setBoolOptionValue( + highs: *mut ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set an int-valued option.\n\n @param highs A pointer to the Highs instance.\n @param option The name of the option.\n @param value The new value of the option.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_setIntOptionValue( + highs: *mut ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set a double-valued option.\n\n @param highs A pointer to the Highs instance.\n @param option The name of the option.\n @param value The new value of the option.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_setDoubleOptionValue( + highs: *mut ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set a string-valued option.\n\n @param highs A pointer to the Highs instance.\n @param option The name of the option.\n @param value The new value of the option.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_setStringOptionValue( + highs: *mut ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get a boolean-valued option.\n\n @param highs A pointer to the Highs instance.\n @param option The name of the option.\n @param value The location in which the current value of the option should\n be placed.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getBoolOptionValue( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get an int-valued option.\n\n @param highs A pointer to the Highs instance.\n @param option The name of the option.\n @param value The location in which the current value of the option should\n be placed.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getIntOptionValue( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get a double-valued option.\n\n @param highs A pointer to the Highs instance.\n @param option The name of the option.\n @param value The location in which the current value of the option should\n be placed.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getDoubleOptionValue( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get a string-valued option.\n\n @param highs A pointer to the Highs instance.\n @param option The name of the option.\n @param value A pointer to allocated memory (of at least\n `kMaximumStringLength`) to store the current value of the\n option.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getStringOptionValue( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *mut ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the type expected by an option.\n\n @param highs A pointer to the Highs instance.\n @param option The name of the option.\n @param type A HighsInt in which the corresponding `kHighsOptionType`\n constant should be placed.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getOptionType( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + type_: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Reset all options to their default value.\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_resetOptions(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Write the current options to file.\n\n @param highs A pointer to the Highs instance.\n @param filename The filename to write the options to.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_writeOptions( + highs: *const ::std::os::raw::c_void, + filename: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Write the value of non-default options to file.\n\n This is similar to `Highs_writeOptions`, except only options with\n non-default value are written to `filename`.\n\n @param highs A pointer to the Highs instance.\n @param filename The filename to write the options to.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_writeOptionsDeviations( + highs: *const ::std::os::raw::c_void, + filename: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the number of options\n\n @param highs A pointer to the Highs instance."] + pub fn Highs_getNumOptions(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the name of an option identified by index\n\n @param highs A pointer to the Highs instance.\n @param index The index of the option.\n @param name The name of the option.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getOptionName( + highs: *const ::std::os::raw::c_void, + index: HighsInt, + name: *mut *mut ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the current and default values of a bool option\n\n @param highs A pointer to the Highs instance.\n @param current_value A pointer to the current value of the option.\n @param default_value A pointer to the default value of the option.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getBoolOptionValues( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + current_value: *mut HighsInt, + default_value: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the current and default values of a HighsInt option\n\n @param highs A pointer to the Highs instance.\n @param current_value A pointer to the current value of the option.\n @param min_value A pointer to the minimum value of the option.\n @param max_value A pointer to the maximum value of the option.\n @param default_value A pointer to the default value of the option.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getIntOptionValues( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + current_value: *mut HighsInt, + min_value: *mut HighsInt, + max_value: *mut HighsInt, + default_value: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the current and default values of a double option\n\n @param highs A pointer to the Highs instance.\n @param current_value A pointer to the current value of the option.\n @param min_value A pointer to the minimum value of the option.\n @param max_value A pointer to the maximum value of the option.\n @param default_value A pointer to the default value of the option.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getDoubleOptionValues( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + current_value: *mut f64, + min_value: *mut f64, + max_value: *mut f64, + default_value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the current and default values of a string option\n\n @param highs A pointer to the Highs instance.\n @param current_value A pointer to the current value of the option.\n @param default_value A pointer to the default value of the option.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getStringOptionValues( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + current_value: *mut ::std::os::raw::c_char, + default_value: *mut ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get an int-valued info value.\n\n @param highs A pointer to the Highs instance.\n @param info The name of the info item.\n @param value A reference to an integer that the result will be stored in.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getIntInfoValue( + highs: *const ::std::os::raw::c_void, + info: *const ::std::os::raw::c_char, + value: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get a double-valued info value.\n\n @param highs A pointer to the Highs instance.\n @param info The name of the info item.\n @param value A reference to a double that the result will be stored in.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getDoubleInfoValue( + highs: *const ::std::os::raw::c_void, + info: *const ::std::os::raw::c_char, + value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get an int64-valued info value.\n\n @param highs A pointer to the Highs instance.\n @param info The name of the info item.\n @param value A reference to an int64 that the result will be stored in.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getInt64InfoValue( + highs: *const ::std::os::raw::c_void, + info: *const ::std::os::raw::c_char, + value: *mut i64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the type expected by an info item.\n\n @param highs A pointer to the Highs instance.\n @param info The name of the info item.\n @param type A HighsInt in which the corresponding `kHighsOptionType`\n constant is stored.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getInfoType( + highs: *const ::std::os::raw::c_void, + info: *const ::std::os::raw::c_char, + type_: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the primal and dual solution from an optimized model.\n\n @param highs A pointer to the Highs instance.\n @param col_value An array of length [num_col], to be filled with primal\n column values.\n @param col_dual An array of length [num_col], to be filled with dual column\n values.\n @param row_value An array of length [num_row], to be filled with primal row\n values.\n @param row_dual An array of length [num_row], to be filled with dual row\n values.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getSolution( + highs: *const ::std::os::raw::c_void, + col_value: *mut f64, + col_dual: *mut f64, + row_value: *mut f64, + row_dual: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Given a linear program with a basic feasible solution, get the column and row\n basis statuses.\n\n @param highs A pointer to the Highs instance.\n @param col_status An array of length [num_col], to be filled with the column\n basis statuses in the form of a `kHighsBasisStatus`\n constant.\n @param row_status An array of length [num_row], to be filled with the row\n basis statuses in the form of a `kHighsBasisStatus`\n constant.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getBasis( + highs: *const ::std::os::raw::c_void, + col_status: *mut HighsInt, + row_status: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the optimization status of the model in the form of a\n `kHighsModelStatus` constant.\n\n @param highs A pointer to the Highs instance.\n\n @returns An integer corresponding to the `kHighsModelStatus` constant"] + pub fn Highs_getModelStatus(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Indicates whether a dual ray that is a certificate of primal\n infeasibility currently exists, and (at the expense of solving an\n LP) gets it if it does not and dual_ray_value is not nullptr.\n\n @param highs A pointer to the Highs instance.\n @param has_dual_ray A pointer to a HighsInt to store 1 if a dual ray\n currently exists.\n @param dual_ray_value An array of length [num_row] filled with the\n unbounded ray.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getDualRay( + highs: *const ::std::os::raw::c_void, + has_dual_ray: *mut HighsInt, + dual_ray_value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Indicates whether a dual unboundedness direction (corresponding to a\n certificate of primal infeasibility) exists, and (at the expense of\n solving an LP) gets it if it does not and\n dual_unboundedness_direction is not nullptr\n\n @param highs A pointer to the Highs\n instance.\n @param has_dual_unboundedness_direction A pointer to a HighsInt to\n store 1 if the dual\n unboundedness direction\n exists.\n @param dual_unboundedness_direction_value An array of length [num_col]\n filled with the unboundedness\n direction."] + pub fn Highs_getDualUnboundednessDirection( + highs: *const ::std::os::raw::c_void, + has_dual_unboundedness_direction: *mut HighsInt, + dual_unboundedness_direction_value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Indicates whether a primal ray that is a certificate of primal\n unboundedness currently exists, and (at the expense of solving an\n LP) gets it if it does not and primal_ray_value is not nullptr.\n\n @param highs A pointer to the Highs instance.\n @param has_primal_ray A pointer to a HighsInt to store 1 if the primal ray\n exists.\n @param primal_ray_value An array of length [num_col] filled with the\n unbounded ray.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getPrimalRay( + highs: *const ::std::os::raw::c_void, + has_primal_ray: *mut HighsInt, + primal_ray_value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the primal objective function value.\n\n @param highs A pointer to the Highs instance.\n\n @returns The primal objective function value"] + pub fn Highs_getObjectiveValue(highs: *const ::std::os::raw::c_void) -> f64; +} +unsafe extern "C" { + #[doc = " Get the indices of the rows and columns that make up the basis matrix ``B``\n of a basic feasible solution.\n\n Non-negative entries are indices of columns, and negative entries are\n `-row_index - 1`. For example, `{1, -1}` would be the second column and first\n row.\n\n The order of these rows and columns is important for calls to the functions:\n\n - `Highs_getBasisInverseRow`\n - `Highs_getBasisInverseCol`\n - `Highs_getBasisSolve`\n - `Highs_getBasisTransposeSolve`\n - `Highs_getReducedRow`\n - `Highs_getReducedColumn`\n\n @param highs A pointer to the Highs instance.\n @param basic_variables An array of size [num_rows], filled with the indices\n of the basic variables.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getBasicVariables( + highs: *const ::std::os::raw::c_void, + basic_variables: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get a row of the inverse basis matrix ``B^{-1}``.\n\n See `Highs_getBasicVariables` for a description of the ``B`` matrix.\n\n The arrays `row_vector` and `row_index` must have an allocated length of\n [num_row]. However, check `row_num_nz` to see how many non-zero elements are\n actually stored.\n\n @param highs A pointer to the Highs instance.\n @param row The index of the row to compute.\n @param row_vector An array of length [num_row] in which to store the\n values of the non-zero elements.\n @param row_num_nz The number of non-zeros in the row.\n @param row_index An array of length [num_row] in which to store the\n indices of the non-zero elements.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getBasisInverseRow( + highs: *const ::std::os::raw::c_void, + row: HighsInt, + row_vector: *mut f64, + row_num_nz: *mut HighsInt, + row_index: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get a column of the inverse basis matrix ``B^{-1}``.\n\n See `Highs_getBasicVariables` for a description of the ``B`` matrix.\n\n The arrays `col_vector` and `col_index` must have an allocated length of\n [num_row]. However, check `col_num_nz` to see how many non-zero elements are\n actually stored.\n\n @param highs A pointer to the Highs instance.\n @param col The index of the column to compute.\n @param col_vector An array of length [num_row] in which to store the\n values of the non-zero elements.\n @param col_num_nz The number of non-zeros in the column.\n @param col_index An array of length [num_row] in which to store the\n indices of the non-zero elements.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getBasisInverseCol( + highs: *const ::std::os::raw::c_void, + col: HighsInt, + col_vector: *mut f64, + col_num_nz: *mut HighsInt, + col_index: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Compute ``\\mathbf{x}=B^{-1}\\mathbf{b}`` for a given vector\n ``\\mathbf{b}``.\n\n See `Highs_getBasicVariables` for a description of the ``B`` matrix.\n\n The arrays `solution_vector` and `solution_index` must have an allocated\n length of [num_row]. However, check `solution_num_nz` to see how many\n non-zero elements are actually stored.\n\n @param highs A pointer to the Highs instance.\n @param rhs The right-hand side vector ``b``.\n @param solution_vector An array of length [num_row] in which to store the\n values of the non-zero elements.\n @param solution_num_nz The number of non-zeros in the solution.\n @param solution_index An array of length [num_row] in which to store the\n indices of the non-zero elements.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getBasisSolve( + highs: *const ::std::os::raw::c_void, + rhs: *const f64, + solution_vector: *mut f64, + solution_num_nz: *mut HighsInt, + solution_index: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Compute ``\\mathbf{x}=B^{-T}\\mathbf{b}`` for a given vector\n ``\\mathbf{b}``.\n\n See `Highs_getBasicVariables` for a description of the ``B`` matrix.\n\n The arrays `solution_vector` and `solution_index` must have an allocated\n length of [num_row]. However, check `solution_num_nz` to see how many\n non-zero elements are actually stored.\n\n @param highs A pointer to the Highs instance.\n @param rhs The right-hand side vector ``b``\n @param solution_vector An array of length [num_row] in which to store the\n values of the non-zero elements.\n @param solution_num_nz The number of non-zeros in the solution.\n @param solution_index An array of length [num_row] in which to store the\n indices of the non-zero elements.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getBasisTransposeSolve( + highs: *const ::std::os::raw::c_void, + rhs: *const f64, + solution_vector: *mut f64, + solution_nz: *mut HighsInt, + solution_index: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Compute a row of ``B^{-1}A``.\n\n See `Highs_getBasicVariables` for a description of the ``B`` matrix.\n\n The arrays `row_vector` and `row_index` must have an allocated length of\n [num_col]. However, check `row_num_nz` to see how many non-zero elements are\n actually stored.\n\n @param highs A pointer to the Highs instance.\n @param row The index of the row to compute.\n @param row_vector An array of length [num_col] in which to store the\n values of the non-zero elements.\n @param row_num_nz The number of non-zeros in the row.\n @param row_index An array of length [num_col] in which to store the\n indices of the non-zero elements.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getReducedRow( + highs: *const ::std::os::raw::c_void, + row: HighsInt, + row_vector: *mut f64, + row_num_nz: *mut HighsInt, + row_index: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Compute a column of ``B^{-1}A``.\n\n See `Highs_getBasicVariables` for a description of the ``B`` matrix.\n\n The arrays `col_vector` and `col_index` must have an allocated length of\n [num_row]. However, check `col_num_nz` to see how many non-zero elements are\n actually stored.\n\n @param highs A pointer to the Highs instance.\n @param col The index of the column to compute.\n @param col_vector An array of length [num_row] in which to store the\n values of the non-zero elements.\n @param col_num_nz The number of non-zeros in the column.\n @param col_index An array of length [num_row] in which to store the\n indices of the non-zero elements.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getReducedColumn( + highs: *const ::std::os::raw::c_void, + col: HighsInt, + col_vector: *mut f64, + col_num_nz: *mut HighsInt, + col_index: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set a basic feasible solution by passing the column and row basis statuses to\n the model.\n\n @param highs A pointer to the Highs instance.\n @param col_status an array of length [num_col] with the column basis status\n in the form of `kHighsBasisStatus` constants\n @param row_status an array of length [num_row] with the row basis status\n in the form of `kHighsBasisStatus` constants\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_setBasis( + highs: *mut ::std::os::raw::c_void, + col_status: *const HighsInt, + row_status: *const HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set a logical basis in the model.\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_setLogicalBasis(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set a solution by passing the column and row primal and dual solution values.\n\n For any values that are unavailable, pass NULL.\n\n @param highs A pointer to the Highs instance.\n @param col_value An array of length [num_col] with the column solution\n values.\n @param row_value An array of length [num_row] with the row solution\n values.\n @param col_dual An array of length [num_col] with the column dual values.\n @param row_dual An array of length [num_row] with the row dual values.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_setSolution( + highs: *mut ::std::os::raw::c_void, + col_value: *const f64, + row_value: *const f64, + col_dual: *const f64, + row_dual: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set a partial primal solution by passing values for a set of variables\n\n @param highs A pointer to the Highs instance.\n @param num_entries Number of variables in the set\n @param index Indices of variables in the set\n @param value Values of variables in the set\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_setSparseSolution( + highs: *mut ::std::os::raw::c_void, + num_entries: HighsInt, + index: *const HighsInt, + value: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set the callback method to use for HiGHS\n\n @param highs A pointer to the Highs instance.\n @param user_callback A pointer to the user callback\n @param user_callback_data A pointer to the user callback data\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_setCallback( + highs: *mut ::std::os::raw::c_void, + user_callback: HighsCCallbackType, + user_callback_data: *mut ::std::os::raw::c_void, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Start callback of given type\n\n @param highs A pointer to the Highs instance.\n @param callback_type The type of callback to be started\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_startCallback( + highs: *mut ::std::os::raw::c_void, + callback_type: HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Stop callback of given type\n\n @param highs A pointer to the Highs instance.\n @param callback_type The type of callback to be stopped\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_stopCallback( + highs: *mut ::std::os::raw::c_void, + callback_type: HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the cumulative wall-clock time spent in `Highs_run`.\n\n @param highs A pointer to the Highs instance.\n\n @returns The cumulative wall-clock time spent in `Highs_run`"] + pub fn Highs_getRunTime(highs: *const ::std::os::raw::c_void) -> f64; +} +unsafe extern "C" { + #[doc = " Reset the clocks in a `highs` model.\n\n Each `highs` model contains a single instance of clock that records how much\n time is spent in various parts of the algorithm. This clock is not reset on\n entry to `Highs_run`, so repeated calls to `Highs_run` report the cumulative\n time spent in the algorithm. A side-effect is that this will trigger a time\n limit termination once the cumulative run time exceeds the time limit, rather\n than the run time of each individual call to `Highs_run`.\n\n As a work-around, call `Highs_zeroAllClocks` before each call to `Highs_run`.\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_zeroAllClocks(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Add a new column (variable) to the model.\n\n @param highs A pointer to the Highs instance.\n @param cost The objective coefficient of the column.\n @param lower The lower bound of the column.\n @param upper The upper bound of the column.\n @param num_new_nz The number of non-zeros in the column.\n @param index An array of size [num_new_nz] with the row indices.\n @param value An array of size [num_new_nz] with row values.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_addCol( + highs: *mut ::std::os::raw::c_void, + cost: f64, + lower: f64, + upper: f64, + num_new_nz: HighsInt, + index: *const HighsInt, + value: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Add multiple columns (variables) to the model.\n\n @param highs A pointer to the Highs instance.\n @param num_new_col The number of new columns to add.\n @param costs An array of size [num_new_col] with objective\n coefficients.\n @param lower An array of size [num_new_col] with lower bounds.\n @param upper An array of size [num_new_col] with upper bounds.\n @param num_new_nz The number of new nonzeros in the constraint matrix.\n @param starts The constraint coefficients are given as a matrix in\n compressed sparse column form by the arrays `starts`,\n `index`, and `value`. `starts` is an array of size\n [num_new_cols] with the start index of each row in\n indices and values.\n @param index An array of size [num_new_nz] with row indices.\n @param value An array of size [num_new_nz] with row values.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_addCols( + highs: *mut ::std::os::raw::c_void, + num_new_col: HighsInt, + costs: *const f64, + lower: *const f64, + upper: *const f64, + num_new_nz: HighsInt, + starts: *const HighsInt, + index: *const HighsInt, + value: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Add a new variable to the model.\n\n @param highs A pointer to the Highs instance.\n @param lower The lower bound of the column.\n @param upper The upper bound of the column.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_addVar(highs: *mut ::std::os::raw::c_void, lower: f64, upper: f64) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Add multiple variables to the model.\n\n @param highs A pointer to the Highs instance.\n @param num_new_var The number of new variables to add.\n @param lower An array of size [num_new_var] with lower bounds.\n @param upper An array of size [num_new_var] with upper bounds.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_addVars( + highs: *mut ::std::os::raw::c_void, + num_new_var: HighsInt, + lower: *const f64, + upper: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Add a new row (a linear constraint) to the model.\n\n @param highs A pointer to the Highs instance.\n @param lower The lower bound of the row.\n @param upper The upper bound of the row.\n @param num_new_nz The number of non-zeros in the row\n @param index An array of size [num_new_nz] with column indices.\n @param value An array of size [num_new_nz] with column values.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_addRow( + highs: *mut ::std::os::raw::c_void, + lower: f64, + upper: f64, + num_new_nz: HighsInt, + index: *const HighsInt, + value: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Add multiple rows (linear constraints) to the model.\n\n @param highs A pointer to the Highs instance.\n @param num_new_row The number of new rows to add\n @param lower An array of size [num_new_row] with the lower bounds of\n the rows.\n @param upper An array of size [num_new_row] with the upper bounds of\n the rows.\n @param num_new_nz The number of non-zeros in the rows.\n @param starts The constraint coefficients are given as a matrix in\n compressed sparse row form by the arrays `starts`,\n `index`, and `value`. `starts` is an array of size\n [num_new_rows] with the start index of each row in\n indices and values.\n @param index An array of size [num_new_nz] with column indices.\n @param value An array of size [num_new_nz] with column values.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_addRows( + highs: *mut ::std::os::raw::c_void, + num_new_row: HighsInt, + lower: *const f64, + upper: *const f64, + num_new_nz: HighsInt, + starts: *const HighsInt, + index: *const HighsInt, + value: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Ensure that the constraint matrix of the incumbent model is stored\n column-wise.\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_ensureColwise(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Ensure that the constraint matrix of the incumbent model is stored row-wise.\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_ensureRowwise(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the objective sense of the model.\n\n @param highs A pointer to the Highs instance.\n @param sense The new optimization sense in the form of a `kHighsObjSense`\n constant.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeObjectiveSense( + highs: *mut ::std::os::raw::c_void, + sense: HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the objective offset of the model.\n\n @param highs A pointer to the Highs instance.\n @param offset The new objective offset.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeObjectiveOffset(highs: *mut ::std::os::raw::c_void, offset: f64) + -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the integrality of a column.\n\n @param highs A pointer to the Highs instance.\n @param col The column index to change.\n @param integrality The new integrality of the column in the form of a\n `kHighsVarType` constant.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColIntegrality( + highs: *mut ::std::os::raw::c_void, + col: HighsInt, + integrality: HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the integrality of multiple adjacent columns.\n\n @param highs A pointer to the Highs instance.\n @param from_col The index of the first column whose integrality changes.\n @param to_col The index of the last column whose integrality\n changes.\n @param integrality An array of length [to_col - from_col + 1] with the new\n integralities of the columns in the form of\n `kHighsVarType` constants.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColsIntegralityByRange( + highs: *mut ::std::os::raw::c_void, + from_col: HighsInt, + to_col: HighsInt, + integrality: *const HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the integrality of multiple columns given by an array of indices.\n\n @param highs A pointer to the Highs instance.\n @param num_set_entries The number of columns to change.\n @param set An array of size [num_set_entries] with the indices\n of the columns to change.\n @param integrality An array of length [num_set_entries] with the new\n integralities of the columns in the form of\n `kHighsVarType` constants.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColsIntegralityBySet( + highs: *mut ::std::os::raw::c_void, + num_set_entries: HighsInt, + set: *const HighsInt, + integrality: *const HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the integrality of multiple columns given by a mask.\n\n @param highs A pointer to the Highs instance.\n @param mask An array of length [num_col] with 1 if the column\n integrality should be changed and 0 otherwise.\n @param integrality An array of length [num_col] with the new\n integralities of the columns in the form of\n `kHighsVarType` constants.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColsIntegralityByMask( + highs: *mut ::std::os::raw::c_void, + mask: *const HighsInt, + integrality: *const HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Clear the integrality of all columns\n\n @param highs A pointer to the Highs instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_clearIntegrality(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the objective coefficient of a column.\n\n @param highs A pointer to the Highs instance.\n @param col The index of the column fo change.\n @param cost The new objective coefficient.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColCost( + highs: *mut ::std::os::raw::c_void, + col: HighsInt, + cost: f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the cost coefficients of multiple adjacent columns.\n\n @param highs A pointer to the Highs instance.\n @param from_col The index of the first column whose cost changes.\n @param to_col The index of the last column whose cost changes.\n @param cost An array of length [to_col - from_col + 1] with the new\n objective coefficients.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColsCostByRange( + highs: *mut ::std::os::raw::c_void, + from_col: HighsInt, + to_col: HighsInt, + cost: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the cost of multiple columns given by an array of indices.\n\n @param highs A pointer to the Highs instance.\n @param num_set_entries The number of columns to change.\n @param set An array of size [num_set_entries] with the indices\n of the columns to change.\n @param cost An array of length [num_set_entries] with the new\n costs of the columns.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColsCostBySet( + highs: *mut ::std::os::raw::c_void, + num_set_entries: HighsInt, + set: *const HighsInt, + cost: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the cost of multiple columns given by a mask.\n\n @param highs A pointer to the Highs instance.\n @param mask An array of length [num_col] with 1 if the column\n cost should be changed and 0 otherwise.\n @param cost An array of length [num_col] with the new costs.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColsCostByMask( + highs: *mut ::std::os::raw::c_void, + mask: *const HighsInt, + cost: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the variable bounds of a column.\n\n @param highs A pointer to the Highs instance.\n @param col The index of the column whose bounds are to change.\n @param lower The new lower bound.\n @param upper The new upper bound.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColBounds( + highs: *mut ::std::os::raw::c_void, + col: HighsInt, + lower: f64, + upper: f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the variable bounds of multiple adjacent columns.\n\n @param highs A pointer to the Highs instance.\n @param from_col The index of the first column whose bound changes.\n @param to_col The index of the last column whose bound changes.\n @param lower An array of length [to_col - from_col + 1] with the new\n lower bounds.\n @param upper An array of length [to_col - from_col + 1] with the new\n upper bounds.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColsBoundsByRange( + highs: *mut ::std::os::raw::c_void, + from_col: HighsInt, + to_col: HighsInt, + lower: *const f64, + upper: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the bounds of multiple columns given by an array of indices.\n\n @param highs A pointer to the Highs instance.\n @param num_set_entries The number of columns to change.\n @param set An array of size [num_set_entries] with the indices\n of the columns to change.\n @param lower An array of length [num_set_entries] with the new\n lower bounds.\n @param upper An array of length [num_set_entries] with the new\n upper bounds.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColsBoundsBySet( + highs: *mut ::std::os::raw::c_void, + num_set_entries: HighsInt, + set: *const HighsInt, + lower: *const f64, + upper: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the variable bounds of multiple columns given by a mask.\n\n @param highs A pointer to the Highs instance.\n @param mask An array of length [num_col] with 1 if the column\n bounds should be changed and 0 otherwise.\n @param lower An array of length [num_col] with the new lower bounds.\n @param upper An array of length [num_col] with the new upper bounds.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeColsBoundsByMask( + highs: *mut ::std::os::raw::c_void, + mask: *const HighsInt, + lower: *const f64, + upper: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the bounds of a row.\n\n @param highs A pointer to the Highs instance.\n @param row The index of the row whose bounds are to change.\n @param lower The new lower bound.\n @param upper The new upper bound.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeRowBounds( + highs: *mut ::std::os::raw::c_void, + row: HighsInt, + lower: f64, + upper: f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the variable bounds of multiple adjacent rows.\n\n @param highs A pointer to the Highs instance.\n @param from_row The index of the first row whose bound changes.\n @param to_row The index of the last row whose bound changes.\n @param lower An array of length [to_row - from_row + 1] with the new\n lower bounds.\n @param upper An array of length [to_row - from_row + 1] with the new\n upper bounds.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeRowsBoundsByRange( + highs: *mut ::std::os::raw::c_void, + from_row: HighsInt, + to_row: HighsInt, + lower: *const f64, + upper: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the bounds of multiple rows given by an array of indices.\n\n @param highs A pointer to the Highs instance.\n @param num_set_entries The number of rows to change.\n @param set An array of size [num_set_entries] with the indices\n of the rows to change.\n @param lower An array of length [num_set_entries] with the new\n lower bounds.\n @param upper An array of length [num_set_entries] with the new\n upper bounds.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeRowsBoundsBySet( + highs: *mut ::std::os::raw::c_void, + num_set_entries: HighsInt, + set: *const HighsInt, + lower: *const f64, + upper: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change the bounds of multiple rows given by a mask.\n\n @param highs A pointer to the Highs instance.\n @param mask An array of length [num_row] with 1 if the row\n bounds should be changed and 0 otherwise.\n @param lower An array of length [num_row] with the new lower bounds.\n @param upper An array of length [num_row] with the new upper bounds.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeRowsBoundsByMask( + highs: *mut ::std::os::raw::c_void, + mask: *const HighsInt, + lower: *const f64, + upper: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Change a coefficient in the constraint matrix.\n\n @param highs A pointer to the Highs instance.\n @param row The index of the row to change.\n @param col The index of the column to change.\n @param value The new constraint coefficient.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_changeCoeff( + highs: *mut ::std::os::raw::c_void, + row: HighsInt, + col: HighsInt, + value: f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the objective sense.\n\n @param highs A pointer to the Highs instance.\n @param sense The location in which the current objective sense should be\n placed. The sense is a `kHighsObjSense` constant.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getObjectiveSense( + highs: *const ::std::os::raw::c_void, + sense: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the objective offset.\n\n @param highs A pointer to the Highs instance.\n @param offset The location in which the current objective offset should be\n placed.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getObjectiveOffset( + highs: *const ::std::os::raw::c_void, + offset: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get data associated with multiple adjacent columns from the model.\n\n To query the constraint coefficients, this function should be called twice.\n\n First, call this function with `matrix_start`, `matrix_index`, and\n `matrix_value` as `NULL`. This call will populate `num_nz` with the number of\n nonzero elements in the corresponding section of the constraint matrix.\n\n Second, allocate new `matrix_index` and `matrix_value` arrays of length\n `num_nz` and call this function again to populate the new arrays with their\n contents.\n\n @param highs A pointer to the Highs instance.\n @param from_col The first column for which to query data for.\n @param to_col The last column (inclusive) for which to query data for.\n @param num_col A HighsInt populated with the number of columns got from\n the model (this should equal `to_col - from_col + 1`).\n @param costs An array of size [to_col - from_col + 1] for the column\n cost coefficients.\n @param lower An array of size [to_col - from_col + 1] for the column\n lower bounds.\n @param upper An array of size [to_col - from_col + 1] for the column\n upper bounds.\n @param num_nz A HighsInt to be populated with the number of non-zero\n elements in the constraint matrix.\n @param matrix_start An array of size [to_col - from_col + 1] with the start\n indices of each column in `matrix_index` and\n `matrix_value`.\n @param matrix_index An array of size [num_nz] with the row indices of each\n element in the constraint matrix.\n @param matrix_value An array of size [num_nz] with the non-zero elements of\n the constraint matrix.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getColsByRange( + highs: *const ::std::os::raw::c_void, + from_col: HighsInt, + to_col: HighsInt, + num_col: *mut HighsInt, + costs: *mut f64, + lower: *mut f64, + upper: *mut f64, + num_nz: *mut HighsInt, + matrix_start: *mut HighsInt, + matrix_index: *mut HighsInt, + matrix_value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get data associated with multiple columns given by an array.\n\n This function is identical to `Highs_getColsByRange`, except for how the\n columns are specified.\n\n @param num_set_indices The number of indices in `set`.\n @param set An array of size [num_set_entries] with the column\n indices to get.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getColsBySet( + highs: *const ::std::os::raw::c_void, + num_set_entries: HighsInt, + set: *const HighsInt, + num_col: *mut HighsInt, + costs: *mut f64, + lower: *mut f64, + upper: *mut f64, + num_nz: *mut HighsInt, + matrix_start: *mut HighsInt, + matrix_index: *mut HighsInt, + matrix_value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get data associated with multiple columns given by a mask.\n\n This function is identical to `Highs_getColsByRange`, except for how the\n columns are specified.\n\n @param mask An array of length [num_col] containing a `1` to get the column\n and `0` otherwise.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getColsByMask( + highs: *const ::std::os::raw::c_void, + mask: *const HighsInt, + num_col: *mut HighsInt, + costs: *mut f64, + lower: *mut f64, + upper: *mut f64, + num_nz: *mut HighsInt, + matrix_start: *mut HighsInt, + matrix_index: *mut HighsInt, + matrix_value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get data associated with multiple adjacent rows from the model.\n\n To query the constraint coefficients, this function should be called twice.\n\n First, call this function with `matrix_start`, `matrix_index`, and\n `matrix_value` as `NULL`. This call will populate `num_nz` with the number of\n nonzero elements in the corresponding section of the constraint matrix.\n\n Second, allocate new `matrix_index` and `matrix_value` arrays of length\n `num_nz` and call this function again to populate the new arrays with their\n contents.\n\n @param highs A pointer to the Highs instance.\n @param from_row The first row for which to query data for.\n @param to_row The last row (inclusive) for which to query data for.\n @param num_row A HighsInt to be populated with the number of rows got\n from the model.\n @param lower An array of size [to_row - from_row + 1] for the row\n lower bounds.\n @param upper An array of size [to_row - from_row + 1] for the row\n upper bounds.\n @param num_nz A HighsInt to be populated with the number of non-zero\n elements in the constraint matrix.\n @param matrix_start An array of size [to_row - from_row + 1] with the start\n indices of each row in `matrix_index` and\n `matrix_value`.\n @param matrix_index An array of size [num_nz] with the column indices of\n each element in the constraint matrix.\n @param matrix_value An array of size [num_nz] with the non-zero elements of\n the constraint matrix.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getRowsByRange( + highs: *const ::std::os::raw::c_void, + from_row: HighsInt, + to_row: HighsInt, + num_row: *mut HighsInt, + lower: *mut f64, + upper: *mut f64, + num_nz: *mut HighsInt, + matrix_start: *mut HighsInt, + matrix_index: *mut HighsInt, + matrix_value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get data associated with multiple rows given by an array.\n\n This function is identical to `Highs_getRowsByRange`, except for how the\n rows are specified.\n\n @param num_set_indices The number of indices in `set`.\n @param set An array of size [num_set_entries] containing the\n row indices to get.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getRowsBySet( + highs: *const ::std::os::raw::c_void, + num_set_entries: HighsInt, + set: *const HighsInt, + num_row: *mut HighsInt, + lower: *mut f64, + upper: *mut f64, + num_nz: *mut HighsInt, + matrix_start: *mut HighsInt, + matrix_index: *mut HighsInt, + matrix_value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get data associated with multiple rows given by a mask.\n\n This function is identical to `Highs_getRowsByRange`, except for how the\n rows are specified.\n\n @param mask An array of length [num_row] containing a `1` to get the row and\n `0` otherwise.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getRowsByMask( + highs: *const ::std::os::raw::c_void, + mask: *const HighsInt, + num_row: *mut HighsInt, + lower: *mut f64, + upper: *mut f64, + num_nz: *mut HighsInt, + matrix_start: *mut HighsInt, + matrix_index: *mut HighsInt, + matrix_value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the name of a row.\n\n @param row The index of the row to query.\n @param name A pointer in which to store the name of the row. This must have\n length `kHighsMaximumStringLength`.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getRowName( + highs: *const ::std::os::raw::c_void, + row: HighsInt, + name: *mut ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the index of a row from its name.\n\n If multiple rows have the same name, or if no row exists with `name`, this\n function returns `kHighsStatusError`.\n\n @param name A pointer of the name of the row to query.\n @param row A pointer in which to store the index of the row\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getRowByName( + highs: *const ::std::os::raw::c_void, + name: *const ::std::os::raw::c_char, + row: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the name of a column.\n\n @param col The index of the column to query.\n @param name A pointer in which to store the name of the column. This must\n have length `kHighsMaximumStringLength`.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getColName( + highs: *const ::std::os::raw::c_void, + col: HighsInt, + name: *mut ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the index of a column from its name.\n\n If multiple columns have the same name, or if no column exists with `name`,\n this function returns `kHighsStatusError`.\n\n @param name A pointer of the name of the column to query.\n @param col A pointer in which to store the index of the column\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getColByName( + highs: *const ::std::os::raw::c_void, + name: *const ::std::os::raw::c_char, + col: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the integrality of a column.\n\n @param col The index of the column to query.\n @param integrality A HighsInt in which the integrality of the column should\n be placed. The integer is one of the `kHighsVarTypeXXX`\n constants.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getColIntegrality( + highs: *const ::std::os::raw::c_void, + col: HighsInt, + integrality: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Delete multiple adjacent columns.\n\n @param highs A pointer to the Highs instance.\n @param from_col The index of the first column to delete.\n @param to_col The index of the last column to delete.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_deleteColsByRange( + highs: *mut ::std::os::raw::c_void, + from_col: HighsInt, + to_col: HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Delete multiple columns given by an array of indices.\n\n @param highs A pointer to the Highs instance.\n @param num_set_entries The number of columns to delete.\n @param set An array of size [num_set_entries] with the indices\n of the columns to delete.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_deleteColsBySet( + highs: *mut ::std::os::raw::c_void, + num_set_entries: HighsInt, + set: *const HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Delete multiple columns given by a mask.\n\n @param highs A pointer to the Highs instance.\n @param mask An array of length [num_col] with 1 if the column\n should be deleted and 0 otherwise.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_deleteColsByMask( + highs: *mut ::std::os::raw::c_void, + mask: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Delete multiple adjacent rows.\n\n @param highs A pointer to the Highs instance.\n @param from_row The index of the first row to delete.\n @param to_row The index of the last row to delete.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_deleteRowsByRange( + highs: *mut ::std::os::raw::c_void, + from_row: HighsInt, + to_row: HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Delete multiple rows given by an array of indices.\n\n @param highs A pointer to the Highs instance.\n @param num_set_entries The number of rows to delete.\n @param set An array of size [num_set_entries] with the indices\n of the rows to delete.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_deleteRowsBySet( + highs: *mut ::std::os::raw::c_void, + num_set_entries: HighsInt, + set: *const HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Delete multiple rows given by a mask.\n\n @param highs A pointer to the Highs instance.\n @param mask An array of length [num_row] with `1` if the row should be\n deleted and `0` otherwise. The new index of any column not\n deleted is stored in place of the value `0`.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_deleteRowsByMask( + highs: *mut ::std::os::raw::c_void, + mask: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Scale a column by a constant.\n\n Scaling a column modifies the elements in the constraint matrix, the variable\n bounds, and the objective coefficient.\n\n @param highs A pointer to the Highs instance.\n @param col The index of the column to scale.\n @param scaleval The value by which to scale the column. If `scaleval < 0`,\n the variable bounds flipped.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_scaleCol( + highs: *mut ::std::os::raw::c_void, + col: HighsInt, + scaleval: f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Scale a row by a constant.\n\n @param highs A pointer to the Highs instance.\n @param row The index of the row to scale.\n @param scaleval The value by which to scale the row. If `scaleval < 0`, the\n row bounds are flipped.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_scaleRow( + highs: *mut ::std::os::raw::c_void, + row: HighsInt, + scaleval: f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the value of infinity used by HiGHS.\n\n @param highs A pointer to the Highs instance.\n\n @returns The value of infinity used by HiGHS."] + pub fn Highs_getInfinity(highs: *const ::std::os::raw::c_void) -> f64; +} +unsafe extern "C" { + #[doc = " Return the size of integers used by HiGHS.\n\n @param highs A pointer to the Highs instance.\n\n @returns The size of integers used by HiGHS."] + pub fn Highs_getSizeofHighsInt(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the number of columns in the model.\n\n @param highs A pointer to the Highs instance.\n\n @returns The number of columns in the model."] + pub fn Highs_getNumCol(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the number of rows in the model.\n\n @param highs A pointer to the Highs instance.\n\n @returns The number of rows in the model."] + pub fn Highs_getNumRow(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the number of nonzeros in the constraint matrix of the model.\n\n @param highs A pointer to the Highs instance.\n\n @returns The number of nonzeros in the constraint matrix of the model."] + pub fn Highs_getNumNz(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the number of nonzeroes in the Hessian matrix of the model.\n\n @param highs A pointer to the Highs instance.\n\n @returns The number of nonzeroes in the Hessian matrix of the model."] + pub fn Highs_getHessianNumNz(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the number of columns in the presolved model.\n\n @param highs A pointer to the Highs instance.\n\n @returns The number of columns in the presolved model."] + pub fn Highs_getPresolvedNumCol(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the number of rows in the presolved model.\n\n @param highs A pointer to the Highs instance.\n\n @returns The number of rows in the presolved model."] + pub fn Highs_getPresolvedNumRow(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the number of nonzeros in the constraint matrix of the presolved\n model.\n\n @param highs A pointer to the Highs instance.\n\n @returns The number of nonzeros in the constraint matrix of the presolved\n model."] + pub fn Highs_getPresolvedNumNz(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the data from a HiGHS model.\n\n The input arguments have the same meaning (in a different order) to those\n used in `Highs_passModel`.\n\n Note that all arrays must be pre-allocated to the correct size before calling\n `Highs_getModel`. Use the following query methods to check the appropriate\n size:\n - `Highs_getNumCol`\n - `Highs_getNumRow`\n - `Highs_getNumNz`\n - `Highs_getHessianNumNz`\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getModel( + highs: *const ::std::os::raw::c_void, + a_format: HighsInt, + q_format: HighsInt, + num_col: *mut HighsInt, + num_row: *mut HighsInt, + num_nz: *mut HighsInt, + hessian_num_nz: *mut HighsInt, + sense: *mut HighsInt, + offset: *mut f64, + col_cost: *mut f64, + col_lower: *mut f64, + col_upper: *mut f64, + row_lower: *mut f64, + row_upper: *mut f64, + a_start: *mut HighsInt, + a_index: *mut HighsInt, + a_value: *mut f64, + q_start: *mut HighsInt, + q_index: *mut HighsInt, + q_value: *mut f64, + integrality: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the data from a HiGHS LP.\n\n The input arguments have the same meaning (in a different order) to those\n used in `Highs_passModel`.\n\n Note that all arrays must be pre-allocated to the correct size before calling\n `Highs_getModel`. Use the following query methods to check the appropriate\n size:\n - `Highs_getNumCol`\n - `Highs_getNumRow`\n - `Highs_getNumNz`\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getLp( + highs: *const ::std::os::raw::c_void, + a_format: HighsInt, + num_col: *mut HighsInt, + num_row: *mut HighsInt, + num_nz: *mut HighsInt, + sense: *mut HighsInt, + offset: *mut f64, + col_cost: *mut f64, + col_lower: *mut f64, + col_upper: *mut f64, + row_lower: *mut f64, + row_upper: *mut f64, + a_start: *mut HighsInt, + a_index: *mut HighsInt, + a_value: *mut f64, + integrality: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Get the data from a HiGHS presolved LP.\n\n The input arguments have the same meaning (in a different order) to those\n used in `Highs_passModel`.\n\n Note that all arrays must be pre-allocated to the correct size before calling\n `Highs_getModel`. Use the following query methods to check the appropriate\n size:\n - `Highs_getPresolvedNumCol`\n - `Highs_getPresolvedNumRow`\n - `Highs_getPresolvedNumNz`\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getPresolvedLp( + highs: *const ::std::os::raw::c_void, + a_format: HighsInt, + num_col: *mut HighsInt, + num_row: *mut HighsInt, + num_nz: *mut HighsInt, + sense: *mut HighsInt, + offset: *mut f64, + col_cost: *mut f64, + col_lower: *mut f64, + col_upper: *mut f64, + row_lower: *mut f64, + row_upper: *mut f64, + a_start: *mut HighsInt, + a_index: *mut HighsInt, + a_value: *mut f64, + integrality: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set a primal (and possibly dual) solution as a starting point, then run\n crossover to compute a basic feasible solution.\n\n @param highs A pointer to the Highs instance.\n @param num_col The number of variables.\n @param num_row The number of rows.\n @param col_value An array of length [num_col] with optimal primal solution\n for each column.\n @param col_dual An array of length [num_col] with optimal dual solution for\n each column. May be `NULL`, in which case no dual solution\n is passed.\n @param row_dual An array of length [num_row] with optimal dual solution for\n each row. . May be `NULL`, in which case no dual solution\n is passed.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_crossover( + highs: *mut ::std::os::raw::c_void, + num_col: HighsInt, + num_row: HighsInt, + col_value: *const f64, + col_dual: *const f64, + row_dual: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Compute the ranging information for all costs and bounds. For\n nonbasic variables the ranging information is relative to the\n active bound. For basic variables the ranging information relates\n to...\n\n For any values that are not required, pass NULL.\n\n @param highs A pointer to the Highs instance.\n @param col_cost_up_value The upper range of the cost value\n @param col_cost_up_objective The objective at the upper cost range\n @param col_cost_up_in_var The variable entering the basis at the upper\n cost range\n @param col_cost_up_ou_var The variable leaving the basis at the upper\n cost range\n @param col_cost_dn_value The lower range of the cost value\n @param col_cost_dn_objective The objective at the lower cost range\n @param col_cost_dn_in_var The variable entering the basis at the lower\n cost range\n @param col_cost_dn_ou_var The variable leaving the basis at the lower\n cost range\n @param col_bound_up_value The upper range of the column bound value\n @param col_bound_up_objective The objective at the upper column bound range\n @param col_bound_up_in_var The variable entering the basis at the upper\n column bound range\n @param col_bound_up_ou_var The variable leaving the basis at the upper\n column bound range\n @param col_bound_dn_value The lower range of the column bound value\n @param col_bound_dn_objective The objective at the lower column bound range\n @param col_bound_dn_in_var The variable entering the basis at the lower\n column bound range\n @param col_bound_dn_ou_var The variable leaving the basis at the lower\n column bound range\n @param row_bound_up_value The upper range of the row bound value\n @param row_bound_up_objective The objective at the upper row bound range\n @param row_bound_up_in_var The variable entering the basis at the upper\n row bound range\n @param row_bound_up_ou_var The variable leaving the basis at the upper row\n bound range\n @param row_bound_dn_value The lower range of the row bound value\n @param row_bound_dn_objective The objective at the lower row bound range\n @param row_bound_dn_in_var The variable entering the basis at the lower\n row bound range\n @param row_bound_dn_ou_var The variable leaving the basis at the lower row\n bound range\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_getRanging( + highs: *mut ::std::os::raw::c_void, + col_cost_up_value: *mut f64, + col_cost_up_objective: *mut f64, + col_cost_up_in_var: *mut HighsInt, + col_cost_up_ou_var: *mut HighsInt, + col_cost_dn_value: *mut f64, + col_cost_dn_objective: *mut f64, + col_cost_dn_in_var: *mut HighsInt, + col_cost_dn_ou_var: *mut HighsInt, + col_bound_up_value: *mut f64, + col_bound_up_objective: *mut f64, + col_bound_up_in_var: *mut HighsInt, + col_bound_up_ou_var: *mut HighsInt, + col_bound_dn_value: *mut f64, + col_bound_dn_objective: *mut f64, + col_bound_dn_in_var: *mut HighsInt, + col_bound_dn_ou_var: *mut HighsInt, + row_bound_up_value: *mut f64, + row_bound_up_objective: *mut f64, + row_bound_up_in_var: *mut HighsInt, + row_bound_up_ou_var: *mut HighsInt, + row_bound_dn_value: *mut f64, + row_bound_dn_objective: *mut f64, + row_bound_dn_in_var: *mut HighsInt, + row_bound_dn_ou_var: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Compute the solution corresponding to a (possibly weighted) sum of\n (allowable) infeasibilities in an LP/MIP.\n\n If local penalties are not defined, pass NULL, and the global\n penalty will be used. Negative penalty values imply that the bound\n or RHS value cannot be violated\n\n @param highs A pointer to the Highs instance.\n @param const double global_lower_penalty The penalty for violating lower\n bounds on variables\n @param const double global_upper_penalty The penalty for violating upper\n bounds on variables\n @param const double global_rhs_penalty The penalty for violating constraint\n RHS values\n @param const double* local_lower_penalty The penalties for violating specific\n lower bounds on variables\n @param const double* local_upper_penalty The penalties for violating specific\n upper bounds on variables\n @param const double* local_rhs_penalty The penalties for violating specific\n constraint RHS values\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_feasibilityRelaxation( + highs: *mut ::std::os::raw::c_void, + global_lower_penalty: f64, + global_upper_penalty: f64, + global_rhs_penalty: f64, + local_lower_penalty: *const f64, + local_upper_penalty: *const f64, + local_rhs_penalty: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Releases all resources held by the global scheduler instance.\n\n It is not thread-safe to call this function while calling `Highs_run` or one\n of the `Highs_XXXcall` methods on any other Highs instance in any thread.\n\n After this function has terminated, it is guaranteed that eventually all\n previously created scheduler threads will terminate and allocated memory will\n be released.\n\n After this function has returned, the option value for the number of threads\n may be altered to a new value before the next call to `Highs_run` or one of\n the `Highs_XXXcall` methods.\n\n @param blocking If the `blocking` parameter has a nonzero value, then this\n function will not return until all memory is freed, which\n might be desirable when debugging heap memory, but it\n requires the calling thread to wait for all scheduler\n threads to wake-up which is usually not necessary.\n\n @returns No status is returned since the function call cannot fail. Calling\n this function while any Highs instance is in use on any thread is\n undefined behavior and may cause crashes, but cannot be detected and hence\n is fully in the callers responsibility."] + pub fn Highs_resetGlobalScheduler(blocking: HighsInt); +} +unsafe extern "C" { + #[doc = " Get a void* pointer to a callback data item\n\n @param data_out A pointer to the HighsCallbackDataOut instance.\n @param item_name The name of the item.\n\n @returns A void* pointer to the callback data item, or NULL if item_name not\n valid"] + pub fn Highs_getCallbackDataOutItem( + data_out: *const HighsCallbackDataOut, + item_name: *const ::std::os::raw::c_char, + ) -> *const ::std::os::raw::c_void; +} +unsafe extern "C" { + #[doc = " Set a solution within a callback by passing a subset of the values.\n\n For any values that are unavailable/unknown, pass kHighsUndefined.\n\n @param data_in A pointer to the callback input data instance.\n @param num_entries Number of variables in the set\n @param value An array of length [num_entries <= num_col] with\n column solution values.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_setCallbackSolution( + data_in: *mut HighsCallbackDataIn, + num_entries: HighsInt, + value: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Set a partial primal solution by passing values for a set of variables,\n within a valid callback.\n\n @param data_in A pointer to the callback input data instance.\n @param num_entries Number of variables in the set\n @param index Indices of variables in the set\n @param value Values of variables in the set\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_setCallbackSparseSolution( + data_in: *mut HighsCallbackDataIn, + num_entries: HighsInt, + index: *const HighsInt, + value: *const f64, + ) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Finds a feasible solution for a given (partial) primal user solution,\n within a valid callback.\n\n On success, the user solution is updated within the callback input data\n instance.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] + pub fn Highs_repairCallbackSolution(data_in: *mut HighsCallbackDataIn) -> HighsInt; +} +unsafe extern "C" { + #[doc = " Return the HiGHS compilation date.\n\n @returns Thse HiGHS compilation date."] + pub fn Highs_compilationDate() -> *const ::std::os::raw::c_char; +} +pub const HighsStatuskError: HighsInt = -1; +pub const HighsStatuskOk: HighsInt = 0; +pub const HighsStatuskWarning: HighsInt = 1; +unsafe extern "C" { + pub fn Highs_call( + num_col: HighsInt, + num_row: HighsInt, + num_nz: HighsInt, + col_cost: *const f64, + col_lower: *const f64, + col_upper: *const f64, + row_lower: *const f64, + row_upper: *const f64, + a_start: *const HighsInt, + a_index: *const HighsInt, + a_value: *const f64, + col_value: *mut f64, + col_dual: *mut f64, + row_value: *mut f64, + row_dual: *mut f64, + col_basis_status: *mut HighsInt, + row_basis_status: *mut HighsInt, + model_status: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_runQuiet(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_setHighsLogfile( + highs: *mut ::std::os::raw::c_void, + logfile: *const ::std::os::raw::c_void, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_setHighsOutput( + highs: *mut ::std::os::raw::c_void, + outputfile: *const ::std::os::raw::c_void, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getIterationCount(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getSimplexIterationCount(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_setHighsBoolOptionValue( + highs: *mut ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_setHighsIntOptionValue( + highs: *mut ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_setHighsDoubleOptionValue( + highs: *mut ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: f64, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_setHighsStringOptionValue( + highs: *mut ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_setHighsOptionValue( + highs: *mut ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getHighsBoolOptionValue( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getHighsIntOptionValue( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getHighsDoubleOptionValue( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getHighsStringOptionValue( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *mut ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getHighsOptionType( + highs: *const ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + type_: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_resetHighsOptions(highs: *mut ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getHighsIntInfoValue( + highs: *const ::std::os::raw::c_void, + info: *const ::std::os::raw::c_char, + value: *mut HighsInt, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getHighsDoubleInfoValue( + highs: *const ::std::os::raw::c_void, + info: *const ::std::os::raw::c_char, + value: *mut f64, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getNumCols(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getNumRows(highs: *const ::std::os::raw::c_void) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getHighsInfinity(highs: *const ::std::os::raw::c_void) -> f64; +} +unsafe extern "C" { + pub fn Highs_getHighsRunTime(highs: *const ::std::os::raw::c_void) -> f64; +} +unsafe extern "C" { + pub fn Highs_setOptionValue( + highs: *mut ::std::os::raw::c_void, + option: *const ::std::os::raw::c_char, + value: *const ::std::os::raw::c_char, + ) -> HighsInt; +} +unsafe extern "C" { + pub fn Highs_getScaledModelStatus(highs: *const ::std::os::raw::c_void) -> HighsInt; +} diff --git a/ext/highs-sys/src/lib.rs b/ext/highs-sys/src/lib.rs new file mode 100644 index 00000000..f03061a7 --- /dev/null +++ b/ext/highs-sys/src/lib.rs @@ -0,0 +1,35 @@ +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +include!("c_bindings.rs"); + +pub const MODEL_STATUS_NOTSET: HighsInt = 0; +pub const MODEL_STATUS_LOAD_ERROR: HighsInt = 1; +pub const MODEL_STATUS_MODEL_ERROR: HighsInt = 2; +pub const MODEL_STATUS_PRESOLVE_ERROR: HighsInt = 3; +pub const MODEL_STATUS_SOLVE_ERROR: HighsInt = 4; +pub const MODEL_STATUS_POSTSOLVE_ERROR: HighsInt = 5; +pub const MODEL_STATUS_MODEL_EMPTY: HighsInt = 6; +pub const MODEL_STATUS_OPTIMAL: HighsInt = 7; +pub const MODEL_STATUS_INFEASIBLE: HighsInt = 8; +pub const MODEL_STATUS_UNBOUNDED_OR_INFEASIBLE: HighsInt = 9; +pub const MODEL_STATUS_UNBOUNDED: HighsInt = 10; +pub const MODEL_STATUS_OBJECTIVE_BOUND: HighsInt = 11; +pub const MODEL_STATUS_OBJECTIVE_TARGET: HighsInt = 12; +pub const MODEL_STATUS_REACHED_TIME_LIMIT: HighsInt = 13; +pub const MODEL_STATUS_REACHED_ITERATION_LIMIT: HighsInt = 14; +pub const MODEL_STATUS_UNKNOWN: HighsInt = 15; +pub const MODEL_STATUS_MIN: HighsInt = MODEL_STATUS_NOTSET; +pub const MODEL_STATUS_MAX: HighsInt = MODEL_STATUS_UNKNOWN; + +pub const STATUS_OK: HighsInt = 0; +pub const STATUS_WARNING: HighsInt = 1; +pub const STATUS_ERROR: HighsInt = -1; + +pub const MATRIX_FORMAT_NONE: HighsInt = 0; +pub const MATRIX_FORMAT_COLUMN_WISE: HighsInt = 1; +pub const MATRIX_FORMAT_ROW_WISE: HighsInt = 2; + +pub const OBJECTIVE_SENSE_MINIMIZE: HighsInt = 1; +pub const OBJECTIVE_SENSE_MAXIMIZE: HighsInt = -1; diff --git a/ext/highs-sys/tests/test_highs_call.rs b/ext/highs-sys/tests/test_highs_call.rs new file mode 100644 index 00000000..fbb8fa42 --- /dev/null +++ b/ext/highs-sys/tests/test_highs_call.rs @@ -0,0 +1,115 @@ +use highs_sys::*; +use std::convert::TryInto; + +#[test] +fn highs_call() { + // This illustrates the use of Highs_call, the simple C interface to + // HiGHS. It's designed to solve the general LP problem + // + // Min c^Tx subject to L <= Ax <= U; l <= x <= u + // + // where A is a matrix with m rows and n columns + // + // The scalar n is numcol + // The scalar m is numrow + // + // The vector c is colcost + // The vector l is collower + // The vector u is colupper + // The vector L is rowlower + // The vector U is rowupper + // + // The matrix A is represented in packed column-wise form: only its + // nonzeros are stored + // + // * The number of nonzeros in A is nnz + // + // * The row indices of the nonnzeros in A are stored column-by-column + // in aindex + // + // * The values of the nonnzeros in A are stored column-by-column in + // avalue + // + // * The position in aindex/avalue of the index/value of the first + // nonzero in each column is stored in astart + // + // Note that astart[0] must be zero + // + // After a successful call to Highs_call, the primal and dual + // solution, and the simplex basis are returned as follows + // + // The vector x is colvalue + // The vector Ax is rowvalue + // The vector of dual values for the variables x is coldual + // The vector of dual values for the variables Ax is rowdual + // The basic/nonbasic status of the variables x is colbasisstatus + // The basic/nonbasic status of the variables Ax is rowbasisstatus + // + // The status of the solution obtained is modelstatus + // + // To solve maximization problems, the values in c must be negated + // + // The use of Highs_call is illustrated for the LP + // + // Min f = 2x_0 + 3x_1 + // s.t. x_1 <= 6 + // 10 <= x_0 + 2x_1 <= 14 + // 8 <= 2x_0 + x_1 + // 0 <= x_0 <= 3; 1 <= x_1 + + let numcol: usize = 2; + let numrow: usize = 3; + let nnz: usize = 5; + + // Define the column costs, lower bounds and upper bounds + let colcost: &[f64] = &[2.0, 3.0]; + let collower: &[f64] = &[0.0, 1.0]; + let colupper: &[f64] = &[3.0, 1.0e30]; + // Define the row lower bounds and upper bounds + let rowlower: &[f64] = &[-1.0e30, 10.0, 8.0]; + let rowupper: &[f64] = &[6.0, 14.0, 1.0e30]; + // Define the constraint matrix column-wise + let astart: &[HighsInt] = &[0, 2]; + let aindex: &[HighsInt] = &[1, 2, 0, 1, 2]; + let avalue: &[f64] = &[1.0, 2.0, 1.0, 2.0, 1.0]; + + let colvalue: &mut [f64] = &mut vec![0.; numcol]; + let coldual: &mut [f64] = &mut vec![0.; numcol]; + let rowvalue: &mut [f64] = &mut vec![0.; numrow]; + let rowdual: &mut [f64] = &mut vec![0.; numrow]; + + let colbasisstatus: &mut [HighsInt] = &mut vec![0; numcol]; + let rowbasisstatus: &mut [HighsInt] = &mut vec![0; numrow]; + + let modelstatus: &mut HighsInt = &mut 0; + let offset = 0.0; + + let status: HighsInt = unsafe { + Highs_lpCall( + numcol.try_into().unwrap(), + numrow.try_into().unwrap(), + nnz.try_into().unwrap(), + MATRIX_FORMAT_COLUMN_WISE, + OBJECTIVE_SENSE_MINIMIZE, + offset, + colcost.as_ptr(), + collower.as_ptr(), + colupper.as_ptr(), + rowlower.as_ptr(), + rowupper.as_ptr(), + astart.as_ptr(), + aindex.as_ptr(), + avalue.as_ptr(), + colvalue.as_mut_ptr(), + coldual.as_mut_ptr(), + rowvalue.as_mut_ptr(), + rowdual.as_mut_ptr(), + colbasisstatus.as_mut_ptr(), + rowbasisstatus.as_mut_ptr(), + modelstatus, + ) + }; + + assert_eq!(status, STATUS_OK); + assert_eq!(colvalue, &[2., 4.]); +} diff --git a/ext/highs-sys/tests/test_highs_functions.rs b/ext/highs-sys/tests/test_highs_functions.rs new file mode 100644 index 00000000..a324fa26 --- /dev/null +++ b/ext/highs-sys/tests/test_highs_functions.rs @@ -0,0 +1,134 @@ +use std::convert::TryInto; +use std::ffi::CString; + +use highs_sys::*; + +fn c(n: usize) -> HighsInt { + n.try_into().unwrap() +} + +fn ptr(a: &mut [T]) -> *mut T { + a.as_mut_ptr() +} + +#[test] +fn highs_functions() { + unsafe { + // Form and solve the LP + // Max f = 2x_0 + 3x_1 + // s.t. x_1 <= 6 + // 10 <= x_0 + 2x_1 <= 14 + // 8 <= 2x_0 + x_1 + // 0 <= x_0 <= 3; 1 <= x_1 + + let highs = Highs_create(); + + // Solving the problem without printing to the standard output + let option_name = CString::new("output_flag").unwrap(); + Highs_setBoolOptionValue(highs, option_name.as_ptr(), 0); + + let numcol: usize = 2; + let numrow: usize = 3; + let nnz: usize = 5; + + let inf = Highs_getInfinity(highs); + + // Define the column costs, lower bounds and upper bounds + let colcost: &mut [f64] = &mut [2.0, 3.0]; + let collower: &mut [f64] = &mut [0.0, 1.0]; + let colupper: &mut [f64] = &mut [3.0, inf]; + // Define the row lower bounds and upper bounds + let rowlower: &mut [f64] = &mut [-inf, 10.0, 8.0]; + let rowupper: &mut [f64] = &mut [6.0, 14.0, inf]; + + // Define the constraint matrix row-wise, as it is added to the LP + // with the rows + let arstart: &mut [HighsInt] = &mut [0, 1, 3]; + let arindex: &mut [HighsInt] = &mut [1, 0, 1, 0, 1]; + let arvalue: &mut [f64] = &mut [1.0, 1.0, 2.0, 2.0, 1.0]; + + use std::ptr::null; + + // Add two columns to the empty LP + let success = Highs_addCols( + highs, + c(numcol), + ptr(colcost), + ptr(collower), + ptr(colupper), + 0, + null(), + null(), + null(), + ); + assert_eq!(STATUS_OK, success, "addCols"); + // Add three rows to the 2-column LP + let success = Highs_addRows( + highs, + c(numrow), + ptr(rowlower), + ptr(rowupper), + c(nnz), + ptr(arstart), + ptr(arindex), + ptr(arvalue), + ); + assert_eq!(STATUS_OK, success, "addRows"); + + let success = Highs_changeObjectiveSense(highs, OBJECTIVE_SENSE_MAXIMIZE); + assert_eq!(success, STATUS_OK); + + let simplex_scale_strategy = 3; + let option_name = CString::new("simplex_scale_strategy").unwrap(); + Highs_setIntOptionValue(highs, option_name.as_ptr(), simplex_scale_strategy); + + let status = Highs_run(highs); + assert_eq!(status, STATUS_OK); + + let model_status = Highs_getModelStatus(highs); + assert_eq!(model_status, MODEL_STATUS_OPTIMAL); + + let mut objective_function_value = 0.; + let info_name = CString::new("objective_function_value").unwrap(); + Highs_getHighsDoubleInfoValue( + highs, + info_name.as_ptr(), + (&mut objective_function_value) as *mut f64, + ); + assert_eq!(objective_function_value, 2. * 3. + 3. * 5.5); + + let colvalue: &mut [f64] = &mut vec![0.; numcol]; + let coldual: &mut [f64] = &mut vec![0.; numcol]; + let rowvalue: &mut [f64] = &mut vec![0.; numrow]; + let rowdual: &mut [f64] = &mut vec![0.; numrow]; + + // Get the primal and dual solution + Highs_getSolution( + highs, + ptr(colvalue), + ptr(coldual), + ptr(rowvalue), + ptr(rowdual), + ); + assert_eq!(colvalue, &[3.0, 5.5]); + + let colbasisstatus: &mut [HighsInt] = &mut vec![0; numcol]; + let rowbasisstatus: &mut [HighsInt] = &mut vec![0; numrow]; + // Get the basis + Highs_getBasis(highs, ptr(colbasisstatus), ptr(rowbasisstatus)); + assert_eq!(colbasisstatus, &[2, 1]); + + Highs_destroy(highs); + } +} + +#[cfg(not(target_os = "windows"))] // broken on windows +#[test] +fn highs_functions_multithread() { + let threads: Vec<_> = (0..128) + .map(|_| std::thread::spawn(highs_functions)) + .collect(); + for t in threads { + t.join().expect("Thread should not panic"); + } +} diff --git a/ext/highs-sys/wrapper.h b/ext/highs-sys/wrapper.h new file mode 100644 index 00000000..c4765bc0 --- /dev/null +++ b/ext/highs-sys/wrapper.h @@ -0,0 +1 @@ +#include From 9983f4119dec541338e77105b4e8ad700d92ab4e Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Mon, 20 Oct 2025 21:46:07 +0000 Subject: [PATCH 08/22] Regenerate bindings --- ext/highs-sys/build.rs | 8 +- ext/highs-sys/src/c_bindings.rs | 493 -------------------------------- 2 files changed, 5 insertions(+), 496 deletions(-) diff --git a/ext/highs-sys/build.rs b/ext/highs-sys/build.rs index 3d4c55d1..1f03c465 100644 --- a/ext/highs-sys/build.rs +++ b/ext/highs-sys/build.rs @@ -1,7 +1,8 @@ use std::env; -// use std::path::Path; /* +use std::path::Path; + fn generate_bindings<'a>(include_paths: impl Iterator) { // The bindgen::Builder is the main entry point // to bindgen, and lets you build up options for @@ -17,6 +18,7 @@ fn generate_bindings<'a>(include_paths: impl Iterator) { // Tell cargo to invalidate the built crate whenever any of the // included header files changed. .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) + .allowlist_function("^Highs.*") // Finish the builder and generate the bindings. .generate() // Unwrap the Result and panic on failure. @@ -47,8 +49,8 @@ fn build() -> bool { .define("ZLIB", if cfg!(feature = "libz") { "ON" } else { "OFF" }) .build(); - // let include_path = dst.join("include").join("highs"); - // generate_bindings(Some(include_path.as_path()).into_iter()); + //let include_path = dst.join("include").join("highs"); + //generate_bindings(Some(include_path.as_path()).into_iter()); println!("cargo:rustc-link-search=native={}/lib", dst.display()); println!("cargo:rustc-link-search=native={}/lib64", dst.display()); diff --git a/ext/highs-sys/src/c_bindings.rs b/ext/highs-sys/src/c_bindings.rs index a2c514a0..2d3a1727 100644 --- a/ext/highs-sys/src/c_bindings.rs +++ b/ext/highs-sys/src/c_bindings.rs @@ -1,405 +1,6 @@ /* automatically generated by rust-bindgen 0.71.1 */ -pub const _STDINT_H: u32 = 1; -pub const _FEATURES_H: u32 = 1; -pub const _DEFAULT_SOURCE: u32 = 1; -pub const __GLIBC_USE_ISOC2X: u32 = 0; -pub const __USE_ISOC11: u32 = 1; -pub const __USE_ISOC99: u32 = 1; -pub const __USE_ISOC95: u32 = 1; -pub const __USE_POSIX_IMPLICITLY: u32 = 1; -pub const _POSIX_SOURCE: u32 = 1; -pub const _POSIX_C_SOURCE: u32 = 200809; -pub const __USE_POSIX: u32 = 1; -pub const __USE_POSIX2: u32 = 1; -pub const __USE_POSIX199309: u32 = 1; -pub const __USE_POSIX199506: u32 = 1; -pub const __USE_XOPEN2K: u32 = 1; -pub const __USE_XOPEN2K8: u32 = 1; -pub const _ATFILE_SOURCE: u32 = 1; -pub const __WORDSIZE: u32 = 64; -pub const __WORDSIZE_TIME64_COMPAT32: u32 = 1; -pub const __SYSCALL_WORDSIZE: u32 = 64; -pub const __TIMESIZE: u32 = 64; -pub const __USE_MISC: u32 = 1; -pub const __USE_ATFILE: u32 = 1; -pub const __USE_FORTIFY_LEVEL: u32 = 0; -pub const __GLIBC_USE_DEPRECATED_GETS: u32 = 0; -pub const __GLIBC_USE_DEPRECATED_SCANF: u32 = 0; -pub const _STDC_PREDEF_H: u32 = 1; -pub const __STDC_IEC_559__: u32 = 1; -pub const __STDC_IEC_60559_BFP__: u32 = 201404; -pub const __STDC_IEC_559_COMPLEX__: u32 = 1; -pub const __STDC_IEC_60559_COMPLEX__: u32 = 201404; -pub const __STDC_ISO_10646__: u32 = 201706; -pub const __GNU_LIBRARY__: u32 = 6; -pub const __GLIBC__: u32 = 2; -pub const __GLIBC_MINOR__: u32 = 35; -pub const _SYS_CDEFS_H: u32 = 1; -pub const __glibc_c99_flexarr_available: u32 = 1; -pub const __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI: u32 = 0; -pub const __HAVE_GENERIC_SELECTION: u32 = 1; -pub const __GLIBC_USE_LIB_EXT2: u32 = 0; -pub const __GLIBC_USE_IEC_60559_BFP_EXT: u32 = 0; -pub const __GLIBC_USE_IEC_60559_BFP_EXT_C2X: u32 = 0; -pub const __GLIBC_USE_IEC_60559_EXT: u32 = 0; -pub const __GLIBC_USE_IEC_60559_FUNCS_EXT: u32 = 0; -pub const __GLIBC_USE_IEC_60559_FUNCS_EXT_C2X: u32 = 0; -pub const __GLIBC_USE_IEC_60559_TYPES_EXT: u32 = 0; -pub const _BITS_TYPES_H: u32 = 1; -pub const _BITS_TYPESIZES_H: u32 = 1; -pub const __OFF_T_MATCHES_OFF64_T: u32 = 1; -pub const __INO_T_MATCHES_INO64_T: u32 = 1; -pub const __RLIM_T_MATCHES_RLIM64_T: u32 = 1; -pub const __STATFS_MATCHES_STATFS64: u32 = 1; -pub const __KERNEL_OLD_TIMEVAL_MATCHES_TIMEVAL64: u32 = 1; -pub const __FD_SETSIZE: u32 = 1024; -pub const _BITS_TIME64_H: u32 = 1; -pub const _BITS_WCHAR_H: u32 = 1; -pub const _BITS_STDINT_INTN_H: u32 = 1; -pub const _BITS_STDINT_UINTN_H: u32 = 1; -pub const INT8_MIN: i32 = -128; -pub const INT16_MIN: i32 = -32768; -pub const INT32_MIN: i32 = -2147483648; -pub const INT8_MAX: u32 = 127; -pub const INT16_MAX: u32 = 32767; -pub const INT32_MAX: u32 = 2147483647; -pub const UINT8_MAX: u32 = 255; -pub const UINT16_MAX: u32 = 65535; -pub const UINT32_MAX: u32 = 4294967295; -pub const INT_LEAST8_MIN: i32 = -128; -pub const INT_LEAST16_MIN: i32 = -32768; -pub const INT_LEAST32_MIN: i32 = -2147483648; -pub const INT_LEAST8_MAX: u32 = 127; -pub const INT_LEAST16_MAX: u32 = 32767; -pub const INT_LEAST32_MAX: u32 = 2147483647; -pub const UINT_LEAST8_MAX: u32 = 255; -pub const UINT_LEAST16_MAX: u32 = 65535; -pub const UINT_LEAST32_MAX: u32 = 4294967295; -pub const INT_FAST8_MIN: i32 = -128; -pub const INT_FAST16_MIN: i64 = -9223372036854775808; -pub const INT_FAST32_MIN: i64 = -9223372036854775808; -pub const INT_FAST8_MAX: u32 = 127; -pub const INT_FAST16_MAX: u64 = 9223372036854775807; -pub const INT_FAST32_MAX: u64 = 9223372036854775807; -pub const UINT_FAST8_MAX: u32 = 255; -pub const UINT_FAST16_MAX: i32 = -1; -pub const UINT_FAST32_MAX: i32 = -1; -pub const INTPTR_MIN: i64 = -9223372036854775808; -pub const INTPTR_MAX: u64 = 9223372036854775807; -pub const UINTPTR_MAX: i32 = -1; -pub const PTRDIFF_MIN: i64 = -9223372036854775808; -pub const PTRDIFF_MAX: u64 = 9223372036854775807; -pub const SIG_ATOMIC_MIN: i32 = -2147483648; -pub const SIG_ATOMIC_MAX: u32 = 2147483647; -pub const SIZE_MAX: i32 = -1; -pub const WINT_MIN: u32 = 0; -pub const WINT_MAX: u32 = 4294967295; -pub const _INTTYPES_H: u32 = 1; -pub const ____gwchar_t_defined: u32 = 1; -pub const __PRI64_PREFIX: &[u8; 2] = b"l\0"; -pub const __PRIPTR_PREFIX: &[u8; 2] = b"l\0"; -pub const PRId8: &[u8; 2] = b"d\0"; -pub const PRId16: &[u8; 2] = b"d\0"; -pub const PRId32: &[u8; 2] = b"d\0"; -pub const PRId64: &[u8; 3] = b"ld\0"; -pub const PRIdLEAST8: &[u8; 2] = b"d\0"; -pub const PRIdLEAST16: &[u8; 2] = b"d\0"; -pub const PRIdLEAST32: &[u8; 2] = b"d\0"; -pub const PRIdLEAST64: &[u8; 3] = b"ld\0"; -pub const PRIdFAST8: &[u8; 2] = b"d\0"; -pub const PRIdFAST16: &[u8; 3] = b"ld\0"; -pub const PRIdFAST32: &[u8; 3] = b"ld\0"; -pub const PRIdFAST64: &[u8; 3] = b"ld\0"; -pub const PRIi8: &[u8; 2] = b"i\0"; -pub const PRIi16: &[u8; 2] = b"i\0"; -pub const PRIi32: &[u8; 2] = b"i\0"; -pub const PRIi64: &[u8; 3] = b"li\0"; -pub const PRIiLEAST8: &[u8; 2] = b"i\0"; -pub const PRIiLEAST16: &[u8; 2] = b"i\0"; -pub const PRIiLEAST32: &[u8; 2] = b"i\0"; -pub const PRIiLEAST64: &[u8; 3] = b"li\0"; -pub const PRIiFAST8: &[u8; 2] = b"i\0"; -pub const PRIiFAST16: &[u8; 3] = b"li\0"; -pub const PRIiFAST32: &[u8; 3] = b"li\0"; -pub const PRIiFAST64: &[u8; 3] = b"li\0"; -pub const PRIo8: &[u8; 2] = b"o\0"; -pub const PRIo16: &[u8; 2] = b"o\0"; -pub const PRIo32: &[u8; 2] = b"o\0"; -pub const PRIo64: &[u8; 3] = b"lo\0"; -pub const PRIoLEAST8: &[u8; 2] = b"o\0"; -pub const PRIoLEAST16: &[u8; 2] = b"o\0"; -pub const PRIoLEAST32: &[u8; 2] = b"o\0"; -pub const PRIoLEAST64: &[u8; 3] = b"lo\0"; -pub const PRIoFAST8: &[u8; 2] = b"o\0"; -pub const PRIoFAST16: &[u8; 3] = b"lo\0"; -pub const PRIoFAST32: &[u8; 3] = b"lo\0"; -pub const PRIoFAST64: &[u8; 3] = b"lo\0"; -pub const PRIu8: &[u8; 2] = b"u\0"; -pub const PRIu16: &[u8; 2] = b"u\0"; -pub const PRIu32: &[u8; 2] = b"u\0"; -pub const PRIu64: &[u8; 3] = b"lu\0"; -pub const PRIuLEAST8: &[u8; 2] = b"u\0"; -pub const PRIuLEAST16: &[u8; 2] = b"u\0"; -pub const PRIuLEAST32: &[u8; 2] = b"u\0"; -pub const PRIuLEAST64: &[u8; 3] = b"lu\0"; -pub const PRIuFAST8: &[u8; 2] = b"u\0"; -pub const PRIuFAST16: &[u8; 3] = b"lu\0"; -pub const PRIuFAST32: &[u8; 3] = b"lu\0"; -pub const PRIuFAST64: &[u8; 3] = b"lu\0"; -pub const PRIx8: &[u8; 2] = b"x\0"; -pub const PRIx16: &[u8; 2] = b"x\0"; -pub const PRIx32: &[u8; 2] = b"x\0"; -pub const PRIx64: &[u8; 3] = b"lx\0"; -pub const PRIxLEAST8: &[u8; 2] = b"x\0"; -pub const PRIxLEAST16: &[u8; 2] = b"x\0"; -pub const PRIxLEAST32: &[u8; 2] = b"x\0"; -pub const PRIxLEAST64: &[u8; 3] = b"lx\0"; -pub const PRIxFAST8: &[u8; 2] = b"x\0"; -pub const PRIxFAST16: &[u8; 3] = b"lx\0"; -pub const PRIxFAST32: &[u8; 3] = b"lx\0"; -pub const PRIxFAST64: &[u8; 3] = b"lx\0"; -pub const PRIX8: &[u8; 2] = b"X\0"; -pub const PRIX16: &[u8; 2] = b"X\0"; -pub const PRIX32: &[u8; 2] = b"X\0"; -pub const PRIX64: &[u8; 3] = b"lX\0"; -pub const PRIXLEAST8: &[u8; 2] = b"X\0"; -pub const PRIXLEAST16: &[u8; 2] = b"X\0"; -pub const PRIXLEAST32: &[u8; 2] = b"X\0"; -pub const PRIXLEAST64: &[u8; 3] = b"lX\0"; -pub const PRIXFAST8: &[u8; 2] = b"X\0"; -pub const PRIXFAST16: &[u8; 3] = b"lX\0"; -pub const PRIXFAST32: &[u8; 3] = b"lX\0"; -pub const PRIXFAST64: &[u8; 3] = b"lX\0"; -pub const PRIdMAX: &[u8; 3] = b"ld\0"; -pub const PRIiMAX: &[u8; 3] = b"li\0"; -pub const PRIoMAX: &[u8; 3] = b"lo\0"; -pub const PRIuMAX: &[u8; 3] = b"lu\0"; -pub const PRIxMAX: &[u8; 3] = b"lx\0"; -pub const PRIXMAX: &[u8; 3] = b"lX\0"; -pub const PRIdPTR: &[u8; 3] = b"ld\0"; -pub const PRIiPTR: &[u8; 3] = b"li\0"; -pub const PRIoPTR: &[u8; 3] = b"lo\0"; -pub const PRIuPTR: &[u8; 3] = b"lu\0"; -pub const PRIxPTR: &[u8; 3] = b"lx\0"; -pub const PRIXPTR: &[u8; 3] = b"lX\0"; -pub const SCNd8: &[u8; 4] = b"hhd\0"; -pub const SCNd16: &[u8; 3] = b"hd\0"; -pub const SCNd32: &[u8; 2] = b"d\0"; -pub const SCNd64: &[u8; 3] = b"ld\0"; -pub const SCNdLEAST8: &[u8; 4] = b"hhd\0"; -pub const SCNdLEAST16: &[u8; 3] = b"hd\0"; -pub const SCNdLEAST32: &[u8; 2] = b"d\0"; -pub const SCNdLEAST64: &[u8; 3] = b"ld\0"; -pub const SCNdFAST8: &[u8; 4] = b"hhd\0"; -pub const SCNdFAST16: &[u8; 3] = b"ld\0"; -pub const SCNdFAST32: &[u8; 3] = b"ld\0"; -pub const SCNdFAST64: &[u8; 3] = b"ld\0"; -pub const SCNi8: &[u8; 4] = b"hhi\0"; -pub const SCNi16: &[u8; 3] = b"hi\0"; -pub const SCNi32: &[u8; 2] = b"i\0"; -pub const SCNi64: &[u8; 3] = b"li\0"; -pub const SCNiLEAST8: &[u8; 4] = b"hhi\0"; -pub const SCNiLEAST16: &[u8; 3] = b"hi\0"; -pub const SCNiLEAST32: &[u8; 2] = b"i\0"; -pub const SCNiLEAST64: &[u8; 3] = b"li\0"; -pub const SCNiFAST8: &[u8; 4] = b"hhi\0"; -pub const SCNiFAST16: &[u8; 3] = b"li\0"; -pub const SCNiFAST32: &[u8; 3] = b"li\0"; -pub const SCNiFAST64: &[u8; 3] = b"li\0"; -pub const SCNu8: &[u8; 4] = b"hhu\0"; -pub const SCNu16: &[u8; 3] = b"hu\0"; -pub const SCNu32: &[u8; 2] = b"u\0"; -pub const SCNu64: &[u8; 3] = b"lu\0"; -pub const SCNuLEAST8: &[u8; 4] = b"hhu\0"; -pub const SCNuLEAST16: &[u8; 3] = b"hu\0"; -pub const SCNuLEAST32: &[u8; 2] = b"u\0"; -pub const SCNuLEAST64: &[u8; 3] = b"lu\0"; -pub const SCNuFAST8: &[u8; 4] = b"hhu\0"; -pub const SCNuFAST16: &[u8; 3] = b"lu\0"; -pub const SCNuFAST32: &[u8; 3] = b"lu\0"; -pub const SCNuFAST64: &[u8; 3] = b"lu\0"; -pub const SCNo8: &[u8; 4] = b"hho\0"; -pub const SCNo16: &[u8; 3] = b"ho\0"; -pub const SCNo32: &[u8; 2] = b"o\0"; -pub const SCNo64: &[u8; 3] = b"lo\0"; -pub const SCNoLEAST8: &[u8; 4] = b"hho\0"; -pub const SCNoLEAST16: &[u8; 3] = b"ho\0"; -pub const SCNoLEAST32: &[u8; 2] = b"o\0"; -pub const SCNoLEAST64: &[u8; 3] = b"lo\0"; -pub const SCNoFAST8: &[u8; 4] = b"hho\0"; -pub const SCNoFAST16: &[u8; 3] = b"lo\0"; -pub const SCNoFAST32: &[u8; 3] = b"lo\0"; -pub const SCNoFAST64: &[u8; 3] = b"lo\0"; -pub const SCNx8: &[u8; 4] = b"hhx\0"; -pub const SCNx16: &[u8; 3] = b"hx\0"; -pub const SCNx32: &[u8; 2] = b"x\0"; -pub const SCNx64: &[u8; 3] = b"lx\0"; -pub const SCNxLEAST8: &[u8; 4] = b"hhx\0"; -pub const SCNxLEAST16: &[u8; 3] = b"hx\0"; -pub const SCNxLEAST32: &[u8; 2] = b"x\0"; -pub const SCNxLEAST64: &[u8; 3] = b"lx\0"; -pub const SCNxFAST8: &[u8; 4] = b"hhx\0"; -pub const SCNxFAST16: &[u8; 3] = b"lx\0"; -pub const SCNxFAST32: &[u8; 3] = b"lx\0"; -pub const SCNxFAST64: &[u8; 3] = b"lx\0"; -pub const SCNdMAX: &[u8; 3] = b"ld\0"; -pub const SCNiMAX: &[u8; 3] = b"li\0"; -pub const SCNoMAX: &[u8; 3] = b"lo\0"; -pub const SCNuMAX: &[u8; 3] = b"lu\0"; -pub const SCNxMAX: &[u8; 3] = b"lx\0"; -pub const SCNdPTR: &[u8; 3] = b"ld\0"; -pub const SCNiPTR: &[u8; 3] = b"li\0"; -pub const SCNoPTR: &[u8; 3] = b"lo\0"; -pub const SCNuPTR: &[u8; 3] = b"lu\0"; -pub const SCNxPTR: &[u8; 3] = b"lx\0"; -pub const CMAKE_BUILD_TYPE: &[u8; 8] = b"Release\0"; -pub const HIGHS_GITHASH: &[u8; 10] = b"2584a4d6b\0"; -pub const HIGHS_VERSION_MAJOR: u32 = 1; -pub const HIGHS_VERSION_MINOR: u32 = 11; -pub const HIGHS_VERSION_PATCH: u32 = 0; -pub const HIGHSINT_FORMAT: &[u8; 2] = b"d\0"; -pub type __u_char = ::std::os::raw::c_uchar; -pub type __u_short = ::std::os::raw::c_ushort; -pub type __u_int = ::std::os::raw::c_uint; -pub type __u_long = ::std::os::raw::c_ulong; -pub type __int8_t = ::std::os::raw::c_schar; -pub type __uint8_t = ::std::os::raw::c_uchar; -pub type __int16_t = ::std::os::raw::c_short; -pub type __uint16_t = ::std::os::raw::c_ushort; -pub type __int32_t = ::std::os::raw::c_int; -pub type __uint32_t = ::std::os::raw::c_uint; -pub type __int64_t = ::std::os::raw::c_long; -pub type __uint64_t = ::std::os::raw::c_ulong; -pub type __int_least8_t = __int8_t; -pub type __uint_least8_t = __uint8_t; -pub type __int_least16_t = __int16_t; -pub type __uint_least16_t = __uint16_t; -pub type __int_least32_t = __int32_t; -pub type __uint_least32_t = __uint32_t; -pub type __int_least64_t = __int64_t; -pub type __uint_least64_t = __uint64_t; -pub type __quad_t = ::std::os::raw::c_long; -pub type __u_quad_t = ::std::os::raw::c_ulong; -pub type __intmax_t = ::std::os::raw::c_long; -pub type __uintmax_t = ::std::os::raw::c_ulong; -pub type __dev_t = ::std::os::raw::c_ulong; -pub type __uid_t = ::std::os::raw::c_uint; -pub type __gid_t = ::std::os::raw::c_uint; -pub type __ino_t = ::std::os::raw::c_ulong; -pub type __ino64_t = ::std::os::raw::c_ulong; -pub type __mode_t = ::std::os::raw::c_uint; -pub type __nlink_t = ::std::os::raw::c_ulong; -pub type __off_t = ::std::os::raw::c_long; -pub type __off64_t = ::std::os::raw::c_long; -pub type __pid_t = ::std::os::raw::c_int; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __fsid_t { - pub __val: [::std::os::raw::c_int; 2usize], -} -#[allow(clippy::unnecessary_operation, clippy::identity_op)] -const _: () = { - ["Size of __fsid_t"][::std::mem::size_of::<__fsid_t>() - 8usize]; - ["Alignment of __fsid_t"][::std::mem::align_of::<__fsid_t>() - 4usize]; - ["Offset of field: __fsid_t::__val"][::std::mem::offset_of!(__fsid_t, __val) - 0usize]; -}; -pub type __clock_t = ::std::os::raw::c_long; -pub type __rlim_t = ::std::os::raw::c_ulong; -pub type __rlim64_t = ::std::os::raw::c_ulong; -pub type __id_t = ::std::os::raw::c_uint; -pub type __time_t = ::std::os::raw::c_long; -pub type __useconds_t = ::std::os::raw::c_uint; -pub type __suseconds_t = ::std::os::raw::c_long; -pub type __suseconds64_t = ::std::os::raw::c_long; -pub type __daddr_t = ::std::os::raw::c_int; -pub type __key_t = ::std::os::raw::c_int; -pub type __clockid_t = ::std::os::raw::c_int; -pub type __timer_t = *mut ::std::os::raw::c_void; -pub type __blksize_t = ::std::os::raw::c_long; -pub type __blkcnt_t = ::std::os::raw::c_long; -pub type __blkcnt64_t = ::std::os::raw::c_long; -pub type __fsblkcnt_t = ::std::os::raw::c_ulong; -pub type __fsblkcnt64_t = ::std::os::raw::c_ulong; -pub type __fsfilcnt_t = ::std::os::raw::c_ulong; -pub type __fsfilcnt64_t = ::std::os::raw::c_ulong; -pub type __fsword_t = ::std::os::raw::c_long; -pub type __ssize_t = ::std::os::raw::c_long; -pub type __syscall_slong_t = ::std::os::raw::c_long; -pub type __syscall_ulong_t = ::std::os::raw::c_ulong; -pub type __loff_t = __off64_t; -pub type __caddr_t = *mut ::std::os::raw::c_char; -pub type __intptr_t = ::std::os::raw::c_long; -pub type __socklen_t = ::std::os::raw::c_uint; -pub type __sig_atomic_t = ::std::os::raw::c_int; -pub type int_least8_t = __int_least8_t; -pub type int_least16_t = __int_least16_t; -pub type int_least32_t = __int_least32_t; -pub type int_least64_t = __int_least64_t; -pub type uint_least8_t = __uint_least8_t; -pub type uint_least16_t = __uint_least16_t; -pub type uint_least32_t = __uint_least32_t; -pub type uint_least64_t = __uint_least64_t; -pub type int_fast8_t = ::std::os::raw::c_schar; -pub type int_fast16_t = ::std::os::raw::c_long; -pub type int_fast32_t = ::std::os::raw::c_long; -pub type int_fast64_t = ::std::os::raw::c_long; -pub type uint_fast8_t = ::std::os::raw::c_uchar; -pub type uint_fast16_t = ::std::os::raw::c_ulong; -pub type uint_fast32_t = ::std::os::raw::c_ulong; -pub type uint_fast64_t = ::std::os::raw::c_ulong; -pub type intmax_t = __intmax_t; -pub type uintmax_t = __uintmax_t; -pub type __gwchar_t = ::std::os::raw::c_int; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct imaxdiv_t { - pub quot: ::std::os::raw::c_long, - pub rem: ::std::os::raw::c_long, -} -#[allow(clippy::unnecessary_operation, clippy::identity_op)] -const _: () = { - ["Size of imaxdiv_t"][::std::mem::size_of::() - 16usize]; - ["Alignment of imaxdiv_t"][::std::mem::align_of::() - 8usize]; - ["Offset of field: imaxdiv_t::quot"][::std::mem::offset_of!(imaxdiv_t, quot) - 0usize]; - ["Offset of field: imaxdiv_t::rem"][::std::mem::offset_of!(imaxdiv_t, rem) - 8usize]; -}; -unsafe extern "C" { - pub fn imaxabs(__n: intmax_t) -> intmax_t; -} -unsafe extern "C" { - pub fn imaxdiv(__numer: intmax_t, __denom: intmax_t) -> imaxdiv_t; -} -unsafe extern "C" { - pub fn strtoimax( - __nptr: *const ::std::os::raw::c_char, - __endptr: *mut *mut ::std::os::raw::c_char, - __base: ::std::os::raw::c_int, - ) -> intmax_t; -} -unsafe extern "C" { - pub fn strtoumax( - __nptr: *const ::std::os::raw::c_char, - __endptr: *mut *mut ::std::os::raw::c_char, - __base: ::std::os::raw::c_int, - ) -> uintmax_t; -} -unsafe extern "C" { - pub fn wcstoimax( - __nptr: *const __gwchar_t, - __endptr: *mut *mut __gwchar_t, - __base: ::std::os::raw::c_int, - ) -> intmax_t; -} -unsafe extern "C" { - pub fn wcstoumax( - __nptr: *const __gwchar_t, - __endptr: *mut *mut __gwchar_t, - __base: ::std::os::raw::c_int, - ) -> uintmax_t; -} pub type HighsInt = ::std::os::raw::c_int; -pub type HighsUInt = ::std::os::raw::c_uint; #[doc = " Struct to handle callback output data\n"] #[repr(C)] #[derive(Debug, Copy, Clone)] @@ -512,97 +113,6 @@ pub type HighsCCallbackType = ::std::option::Option< arg5: *mut ::std::os::raw::c_void, ), >; -pub const kHighsMaximumStringLength: HighsInt = 512; -pub const kHighsStatusError: HighsInt = -1; -pub const kHighsStatusOk: HighsInt = 0; -pub const kHighsStatusWarning: HighsInt = 1; -pub const kHighsVarTypeContinuous: HighsInt = 0; -pub const kHighsVarTypeInteger: HighsInt = 1; -pub const kHighsVarTypeSemiContinuous: HighsInt = 2; -pub const kHighsVarTypeSemiInteger: HighsInt = 3; -pub const kHighsVarTypeImplicitInteger: HighsInt = 4; -pub const kHighsOptionTypeBool: HighsInt = 0; -pub const kHighsOptionTypeInt: HighsInt = 1; -pub const kHighsOptionTypeDouble: HighsInt = 2; -pub const kHighsOptionTypeString: HighsInt = 3; -pub const kHighsInfoTypeInt64: HighsInt = -1; -pub const kHighsInfoTypeInt: HighsInt = 1; -pub const kHighsInfoTypeDouble: HighsInt = 2; -pub const kHighsObjSenseMinimize: HighsInt = 1; -pub const kHighsObjSenseMaximize: HighsInt = -1; -pub const kHighsMatrixFormatColwise: HighsInt = 1; -pub const kHighsMatrixFormatRowwise: HighsInt = 2; -pub const kHighsHessianFormatTriangular: HighsInt = 1; -pub const kHighsHessianFormatSquare: HighsInt = 2; -pub const kHighsSolutionStatusNone: HighsInt = 0; -pub const kHighsSolutionStatusInfeasible: HighsInt = 1; -pub const kHighsSolutionStatusFeasible: HighsInt = 2; -pub const kHighsBasisValidityInvalid: HighsInt = 0; -pub const kHighsBasisValidityValid: HighsInt = 1; -pub const kHighsPresolveStatusNotPresolved: HighsInt = -1; -pub const kHighsPresolveStatusNotReduced: HighsInt = 0; -pub const kHighsPresolveStatusInfeasible: HighsInt = 1; -pub const kHighsPresolveStatusUnboundedOrInfeasible: HighsInt = 2; -pub const kHighsPresolveStatusReduced: HighsInt = 3; -pub const kHighsPresolveStatusReducedToEmpty: HighsInt = 4; -pub const kHighsPresolveStatusTimeout: HighsInt = 5; -pub const kHighsPresolveStatusNullError: HighsInt = 6; -pub const kHighsPresolveStatusOptionsError: HighsInt = 7; -pub const kHighsPresolveStatusOutOfMemory: HighsInt = 8; -pub const kHighsModelStatusNotset: HighsInt = 0; -pub const kHighsModelStatusLoadError: HighsInt = 1; -pub const kHighsModelStatusModelError: HighsInt = 2; -pub const kHighsModelStatusPresolveError: HighsInt = 3; -pub const kHighsModelStatusSolveError: HighsInt = 4; -pub const kHighsModelStatusPostsolveError: HighsInt = 5; -pub const kHighsModelStatusModelEmpty: HighsInt = 6; -pub const kHighsModelStatusOptimal: HighsInt = 7; -pub const kHighsModelStatusInfeasible: HighsInt = 8; -pub const kHighsModelStatusUnboundedOrInfeasible: HighsInt = 9; -pub const kHighsModelStatusUnbounded: HighsInt = 10; -pub const kHighsModelStatusObjectiveBound: HighsInt = 11; -pub const kHighsModelStatusObjectiveTarget: HighsInt = 12; -pub const kHighsModelStatusTimeLimit: HighsInt = 13; -pub const kHighsModelStatusIterationLimit: HighsInt = 14; -pub const kHighsModelStatusUnknown: HighsInt = 15; -pub const kHighsModelStatusSolutionLimit: HighsInt = 16; -pub const kHighsModelStatusInterrupt: HighsInt = 17; -pub const kHighsBasisStatusLower: HighsInt = 0; -pub const kHighsBasisStatusBasic: HighsInt = 1; -pub const kHighsBasisStatusUpper: HighsInt = 2; -pub const kHighsBasisStatusZero: HighsInt = 3; -pub const kHighsBasisStatusNonbasic: HighsInt = 4; -pub const kHighsCallbackLogging: HighsInt = 0; -pub const kHighsCallbackSimplexInterrupt: HighsInt = 1; -pub const kHighsCallbackIpmInterrupt: HighsInt = 2; -pub const kHighsCallbackMipSolution: HighsInt = 3; -pub const kHighsCallbackMipImprovingSolution: HighsInt = 4; -pub const kHighsCallbackMipLogging: HighsInt = 5; -pub const kHighsCallbackMipInterrupt: HighsInt = 6; -pub const kHighsCallbackMipGetCutPool: HighsInt = 7; -pub const kHighsCallbackMipDefineLazyConstraints: HighsInt = 8; -pub const kHighsCallbackCallbackMipUserSolution: HighsInt = 9; -pub const kHighsCallbackDataOutLogTypeName: &[u8; 9] = b"log_type\0"; -pub const kHighsCallbackDataOutRunningTimeName: &[u8; 13] = b"running_time\0"; -pub const kHighsCallbackDataOutSimplexIterationCountName: &[u8; 24] = b"simplex_iteration_count\0"; -pub const kHighsCallbackDataOutIpmIterationCountName: &[u8; 20] = b"ipm_iteration_count\0"; -pub const kHighsCallbackDataOutPdlpIterationCountName: &[u8; 21] = b"pdlp_iteration_count\0"; -pub const kHighsCallbackDataOutObjectiveFunctionValueName: &[u8; 25] = - b"objective_function_value\0"; -pub const kHighsCallbackDataOutMipNodeCountName: &[u8; 15] = b"mip_node_count\0"; -pub const kHighsCallbackDataOutMipTotalLpIterationsName: &[u8; 24] = b"mip_total_lp_iterations\0"; -pub const kHighsCallbackDataOutMipPrimalBoundName: &[u8; 17] = b"mip_primal_bound\0"; -pub const kHighsCallbackDataOutMipDualBoundName: &[u8; 15] = b"mip_dual_bound\0"; -pub const kHighsCallbackDataOutMipGapName: &[u8; 8] = b"mip_gap\0"; -pub const kHighsCallbackDataOutMipSolutionName: &[u8; 13] = b"mip_solution\0"; -pub const kHighsCallbackDataOutCutpoolNumColName: &[u8; 16] = b"cutpool_num_col\0"; -pub const kHighsCallbackDataOutCutpoolNumCutName: &[u8; 16] = b"cutpool_num_cut\0"; -pub const kHighsCallbackDataOutCutpoolNumNzName: &[u8; 15] = b"cutpool_num_nz\0"; -pub const kHighsCallbackDataOutCutpoolStartName: &[u8; 14] = b"cutpool_start\0"; -pub const kHighsCallbackDataOutCutpoolIndexName: &[u8; 14] = b"cutpool_index\0"; -pub const kHighsCallbackDataOutCutpoolValueName: &[u8; 14] = b"cutpool_value\0"; -pub const kHighsCallbackDataOutCutpoolLowerName: &[u8; 14] = b"cutpool_lower\0"; -pub const kHighsCallbackDataOutCutpoolUpperName: &[u8; 14] = b"cutpool_upper\0"; unsafe extern "C" { #[doc = " Formulate and solve a linear program using HiGHS.\n\n @param num_col The number of columns.\n @param num_row The number of rows.\n @param num_nz The number of nonzeros in the constraint matrix.\n @param a_format The format of the constraint matrix as a\n `kHighsMatrixFormat` constant.\n @param sense The optimization sense as a `kHighsObjSense` constant.\n @param offset The objective constant.\n @param col_cost An array of length [num_col] with the column costs.\n @param col_lower An array of length [num_col] with the column lower bounds.\n @param col_upper An array of length [num_col] with the column upper bounds.\n @param row_lower An array of length [num_row] with the row lower bounds.\n @param row_upper An array of length [num_row] with the row upper bounds.\n @param a_start The constraint matrix is provided to HiGHS in compressed\n sparse column form (if `a_format` is\n `kHighsMatrixFormatColwise`, otherwise compressed sparse row\n form). The sparse matrix consists of three arrays,\n `a_start`, `a_index`, and `a_value`. `a_start` is an array\n of length [num_col] containing the starting index of each\n column in `a_index`. If `a_format` is\n `kHighsMatrixFormatRowwise` the array is of length [num_row]\n corresponding to each row.\n @param a_index An array of length [num_nz] with indices of matrix entries.\n @param a_value An array of length [num_nz] with values of matrix entries.\n\n @param col_value An array of length [num_col], to be filled with the\n primal column solution.\n @param col_dual An array of length [num_col], to be filled with the\n dual column solution.\n @param row_value An array of length [num_row], to be filled with the\n primal row solution.\n @param row_dual An array of length [num_row], to be filled with the\n dual row solution.\n @param col_basis_status An array of length [num_col], to be filled with the\n basis status of the columns in the form of a\n `kHighsBasisStatus` constant.\n @param row_basis_status An array of length [num_row], to be filled with the\n basis status of the rows in the form of a\n `kHighsBasisStatus` constant.\n @param model_status The location in which to place the termination\n status of the model after the solve in the form of a\n `kHighsModelStatus` constant.\n\n @returns A `kHighsStatus` constant indicating whether the call succeeded."] pub fn Highs_lpCall( @@ -1905,9 +1415,6 @@ unsafe extern "C" { #[doc = " Return the HiGHS compilation date.\n\n @returns Thse HiGHS compilation date."] pub fn Highs_compilationDate() -> *const ::std::os::raw::c_char; } -pub const HighsStatuskError: HighsInt = -1; -pub const HighsStatuskOk: HighsInt = 0; -pub const HighsStatuskWarning: HighsInt = 1; unsafe extern "C" { pub fn Highs_call( num_col: HighsInt, From 9b850059fd16ee5c5b0a6b4ade9013cf98274912 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Mon, 20 Oct 2025 23:10:09 +0000 Subject: [PATCH 09/22] Fix unit tests --- .../instruction_mode_to_global_mode/test.rs | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/test.rs b/ptx/src/pass/instruction_mode_to_global_mode/test.rs index 94fe7350..8d2b4933 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/test.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/test.rs @@ -43,7 +43,8 @@ fn transitive_mixed() { graph.add_jump(empty, false2_id); let false2_ = graph.get_or_add_basic_block(false2_id); graph.set_modes(false2_, ftz(), ftz()); - let partial_result = super::compute_single_mode_insertions(&graph, |node| node.denormal_f32); + let partial_result = + super::compute_single_mode_insertions(&graph, |node| node.denormal_f32).unwrap(); assert_eq!(partial_result.bb_must_insert_mode.len(), 0); assert_eq!(partial_result.bb_maybe_insert_mode.len(), 1); assert_eq!( @@ -51,7 +52,8 @@ fn transitive_mixed() { (DenormalMode::FlushToZero, iter::once(entry_id).collect()) ); - let result = optimize_mode_insertions::(partial_result); + let result = + optimize_mode_insertions::(partial_result).unwrap(); assert_eq!(result.basic_blocks.len(), 0); assert_eq!(result.kernels.len(), 1); assert_eq!(result.kernels[&entry_id], DenormalMode::FlushToZero); @@ -73,7 +75,8 @@ fn transitive_change_twice() { graph.add_jump(empty, true_id); let true_ = graph.get_or_add_basic_block(true_id); graph.set_modes(true_, preserve(), preserve()); - let partial_result = super::compute_single_mode_insertions(&graph, |node| node.denormal_f32); + let partial_result = + super::compute_single_mode_insertions(&graph, |node| node.denormal_f32).unwrap(); assert_eq!(partial_result.bb_must_insert_mode.len(), 1); assert!(partial_result.bb_must_insert_mode.contains(&true_id)); assert_eq!(partial_result.bb_maybe_insert_mode.len(), 1); @@ -82,7 +85,8 @@ fn transitive_change_twice() { (DenormalMode::FlushToZero, iter::once(entry_id).collect()) ); - let result = optimize_mode_insertions::(partial_result); + let result = + optimize_mode_insertions::(partial_result).unwrap(); assert_eq!(result.basic_blocks, iter::once(true_id).collect()); assert_eq!(result.kernels.len(), 1); assert_eq!(result.kernels[&entry_id], DenormalMode::FlushToZero); @@ -100,7 +104,8 @@ fn transitive_change() { graph.add_jump(empty, true_id); let true_ = graph.get_or_add_basic_block(true_id); graph.set_modes(true_, preserve(), preserve()); - let partial_result = super::compute_single_mode_insertions(&graph, |node| node.denormal_f32); + let partial_result = + super::compute_single_mode_insertions(&graph, |node| node.denormal_f32).unwrap(); assert_eq!(partial_result.bb_must_insert_mode.len(), 0); assert_eq!(partial_result.bb_maybe_insert_mode.len(), 1); assert_eq!( @@ -108,7 +113,8 @@ fn transitive_change() { (DenormalMode::Preserve, iter::once(entry_id).collect()) ); - let result = optimize_mode_insertions::(partial_result); + let result = + optimize_mode_insertions::(partial_result).unwrap(); assert_eq!(result.basic_blocks.len(), 0); assert_eq!(result.kernels.len(), 1); assert_eq!(result.kernels[&entry_id], DenormalMode::Preserve); @@ -143,7 +149,8 @@ fn codependency() { // "{:?}", // petgraph::dot::Dot::with_config(&graph.graph, &[petgraph::dot::Config::EdgeNoLabel]) //); - let partial_result = super::compute_single_mode_insertions(&graph, |node| node.denormal_f32); + let partial_result = + super::compute_single_mode_insertions(&graph, |node| node.denormal_f32).unwrap(); assert_eq!(partial_result.bb_must_insert_mode.len(), 0); assert_eq!(partial_result.bb_maybe_insert_mode.len(), 2); assert_eq!( @@ -155,7 +162,8 @@ fn codependency() { (DenormalMode::FlushToZero, iter::once(entry_id).collect()) ); - let result = optimize_mode_insertions::(partial_result); + let result = + optimize_mode_insertions::(partial_result).unwrap(); assert_eq!(result.basic_blocks.len(), 0); assert_eq!(result.kernels.len(), 1); assert_eq!(result.kernels[&entry_id], DenormalMode::FlushToZero); From 44c208e206365f3851987f86d5dfef290bd16b4d Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Mon, 20 Oct 2025 23:37:20 +0000 Subject: [PATCH 10/22] Reset HiGHS to 0.11 --- ext/HiGHS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/HiGHS b/ext/HiGHS index 2584a4d6..364c83a5 160000 --- a/ext/HiGHS +++ b/ext/HiGHS @@ -1 +1 @@ -Subproject commit 2584a4d6b8d9ac08c70757f4f21afa77c74b10cd +Subproject commit 364c83a51e44ba6c27def9c8fc1a49b1daf5ad5c From 07c653bc23496d7b742f86135eedd6127f823f26 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Mon, 20 Oct 2025 23:53:32 +0000 Subject: [PATCH 11/22] Add rocm-smi to tests --- .github/workflows/rocm_setup_run.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/rocm_setup_run.sh b/.github/workflows/rocm_setup_run.sh index 78419f30..5c849c06 100644 --- a/.github/workflows/rocm_setup_run.sh +++ b/.github/workflows/rocm_setup_run.sh @@ -13,7 +13,9 @@ echo deb [arch=amd64 signed-by=/etc/apt/keyrings/rocm.gpg] https://repo.radeon.c echo -e 'Package: *\nPin: release o=repo.radeon.com\nPin-Priority: 600' \ | tee /etc/apt/preferences.d/rocm-pin-600 DEBIAN_FRONTEND=noninteractive apt update -y -DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends amdgpu-dkms hip-runtime-amd +# rocm-smi-lib shouldn't be necessary, but somehow ptx tests started linking to it. +# Result of Rust 1.90 linker change? +DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends amdgpu-dkms hip-runtime-amd rocm-smi-lib echo 'export PATH="$PATH:/opt/rocm/bin"' | tee /etc/profile.d/rocm.sh echo "/opt/rocm/lib" | tee /etc/ld.so.conf.d/rocm.conf ldconfig From 4870a566661e1e3b4b1413adf468c627a24625c2 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Tue, 21 Oct 2025 00:14:06 +0000 Subject: [PATCH 12/22] Trim bbs that are not kernel-reachable --- ptx/src/pass/instruction_mode_to_global_mode/mod.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index 69cc1fb6..74e3476e 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -1781,7 +1781,12 @@ fn compute_single_mode_insertions( return Err(error_unreachable()); } ModeReachability::Value(Some(value), reachable_kernels) => { - maybe_insert_mode.insert(node_id, (value, reachable_kernels)); + if value != expected_mode { + return Err(error_unreachable()); + } + if !reachable_kernels.is_empty() { + maybe_insert_mode.insert(node_id, (value, reachable_kernels)); + } } } } From 959fcfb93547d8bb5a6155a8ab912d9be301b612 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Tue, 21 Oct 2025 00:21:58 +0000 Subject: [PATCH 13/22] Run LLVMVerifyModule only in debug --- ptx/src/pass/llvm/attributes.rs | 6 ++++-- ptx/src/pass/llvm/emit.rs | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ptx/src/pass/llvm/attributes.rs b/ptx/src/pass/llvm/attributes.rs index 6413236f..5170372d 100644 --- a/ptx/src/pass/llvm/attributes.rs +++ b/ptx/src/pass/llvm/attributes.rs @@ -12,8 +12,10 @@ pub(crate) fn run( emit_attribute(context, &module, "clock_rate", attributes.clock_rate)?; - if let Err(err) = module.verify() { - panic!("{:?}", err); + if cfg!(debug_assertions) { + if let Err(err) = module.verify() { + panic!("{:?}", err); + } } Ok(module) diff --git a/ptx/src/pass/llvm/emit.rs b/ptx/src/pass/llvm/emit.rs index 16237943..ae84bc21 100644 --- a/ptx/src/pass/llvm/emit.rs +++ b/ptx/src/pass/llvm/emit.rs @@ -71,8 +71,10 @@ pub(crate) fn run<'input>( Directive2::Method(method) => emit_ctx.emit_method(method)?, } } - if let Err(err) = module.verify() { - panic!("{:?}", err); + if cfg!(debug_assertions) { + if let Err(err) = module.verify() { + panic!("{:?}", err); + } } Ok(module) } From a3ee6fbe2dff9864e83a06f416dc1f94a49c83b2 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Tue, 21 Oct 2025 00:53:48 +0000 Subject: [PATCH 14/22] Debug insertions --- .../instruction_mode_to_global_mode/mod.rs | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index 74e3476e..872216d2 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -1687,7 +1687,7 @@ impl<'a> Drop for BasicBlockState<'a> { } } -fn compute_single_mode_insertions( +fn compute_single_mode_insertions( cfg: &ControlFlowGraph, mut getter: impl FnMut(&Node) -> Mode, ) -> Result, TranslateError> { @@ -1790,6 +1790,44 @@ fn compute_single_mode_insertions( } } } + { + use std::io::Write; + let mut graph_dot = std::fs::File::create("/tmp/graph.dot").unwrap(); + writeln!( + &mut graph_dot, + "{:?}", + petgraph::dot::Dot::with_config(&cfg.graph, &[petgraph::dot::Config::EdgeNoLabel]) + ); + } + { + use std::io::Write; + let mut must_insert_mode = must_insert_mode.iter().copied().collect::>(); + must_insert_mode.sort_unstable(); + let must_insert_mode = must_insert_mode + .into_iter() + .map(|x| x.0.to_string()) + .collect::>(); + let must_insert_mode = must_insert_mode.join("\n"); + let mut must_insert_file = std::fs::File::create("/tmp/must_insert_mode.txt").unwrap(); + must_insert_file + .write_all(must_insert_mode.as_bytes()) + .unwrap(); + } + { + use std::io::Write; + let maybe_insert_mode = maybe_insert_mode.clone(); + let mut maybe_insert_mode = maybe_insert_mode.iter().collect::>(); + maybe_insert_mode.sort_unstable_by_key(|(x, _)| *x); + let maybe_insert_mode = maybe_insert_mode + .into_iter() + .map(|(x, (val, kernels))| format!("{} => {:?} => {:?}", x, val, kernels)) + .collect::>(); + let maybe_insert_mode = maybe_insert_mode.join("\n"); + let mut maybe_insert_files = std::fs::File::create("/tmp/maybe_insert_mode.txt").unwrap(); + maybe_insert_files + .write_all(maybe_insert_mode.as_bytes()) + .unwrap(); + } Ok(PartialModeInsertion { bb_must_insert_mode: must_insert_mode, bb_maybe_insert_mode: maybe_insert_mode, From 806c41d4ec765099c8e190bc167dd3004b4e5263 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Wed, 22 Oct 2025 01:37:10 +0000 Subject: [PATCH 15/22] [BROKEN] Compute ssc and propagate --- ext/highs-sys/build.rs | 2 +- ptx/Cargo.toml | 1 + .../instruction_mode_to_global_mode/mod.rs | 265 ++++++++++++++++-- 3 files changed, 239 insertions(+), 29 deletions(-) diff --git a/ext/highs-sys/build.rs b/ext/highs-sys/build.rs index 1f03c465..c5a70873 100644 --- a/ext/highs-sys/build.rs +++ b/ext/highs-sys/build.rs @@ -69,7 +69,7 @@ fn build() -> bool { } else if linux || mingw { println!("cargo:rustc-link-lib=dylib=stdc++"); } - println!("cargo:rerun-if-changed=../HiGHS/src/interfaces/highs_c_api.h"); + println!("cargo:rerun-if-changed=../HiGHS/highs/interfaces/highs_c_api.h"); true } diff --git a/ptx/Cargo.toml b/ptx/Cargo.toml index 996b85fe..64bdbc4b 100644 --- a/ptx/Cargo.toml +++ b/ptx/Cargo.toml @@ -22,6 +22,7 @@ highs = "1.12.0" int-enum = "1.1" unwrap_or = "1.0.1" smallvec = "1.15.1" +fixedbitset = "0.5" [dev-dependencies] hip_runtime-sys = { path = "../ext/hip_runtime-sys" } diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index 872216d2..dd36a3ac 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -7,14 +7,22 @@ use super::SpirvWord; use super::Statement; use super::TranslateError; use crate::pass::error_unreachable; +use fixedbitset::FixedBitSet; use highs::HighsStatus; use petgraph::graph::NodeIndex; +use petgraph::visit::GraphBase; +use petgraph::visit::GraphRef; +use petgraph::visit::IntoNeighbors; +use petgraph::visit::IntoNodeIdentifiers; use petgraph::visit::IntoNodeReferences; +use petgraph::visit::NodeIndexable; +use petgraph::visit::Reversed; use petgraph::Direction; use petgraph::Graph; use ptx_parser as ast; use rustc_hash::FxHashMap; use rustc_hash::FxHashSet; +use std::f64::consts::E; use std::iter; use std::mem; use std::ops::ControlFlow; @@ -810,10 +818,12 @@ fn compute_minimal_mode_insertions( ), TranslateError, > { - let rounding_f32 = compute_single_mode_insertions(cfg, |node| node.rounding_f32)?; - let denormal_f32 = compute_single_mode_insertions(cfg, |node| node.denormal_f32)?; - let denormal_f16f64 = compute_single_mode_insertions(cfg, |node| node.denormal_f16f64)?; - let rounding_f16f64 = compute_single_mode_insertions(cfg, |node| node.rounding_f16f64)?; + // SCC of a graph is the same as SCC of the transposed (edge direction reversed) graph + let scc = SccGraph::new(&cfg.graph); + let rounding_f32 = compute_single_mode_insertions(cfg, &scc, |node| node.rounding_f32)?; + let denormal_f32 = compute_single_mode_insertions(cfg, &scc, |node| node.denormal_f32)?; + let denormal_f16f64 = compute_single_mode_insertions(cfg, &scc, |node| node.denormal_f16f64)?; + let rounding_f16f64 = compute_single_mode_insertions(cfg, &scc, |node| node.rounding_f16f64)?; let denormal_f32 = optimize_mode_insertions::(denormal_f32) .map_err(|_| error_unreachable())?; @@ -829,6 +839,28 @@ fn compute_minimal_mode_insertions( Ok((denormal_f32, denormal_f16f64, rounding_f32, rounding_f16f64)) } +fn build_scc(graph: &Graph) -> Graph, ()> { + use petgraph::visit::EdgeRef; + let mut workspace = petgraph::algo::TarjanScc::new(); + let mut sccs = Vec::new(); + workspace.run(graph, |scc| { + sccs.push(scc.into_iter().copied().collect::>()) + }); + let mut new_graph = Graph::from_edges(graph.edge_references().filter_map(|edge| { + let from_scc = workspace.node_component_index(graph, edge.source()); + let to_scc = workspace.node_component_index(graph, edge.target()); + if from_scc != to_scc { + Some((from_scc as u32, to_scc as u32)) + } else { + None + } + })); + for (scc_index, scc) in sccs.drain(..).enumerate() { + new_graph[NodeIndex::new(scc_index)] = scc; + } + new_graph +} + // This function creates control flow graph for the whole module. This control // flow graph expresses function calls as edges in the control flow graph fn create_control_flow_graph( @@ -1687,10 +1719,126 @@ impl<'a> Drop for BasicBlockState<'a> { } } +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +struct SccNodeIndex(u32); + +impl SccNodeIndex { + fn usize(self) -> usize { + self.0 as usize + } +} + +impl Into for SccNodeIndex { + fn into(self) -> usize { + self.0 as usize + } +} + +struct SccGraph<'a> { + source_graph: &'a Graph, + scc_graph: Graph, (), petgraph::Directed>, + workspace: petgraph::algo::TarjanScc, +} + +impl<'this> SccGraph<'this> { + fn new(source_graph: &'this Graph) -> SccGraph<'this> { + use petgraph::visit::EdgeRef; + let mut workspace = petgraph::algo::TarjanScc::new(); + let mut sccs = Vec::new(); + workspace.run(source_graph, |scc| { + sccs.push(scc.into_iter().copied().collect::>()) + }); + let mut scc_graph = Graph::from_edges(source_graph.edge_references().filter_map(|edge| { + let from_scc = workspace.node_component_index(source_graph, edge.source()); + let to_scc = workspace.node_component_index(source_graph, edge.target()); + if from_scc != to_scc { + Some((from_scc as u32, to_scc as u32)) + } else { + None + } + })); + for (scc_index, scc) in sccs.drain(..).enumerate() { + scc_graph[NodeIndex::new(scc_index)] = scc; + } + Self { + scc_graph, + source_graph, + workspace, + } + } + + fn node_count(&self) -> usize { + self.scc_graph.node_count() + } + + fn component(&self, index: NodeIndex) -> SccNodeIndex { + SccNodeIndex( + self.workspace + .node_component_index(self.source_graph, index) as u32, + ) + } + + fn propagate( + &self, + scc_roots: impl Iterator, + mut merge: impl FnMut(SccNodeIndex, SccNodeIndex) -> bool, + ) { + for scc_root in scc_roots { + let mut to_visit = self + .scc_graph + .neighbors_directed(NodeIndex::new(scc_root.0 as usize), Direction::Outgoing) + .filter(|&successor| merge(scc_root, SccNodeIndex(successor.index() as u32))) + .collect::>(); + while let Some(scc_index) = to_visit.pop() { + for successor in self + .scc_graph + .neighbors_directed(scc_index, Direction::Outgoing) + { + if merge( + SccNodeIndex(scc_index.index() as u32), + SccNodeIndex(successor.index() as u32), + ) { + to_visit.push(successor); + } + } + } + } + } +} + fn compute_single_mode_insertions( cfg: &ControlFlowGraph, - mut getter: impl FnMut(&Node) -> Mode, + scc: &SccGraph, + mut get_mode: impl FnMut(&Node) -> Mode, ) -> Result, TranslateError> { + let mut entries = cfg + .entry_points + .iter() + .enumerate() + .map(|(i, (compiler_id, _))| (*compiler_id, i)) + .collect::>(); + let mut state = vec![ModeReachability::::empty(entries.len()); scc.node_count()]; + let mut roots = FxHashSet::default(); + for (index, node) in cfg.graph.node_references() { + let mode = get_mode(node); + if let Some(exit_mode) = mode.exit { + state[scc.component(index).usize()].fold_from(&ModeReachability::from_mode( + entries.len(), + &entries, + exit_mode, + )); + } + if mode.entry.is_some() { + roots.insert(scc.component(index)); + } + } + scc.propagate(roots.iter().copied(), |from, to| { + let [from_state, to_state] = state.get_disjoint_mut([from.usize(), to.usize()]).unwrap(); + to_state.fold_from(from_state) + }); + dbg!(state); + todo!() + /* fn get_exit_mode_reachability( cfg: &ControlFlowGraph, getter: &mut impl FnMut(&Node) -> Mode, @@ -1732,14 +1880,45 @@ fn compute_single_mode_insertions( exit_cache.insert(index, result.clone()); Some(result) } - let mut must_insert_mode: std::collections::HashSet = - FxHashSet::::default(); + /* + let mut to_check = Vec::new(); + cfg.graph.edge_references() + for (scc_index, component) in scc.iter().enumerate() { + let mut specific_mode = None; + let mut kernel_modes = FxHashSet::default(); + // Look for edges outsides of SCC + for &node_index in component { + let node = cfg.graph.node_weight(node_index).unwrap(); + let mode = get_mode(node); + if let Some(ExtendedMode::Entry(_)) = mode.entry { + to_check.push((node_index, SccIndex(scc_index as u32))); + } + match mode.exit { + Some(ExtendedMode::BasicBlock(value)) => { + if let Some(old_value) = specific_mode { + if old_value != value { + specific_mode = None; + } + } else { + specific_mode = Some(value); + } + } + Some(ExtendedMode::Entry(id)) => { + kernel_modes.insert(id); + } + None => {} + } + } + } + */ + + let mut must_insert_mode = FxHashSet::default(); let mut maybe_insert_mode = FxHashMap::default(); let mut remaining = cfg .graph .node_references() .filter_map(|(index, node)| { - getter(node) + get_mode(node) .entry .as_ref() .map(|mode| match mode { @@ -1761,7 +1940,7 @@ fn compute_single_mode_insertions( |old_mode, predecessor| { let new_mode = get_exit_mode_reachability( cfg, - &mut getter, + &mut get_mode, predecessor, &mut exit_cache, &mut visited, @@ -1832,45 +2011,75 @@ fn compute_single_mode_insertions( bb_must_insert_mode: must_insert_mode, bb_maybe_insert_mode: maybe_insert_mode, }) + */ } -#[derive(Clone)] +#[derive(Clone, Debug)] enum ModeReachability { Conflict, - Value(Option, FxHashSet), + Value(Option, FixedBitSet), } impl ModeReachability { fn fold(self, other: Option) -> ControlFlow { - let other = match other { - Some(x) => x, - None => return ControlFlow::Continue(self), - }; - match (self, other) { - (_, ModeReachability::Conflict) => ControlFlow::Break(ModeReachability::Conflict), - (ModeReachability::Conflict, _) => ControlFlow::Break(ModeReachability::Conflict), + todo!() + } + + fn fold_from(&mut self, other: &Self) -> bool { + match (mem::replace(self, ModeReachability::Conflict), other) { + (ModeReachability::Conflict, _) => false, + (_, ModeReachability::Conflict) => { + *self = ModeReachability::Conflict; + true + } ( ModeReachability::Value(old_value, mut old_kernels), ModeReachability::Value(new_value, new_kernels), ) => match (old_value, new_value) { - (Some(x), Some(y)) if x != y => ControlFlow::Break(ModeReachability::Conflict), + (Some(x), Some(y)) if x != *y => { + *self = ModeReachability::Conflict; + false + } _ => { - old_kernels.extend(new_kernels); - ControlFlow::Continue(ModeReachability::Value( - old_value.or(new_value), - old_kernels, - )) + if old_value == *new_value && old_kernels == *new_kernels { + false + } else { + old_kernels.union_with(new_kernels); + *self = ModeReachability::Value(old_value.or(*new_value), old_kernels); + true + } } }, } } - fn empty() -> Self { - ModeReachability::Value(None, FxHashSet::default()) + fn fold_from_owned(&mut self, other: Self) -> bool { + todo!() } - fn from_value(t: T) -> Self { - ModeReachability::Value(Some(t), FxHashSet::default()) + fn empty(bit_set_size: usize) -> Self { + ModeReachability::Value(None, FixedBitSet::with_capacity(bit_set_size)) + } + + fn from_value(bit_set_size: usize, t: T) -> Self { + ModeReachability::Value(Some(t), FixedBitSet::with_capacity(bit_set_size)) + } + + fn from_mode( + bit_set_size: usize, + entry_map: &FxHashMap, + mode: ExtendedMode, + ) -> Self { + match mode { + ExtendedMode::BasicBlock(value) => { + ModeReachability::Value(Some(value), FixedBitSet::with_capacity(bit_set_size)) + } + ExtendedMode::Entry(id) => { + let mut bit_set = FixedBitSet::with_capacity(bit_set_size); + bit_set.set(entry_map[&id], true); + ModeReachability::Value(None, bit_set) + } + } } } From 2ed4b4592767d28b7010dcc5b901b946e6256ab6 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Thu, 23 Oct 2025 02:04:09 +0000 Subject: [PATCH 16/22] Do full propagation instead --- .../instruction_mode_to_global_mode/mod.rs | 301 +++++++++++++++--- 1 file changed, 250 insertions(+), 51 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index dd36a3ac..4ca63cc2 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -1808,16 +1808,57 @@ impl<'this> SccGraph<'this> { fn compute_single_mode_insertions( cfg: &ControlFlowGraph, - scc: &SccGraph, + _scc: &SccGraph, mut get_mode: impl FnMut(&Node) -> Mode, ) -> Result, TranslateError> { + let mut kernel_ids = cfg + .entry_points + .iter() + .enumerate() + .map(|(i, (compiler_id, _))| (*compiler_id, i)) + .collect::>(); + let mut propagation_state = (0..cfg.graph.node_count()) + .map(|_| PropagationState::::empty()) + .collect::>(); + let mut roots = cfg + .graph + .node_references() + .filter_map(|(index, node)| { + let mode = get_mode(node); + match (mode.entry, mode.exit) { + (Some(entry_mode), Some(exit_mode)) => { + propagation_state[index.index()] = + PropagationState::from_mode(&kernel_ids, entry_mode, exit_mode); + Some(index) + } + (None, None) => None, + _ => unreachable!(), + } + }) + .collect::>(); + while let Some(current) = roots.pop() { + for next in cfg.graph.neighbors_directed(current, Direction::Outgoing) { + if current.index() == next.index() { + if propagation_state[current.index()].fold_from_self(kernel_ids.len()) { + roots.push(current); + } + } else { + let [current_state, next_state] = propagation_state + .get_disjoint_mut([current.index(), next.index()]) + .unwrap(); + if next_state.fold_from(current_state, kernel_ids.len()) { + roots.push(next); + } + } + } + } + /* let mut entries = cfg .entry_points .iter() .enumerate() .map(|(i, (compiler_id, _))| (*compiler_id, i)) .collect::>(); - let mut state = vec![ModeReachability::::empty(entries.len()); scc.node_count()]; let mut roots = FxHashSet::default(); for (index, node) in cfg.graph.node_references() { let mode = get_mode(node); @@ -1837,6 +1878,7 @@ fn compute_single_mode_insertions( to_state.fold_from(from_state) }); dbg!(state); + */ todo!() /* fn get_exit_mode_reachability( @@ -2014,72 +2056,229 @@ fn compute_single_mode_insertions( */ } -#[derive(Clone, Debug)] -enum ModeReachability { +#[derive(Eq, PartialEq, Clone, Debug)] +enum RootExitValue { + Value(T), + Entry(FixedBitSet), +} + +enum ComputedValue { Conflict, Value(Option, FixedBitSet), } -impl ModeReachability { - fn fold(self, other: Option) -> ControlFlow { - todo!() - } +enum PropagationState { + // This basic block sets a mode, hence its mode at the exit is independent from its predecessors + Fixed { + entry: ComputedValue, + exit: RootExitValue, + }, + // This basic block does not set mode, hence its mode at the exit is propagated from its predecessors + // It is None at the start, before we start propagating values + Propagated(ComputedValue), +} - fn fold_from(&mut self, other: &Self) -> bool { - match (mem::replace(self, ModeReachability::Conflict), other) { - (ModeReachability::Conflict, _) => false, - (_, ModeReachability::Conflict) => { - *self = ModeReachability::Conflict; - true - } - ( - ModeReachability::Value(old_value, mut old_kernels), - ModeReachability::Value(new_value, new_kernels), - ) => match (old_value, new_value) { - (Some(x), Some(y)) if x != *y => { - *self = ModeReachability::Conflict; - false - } - _ => { - if old_value == *new_value && old_kernels == *new_kernels { - false - } else { - old_kernels.union_with(new_kernels); - *self = ModeReachability::Value(old_value.or(*new_value), old_kernels); - true - } - } +impl PropagationState { + fn get_entry(&mut self) -> (&mut ComputedValue, bool) { + match self { + PropagationState::Fixed { entry, .. } => (entry, false), + PropagationState::Propagated(entry) => (entry, true), + } + } + fn get_exit(&self) -> Option<(Option, &FixedBitSet)> { + static EMPTY_BITSET: FixedBitSet = FixedBitSet::new(); + match self { + PropagationState::Fixed { exit, .. } => match exit { + RootExitValue::Value(v) => Some((Some(*v), &EMPTY_BITSET)), + RootExitValue::Entry(kernels) => Some((None, kernels)), }, + PropagationState::Propagated(ComputedValue::Conflict) => None, + PropagationState::Propagated(ComputedValue::Value(v, kernels)) => Some((*v, kernels)), } } +} - fn fold_from_owned(&mut self, other: Self) -> bool { - todo!() +impl PropagationState { + fn empty() -> Self { + PropagationState::Propagated(ComputedValue::Value(None, FixedBitSet::new())) } - fn empty(bit_set_size: usize) -> Self { - ModeReachability::Value(None, FixedBitSet::with_capacity(bit_set_size)) + fn from_mode( + kernel_map: &FxHashMap, + entry_mode: ExtendedMode, + exit_mode: ExtendedMode, + ) -> Self { + match entry_mode { + ExtendedMode::BasicBlock(entry_mode) => match exit_mode { + ExtendedMode::BasicBlock(exit_mode) => PropagationState::Fixed { + entry: ComputedValue::Value(Some(entry_mode), FixedBitSet::new()), + exit: RootExitValue::Value(exit_mode), + }, + _ => unreachable!(), + }, + ExtendedMode::Entry(id) => { + let mut bit_set = FixedBitSet::with_capacity(kernel_map.len()); + bit_set.set(kernel_map[&id], true); + PropagationState::Fixed { + entry: ComputedValue::Conflict, // technically incorrect, but impossible to reach it + exit: RootExitValue::Entry(bit_set), + } + } + } } - fn from_value(bit_set_size: usize, t: T) -> Self { - ModeReachability::Value(Some(t), FixedBitSet::with_capacity(bit_set_size)) + fn fold(self, other: Option) -> ControlFlow { + todo!() } - fn from_mode( - bit_set_size: usize, - entry_map: &FxHashMap, - mode: ExtendedMode, - ) -> Self { - match mode { - ExtendedMode::BasicBlock(value) => { - ModeReachability::Value(Some(value), FixedBitSet::with_capacity(bit_set_size)) + fn fold_from_self(&mut self, kernel_count: usize) -> bool { + match self { + PropagationState::Fixed { entry, exit } => match entry { + ComputedValue::Conflict => false, + ComputedValue::Value(value, kernels) => match exit { + RootExitValue::Value(new_value) => { + if *value != Some(*new_value) { + *value = Some(*new_value); + true + } else { + false + } + } + RootExitValue::Entry(other_kernels) => { + if kernels != other_kernels { + kernels.grow(kernel_count); + kernels.union_with(other_kernels); + true + } else { + false + } + } + }, + }, + PropagationState::Propagated(_) => false, + } + } + + fn fold_from(&mut self, from: &Self, kernel_count: usize) -> bool { + let (self_ref, can_propagate) = self.get_entry(); + let this = mem::replace(self_ref, ComputedValue::Conflict); + let (this, is_different) = match (this, from.get_exit()) { + (ComputedValue::Conflict, _) => (ComputedValue::Conflict, false), + (ComputedValue::Value(..), None) => (ComputedValue::Conflict, can_propagate), + (ComputedValue::Value(value, mut kernels), Some((other_value, other_kernels))) => { + let new_value = value.or(other_value); + if new_value != value || &kernels != other_kernels { + kernels.grow(kernel_count); + kernels.union_with(other_kernels); + (ComputedValue::Value(new_value, kernels), can_propagate) + } else { + (ComputedValue::Value(value, kernels), false) + } } - ExtendedMode::Entry(id) => { - let mut bit_set = FixedBitSet::with_capacity(bit_set_size); - bit_set.set(entry_map[&id], true); - ModeReachability::Value(None, bit_set) + }; + *self_ref = this; + is_different + /* + let (this, is_different) = match (mem::replace(self, PropagationState::empty()), from) { + (PropagationState::Propagated(RecipientValue::Conflict), _) => ( + PropagationState::Propagated(RecipientValue::Conflict), + false, + ), + ( + PropagationState::Propagated(RecipientValue::Value(..)), + PropagationState::Propagated(RecipientValue::Conflict), + ) => (PropagationState::Propagated(RecipientValue::Conflict), true), + ( + PropagationState::Fixed { + entry: RootEntryValue::Value(_), + exit, + }, + PropagationState::Propagated(RecipientValue::Conflict), + ) => ( + PropagationState::Fixed { + entry: RootEntryValue::Conflict, + exit, + }, + true, + ), + ( + PropagationState::Propagated(RecipientValue::Value(value, mut kernels)), + PropagationState::Propagated(RecipientValue::Value(other_value, other_kernels)), + ) => { + let new_value = value.or(*other_value); + if &kernels != other_kernels && new_value != value { + kernels.grow(kernel_count); + kernels.union_with(other_kernels); + ( + PropagationState::Propagated(RecipientValue::Value(new_value, kernels)), + true, + ) + } else { + ( + PropagationState::Propagated(RecipientValue::Value(value, kernels)), + false, + ) + } } - } + ( + PropagationState::Propagated(RecipientValue::Value(value, mut kernels)), + PropagationState::Fixed { exit, .. }, + ) => match exit { + RootExitValue::Value(new_value) => match value { + Some(old_value) => { + if old_value != *new_value { + (PropagationState::Propagated(RecipientValue::Conflict), true) + } else { + ( + PropagationState::Propagated(RecipientValue::Value(value, kernels)), + false, + ) + } + } + None => ( + PropagationState::Propagated(RecipientValue::Value( + Some(*new_value), + kernels, + )), + true, + ), + }, + RootExitValue::Entry(other_kernels) => { + if &kernels != other_kernels { + kernels.grow(kernel_count); + kernels.union_with(other_kernels); + ( + PropagationState::Propagated(RecipientValue::Value(value, kernels)), + true, + ) + } else { + ( + PropagationState::Propagated(RecipientValue::Value(value, kernels)), + false, + ) + } + } + }, + ( + PropagationState::Fixed { + entry: RootEntryValue::Conflict, + exit, + }, + _, + ) => ( + PropagationState::Fixed { + entry: RootEntryValue::Conflict, + exit, + }, + false, + ), + (PropagationState::Fixed { entry, .. }, PropagationState::Propagated(other_value)) => { + todo!() + } + }; + *self = this; + is_different + */ } } From 56f253f07ae23f2a2102d5722cd669ca732ba5c5 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Thu, 23 Oct 2025 19:29:16 +0000 Subject: [PATCH 17/22] Fix partial mode insertions calculation --- .../instruction_mode_to_global_mode/mod.rs | 580 +++++------------- 1 file changed, 139 insertions(+), 441 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index 4ca63cc2..c8957f28 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -10,19 +10,12 @@ use crate::pass::error_unreachable; use fixedbitset::FixedBitSet; use highs::HighsStatus; use petgraph::graph::NodeIndex; -use petgraph::visit::GraphBase; -use petgraph::visit::GraphRef; -use petgraph::visit::IntoNeighbors; -use petgraph::visit::IntoNodeIdentifiers; use petgraph::visit::IntoNodeReferences; -use petgraph::visit::NodeIndexable; -use petgraph::visit::Reversed; use petgraph::Direction; use petgraph::Graph; use ptx_parser as ast; use rustc_hash::FxHashMap; use rustc_hash::FxHashSet; -use std::f64::consts::E; use std::iter; use std::mem; use std::ops::ControlFlow; @@ -818,49 +811,25 @@ fn compute_minimal_mode_insertions( ), TranslateError, > { - // SCC of a graph is the same as SCC of the transposed (edge direction reversed) graph - let scc = SccGraph::new(&cfg.graph); - let rounding_f32 = compute_single_mode_insertions(cfg, &scc, |node| node.rounding_f32)?; - let denormal_f32 = compute_single_mode_insertions(cfg, &scc, |node| node.denormal_f32)?; - let denormal_f16f64 = compute_single_mode_insertions(cfg, &scc, |node| node.denormal_f16f64)?; - let rounding_f16f64 = compute_single_mode_insertions(cfg, &scc, |node| node.rounding_f16f64)?; + let rounding_f32 = compute_single_mode_insertions(cfg, |node| node.rounding_f32)?; + let denormal_f32 = compute_single_mode_insertions(cfg, |node| node.denormal_f32)?; + let denormal_f16f64 = compute_single_mode_insertions(cfg, |node| node.denormal_f16f64)?; + let rounding_f16f64 = compute_single_mode_insertions(cfg, |node| node.rounding_f16f64)?; let denormal_f32 = optimize_mode_insertions::(denormal_f32) .map_err(|_| error_unreachable())?; - let denormal_f16f64: MandatoryModeInsertions = + let denormal_f16f64 = optimize_mode_insertions::(denormal_f16f64) .map_err(|_| error_unreachable())?; let rounding_f32 = optimize_mode_insertions::(rounding_f32) .map_err(|_| error_unreachable())?; - let rounding_f16f64: MandatoryModeInsertions = + let rounding_f16f64 = optimize_mode_insertions::(rounding_f16f64) .map_err(|_| error_unreachable())?; Ok((denormal_f32, denormal_f16f64, rounding_f32, rounding_f16f64)) } -fn build_scc(graph: &Graph) -> Graph, ()> { - use petgraph::visit::EdgeRef; - let mut workspace = petgraph::algo::TarjanScc::new(); - let mut sccs = Vec::new(); - workspace.run(graph, |scc| { - sccs.push(scc.into_iter().copied().collect::>()) - }); - let mut new_graph = Graph::from_edges(graph.edge_references().filter_map(|edge| { - let from_scc = workspace.node_component_index(graph, edge.source()); - let to_scc = workspace.node_component_index(graph, edge.target()); - if from_scc != to_scc { - Some((from_scc as u32, to_scc as u32)) - } else { - None - } - })); - for (scc_index, scc) in sccs.drain(..).enumerate() { - new_graph[NodeIndex::new(scc_index)] = scc; - } - new_graph -} - // This function creates control flow graph for the whole module. This control // flow graph expresses function calls as edges in the control flow graph fn create_control_flow_graph( @@ -1719,99 +1688,11 @@ impl<'a> Drop for BasicBlockState<'a> { } } -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -struct SccNodeIndex(u32); - -impl SccNodeIndex { - fn usize(self) -> usize { - self.0 as usize - } -} - -impl Into for SccNodeIndex { - fn into(self) -> usize { - self.0 as usize - } -} - -struct SccGraph<'a> { - source_graph: &'a Graph, - scc_graph: Graph, (), petgraph::Directed>, - workspace: petgraph::algo::TarjanScc, -} - -impl<'this> SccGraph<'this> { - fn new(source_graph: &'this Graph) -> SccGraph<'this> { - use petgraph::visit::EdgeRef; - let mut workspace = petgraph::algo::TarjanScc::new(); - let mut sccs = Vec::new(); - workspace.run(source_graph, |scc| { - sccs.push(scc.into_iter().copied().collect::>()) - }); - let mut scc_graph = Graph::from_edges(source_graph.edge_references().filter_map(|edge| { - let from_scc = workspace.node_component_index(source_graph, edge.source()); - let to_scc = workspace.node_component_index(source_graph, edge.target()); - if from_scc != to_scc { - Some((from_scc as u32, to_scc as u32)) - } else { - None - } - })); - for (scc_index, scc) in sccs.drain(..).enumerate() { - scc_graph[NodeIndex::new(scc_index)] = scc; - } - Self { - scc_graph, - source_graph, - workspace, - } - } - - fn node_count(&self) -> usize { - self.scc_graph.node_count() - } - - fn component(&self, index: NodeIndex) -> SccNodeIndex { - SccNodeIndex( - self.workspace - .node_component_index(self.source_graph, index) as u32, - ) - } - - fn propagate( - &self, - scc_roots: impl Iterator, - mut merge: impl FnMut(SccNodeIndex, SccNodeIndex) -> bool, - ) { - for scc_root in scc_roots { - let mut to_visit = self - .scc_graph - .neighbors_directed(NodeIndex::new(scc_root.0 as usize), Direction::Outgoing) - .filter(|&successor| merge(scc_root, SccNodeIndex(successor.index() as u32))) - .collect::>(); - while let Some(scc_index) = to_visit.pop() { - for successor in self - .scc_graph - .neighbors_directed(scc_index, Direction::Outgoing) - { - if merge( - SccNodeIndex(scc_index.index() as u32), - SccNodeIndex(successor.index() as u32), - ) { - to_visit.push(successor); - } - } - } - } - } -} - fn compute_single_mode_insertions( cfg: &ControlFlowGraph, - _scc: &SccGraph, mut get_mode: impl FnMut(&Node) -> Mode, ) -> Result, TranslateError> { - let mut kernel_ids = cfg + let kernel_ids = cfg .entry_points .iter() .enumerate() @@ -1829,231 +1710,30 @@ fn compute_single_mode_insertions( (Some(entry_mode), Some(exit_mode)) => { propagation_state[index.index()] = PropagationState::from_mode(&kernel_ids, entry_mode, exit_mode); - Some(index) + Some(Ok(index)) } (None, None) => None, - _ => unreachable!(), + _ => Some(Err(error_unreachable())), } }) - .collect::>(); + .collect::, _>>()?; while let Some(current) = roots.pop() { for next in cfg.graph.neighbors_directed(current, Direction::Outgoing) { if current.index() == next.index() { - if propagation_state[current.index()].fold_from_self(kernel_ids.len()) { + if propagation_state[current.index()].merge_with_self(kernel_ids.len()) { roots.push(current); } } else { let [current_state, next_state] = propagation_state .get_disjoint_mut([current.index(), next.index()]) - .unwrap(); - if next_state.fold_from(current_state, kernel_ids.len()) { + .map_err(|_| error_unreachable())?; + if next_state.merge_from(current_state, kernel_ids.len()) { roots.push(next); } } } } - /* - let mut entries = cfg - .entry_points - .iter() - .enumerate() - .map(|(i, (compiler_id, _))| (*compiler_id, i)) - .collect::>(); - let mut roots = FxHashSet::default(); - for (index, node) in cfg.graph.node_references() { - let mode = get_mode(node); - if let Some(exit_mode) = mode.exit { - state[scc.component(index).usize()].fold_from(&ModeReachability::from_mode( - entries.len(), - &entries, - exit_mode, - )); - } - if mode.entry.is_some() { - roots.insert(scc.component(index)); - } - } - scc.propagate(roots.iter().copied(), |from, to| { - let [from_state, to_state] = state.get_disjoint_mut([from.usize(), to.usize()]).unwrap(); - to_state.fold_from(from_state) - }); - dbg!(state); - */ - todo!() - /* - fn get_exit_mode_reachability( - cfg: &ControlFlowGraph, - getter: &mut impl FnMut(&Node) -> Mode, - index: NodeIndex, - exit_cache: &mut FxHashMap>, - visited: &mut FxHashSet, - ) -> Option> { - if let Some(mode) = getter(cfg.graph.node_weight(index).unwrap()).exit { - return Some(match mode { - ExtendedMode::BasicBlock(value) => { - ModeReachability::Value(Some(value), FxHashSet::default()) - } - ExtendedMode::Entry(id) => { - ModeReachability::Value(None, FxHashSet::from_iter(iter::once(id))) - } - }); - } - if let Some(cached) = exit_cache.get(&index) { - return Some(cached.clone()); - } - if !visited.insert(index) { - return None; - } - let mode = cfg - .graph - .neighbors_directed(index, Direction::Incoming) - .try_fold(ModeReachability::empty(), |old_mode, predecessor| { - if visited.contains(&predecessor) { - return ControlFlow::Continue(old_mode); - } - let new_mode = - get_exit_mode_reachability(cfg, getter, predecessor, exit_cache, visited); - old_mode.fold(new_mode) - }); - let result = match mode { - ControlFlow::Continue(m) => m, - ControlFlow::Break(m) => m, - }; - exit_cache.insert(index, result.clone()); - Some(result) - } - /* - let mut to_check = Vec::new(); - cfg.graph.edge_references() - for (scc_index, component) in scc.iter().enumerate() { - let mut specific_mode = None; - let mut kernel_modes = FxHashSet::default(); - // Look for edges outsides of SCC - for &node_index in component { - let node = cfg.graph.node_weight(node_index).unwrap(); - let mode = get_mode(node); - if let Some(ExtendedMode::Entry(_)) = mode.entry { - to_check.push((node_index, SccIndex(scc_index as u32))); - } - match mode.exit { - Some(ExtendedMode::BasicBlock(value)) => { - if let Some(old_value) = specific_mode { - if old_value != value { - specific_mode = None; - } - } else { - specific_mode = Some(value); - } - } - Some(ExtendedMode::Entry(id)) => { - kernel_modes.insert(id); - } - None => {} - } - } - } - */ - - let mut must_insert_mode = FxHashSet::default(); - let mut maybe_insert_mode = FxHashMap::default(); - let mut remaining = cfg - .graph - .node_references() - .filter_map(|(index, node)| { - get_mode(node) - .entry - .as_ref() - .map(|mode| match mode { - ExtendedMode::BasicBlock(mode) => Some((index, node.label, *mode)), - ExtendedMode::Entry(_) => None, - }) - .flatten() - }) - .collect::>(); - let mut exit_cache = FxHashMap::default(); - let mut visited = FxHashSet::default(); - while let Some((index, node_id, expected_mode)) = remaining.pop() { - visited.clear(); - let folded_mode = cfg - .graph - .neighbors_directed(index, Direction::Incoming) - .try_fold( - ModeReachability::from_value(expected_mode), - |old_mode, predecessor| { - let new_mode = get_exit_mode_reachability( - cfg, - &mut get_mode, - predecessor, - &mut exit_cache, - &mut visited, - ); - old_mode.fold(new_mode) - }, - ); - let result = match folded_mode { - ControlFlow::Continue(m) => m, - ControlFlow::Break(m) => m, - }; - match result { - ModeReachability::Conflict => { - must_insert_mode.insert(node_id); - } - ModeReachability::Value(None, _) => { - return Err(error_unreachable()); - } - ModeReachability::Value(Some(value), reachable_kernels) => { - if value != expected_mode { - return Err(error_unreachable()); - } - if !reachable_kernels.is_empty() { - maybe_insert_mode.insert(node_id, (value, reachable_kernels)); - } - } - } - } - { - use std::io::Write; - let mut graph_dot = std::fs::File::create("/tmp/graph.dot").unwrap(); - writeln!( - &mut graph_dot, - "{:?}", - petgraph::dot::Dot::with_config(&cfg.graph, &[petgraph::dot::Config::EdgeNoLabel]) - ); - } - { - use std::io::Write; - let mut must_insert_mode = must_insert_mode.iter().copied().collect::>(); - must_insert_mode.sort_unstable(); - let must_insert_mode = must_insert_mode - .into_iter() - .map(|x| x.0.to_string()) - .collect::>(); - let must_insert_mode = must_insert_mode.join("\n"); - let mut must_insert_file = std::fs::File::create("/tmp/must_insert_mode.txt").unwrap(); - must_insert_file - .write_all(must_insert_mode.as_bytes()) - .unwrap(); - } - { - use std::io::Write; - let maybe_insert_mode = maybe_insert_mode.clone(); - let mut maybe_insert_mode = maybe_insert_mode.iter().collect::>(); - maybe_insert_mode.sort_unstable_by_key(|(x, _)| *x); - let maybe_insert_mode = maybe_insert_mode - .into_iter() - .map(|(x, (val, kernels))| format!("{} => {:?} => {:?}", x, val, kernels)) - .collect::>(); - let maybe_insert_mode = maybe_insert_mode.join("\n"); - let mut maybe_insert_files = std::fs::File::create("/tmp/maybe_insert_mode.txt").unwrap(); - maybe_insert_files - .write_all(maybe_insert_mode.as_bytes()) - .unwrap(); - } - Ok(PartialModeInsertion { - bb_must_insert_mode: must_insert_mode, - bb_maybe_insert_mode: maybe_insert_mode, - }) - */ + PartialModeInsertion::new(cfg, kernel_ids, propagation_state) } #[derive(Eq, PartialEq, Clone, Debug)] @@ -2127,11 +1807,7 @@ impl PropagationState { } } - fn fold(self, other: Option) -> ControlFlow { - todo!() - } - - fn fold_from_self(&mut self, kernel_count: usize) -> bool { + fn merge_with_self(&mut self, kernel_count: usize) -> bool { match self { PropagationState::Fixed { entry, exit } => match entry { ComputedValue::Conflict => false, @@ -2159,7 +1835,7 @@ impl PropagationState { } } - fn fold_from(&mut self, from: &Self, kernel_count: usize) -> bool { + fn merge_from(&mut self, from: &Self, kernel_count: usize) -> bool { let (self_ref, can_propagate) = self.get_entry(); let this = mem::replace(self_ref, ComputedValue::Conflict); let (this, is_different) = match (this, from.get_exit()) { @@ -2178,114 +1854,126 @@ impl PropagationState { }; *self_ref = this; is_different - /* - let (this, is_different) = match (mem::replace(self, PropagationState::empty()), from) { - (PropagationState::Propagated(RecipientValue::Conflict), _) => ( - PropagationState::Propagated(RecipientValue::Conflict), - false, - ), - ( - PropagationState::Propagated(RecipientValue::Value(..)), - PropagationState::Propagated(RecipientValue::Conflict), - ) => (PropagationState::Propagated(RecipientValue::Conflict), true), - ( - PropagationState::Fixed { - entry: RootEntryValue::Value(_), - exit, - }, - PropagationState::Propagated(RecipientValue::Conflict), - ) => ( - PropagationState::Fixed { - entry: RootEntryValue::Conflict, - exit, - }, - true, - ), - ( - PropagationState::Propagated(RecipientValue::Value(value, mut kernels)), - PropagationState::Propagated(RecipientValue::Value(other_value, other_kernels)), - ) => { - let new_value = value.or(*other_value); - if &kernels != other_kernels && new_value != value { - kernels.grow(kernel_count); - kernels.union_with(other_kernels); - ( - PropagationState::Propagated(RecipientValue::Value(new_value, kernels)), - true, - ) - } else { - ( - PropagationState::Propagated(RecipientValue::Value(value, kernels)), - false, - ) + } +} + +struct PartialModeInsertion { + bb_must_insert_mode: FxHashSet, + bb_maybe_insert_mode: PotentialModeInsertionsDueToKernelMode, +} + +impl PartialModeInsertion { + fn new( + cfg: &ControlFlowGraph, + kernel_map: FxHashMap, + state: Vec>, + ) -> Result { + let mut reverse_kernel_map = vec![SpirvWord(u32::MAX); kernel_map.len()]; + for (kernel_id, kernel_index) in kernel_map { + reverse_kernel_map[kernel_index] = kernel_id; + } + let mut bb_must_insert_mode = FxHashSet::default(); + let mut fast_mode_from_kernels = Some(FxHashMap::default()); + let mut slow_mode_from_kernels = FxHashMap::default(); + for (node_index, bb_state) in state.into_iter().enumerate() { + if let PropagationState::Fixed { + entry: ComputedValue::Conflict, + .. + } = bb_state + { + let bb_id = cfg + .graph + .node_weight(NodeIndex::new(node_index)) + .ok_or_else(error_unreachable)? + .label; + if !cfg.entry_points.contains_key(&bb_id) { + bb_must_insert_mode.insert(bb_id); } } - ( - PropagationState::Propagated(RecipientValue::Value(value, mut kernels)), - PropagationState::Fixed { exit, .. }, - ) => match exit { - RootExitValue::Value(new_value) => match value { - Some(old_value) => { - if old_value != *new_value { - (PropagationState::Propagated(RecipientValue::Conflict), true) - } else { - ( - PropagationState::Propagated(RecipientValue::Value(value, kernels)), - false, - ) + if let PropagationState::Fixed { + entry: ComputedValue::Value(value, kernels), + .. + } = bb_state + { + let value = value.ok_or_else(error_unreachable)?; + let node_id = cfg + .graph + .node_weight(NodeIndex::new(node_index)) + .ok_or_else(error_unreachable)? + .label; + let (_, slow_kernels_source) = slow_mode_from_kernels + .entry(node_id) + .or_insert((value, FxHashSet::default())); + for kernel_index in kernels.into_ones() { + let kernel_id = reverse_kernel_map[kernel_index]; + if let Some(fast_mode_ref) = &mut fast_mode_from_kernels { + let old_value = fast_mode_ref.insert(kernel_id, value); + if let Some(old_value) = old_value { + if old_value != value { + fast_mode_from_kernels = None; + } } } - None => ( - PropagationState::Propagated(RecipientValue::Value( - Some(*new_value), - kernels, - )), - true, - ), - }, - RootExitValue::Entry(other_kernels) => { - if &kernels != other_kernels { - kernels.grow(kernel_count); - kernels.union_with(other_kernels); - ( - PropagationState::Propagated(RecipientValue::Value(value, kernels)), - true, - ) - } else { - ( - PropagationState::Propagated(RecipientValue::Value(value, kernels)), - false, - ) - } + slow_kernels_source.insert(kernel_id); } - }, - ( - PropagationState::Fixed { - entry: RootEntryValue::Conflict, - exit, - }, - _, - ) => ( - PropagationState::Fixed { - entry: RootEntryValue::Conflict, - exit, - }, - false, - ), - (PropagationState::Fixed { entry, .. }, PropagationState::Propagated(other_value)) => { - todo!() } - }; - *self = this; - is_different - */ + } + Ok(PartialModeInsertion { + bb_must_insert_mode, + bb_maybe_insert_mode: match fast_mode_from_kernels { + Some(modes) => PotentialModeInsertionsDueToKernelMode::QuickMode(modes), + None => PotentialModeInsertionsDueToKernelMode::SlowMode(slow_mode_from_kernels), + }, + }) } } -#[derive(Debug)] -struct PartialModeInsertion { - bb_must_insert_mode: FxHashSet, - bb_maybe_insert_mode: FxHashMap)>, +// After computing conflicts in modes we have a set of basic blocks whose modes depend on the modes +// of kernels. +// Consider an example for mode that can have value "true" or "false" and a simple control flow +// graph with kernel basic blocks `k0` and `k1` and a single non-kernel basic block `bb`, +// `bb` has mode "true" and kernel modes are undecided, `bb` is reachable from both `k0` and `k1`: +// ┌────┐ +// │ k0 │─────┐ +// └────┘ │ +// ▼ +// ┌──────────┐ +// │ bb: true │ +// └──────────┘ +// ▲ +// ┌────┐ │ +// │ k1 │─────┘ +// └────┘ +// In this case, if either k0 and k1 have mode "false", then we need to insert an instruction to +// set global mode to "true" at the start of basic block `bb`. +// In the next step we want to pick modes for the kernels in the control flow graph that will +// minimize the number of such insertions. This structure holds all the data necessary to resolve +// the problem +enum PotentialModeInsertionsDueToKernelMode { + // This is the simplest case: we know that if each kernel has a specific mode then no insertions + // are necessary. We can just pick those modes for each kernel + QuickMode(FxHashMap), + // This is the most general case that covers potential conflicts. E.g. consider this example: + // ┌────┐ + // │ k0 │──────────────────────┐ + // └────┘ │ + // ▼ + // ┌───────────┐ + // │ bb0: true │ + // └───────────┘ + // ▲ + // ┌────┐ │ + // │ k1 │──────────────────────┘ + // └────┘ │ + // │ ┌────────────┐ + // ├────────>│ bb1: false │ + // │ └────────────┘ + // │ ┌────────────┐ + // └────────>│ bb2: false │ + // └────────────┘ + // In this case there is no way to pick modes for `k0` and `k1` that would avoid insertions + // altogether. + SlowMode(FxHashMap)>), } // Only returns kernel mode insertions if a kernel is relevant to the optimization problem @@ -2295,10 +1983,20 @@ fn optimize_mode_insertions< >( partial: PartialModeInsertion, ) -> Result, HighsStatus> { + let bb_maybe_insert_mode = match partial.bb_maybe_insert_mode { + PotentialModeInsertionsDueToKernelMode::QuickMode(modes) => { + dbg!(&partial.bb_must_insert_mode); + dbg!(&modes); + return Ok(MandatoryModeInsertions { + basic_blocks: partial.bb_must_insert_mode, + kernels: modes, + }); + } + PotentialModeInsertionsDueToKernelMode::SlowMode(slow_modes) => slow_modes, + }; let mut problem = highs::RowProblem::default(); let mut kernel_modes = FxHashMap::default(); - let basic_block_variables = partial - .bb_maybe_insert_mode + let basic_block_variables = bb_maybe_insert_mode .into_iter() .map(|(basic_block, (value, entry_points))| { let modes = entry_points From b6e23b5bc0c533c8efc3520b54ac292608f2cb2e Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Thu, 23 Oct 2025 20:31:51 +0000 Subject: [PATCH 18/22] Cleanup --- .../instruction_mode_to_global_mode/mod.rs | 72 +++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index c8957f28..db80a79f 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -1720,14 +1720,14 @@ fn compute_single_mode_insertions( while let Some(current) = roots.pop() { for next in cfg.graph.neighbors_directed(current, Direction::Outgoing) { if current.index() == next.index() { - if propagation_state[current.index()].merge_with_self(kernel_ids.len()) { + if propagation_state[current.index()].merge_with_self(kernel_ids.len())? { roots.push(current); } } else { let [current_state, next_state] = propagation_state .get_disjoint_mut([current.index(), next.index()]) .map_err(|_| error_unreachable())?; - if next_state.merge_from(current_state, kernel_ids.len()) { + if next_state.merge_from(current_state, kernel_ids.len())? { roots.push(next); } } @@ -1750,7 +1750,7 @@ enum ComputedValue { enum PropagationState { // This basic block sets a mode, hence its mode at the exit is independent from its predecessors Fixed { - entry: ComputedValue, + entry: Option>, // None if it is a kernel basic block exit: RootExitValue, }, // This basic block does not set mode, hence its mode at the exit is propagated from its predecessors @@ -1759,12 +1759,15 @@ enum PropagationState { } impl PropagationState { - fn get_entry(&mut self) -> (&mut ComputedValue, bool) { - match self { - PropagationState::Fixed { entry, .. } => (entry, false), + fn get_entry(&mut self) -> Result<(&mut ComputedValue, bool), TranslateError> { + Ok(match self { + PropagationState::Fixed { entry, .. } => { + (entry.as_mut().ok_or_else(error_unreachable)?, false) + } PropagationState::Propagated(entry) => (entry, true), - } + }) } + fn get_exit(&self) -> Option<(Option, &FixedBitSet)> { static EMPTY_BITSET: FixedBitSet = FixedBitSet::new(); match self { @@ -1791,7 +1794,7 @@ impl PropagationState { match entry_mode { ExtendedMode::BasicBlock(entry_mode) => match exit_mode { ExtendedMode::BasicBlock(exit_mode) => PropagationState::Fixed { - entry: ComputedValue::Value(Some(entry_mode), FixedBitSet::new()), + entry: Some(ComputedValue::Value(Some(entry_mode), FixedBitSet::new())), exit: RootExitValue::Value(exit_mode), }, _ => unreachable!(), @@ -1800,18 +1803,19 @@ impl PropagationState { let mut bit_set = FixedBitSet::with_capacity(kernel_map.len()); bit_set.set(kernel_map[&id], true); PropagationState::Fixed { - entry: ComputedValue::Conflict, // technically incorrect, but impossible to reach it + entry: None, exit: RootExitValue::Entry(bit_set), } } } } - fn merge_with_self(&mut self, kernel_count: usize) -> bool { - match self { + fn merge_with_self(&mut self, kernel_count: usize) -> Result { + Ok(match self { PropagationState::Fixed { entry, exit } => match entry { - ComputedValue::Conflict => false, - ComputedValue::Value(value, kernels) => match exit { + None => return Err(error_unreachable()), + Some(ComputedValue::Conflict) => false, + Some(ComputedValue::Value(value, kernels)) => match exit { RootExitValue::Value(new_value) => { if *value != Some(*new_value) { *value = Some(*new_value); @@ -1832,11 +1836,11 @@ impl PropagationState { }, }, PropagationState::Propagated(_) => false, - } + }) } - fn merge_from(&mut self, from: &Self, kernel_count: usize) -> bool { - let (self_ref, can_propagate) = self.get_entry(); + fn merge_from(&mut self, from: &Self, kernel_count: usize) -> Result { + let (self_ref, can_propagate) = self.get_entry()?; let this = mem::replace(self_ref, ComputedValue::Conflict); let (this, is_different) = match (this, from.get_exit()) { (ComputedValue::Conflict, _) => (ComputedValue::Conflict, false), @@ -1853,7 +1857,7 @@ impl PropagationState { } }; *self_ref = this; - is_different + Ok(is_different) } } @@ -1877,7 +1881,7 @@ impl PartialModeInsertion { let mut slow_mode_from_kernels = FxHashMap::default(); for (node_index, bb_state) in state.into_iter().enumerate() { if let PropagationState::Fixed { - entry: ComputedValue::Conflict, + entry: Some(ComputedValue::Conflict), .. } = bb_state { @@ -1886,12 +1890,10 @@ impl PartialModeInsertion { .node_weight(NodeIndex::new(node_index)) .ok_or_else(error_unreachable)? .label; - if !cfg.entry_points.contains_key(&bb_id) { - bb_must_insert_mode.insert(bb_id); - } + bb_must_insert_mode.insert(bb_id); } if let PropagationState::Fixed { - entry: ComputedValue::Value(value, kernels), + entry: Some(ComputedValue::Value(value, kernels)), .. } = bb_state { @@ -1934,15 +1936,15 @@ impl PartialModeInsertion { // graph with kernel basic blocks `k0` and `k1` and a single non-kernel basic block `bb`, // `bb` has mode "true" and kernel modes are undecided, `bb` is reachable from both `k0` and `k1`: // ┌────┐ -// │ k0 │─────┐ -// └────┘ │ -// ▼ +// │ k0 │──────┐ +// └────┘ │ +// ▼ // ┌──────────┐ // │ bb: true │ // └──────────┘ -// ▲ -// ┌────┐ │ -// │ k1 │─────┘ +// ▲ +// ┌────┐ │ +// │ k1 │──────┘ // └────┘ // In this case, if either k0 and k1 have mode "false", then we need to insert an instruction to // set global mode to "true" at the start of basic block `bb`. @@ -1965,12 +1967,12 @@ enum PotentialModeInsertionsDueToKernelMode { // ┌────┐ │ // │ k1 │──────────────────────┘ // └────┘ │ - // │ ┌────────────┐ - // ├────────>│ bb1: false │ - // │ └────────────┘ - // │ ┌────────────┐ - // └────────>│ bb2: false │ - // └────────────┘ + // │ ┌────────────┐ + // ├─────────>│ bb1: false │ + // │ └────────────┘ + // │ ┌────────────┐ + // └─────────>│ bb2: false │ + // └────────────┘ // In this case there is no way to pick modes for `k0` and `k1` that would avoid insertions // altogether. SlowMode(FxHashMap)>), @@ -1985,8 +1987,6 @@ fn optimize_mode_insertions< ) -> Result, HighsStatus> { let bb_maybe_insert_mode = match partial.bb_maybe_insert_mode { PotentialModeInsertionsDueToKernelMode::QuickMode(modes) => { - dbg!(&partial.bb_must_insert_mode); - dbg!(&modes); return Ok(MandatoryModeInsertions { basic_blocks: partial.bb_must_insert_mode, kernels: modes, From 8d2d60437f3b025a224b35d0d068551fae91c2d8 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Thu, 23 Oct 2025 20:34:52 +0000 Subject: [PATCH 19/22] Cleanup --- ptx/src/pass/instruction_mode_to_global_mode/mod.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index db80a79f..2a5c29bf 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -1750,10 +1750,12 @@ enum ComputedValue { enum PropagationState { // This basic block sets a mode, hence its mode at the exit is independent from its predecessors Fixed { - entry: Option>, // None if it is a kernel basic block + // None if it is a kernel basic block, if we enter kernel it means that a function calls + // into a kernel directly, which is impossible + entry: Option>, exit: RootExitValue, }, - // This basic block does not set mode, hence its mode at the exit is propagated from its predecessors + // This basic block does not set mode, hence its mode is the sum of modes from its predecessors // It is None at the start, before we start propagating values Propagated(ComputedValue), } @@ -2012,7 +2014,6 @@ fn optimize_mode_insertions< (basic_block, bb) }) .collect::>(); - // TODO: add fallback on Error let mut solver = problem.try_optimise(highs::Sense::Maximise)?; solver.make_quiet(); // Takes minutes for a problem that is solved sub-second From 23ac2ccca6eb7f5fde8a98e574177ddb085dd913 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Fri, 24 Oct 2025 01:18:13 +0000 Subject: [PATCH 20/22] Make sure that the conflicts we computed are used in later compilation phase and explain wtf is going on here --- .../instruction_mode_to_global_mode/mod.rs | 420 ++++++++---------- 1 file changed, 179 insertions(+), 241 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index 2a5c29bf..bbde22c3 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -384,264 +384,150 @@ struct ResolvedControlFlowGraph { } impl ResolvedControlFlowGraph { - // This function takes the initial control flow graph. Initial control flow - // graph only has mode values for basic blocks if any instruction in the - // given basic block requires a mode. All the other basic blocks have no - // value. This pass resolved the values for all basic blocks. If a basic - // block sets no value then and there are multiple incoming edges from - // basic block with different values then the value is set to a special - // value "Conflict". - // After this pass every basic block either has a concrete value or "Conflict" + // This function merges previously computed information about conflicts and + // mandatory insertion points fn new( cfg: ControlFlowGraph, - f32_denormal_kernels: &FxHashMap, - f16f64_denormal_kernels: &FxHashMap, - f32_rounding_kernels: &FxHashMap, - f16f64_rounding_kernels: &FxHashMap, + f32_denormal_modes: &MandatoryModeInsertions, + f16f64_denormal_modes: &MandatoryModeInsertions, + f32_rounding_modes: &MandatoryModeInsertions, + f16f64_rounding_modes: &MandatoryModeInsertions, ) -> Result { - fn get_exit_mode_from_dependencies( - cfg: &ControlFlowGraph, - kernels: &FxHashMap, - index: NodeIndex, - getter: &mut impl FnMut(&Node) -> Mode, - exit_cache: &mut FxHashMap>, - unknown: &mut FxHashSet, - ) -> Option> { - unknown.insert(index); - let mode = cfg - .graph - .neighbors_directed(index, Direction::Incoming) - .try_fold(None, |prevailing_mode: Option, predecessor| { - if unknown.contains(&predecessor) { - return ControlFlow::Continue(prevailing_mode); - } - let mode = get_exit_mode( - cfg, - kernels, - predecessor, - &cfg.graph[predecessor], - getter, - exit_cache, - unknown, - ); - match (prevailing_mode, mode) { - (_, None) => { - return ControlFlow::Continue(prevailing_mode); - } - (_, Some(Resolved::Conflict)) => ControlFlow::Break(()), - (None, Some(Resolved::Value(x))) => ControlFlow::Continue(Some(x)), - (Some(prevailing_mode), Some(Resolved::Value(x))) => { - if prevailing_mode == x { - ControlFlow::Continue(Some(prevailing_mode)) - } else { - ControlFlow::Break(()) - } - } - } - }); - match mode { - ControlFlow::Break(_) => Some(Resolved::Conflict), - ControlFlow::Continue(None) => None, - ControlFlow::Continue(Some(x)) => Some(Resolved::Value(x)), - } - } - fn get_exit_mode( - cfg: &ControlFlowGraph, - kernels: &FxHashMap, - index: NodeIndex, - node: &Node, - getter: &mut impl FnMut(&Node) -> Mode, - exit_cache: &mut FxHashMap>, - unknown: &mut FxHashSet, - ) -> Option> { - let mode = getter(node); - let exit = match mode.exit { + fn resolve_mode( + modes: &MandatoryModeInsertions, + node: NodeIndex, + mode: &Mode, + ) -> Result, TranslateError> { + let entry = match mode.entry { Some(ExtendedMode::Entry(kernel)) => { - Resolved::Value(kernels.get(&kernel).copied().unwrap_or_default()) + Resolved::Value(modes.kernels.get(&kernel).copied().unwrap_or_default()) } Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), - None => { - if let Some(mode) = exit_cache.get(&index) { - return Some(*mode); - } - let result = get_exit_mode_from_dependencies( - cfg, kernels, index, getter, exit_cache, unknown, - ); - let result = unwrap_some_or!(result, return None); - exit_cache.insert(index, result); - unknown.remove(&index); - result - } + None => match &modes.propagation_state[node.index()] { + PropagationState::Fixed { entry, .. } => match entry { + None => return Err(error_unreachable()), + Some(ComputedValue::Conflict) => Resolved::Conflict, + Some(ComputedValue::Value(value, _)) => { + Resolved::Value(value.ok_or_else(error_unreachable)?) + } + }, + PropagationState::Propagated(computed_value) => match computed_value { + ComputedValue::Conflict => Resolved::Conflict, + ComputedValue::Value(value, kernels) => match value { + Some(value) => Resolved::Value(*value), + None => { + let mode = kernels.ones().try_fold(None, |state, kernel_index| { + let kernel_id = modes.kernel_propagation_lookup[kernel_index]; + let mode = + modes.kernels.get(&kernel_id).copied().unwrap_or_default(); + ControlFlow::Continue(Some(match state { + None => mode, + Some(existing) => { + if existing == mode { + existing + } else { + return ControlFlow::Break(()); + } + } + })) + }); + match mode { + ControlFlow::Continue(Some(value)) => Resolved::Value(value), + ControlFlow::Continue(None) => return Err(error_unreachable()), + ControlFlow::Break(()) => Resolved::Conflict, + } + } + }, + }, + }, }; - Some(exit) - } - fn get_entry_mode( - cfg: &ControlFlowGraph, - kernels: &FxHashMap, - index: NodeIndex, - node: &Node, - getter: &mut impl FnMut(&Node) -> Mode, - exit_cache: &mut FxHashMap>, - unknown: &mut FxHashSet, - ) -> Result, TranslateError> { - let mode = getter(node); - let entry = match mode.entry { - Some(ExtendedMode::Entry(kernel)) => { - Resolved::Value(kernels.get(&kernel).copied().unwrap_or_default()) - } + let exit = match mode.entry { Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), - None => { - unknown.clear(); - let result = get_exit_mode_from_dependencies( - cfg, kernels, index, getter, exit_cache, unknown, - ); - unwrap_some_or!(result, return Err(error_unreachable())) - } + Some(ExtendedMode::Entry(_)) | None => entry, }; - Ok(entry) + Ok(ResolvedMode { entry, exit }) } - fn resolve_node( - cfg: &ControlFlowGraph, - f32_denormal_kernels: &FxHashMap, - f16f64_denormal_kernels: &FxHashMap, - f32_rounding_kernels: &FxHashMap, - f16f64_rounding_kernels: &FxHashMap, + fn resolve_node_impl( + f32_denormal_modes: &MandatoryModeInsertions, + f16f64_denormal_modes: &MandatoryModeInsertions, + f32_rounding_modes: &MandatoryModeInsertions, + f16f64_rounding_modes: &MandatoryModeInsertions, index: NodeIndex, node: &Node, - visited: &mut FxHashSet, - denormal_f32_cache: &mut FxHashMap>, - denormal_f16f64_cache: &mut FxHashMap>, - rounding_f32_cache: &mut FxHashMap>, - rounding_f16f64_cache: &mut FxHashMap>, ) -> Result { - visited.clear(); - let denormal_f32_entry = get_entry_mode( - cfg, - f32_denormal_kernels, - index, - node, - &mut |node| node.denormal_f32, - denormal_f32_cache, - visited, - )?; - let denormal_f32_exit = match node.denormal_f32.exit { - Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), - Some(ExtendedMode::Entry(_)) | None => denormal_f32_entry, - }; - visited.clear(); - let denormal_f16f64_entry = get_entry_mode( - cfg, - f16f64_denormal_kernels, - index, - node, - &mut |node| node.denormal_f16f64, - denormal_f16f64_cache, - visited, - )?; - let denormal_f16f64_exit = match node.denormal_f16f64.exit { - Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), - Some(ExtendedMode::Entry(_)) | None => denormal_f16f64_entry, - }; - visited.clear(); - let rounding_f32_entry = get_entry_mode( - cfg, - f32_rounding_kernels, - index, - node, - &mut |node| node.rounding_f32, - rounding_f32_cache, - visited, - )?; - let rounding_f32_exit = match node.rounding_f32.exit { - Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), - Some(ExtendedMode::Entry(_)) | None => rounding_f32_entry, - }; - visited.clear(); - let rounding_f16f64_entry = get_entry_mode( - cfg, - f16f64_rounding_kernels, - index, - node, - &mut |node| node.rounding_f16f64, - rounding_f16f64_cache, - visited, - )?; - let rounding_f16f64_exit = match node.rounding_f16f64.exit { - Some(ExtendedMode::BasicBlock(bb)) => Resolved::Value(bb), - Some(ExtendedMode::Entry(_)) | None => rounding_f16f64_entry, - }; + let denormal_f32 = resolve_mode(f32_denormal_modes, index, &node.denormal_f32)?; + let denormal_f16f64 = + resolve_mode(f16f64_denormal_modes, index, &node.denormal_f16f64)?; + let rounding_f32 = resolve_mode(f32_rounding_modes, index, &node.rounding_f32)?; + let rounding_f16f64 = + resolve_mode(f16f64_rounding_modes, index, &node.rounding_f16f64)?; Ok(ResolvedNode { label: node.label, - denormal_f32: ResolvedMode { - entry: denormal_f32_entry, - exit: denormal_f32_exit, - }, - denormal_f16f64: ResolvedMode { - entry: denormal_f16f64_entry, - exit: denormal_f16f64_exit, - }, - rounding_f32: ResolvedMode { - entry: rounding_f32_entry, - exit: rounding_f32_exit, - }, - rounding_f16f64: ResolvedMode { - entry: rounding_f16f64_entry, - exit: rounding_f16f64_exit, - }, + denormal_f32, + denormal_f16f64, + rounding_f32, + rounding_f16f64, }) } - let mut error = None; - let mut visited = FxHashSet::default(); - let mut denormal_f32_cache = FxHashMap::default(); - let mut denormal_f16f64_cache = FxHashMap::default(); - let mut rounding_f32_cache = FxHashMap::default(); - let mut rounding_f16f64_cache = FxHashMap::default(); + fn resolve_node( + f32_denormal_modes: &MandatoryModeInsertions, + f16f64_denormal_modes: &MandatoryModeInsertions, + f32_rounding_modes: &MandatoryModeInsertions, + f16f64_rounding_modes: &MandatoryModeInsertions, + index: NodeIndex, + node: &Node, + error: &mut bool, + ) -> ResolvedNode { + match resolve_node_impl( + f32_denormal_modes, + f16f64_denormal_modes, + f32_rounding_modes, + f16f64_rounding_modes, + index, + node, + ) { + Ok(node) => node, + Err(_) => { + *error = true; + ResolvedNode { + label: SpirvWord(u32::MAX), + denormal_f32: ResolvedMode { + entry: Resolved::Conflict, + exit: Resolved::Conflict, + }, + denormal_f16f64: ResolvedMode { + entry: Resolved::Conflict, + exit: Resolved::Conflict, + }, + rounding_f32: ResolvedMode { + entry: Resolved::Conflict, + exit: Resolved::Conflict, + }, + rounding_f16f64: ResolvedMode { + entry: Resolved::Conflict, + exit: Resolved::Conflict, + }, + } + } + } + } + let mut error = false; let graph = cfg.graph.map( |index, node| { - let maybe_node = resolve_node( - &cfg, - f32_denormal_kernels, - f16f64_denormal_kernels, - f32_rounding_kernels, - f16f64_rounding_kernels, + resolve_node( + f32_denormal_modes, + f16f64_denormal_modes, + f32_rounding_modes, + f16f64_rounding_modes, index, node, - &mut visited, - &mut denormal_f32_cache, - &mut denormal_f16f64_cache, - &mut rounding_f32_cache, - &mut rounding_f16f64_cache, - ); - match maybe_node { - Ok(node) => node, - Err(e) => { - error = Some(e); - ResolvedNode { - label: node.label, - denormal_f32: ResolvedMode { - entry: Resolved::Conflict, - exit: Resolved::Conflict, - }, - denormal_f16f64: ResolvedMode { - entry: Resolved::Conflict, - exit: Resolved::Conflict, - }, - rounding_f32: ResolvedMode { - entry: Resolved::Conflict, - exit: Resolved::Conflict, - }, - rounding_f16f64: ResolvedMode { - entry: Resolved::Conflict, - exit: Resolved::Conflict, - }, - } - } - } + &mut error, + ) }, |_, ()| (), ); - if let Some(error) = error { - Err(error) + if error { + Err(error_unreachable()) } else { Ok(Self { basic_blocks: cfg.basic_blocks, @@ -775,10 +661,10 @@ fn compute_full_mode_insertions( ) -> Result { let cfg = ResolvedControlFlowGraph::new( cfg, - &denormal_f32.kernels, - &denormal_f16f64.kernels, - &rounding_f32.kernels, - &rounding_f16f64.kernels, + &denormal_f32, + &denormal_f16f64, + &rounding_f32, + &rounding_f16f64, )?; join_modes( flat_resolver, @@ -1688,6 +1574,48 @@ impl<'a> Drop for BasicBlockState<'a> { } } +// For every basic block this function computes two pieces of information: +// * Is this basic block have a conflict at entry (i.e. different modes from +// different predecessors) and will certainly require inserting a +// mode instruction? +// * Does this basic block value has a value that can lead to a conflict if we +// pick a different mode in one of its preceding kernels +// Computing this is surprisingly difficult and here's a list of approaches that +// __failed__: +// * Simply traversing all the predecessors and merging their modes - it works +// fine, but gets really slow on large modules (megabytes of PTX code) +// * Traversing all the predecessors but caching intermediate results - it +// does not work too well, because you can't really cache intermediates. +// Consider a graph like this: +// ┌─────┐ ┌─────┐ ┌─────┐ +// │ D │<───┤ C │ │ Z │ +// └─────┘ └─────┘ └─────┘ +// ▲ │ │ +// │ ▼ ▼ +// │ ┌──────────────┐ +// └───────┤ B │ +// └──────────────┘ +// │ +// ▼ +// ┌─────┐ +// │ A │ +// └─────┘ +// Imagine we start at A and we are visiting predecessors depth-first with +// particular order being A->B->C->D. Now from D we can't visit B again, +// because we have to avoid cycles, we can't know whether B's value depends on +// Z or not. This kills cachability completely +// * Computing strongly-connected components does not seem to help much either. +// It neatly gets rid of cycles, but we still have a fairly large graph (half +// as many nodes as the original) and you still need to do whatever expensive +// computations you had to be within each component. In a component, each +// node is reachable from every other node, but that does not mean the value +// of every node is relevant to every node. Consider a simple graph like this: +// A -> B -> C. If B has a fixed exit value, then C's value does not depend on +// A at all. +// Current solution is to simply propagate values. We start with all basic block +// that have an exit value and propagate it to all the outgoing neighbors until +// there's nothing more to propagate. While it sounds expensive, in practice it +// converges quickly enough fn compute_single_mode_insertions( cfg: &ControlFlowGraph, mut get_mode: impl FnMut(&Node) -> Mode, @@ -1866,13 +1794,15 @@ impl PropagationState { struct PartialModeInsertion { bb_must_insert_mode: FxHashSet, bb_maybe_insert_mode: PotentialModeInsertionsDueToKernelMode, + propagation_state: Vec>, + kernel_propagation_lookup: Vec, } impl PartialModeInsertion { fn new( cfg: &ControlFlowGraph, kernel_map: FxHashMap, - state: Vec>, + propagation_state: Vec>, ) -> Result { let mut reverse_kernel_map = vec![SpirvWord(u32::MAX); kernel_map.len()]; for (kernel_id, kernel_index) in kernel_map { @@ -1881,7 +1811,7 @@ impl PartialModeInsertion { let mut bb_must_insert_mode = FxHashSet::default(); let mut fast_mode_from_kernels = Some(FxHashMap::default()); let mut slow_mode_from_kernels = FxHashMap::default(); - for (node_index, bb_state) in state.into_iter().enumerate() { + for (node_index, bb_state) in propagation_state.iter().enumerate() { if let PropagationState::Fixed { entry: Some(ComputedValue::Conflict), .. @@ -1908,7 +1838,7 @@ impl PartialModeInsertion { let (_, slow_kernels_source) = slow_mode_from_kernels .entry(node_id) .or_insert((value, FxHashSet::default())); - for kernel_index in kernels.into_ones() { + for kernel_index in kernels.ones() { let kernel_id = reverse_kernel_map[kernel_index]; if let Some(fast_mode_ref) = &mut fast_mode_from_kernels { let old_value = fast_mode_ref.insert(kernel_id, value); @@ -1923,11 +1853,13 @@ impl PartialModeInsertion { } } Ok(PartialModeInsertion { + propagation_state, bb_must_insert_mode, bb_maybe_insert_mode: match fast_mode_from_kernels { Some(modes) => PotentialModeInsertionsDueToKernelMode::QuickMode(modes), None => PotentialModeInsertionsDueToKernelMode::SlowMode(slow_mode_from_kernels), }, + kernel_propagation_lookup: reverse_kernel_map, }) } } @@ -1992,6 +1924,8 @@ fn optimize_mode_insertions< return Ok(MandatoryModeInsertions { basic_blocks: partial.bb_must_insert_mode, kernels: modes, + propagation_state: partial.propagation_state, + kernel_propagation_lookup: partial.kernel_propagation_lookup, }); } PotentialModeInsertionsDueToKernelMode::SlowMode(slow_modes) => slow_modes, @@ -2042,6 +1976,8 @@ fn optimize_mode_insertions< Ok(MandatoryModeInsertions { basic_blocks, kernels, + propagation_state: partial.propagation_state, + kernel_propagation_lookup: partial.kernel_propagation_lookup, }) } @@ -2067,6 +2003,8 @@ fn one_of(problem: &mut highs::RowProblem) -> [highs::Col; N] { struct MandatoryModeInsertions { basic_blocks: FxHashSet, kernels: FxHashMap, + propagation_state: Vec>, + kernel_propagation_lookup: Vec, } #[derive(Eq, PartialEq, Clone, Copy)] From c58cc6e8c8a13847934591ff8ca58b60e7293d59 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Fri, 24 Oct 2025 02:34:23 +0000 Subject: [PATCH 21/22] Fix bugs --- Cargo.lock | 1 + .../instruction_mode_to_global_mode/mod.rs | 92 +++++++++++++------ 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6eec346..822dbae3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2546,6 +2546,7 @@ dependencies = [ "comgr", "cuda_macros", "cuda_types", + "fixedbitset", "half", "highs", "hip_runtime-sys", diff --git a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs index bbde22c3..285ec5e9 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/mod.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/mod.rs @@ -1648,14 +1648,14 @@ fn compute_single_mode_insertions( while let Some(current) = roots.pop() { for next in cfg.graph.neighbors_directed(current, Direction::Outgoing) { if current.index() == next.index() { - if propagation_state[current.index()].merge_with_self(kernel_ids.len())? { + if propagation_state[current.index()].merge_with_self()? { roots.push(current); } } else { let [current_state, next_state] = propagation_state .get_disjoint_mut([current.index(), next.index()]) .map_err(|_| error_unreachable())?; - if next_state.merge_from(current_state, kernel_ids.len())? { + if next_state.merge_from(current_state)? { roots.push(next); } } @@ -1675,6 +1675,61 @@ enum ComputedValue { Value(Option, FixedBitSet), } +impl ComputedValue { + fn merge_with(self, from: Option<(Option, &FixedBitSet)>) -> (Self, bool) { + fn merge_kernels(mut kernels: FixedBitSet, other: &FixedBitSet) -> FixedBitSet { + kernels.union_with(other); + kernels + } + match (self, from) { + (ComputedValue::Conflict, _) => (ComputedValue::Conflict, false), + (ComputedValue::Value(..), None) => (ComputedValue::Conflict, true), + (ComputedValue::Value(value, kernels), Some((other_value, other_kernels))) => { + match (value, other_value) { + (Some(value), Some(other_value)) => { + return if value != other_value { + (ComputedValue::Conflict, true) + } else { + if &kernels != other_kernels { + ( + ComputedValue::Value( + Some(value), + merge_kernels(kernels, other_kernels), + ), + true, + ) + } else { + (ComputedValue::Value(Some(value), kernels), false) + } + } + } + (Some(value), None) => ( + ComputedValue::Value(Some(value), merge_kernels(kernels, other_kernels)), + true, + ), + (None, Some(other_value)) => ( + ComputedValue::Value( + Some(other_value), + merge_kernels(kernels, other_kernels), + ), + true, + ), + (None, None) => { + if &kernels != other_kernels { + ( + ComputedValue::Value(None, merge_kernels(kernels, other_kernels)), + true, + ) + } else { + (ComputedValue::Value(None, kernels), false) + } + } + } + } + } + } +} + enum PropagationState { // This basic block sets a mode, hence its mode at the exit is independent from its predecessors Fixed { @@ -1740,12 +1795,12 @@ impl PropagationState { } } - fn merge_with_self(&mut self, kernel_count: usize) -> Result { + fn merge_with_self(&mut self) -> Result { Ok(match self { PropagationState::Fixed { entry, exit } => match entry { None => return Err(error_unreachable()), Some(ComputedValue::Conflict) => false, - Some(ComputedValue::Value(value, kernels)) => match exit { + Some(ComputedValue::Value(value, _)) => match exit { RootExitValue::Value(new_value) => { if *value != Some(*new_value) { *value = Some(*new_value); @@ -1754,40 +1809,19 @@ impl PropagationState { false } } - RootExitValue::Entry(other_kernels) => { - if kernels != other_kernels { - kernels.grow(kernel_count); - kernels.union_with(other_kernels); - true - } else { - false - } - } + RootExitValue::Entry(_) => return Err(error_unreachable()), }, }, PropagationState::Propagated(_) => false, }) } - fn merge_from(&mut self, from: &Self, kernel_count: usize) -> Result { + fn merge_from(&mut self, from: &Self) -> Result { let (self_ref, can_propagate) = self.get_entry()?; let this = mem::replace(self_ref, ComputedValue::Conflict); - let (this, is_different) = match (this, from.get_exit()) { - (ComputedValue::Conflict, _) => (ComputedValue::Conflict, false), - (ComputedValue::Value(..), None) => (ComputedValue::Conflict, can_propagate), - (ComputedValue::Value(value, mut kernels), Some((other_value, other_kernels))) => { - let new_value = value.or(other_value); - if new_value != value || &kernels != other_kernels { - kernels.grow(kernel_count); - kernels.union_with(other_kernels); - (ComputedValue::Value(new_value, kernels), can_propagate) - } else { - (ComputedValue::Value(value, kernels), false) - } - } - }; + let (this, is_different) = this.merge_with(from.get_exit()); *self_ref = this; - Ok(is_different) + Ok(is_different && can_propagate) } } From 4ef02079ed20abf20da993e290339e12a65a61f2 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Fri, 24 Oct 2025 02:34:42 +0000 Subject: [PATCH 22/22] Fix some tests --- .../instruction_mode_to_global_mode/test.rs | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/ptx/src/pass/instruction_mode_to_global_mode/test.rs b/ptx/src/pass/instruction_mode_to_global_mode/test.rs index 8d2b4933..77925ecb 100644 --- a/ptx/src/pass/instruction_mode_to_global_mode/test.rs +++ b/ptx/src/pass/instruction_mode_to_global_mode/test.rs @@ -27,6 +27,13 @@ fn preserve() -> InstructionModes { } } +fn unwrap_quick_mode(x: &PotentialModeInsertionsDueToKernelMode) -> &FxHashMap { + match x { + PotentialModeInsertionsDueToKernelMode::QuickMode(x) => x, + PotentialModeInsertionsDueToKernelMode::SlowMode(_) => panic!(), + } +} + #[test] fn transitive_mixed() { let mut graph = ControlFlowGraph::new(); @@ -46,11 +53,9 @@ fn transitive_mixed() { let partial_result = super::compute_single_mode_insertions(&graph, |node| node.denormal_f32).unwrap(); assert_eq!(partial_result.bb_must_insert_mode.len(), 0); - assert_eq!(partial_result.bb_maybe_insert_mode.len(), 1); - assert_eq!( - partial_result.bb_maybe_insert_mode[&false_id], - (DenormalMode::FlushToZero, iter::once(entry_id).collect()) - ); + let kernel_insert_mode = unwrap_quick_mode(&partial_result.bb_maybe_insert_mode); + assert_eq!(kernel_insert_mode.len(), 1); + assert_eq!(kernel_insert_mode[&entry_id], (DenormalMode::FlushToZero)); let result = optimize_mode_insertions::(partial_result).unwrap(); @@ -79,11 +84,9 @@ fn transitive_change_twice() { super::compute_single_mode_insertions(&graph, |node| node.denormal_f32).unwrap(); assert_eq!(partial_result.bb_must_insert_mode.len(), 1); assert!(partial_result.bb_must_insert_mode.contains(&true_id)); - assert_eq!(partial_result.bb_maybe_insert_mode.len(), 1); - assert_eq!( - partial_result.bb_maybe_insert_mode[&false_id], - (DenormalMode::FlushToZero, iter::once(entry_id).collect()) - ); + let kernel_insert_mode = unwrap_quick_mode(&partial_result.bb_maybe_insert_mode); + assert_eq!(kernel_insert_mode.len(), 1); + assert_eq!(kernel_insert_mode[&entry_id], DenormalMode::FlushToZero); let result = optimize_mode_insertions::(partial_result).unwrap(); @@ -92,6 +95,7 @@ fn transitive_change_twice() { assert_eq!(result.kernels[&entry_id], DenormalMode::FlushToZero); } +/* #[test] fn transitive_change() { let mut graph = ControlFlowGraph::new(); @@ -344,6 +348,7 @@ fn call_with_mode() { assert_eq!(post_add2_set_denormal, add2); assert_eq!(post_add2, ret); } + */ fn branches( fn_: &Vec, SpirvWord>>,