From 024a148aaadce4ac86cb94e95600393d4d325634 Mon Sep 17 00:00:00 2001 From: FiveMovesAhead Date: Wed, 21 May 2025 23:38:44 +0100 Subject: [PATCH] Add deterministic hasher. --- tig-algorithms/Cargo.toml | 1 + tig-algorithms/src/hypergraph/template.rs | 6 ++++++ tig-algorithms/src/knapsack/template.rs | 6 ++++++ tig-algorithms/src/lib.rs | 11 +++++++++++ tig-algorithms/src/satisfiability/template.rs | 6 ++++++ tig-algorithms/src/vector_search/template.rs | 6 ++++++ tig-algorithms/src/vehicle_routing/template.rs | 6 ++++++ 7 files changed, 42 insertions(+) diff --git a/tig-algorithms/Cargo.toml b/tig-algorithms/Cargo.toml index 8b3c89c..b76f412 100644 --- a/tig-algorithms/Cargo.toml +++ b/tig-algorithms/Cargo.toml @@ -7,6 +7,7 @@ repository.workspace = true edition.workspace = true [dependencies] +ahash = "0.8.12" anyhow = "1.0.81" cudarc = { git = "https://github.com/tig-foundation/cudarc.git", branch = "automatic-fuel-check", features = [ "cuda-version-from-build-system", diff --git a/tig-algorithms/src/hypergraph/template.rs b/tig-algorithms/src/hypergraph/template.rs index bc72bf4..81a61de 100644 --- a/tig-algorithms/src/hypergraph/template.rs +++ b/tig-algorithms/src/hypergraph/template.rs @@ -36,6 +36,7 @@ acknowledgments below: */ // TIG's UI uses the pattern `tig_challenges::` to automatically detect your algorithm's challenge +use crate::{seeded_hasher, HashMap, HashSet}; use anyhow::{anyhow, Result}; use cudarc::{ driver::{safe::LaunchConfig, CudaModule, CudaStream, PushKernelArg}, @@ -74,6 +75,11 @@ pub fn solve_sub_instance( // use rand::{rngs::SmallRng, Rng, SeedableRng}; // let mut rng = SmallRng::from_seed(challenge.seed); + // If you need HashMap or HashSet, make sure to use a deterministic hasher for consistent runtime_signature: + // use crate::{seeded_hasher, HashMap, HashSet}; + // let hasher = seeded_hasher(&instance.seed); + // let map = HashMap::with_hasher(hasher); + // when launching kernels, you should hardcode the LaunchConfig for determinism: // Example: // LaunchConfig { diff --git a/tig-algorithms/src/knapsack/template.rs b/tig-algorithms/src/knapsack/template.rs index 881feae..3a64e73 100644 --- a/tig-algorithms/src/knapsack/template.rs +++ b/tig-algorithms/src/knapsack/template.rs @@ -36,6 +36,7 @@ acknowledgments below: */ // TIG's UI uses the pattern `tig_challenges::` to automatically detect your algorithm's challenge +use crate::{seeded_hasher, HashMap, HashSet}; use anyhow::{anyhow, Result}; use tig_challenges::knapsack::*; @@ -59,6 +60,11 @@ pub fn solve_sub_instance(instance: &SubInstance) -> Result> // use rand::{rngs::SmallRng, Rng, SeedableRng}; // let mut rng = SmallRng::from_seed(instance.seed); + // If you need HashMap or HashSet, make sure to use a deterministic hasher for consistent runtime_signature: + // use crate::{seeded_hasher, HashMap, HashSet}; + // let hasher = seeded_hasher(&instance.seed); + // let map = HashMap::with_hasher(hasher); + // return Err() if your algorithm encounters an error // return Ok(None) if your algorithm finds no solution or needs to exit early // return Ok(SubSolution { .. }) if your algorithm finds a solution diff --git a/tig-algorithms/src/lib.rs b/tig-algorithms/src/lib.rs index cf41e17..3f7af0f 100644 --- a/tig-algorithms/src/lib.rs +++ b/tig-algorithms/src/lib.rs @@ -1,3 +1,14 @@ +use ahash::RandomState; +pub fn seeded_hasher(seed: &[u8; 32]) -> RandomState { + let seed1 = u64::from_be_bytes(seed[0..8].try_into().unwrap()); + let seed2 = u64::from_be_bytes(seed[8..16].try_into().unwrap()); + let seed3 = u64::from_be_bytes(seed[16..24].try_into().unwrap()); + let seed4 = u64::from_be_bytes(seed[24..32].try_into().unwrap()); + RandomState::with_seeds(seed1, seed2, seed3, seed4) +} +pub(crate) type HashMap = std::collections::HashMap; +pub(crate) type HashSet = std::collections::HashSet; + pub const BUILD_TIME_PATH: &str = env!("CARGO_MANIFEST_DIR"); pub mod knapsack; diff --git a/tig-algorithms/src/satisfiability/template.rs b/tig-algorithms/src/satisfiability/template.rs index 238ac72..2c37063 100644 --- a/tig-algorithms/src/satisfiability/template.rs +++ b/tig-algorithms/src/satisfiability/template.rs @@ -36,6 +36,7 @@ acknowledgments below: */ // TIG's UI uses the pattern `tig_challenges::` to automatically detect your algorithm's challenge +use crate::{seeded_hasher, HashMap, HashSet}; use anyhow::{anyhow, Result}; use tig_challenges::satisfiability::*; @@ -44,6 +45,11 @@ pub fn solve_challenge(challenge: &Challenge) -> Result> { // use rand::{rngs::SmallRng, Rng, SeedableRng}; // let mut rng = SmallRng::from_seed(challenge.seed); + // If you need HashMap or HashSet, make sure to use a deterministic hasher for consistent runtime_signature: + // use crate::{seeded_hasher, HashMap, HashSet}; + // let hasher = seeded_hasher(&challenge.seed); + // let map = HashMap::with_hasher(hasher); + // return Err() if your algorithm encounters an error // return Ok(None) if your algorithm finds no solution or needs to exit early // return Ok(Solution { .. }) if your algorithm finds a solution diff --git a/tig-algorithms/src/vector_search/template.rs b/tig-algorithms/src/vector_search/template.rs index c29bf91..86bdaf1 100644 --- a/tig-algorithms/src/vector_search/template.rs +++ b/tig-algorithms/src/vector_search/template.rs @@ -36,6 +36,7 @@ acknowledgments below: */ // TIG's UI uses the pattern `tig_challenges::` to automatically detect your algorithm's challenge +use crate::{seeded_hasher, HashMap, HashSet}; use anyhow::{anyhow, Result}; use cudarc::{ driver::{safe::LaunchConfig, CudaModule, CudaStream, PushKernelArg}, @@ -57,6 +58,11 @@ pub fn solve_challenge( // use rand::{rngs::SmallRng, Rng, SeedableRng}; // let mut rng = SmallRng::from_seed(challenge.seed); + // If you need HashMap or HashSet, make sure to use a deterministic hasher for consistent runtime_signature: + // use crate::{seeded_hasher, HashMap, HashSet}; + // let hasher = seeded_hasher(&challenge.seed); + // let map = HashMap::with_hasher(hasher); + // when launching kernels, you should hardcode the LaunchConfig for determinism: // Example: // LaunchConfig { diff --git a/tig-algorithms/src/vehicle_routing/template.rs b/tig-algorithms/src/vehicle_routing/template.rs index 4cfa646..2d323a4 100644 --- a/tig-algorithms/src/vehicle_routing/template.rs +++ b/tig-algorithms/src/vehicle_routing/template.rs @@ -36,6 +36,7 @@ acknowledgments below: */ // TIG's UI uses the pattern `tig_challenges::` to automatically detect your algorithm's challenge +use crate::{seeded_hasher, HashMap, HashSet}; use anyhow::{anyhow, Result}; use tig_challenges::vehicle_routing::*; @@ -59,6 +60,11 @@ pub fn solve_sub_instance(instance: &SubInstance) -> Result> // use rand::{rngs::SmallRng, Rng, SeedableRng}; // let mut rng = SmallRng::from_seed(instance.seed); + // If you need HashMap or HashSet, make sure to use a deterministic hasher for consistent runtime_signature: + // use crate::{seeded_hasher, HashMap, HashSet}; + // let hasher = seeded_hasher(&instance.seed); + // let map = HashMap::with_hasher(hasher); + // return Err() if your algorithm encounters an error // return Ok(None) if your algorithm finds no solution or needs to exit early // return Ok(SubSolution { .. }) if your algorithm finds a solution