Skip to content

Commit

Permalink
Use string hash key
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDubray committed Aug 20, 2024
1 parent 9690277 commit feec913
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 128 deletions.
5 changes: 2 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use clap::ValueEnum;
use rug::Float;
use std::hash::Hash;
use crate::core::bitvec::Bitvec;

macro_rules! F128 {
($v:expr) => {
Expand Down Expand Up @@ -80,11 +79,11 @@ impl std::fmt::Display for ApproximateMethod {
#[derive(Default, Clone)]
pub struct CacheKey {
hash: u64,
repr: Bitvec,
repr: String,
}

impl CacheKey {
pub fn new(hash: u64, repr: Bitvec) -> Self {
pub fn new(hash: u64, repr: String) -> Self {
Self {
hash,
repr,
Expand Down
52 changes: 0 additions & 52 deletions src/core/bitvec.rs

This file was deleted.

23 changes: 0 additions & 23 deletions src/core/clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ pub struct Clause {
/// Has the clause been learned during the search
is_learned: bool,
in_degree: usize,
bitmask: u128,
bitword_index: usize,
}

impl Clause {
Expand All @@ -66,8 +64,6 @@ impl Clause {
hash: rand::random(),
is_learned,
in_degree: 0,
bitmask: 0,
bitword_index: 0,
}
}

Expand Down Expand Up @@ -269,25 +265,6 @@ impl Clause {
pub fn in_degree(&self) -> usize {
self.in_degree
}

pub fn set_bitmask(&mut self, mask: u128) {
self.bitmask = mask;
}

pub fn set_bitword_index(&mut self, index: usize) {
self.bitword_index = index;
}

#[inline(always)]
pub fn bitmask(&self) -> u128 {
self.bitmask
}

#[inline(always)]
pub fn bitword_index(&self) -> usize {
self.bitword_index
}

}

// Writes a clause as C{id}: l1 l2 ... ln
Expand Down
45 changes: 31 additions & 14 deletions src/core/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
//! This module also exposes a special component extractor that do not detect any components.
//! It should only be used for debugging purpose to isolate bugs

use super::problem::{ClauseIndex, Problem, DistributionIndex};
use super::problem::{ClauseIndex, VariableIndex, Problem, DistributionIndex};
use crate::common::CacheKey;
use search_trail::{ReversibleUsize, StateManager, UsizeManager};
use crate::core::bitvec::Bitvec;

/// Abstraction used as a typesafe way of retrieving a `Component`
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -92,8 +91,8 @@ pub struct Component {
/// Maximum probability of the sub-problem represented by the component (i.e., all remaining
/// valid interpretation are models)
max_probability: f64,
/// Bitwise representation of the problem
bit_repr: Bitvec,
/// Representation of the component for hash
repr: String,
}

impl Component {
Expand All @@ -107,7 +106,7 @@ impl Component {
}

pub fn get_cache_key(&self) -> CacheKey {
CacheKey::new(self.hash, self.bit_repr.clone())
CacheKey::new(self.hash, self.repr.clone())
}

pub fn number_distribution(&self) -> usize {
Expand All @@ -122,15 +121,14 @@ impl ComponentExtractor {
let clause_positions = (0..g.number_clauses()).collect();
let distributions = (0..g.number_distributions()).map(DistributionIndex).collect();
let distribution_positions = (0..g.number_distributions()).collect();
let bit_repr = Bitvec::ones(g.number_clauses_problem() + g.number_variables());
let components = vec![Component {
start: 0,
size: g.number_clauses(),
distribution_start: 0,
number_distribution: g.number_distributions(),
hash: 0,
max_probability: 1.0,
bit_repr,
repr: String::new(),
}];
Self {
clauses: nodes,
Expand Down Expand Up @@ -180,14 +178,15 @@ impl ComponentExtractor {
comp_number_distribution: &mut usize,
hash: &mut u64,
max_probability: &mut f64,
bit_repr: &mut Bitvec,
clauses: &mut Vec<ClauseIndex>,
variables: &mut Vec<VariableIndex>,
state: &mut StateManager,
) {
while let Some(clause) = self.exploration_stack.pop() {
if self.is_node_visitable(g, clause, comp_start, comp_size, state) {
if !g[clause].is_learned() {
*hash ^= g[clause].hash();
bit_repr.set_bit(g[clause].bitword_index(), g[clause].bitmask());
clauses.push(clause);
}
// The clause is swap with the clause at position comp_sart + comp_size
let current_pos = self.clause_positions[clause.0];
Expand All @@ -207,7 +206,7 @@ impl ComponentExtractor {
if !self.seen_var[variable.0] && !g[variable].is_fixed(state) {
self.seen_var[variable.0] = true;
*hash ^= g[variable].hash();
bit_repr.set_bit(g[variable].bitword_index(), g[variable].bitmask());
variables.push(variable);
}
}

Expand Down Expand Up @@ -286,7 +285,8 @@ impl ComponentExtractor {
let mut hash: u64 = 0;
let mut number_distribution = 0;
let mut max_probability = 1.0;
let mut bit_repr = Bitvec::new(g.number_clauses_problem() + g.number_variables());
let mut clauses: Vec<ClauseIndex> = vec![];
let mut variables: Vec<VariableIndex> = vec![];
self.exploration_stack.push(clause);
self.explore_component(
g,
Expand All @@ -296,11 +296,21 @@ impl ComponentExtractor {
&mut number_distribution,
&mut hash,
&mut max_probability,
&mut bit_repr,
&mut clauses,
&mut variables,
state,
);
if number_distribution > 0 {
self.components.push(Component { start, size, distribution_start, number_distribution, hash, max_probability, bit_repr});
clauses.sort();
variables.sort();
let mut repr = String::new();
for c in clauses.iter().copied() {
repr.push_str(&format!("c{}", c.0));
}
for v in clauses.iter().copied() {
repr.push_str(&format!("v{}", v.0));
}
self.components.push(Component { start, size, distribution_start, number_distribution, hash, max_probability, repr});
}
distribution_start += number_distribution;
start += size;
Expand Down Expand Up @@ -365,14 +375,21 @@ impl ComponentExtractor {
self.distributions.shrink_to_fit();
self.distribution_positions.truncate(number_distribution);
self.distribution_positions.shrink_to_fit();
let mut repr = String::new();
for c in 0..number_clause {
repr.push_str(&format!("c{}", c));
}
for v in 0..number_variables {
repr.push_str(&format!("v{}", v));
}
self.components[0] = Component {
start: 0,
size: self.clauses.len(),
distribution_start: 0,
number_distribution: self.distributions.len(),
hash: 0,
max_probability,
bit_repr: Bitvec::ones(number_clause + number_variables),
repr,
};
}
}
Expand Down
1 change: 0 additions & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ mod clause;
mod distribution;
pub mod watched_vector;
pub mod flags;
pub mod bitvec;
13 changes: 0 additions & 13 deletions src/core/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use super::variable::*;
use super::clause::*;
use super::distribution::*;
use super::watched_vector::WatchedVector;
use super::bitvec::WORD_SIZE;

use rustc_hash::FxHashMap;

Expand Down Expand Up @@ -265,23 +264,11 @@ impl Problem {
}
}

for variable in self.variables_iter() {
let word_index = variable.0 / WORD_SIZE;
let mask: u128 = 1 << (variable.0 % WORD_SIZE);
self[variable].set_bitmask(mask);
self[variable].set_bitword_index(word_index);
}

for clause in self.clauses_iter() {
self[clause].clear_literals(&variables_map);
for v in self[clause].get_watchers().into_iter().flatten() {
self.watchers[v.0].push(clause);
}
let i = clause.0 + self.variables.len();
let word_index = i / WORD_SIZE;
let mask: u128 = 1 << (i % WORD_SIZE);
self[clause].set_bitmask(mask);
self[clause].set_bitword_index(word_index);
}

for distribution in self.distributions_iter() {
Expand Down
22 changes: 0 additions & 22 deletions src/core/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ pub struct Variable {
is_implied: ReversibleBool,
/// Random u64 associated to the variable, used for hash computation
hash: u64,
bitmask: u128,
bitword_index: usize,
}

impl Variable {
Expand All @@ -77,8 +75,6 @@ impl Variable {
reason: None,
is_implied: state.manage_bool(false),
hash: rand::random(),
bitmask: 0,
bitword_index: 0,
}
}

Expand Down Expand Up @@ -250,24 +246,6 @@ impl Variable {
}
self.clauses_positive.len() + self.clauses_negative.len()
}

pub fn set_bitmask(&mut self, mask: u128) {
self.bitmask = mask;
}

pub fn set_bitword_index(&mut self, index: usize) {
self.bitword_index = index;
}

#[inline(always)]
pub fn bitmask(&self) -> u128 {
self.bitmask
}

#[inline(always)]
pub fn bitword_index(&self) -> usize {
self.bitword_index
}
}

impl std::fmt::Display for Variable {
Expand Down

0 comments on commit feec913

Please sign in to comment.