Add deterministic hasher.

This commit is contained in:
FiveMovesAhead 2025-05-21 23:38:44 +01:00
parent abbe0bbec8
commit 024a148aaa
7 changed files with 42 additions and 0 deletions

View File

@ -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",

View File

@ -36,6 +36,7 @@ acknowledgments below:
*/
// TIG's UI uses the pattern `tig_challenges::<challenge_name>` 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 {

View File

@ -36,6 +36,7 @@ acknowledgments below:
*/
// TIG's UI uses the pattern `tig_challenges::<challenge_name>` 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<Option<SubSolution>>
// 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(<msg>) 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

View File

@ -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<K, V> = std::collections::HashMap<K, V, RandomState>;
pub(crate) type HashSet<T> = std::collections::HashSet<T, RandomState>;
pub const BUILD_TIME_PATH: &str = env!("CARGO_MANIFEST_DIR");
pub mod knapsack;

View File

@ -36,6 +36,7 @@ acknowledgments below:
*/
// TIG's UI uses the pattern `tig_challenges::<challenge_name>` 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<Option<Solution>> {
// 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(<msg>) 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

View File

@ -36,6 +36,7 @@ acknowledgments below:
*/
// TIG's UI uses the pattern `tig_challenges::<challenge_name>` 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 {

View File

@ -36,6 +36,7 @@ acknowledgments below:
*/
// TIG's UI uses the pattern `tig_challenges::<challenge_name>` 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<Option<SubSolution>>
// 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(<msg>) 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