-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
909 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "tig-native-wrapper" | ||
version = "0.1.0" | ||
readme = "README.md" | ||
license = "https://github.com/tig-foundation/tig-monorepo/tree/main/docs/agreements/end_user_license_agreement.pdf" | ||
authors.workspace = true | ||
repository.workspace = true | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
serde = { version = "1.0.196", features = ["derive"] } | ||
serde_json = { version = "1.0.113" } | ||
libloading = "0.8.6" | ||
tig-challenges = { path = "../tig-challenges" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use { | ||
libloading::{Library}, | ||
std::path::Path, | ||
std::panic, | ||
}; | ||
|
||
pub fn load_module(path: &Path) -> Result<Library, String> | ||
{ | ||
let res = panic::catch_unwind(|| { | ||
unsafe { Library::new(path) } | ||
}); | ||
|
||
match res { | ||
Ok(lib_result) => lib_result.map_err(|e| e.to_string()), | ||
Err(_) => Err("Failed to load module".to_string()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
mod dylib; | ||
mod solvers; | ||
|
||
use { | ||
tig_challenges::{ | ||
knapsack::{Challenge as KnapsackChallenge}, | ||
satisfiability::{Challenge as SatisfiabilityChallenge}, | ||
vector_search::{Challenge as VectorSearchChallenge}, | ||
vehicle_routing::{Challenge as VehicleRoutingChallenge}, | ||
}, | ||
}; | ||
|
||
macro_rules! handle_challenge { | ||
($challenge_type:expr, $challenge_json:expr, $library_path:expr, $max_fuel:expr, $solver_fn:ident, $challenge:ty) => { | ||
{ | ||
let challenge: $challenge = serde_json::from_str($challenge_json) | ||
.map_err(|e| format!("Failed to parse challenge: {}", e)) | ||
.unwrap(); | ||
|
||
let result = solvers::$solver_fn( | ||
$library_path.to_string(), | ||
challenge, | ||
Some($max_fuel) | ||
); | ||
|
||
if result.is_err() | ||
{ | ||
println!("Error: {:?}", result.err().unwrap()); | ||
return; | ||
} | ||
|
||
let (solution, runtime_signature, fuel_remaining) = result.unwrap(); | ||
println!("{}", serde_json::json!({ | ||
"solution": solution, | ||
"runtime_signature": runtime_signature, | ||
"fuel_consumed": $max_fuel as i64 - fuel_remaining.max(0), | ||
"nonce": 0, | ||
})); | ||
} | ||
}; | ||
} | ||
|
||
fn main() | ||
{ | ||
let args: Vec<String> = std::env::args().collect(); | ||
if args.len() != 5 | ||
{ | ||
println!("Usage: {} <library_path> <challenge_type> <challenge_json> <max_fuel>", args[0]); | ||
println!("Challenge types: knapsack, satisfiability, vector_search, vehicle_routing"); | ||
return; | ||
} | ||
|
||
let library_path = &args[1]; | ||
let challenge_type = &args[2]; | ||
let challenge_json = &args[3]; | ||
let max_fuel = args[4].parse::<u64>().unwrap(); | ||
|
||
match challenge_type.as_str() | ||
{ | ||
"knapsack" => handle_challenge!(challenge_type, challenge_json, library_path, max_fuel, solve_knapsack, KnapsackChallenge), | ||
"satisfiability" => handle_challenge!(challenge_type, challenge_json, library_path, max_fuel, solve_satisfiability, SatisfiabilityChallenge), | ||
"vector_search" => handle_challenge!(challenge_type, challenge_json, library_path, max_fuel, solve_vector_search, VectorSearchChallenge), | ||
"vehicle_routing" => handle_challenge!(challenge_type, challenge_json, library_path, max_fuel, solve_vehicle_routing, VehicleRoutingChallenge), | ||
_ => | ||
{ | ||
println!("Invalid challenge type"); | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use { | ||
std::path::PathBuf, | ||
tig_challenges::{ | ||
knapsack::{Challenge as KnapsackChallenge, Solution as KnapsackSolution}, | ||
satisfiability::{Challenge as SatisfiabilityChallenge, Solution as SatisfiabilitySolution}, | ||
vector_search::{Challenge as VectorSearchChallenge, Solution as VectorSearchSolution}, | ||
vehicle_routing::{Challenge as VehicleRoutingChallenge, Solution as VehicleRoutingSolution}, | ||
}, | ||
crate::dylib::{load_module} | ||
}; | ||
|
||
macro_rules! generate_solvers { | ||
($(($name:ident, $challenge:ty, $solution:ty)),* $(,)?) => { | ||
$( | ||
pub fn $name( | ||
library_path: String, | ||
challenge: $challenge, | ||
fuel: Option<u64> | ||
) -> Result<($solution, u64, i64), String> | ||
{ | ||
let library = load_module(&PathBuf::from(library_path))?; | ||
let solve_fn = unsafe { library.get::<fn($challenge, Option<u64>) -> Option<$solution>>(b"entry_point").map_err(|e| e.to_string())? }; | ||
|
||
if fuel.is_some() | ||
{ | ||
let fuel_remaining_ptr = unsafe { *library.get::<*mut i64>(b"__fuel_remaining").map_err(|e| e.to_string())? }; | ||
unsafe { *fuel_remaining_ptr = fuel.unwrap() as i64 }; | ||
} | ||
|
||
let solution = solve_fn(challenge, fuel).ok_or_else(|| "Solver returned None".to_string())?; | ||
|
||
let fuel_remaining_ptr = unsafe { *library.get::<*const i64>(b"__fuel_remaining").map_err(|e| e.to_string())? }; | ||
let runtime_signature_ptr = unsafe { *library.get::<*const u64>(b"__runtime_signature").map_err(|e| e.to_string())? }; | ||
let fuel_remaining = unsafe { *fuel_remaining_ptr }; | ||
let runtime_signature = unsafe { *runtime_signature_ptr }; | ||
|
||
Ok((solution, runtime_signature, fuel_remaining)) | ||
} | ||
)* | ||
}; | ||
} | ||
|
||
generate_solvers!( | ||
(solve_knapsack, KnapsackChallenge, KnapsackSolution), | ||
(solve_satisfiability, SatisfiabilityChallenge, SatisfiabilitySolution), | ||
(solve_vector_search, VectorSearchChallenge, VectorSearchSolution), | ||
(solve_vehicle_routing, VehicleRoutingChallenge, VehicleRoutingSolution) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "tig-native" | ||
version = "0.1.0" | ||
readme = "README.md" | ||
license = "https://github.com/tig-foundation/tig-monorepo/tree/main/docs/agreements/end_user_license_agreement.pdf" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "staticlib", "rlib"] | ||
|
||
[dependencies] | ||
tig-challenges = { path = "../tig-challenges" } | ||
|
||
[workspace] | ||
|
||
[features] | ||
knapsack = [] | ||
vector_search = [] | ||
satisfiability = [] | ||
vehicle_routing = [] | ||
entry_point = [] |
Oops, something went wrong.